OmniSciDB  5ade3759e0
IRCodegenUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <llvm/IR/Constants.h>
20 #include <llvm/IR/LLVMContext.h>
21 #include <llvm/IR/Type.h>
22 #include "Shared/Logger.h"
23 
24 inline llvm::ArrayType* get_int_array_type(int const width,
25  int count,
26  llvm::LLVMContext& context) {
27  switch (width) {
28  case 64:
29  return llvm::ArrayType::get(llvm::Type::getInt64Ty(context), count);
30  case 32:
31  return llvm::ArrayType::get(llvm::Type::getInt32Ty(context), count);
32  break;
33  case 16:
34  return llvm::ArrayType::get(llvm::Type::getInt16Ty(context), count);
35  break;
36  case 8:
37  return llvm::ArrayType::get(llvm::Type::getInt8Ty(context), count);
38  break;
39  case 1:
40  return llvm::ArrayType::get(llvm::Type::getInt1Ty(context), count);
41  break;
42  default:
43  LOG(FATAL) << "Unsupported integer width: " << width;
44  }
45  return nullptr;
46 }
47 
48 inline llvm::VectorType* get_int_vector_type(int const width,
49  int count,
50  llvm::LLVMContext& context) {
51  switch (width) {
52  case 64:
53  return llvm::VectorType::get(llvm::Type::getInt64Ty(context), count);
54  case 32:
55  return llvm::VectorType::get(llvm::Type::getInt32Ty(context), count);
56  break;
57  case 16:
58  return llvm::VectorType::get(llvm::Type::getInt16Ty(context), count);
59  break;
60  case 8:
61  return llvm::VectorType::get(llvm::Type::getInt8Ty(context), count);
62  break;
63  case 1:
64  return llvm::VectorType::get(llvm::Type::getInt1Ty(context), count);
65  break;
66  default:
67  LOG(FATAL) << "Unsupported integer width: " << width;
68  }
69  return nullptr;
70 }
71 
72 inline llvm::Type* get_int_type(const int width, llvm::LLVMContext& context) {
73  switch (width) {
74  case 64:
75  return llvm::Type::getInt64Ty(context);
76  case 32:
77  return llvm::Type::getInt32Ty(context);
78  break;
79  case 16:
80  return llvm::Type::getInt16Ty(context);
81  break;
82  case 8:
83  return llvm::Type::getInt8Ty(context);
84  break;
85  case 1:
86  return llvm::Type::getInt1Ty(context);
87  break;
88  default:
89  LOG(FATAL) << "Unsupported integer width: " << width;
90  }
91  UNREACHABLE();
92  return nullptr;
93 }
94 
95 inline llvm::Type* get_fp_type(const int width, llvm::LLVMContext& context) {
96  switch (width) {
97  case 64:
98  return llvm::Type::getDoubleTy(context);
99  case 32:
100  return llvm::Type::getFloatTy(context);
101  default:
102  LOG(FATAL) << "Unsupported floating point width: " << width;
103  }
104  UNREACHABLE();
105  return nullptr;
106 }
107 
108 template <class T>
109 inline llvm::ConstantInt* ll_int(const T v, llvm::LLVMContext& context) {
110  return static_cast<llvm::ConstantInt*>(
111  llvm::ConstantInt::get(get_int_type(sizeof(v) * 8, context), v));
112 }
113 
114 inline llvm::ConstantInt* ll_bool(const bool v, llvm::LLVMContext& context) {
115  return static_cast<llvm::ConstantInt*>(
116  llvm::ConstantInt::get(get_int_type(1, context), v));
117 }
118 
119 llvm::Module* read_template_module(llvm::LLVMContext& context);
120 
121 template <class T>
122 std::string serialize_llvm_object(const T* llvm_obj);
123 
124 void verify_function_ir(const llvm::Function* func);
#define LOG(tag)
Definition: Logger.h:182
llvm::ConstantInt * ll_int(const T v, llvm::LLVMContext &context)
#define UNREACHABLE()
Definition: Logger.h:231
llvm::Type * get_fp_type(const int width, llvm::LLVMContext &context)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
void verify_function_ir(const llvm::Function *func)
T v(const TargetValue &r)
llvm::VectorType * get_int_vector_type(int const width, int count, llvm::LLVMContext &context)
std::string serialize_llvm_object(const T *llvm_obj)
llvm::Module * read_template_module(llvm::LLVMContext &context)
llvm::ConstantInt * ll_bool(const bool v, llvm::LLVMContext &context)
llvm::ArrayType * get_int_array_type(int const width, int count, llvm::LLVMContext &context)