OmniSciDB  ab4938a6a3
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/Module.h>
22 #include <llvm/IR/Type.h>
23 #include <llvm/Support/raw_os_ostream.h>
24 
25 #include "Shared/Logger.h"
26 
27 #if LLVM_VERSION_MAJOR >= 10
28 #define LLVM_ALIGN(alignment) llvm::Align(alignment)
29 #define LLVM_MAYBE_ALIGN(alignment) llvm::MaybeAlign(alignment)
30 #else
31 #define LLVM_ALIGN(alignment) alignment
32 #define LLVM_MAYBE_ALIGN(alignment) alignment
33 #endif
34 
35 inline llvm::ArrayType* get_int_array_type(int const width,
36  int count,
37  llvm::LLVMContext& context) {
38  switch (width) {
39  case 64:
40  return llvm::ArrayType::get(llvm::Type::getInt64Ty(context), count);
41  case 32:
42  return llvm::ArrayType::get(llvm::Type::getInt32Ty(context), count);
43  break;
44  case 16:
45  return llvm::ArrayType::get(llvm::Type::getInt16Ty(context), count);
46  break;
47  case 8:
48  return llvm::ArrayType::get(llvm::Type::getInt8Ty(context), count);
49  break;
50  case 1:
51  return llvm::ArrayType::get(llvm::Type::getInt1Ty(context), count);
52  break;
53  default:
54  LOG(FATAL) << "Unsupported integer width: " << width;
55  }
56  return nullptr;
57 }
58 
59 inline llvm::VectorType* get_int_vector_type(int const width,
60  int count,
61  llvm::LLVMContext& context) {
62  switch (width) {
63  case 64:
64  return llvm::VectorType::get(llvm::Type::getInt64Ty(context), count);
65  case 32:
66  return llvm::VectorType::get(llvm::Type::getInt32Ty(context), count);
67  break;
68  case 16:
69  return llvm::VectorType::get(llvm::Type::getInt16Ty(context), count);
70  break;
71  case 8:
72  return llvm::VectorType::get(llvm::Type::getInt8Ty(context), count);
73  break;
74  case 1:
75  return llvm::VectorType::get(llvm::Type::getInt1Ty(context), count);
76  break;
77  default:
78  LOG(FATAL) << "Unsupported integer width: " << width;
79  }
80  return nullptr;
81 }
82 
83 inline llvm::Type* get_int_type(const int width, llvm::LLVMContext& context) {
84  switch (width) {
85  case 64:
86  return llvm::Type::getInt64Ty(context);
87  case 32:
88  return llvm::Type::getInt32Ty(context);
89  break;
90  case 16:
91  return llvm::Type::getInt16Ty(context);
92  break;
93  case 8:
94  return llvm::Type::getInt8Ty(context);
95  break;
96  case 1:
97  return llvm::Type::getInt1Ty(context);
98  break;
99  default:
100  LOG(FATAL) << "Unsupported integer width: " << width;
101  }
102  UNREACHABLE();
103  return nullptr;
104 }
105 
106 inline llvm::Type* get_fp_type(const int width, llvm::LLVMContext& context) {
107  switch (width) {
108  case 64:
109  return llvm::Type::getDoubleTy(context);
110  case 32:
111  return llvm::Type::getFloatTy(context);
112  default:
113  LOG(FATAL) << "Unsupported floating point width: " << width;
114  }
115  UNREACHABLE();
116  return nullptr;
117 }
118 
119 template <class T>
120 inline llvm::ConstantInt* ll_int(const T v, llvm::LLVMContext& context) {
121  return static_cast<llvm::ConstantInt*>(
122  llvm::ConstantInt::get(get_int_type(sizeof(v) * 8, context), v));
123 }
124 
125 inline llvm::ConstantInt* ll_bool(const bool v, llvm::LLVMContext& context) {
126  return static_cast<llvm::ConstantInt*>(
127  llvm::ConstantInt::get(get_int_type(1, context), v));
128 }
129 
130 llvm::Module* read_template_module(llvm::LLVMContext& context);
131 
132 template <class T>
133 std::string serialize_llvm_object(const T* llvm_obj) {
134  std::stringstream ss;
135  llvm::raw_os_ostream os(ss);
136  llvm_obj->print(os);
137  os.flush();
138  return ss.str();
139 }
140 
141 void verify_function_ir(const llvm::Function* func);
#define LOG(tag)
Definition: Logger.h:188
llvm::ConstantInt * ll_int(const T v, llvm::LLVMContext &context)
#define UNREACHABLE()
Definition: Logger.h:241
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)
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)