|
16 | 16 | #include "mlir/IR/BuiltinTypes.h"
|
17 | 17 | #include "mlir/IR/MLIRContext.h"
|
18 | 18 | #include "mlir/Target/LLVMIR/Import.h"
|
| 19 | +#include "mlir/Target/LLVMIR/TypeFromLLVM.h" |
19 | 20 | #include "mlir/Translation.h"
|
20 | 21 |
|
21 | 22 | #include "llvm/ADT/TypeSwitch.h"
|
@@ -45,167 +46,6 @@ static std::string diag(llvm::Value &v) {
|
45 | 46 | return os.str();
|
46 | 47 | }
|
47 | 48 |
|
48 |
| -namespace mlir { |
49 |
| -namespace LLVM { |
50 |
| -namespace detail { |
51 |
| -/// Support for translating LLVM IR types to MLIR LLVM dialect types. |
52 |
| -class TypeFromLLVMIRTranslatorImpl { |
53 |
| -public: |
54 |
| - /// Constructs a class creating types in the given MLIR context. |
55 |
| - TypeFromLLVMIRTranslatorImpl(MLIRContext &context) : context(context) {} |
56 |
| - |
57 |
| - /// Translates the given type. |
58 |
| - Type translateType(llvm::Type *type) { |
59 |
| - if (knownTranslations.count(type)) |
60 |
| - return knownTranslations.lookup(type); |
61 |
| - |
62 |
| - Type translated = |
63 |
| - llvm::TypeSwitch<llvm::Type *, Type>(type) |
64 |
| - .Case<llvm::ArrayType, llvm::FunctionType, llvm::IntegerType, |
65 |
| - llvm::PointerType, llvm::StructType, llvm::FixedVectorType, |
66 |
| - llvm::ScalableVectorType>( |
67 |
| - [this](auto *type) { return this->translate(type); }) |
68 |
| - .Default([this](llvm::Type *type) { |
69 |
| - return translatePrimitiveType(type); |
70 |
| - }); |
71 |
| - knownTranslations.try_emplace(type, translated); |
72 |
| - return translated; |
73 |
| - } |
74 |
| - |
75 |
| -private: |
76 |
| - /// Translates the given primitive, i.e. non-parametric in MLIR nomenclature, |
77 |
| - /// type. |
78 |
| - Type translatePrimitiveType(llvm::Type *type) { |
79 |
| - if (type->isVoidTy()) |
80 |
| - return LLVM::LLVMVoidType::get(&context); |
81 |
| - if (type->isHalfTy()) |
82 |
| - return Float16Type::get(&context); |
83 |
| - if (type->isBFloatTy()) |
84 |
| - return BFloat16Type::get(&context); |
85 |
| - if (type->isFloatTy()) |
86 |
| - return Float32Type::get(&context); |
87 |
| - if (type->isDoubleTy()) |
88 |
| - return Float64Type::get(&context); |
89 |
| - if (type->isFP128Ty()) |
90 |
| - return Float128Type::get(&context); |
91 |
| - if (type->isX86_FP80Ty()) |
92 |
| - return Float80Type::get(&context); |
93 |
| - if (type->isPPC_FP128Ty()) |
94 |
| - return LLVM::LLVMPPCFP128Type::get(&context); |
95 |
| - if (type->isX86_MMXTy()) |
96 |
| - return LLVM::LLVMX86MMXType::get(&context); |
97 |
| - if (type->isLabelTy()) |
98 |
| - return LLVM::LLVMLabelType::get(&context); |
99 |
| - if (type->isMetadataTy()) |
100 |
| - return LLVM::LLVMMetadataType::get(&context); |
101 |
| - llvm_unreachable("not a primitive type"); |
102 |
| - } |
103 |
| - |
104 |
| - /// Translates the given array type. |
105 |
| - Type translate(llvm::ArrayType *type) { |
106 |
| - return LLVM::LLVMArrayType::get(translateType(type->getElementType()), |
107 |
| - type->getNumElements()); |
108 |
| - } |
109 |
| - |
110 |
| - /// Translates the given function type. |
111 |
| - Type translate(llvm::FunctionType *type) { |
112 |
| - SmallVector<Type, 8> paramTypes; |
113 |
| - translateTypes(type->params(), paramTypes); |
114 |
| - return LLVM::LLVMFunctionType::get(translateType(type->getReturnType()), |
115 |
| - paramTypes, type->isVarArg()); |
116 |
| - } |
117 |
| - |
118 |
| - /// Translates the given integer type. |
119 |
| - Type translate(llvm::IntegerType *type) { |
120 |
| - return IntegerType::get(&context, type->getBitWidth()); |
121 |
| - } |
122 |
| - |
123 |
| - /// Translates the given pointer type. |
124 |
| - Type translate(llvm::PointerType *type) { |
125 |
| - return LLVM::LLVMPointerType::get(translateType(type->getElementType()), |
126 |
| - type->getAddressSpace()); |
127 |
| - } |
128 |
| - |
129 |
| - /// Translates the given structure type. |
130 |
| - Type translate(llvm::StructType *type) { |
131 |
| - SmallVector<Type, 8> subtypes; |
132 |
| - if (type->isLiteral()) { |
133 |
| - translateTypes(type->subtypes(), subtypes); |
134 |
| - return LLVM::LLVMStructType::getLiteral(&context, subtypes, |
135 |
| - type->isPacked()); |
136 |
| - } |
137 |
| - |
138 |
| - if (type->isOpaque()) |
139 |
| - return LLVM::LLVMStructType::getOpaque(type->getName(), &context); |
140 |
| - |
141 |
| - LLVM::LLVMStructType translated = |
142 |
| - LLVM::LLVMStructType::getIdentified(&context, type->getName()); |
143 |
| - knownTranslations.try_emplace(type, translated); |
144 |
| - translateTypes(type->subtypes(), subtypes); |
145 |
| - LogicalResult bodySet = translated.setBody(subtypes, type->isPacked()); |
146 |
| - assert(succeeded(bodySet) && |
147 |
| - "could not set the body of an identified struct"); |
148 |
| - (void)bodySet; |
149 |
| - return translated; |
150 |
| - } |
151 |
| - |
152 |
| - /// Translates the given fixed-vector type. |
153 |
| - Type translate(llvm::FixedVectorType *type) { |
154 |
| - return LLVM::getFixedVectorType(translateType(type->getElementType()), |
155 |
| - type->getNumElements()); |
156 |
| - } |
157 |
| - |
158 |
| - /// Translates the given scalable-vector type. |
159 |
| - Type translate(llvm::ScalableVectorType *type) { |
160 |
| - return LLVM::LLVMScalableVectorType::get( |
161 |
| - translateType(type->getElementType()), type->getMinNumElements()); |
162 |
| - } |
163 |
| - |
164 |
| - /// Translates a list of types. |
165 |
| - void translateTypes(ArrayRef<llvm::Type *> types, |
166 |
| - SmallVectorImpl<Type> &result) { |
167 |
| - result.reserve(result.size() + types.size()); |
168 |
| - for (llvm::Type *type : types) |
169 |
| - result.push_back(translateType(type)); |
170 |
| - } |
171 |
| - |
172 |
| - /// Map of known translations. Serves as a cache and as recursion stopper for |
173 |
| - /// translating recursive structs. |
174 |
| - llvm::DenseMap<llvm::Type *, Type> knownTranslations; |
175 |
| - |
176 |
| - /// The context in which MLIR types are created. |
177 |
| - MLIRContext &context; |
178 |
| -}; |
179 |
| -} // end namespace detail |
180 |
| - |
181 |
| -/// Utility class to translate LLVM IR types to the MLIR LLVM dialect. Stores |
182 |
| -/// the translation state, in particular any identified structure types that are |
183 |
| -/// reused across translations. |
184 |
| -class TypeFromLLVMIRTranslator { |
185 |
| -public: |
186 |
| - TypeFromLLVMIRTranslator(MLIRContext &context); |
187 |
| - ~TypeFromLLVMIRTranslator(); |
188 |
| - |
189 |
| - /// Translates the given LLVM IR type to the MLIR LLVM dialect. |
190 |
| - Type translateType(llvm::Type *type); |
191 |
| - |
192 |
| -private: |
193 |
| - /// Private implementation. |
194 |
| - std::unique_ptr<detail::TypeFromLLVMIRTranslatorImpl> impl; |
195 |
| -}; |
196 |
| - |
197 |
| -} // end namespace LLVM |
198 |
| -} // end namespace mlir |
199 |
| - |
200 |
| -LLVM::TypeFromLLVMIRTranslator::TypeFromLLVMIRTranslator(MLIRContext &context) |
201 |
| - : impl(new detail::TypeFromLLVMIRTranslatorImpl(context)) {} |
202 |
| - |
203 |
| -LLVM::TypeFromLLVMIRTranslator::~TypeFromLLVMIRTranslator() {} |
204 |
| - |
205 |
| -Type LLVM::TypeFromLLVMIRTranslator::translateType(llvm::Type *type) { |
206 |
| - return impl->translateType(type); |
207 |
| -} |
208 |
| - |
209 | 49 | // Handles importing globals and functions from an LLVM module.
|
210 | 50 | namespace {
|
211 | 51 | class Importer {
|
|
0 commit comments