14 #include "llvm/ADT/TypeSwitch.h"
15 #include "llvm/IR/DerivedTypes.h"
16 #include "llvm/IR/Type.h"
28 bool importStructsAsLiterals)
29 : context(context), importStructsAsLiterals(importStructsAsLiterals) {}
33 if (knownTranslations.count(type))
34 return knownTranslations.lookup(type);
38 .Case<llvm::ArrayType, llvm::FunctionType, llvm::IntegerType,
39 llvm::PointerType, llvm::StructType, llvm::FixedVectorType,
40 llvm::ScalableVectorType, llvm::TargetExtType>(
41 [
this](
auto *type) {
return this->translate(type); })
42 .Default([
this](llvm::Type *type) {
43 return translatePrimitiveType(type);
45 knownTranslations.try_emplace(type, translated);
52 Type translatePrimitiveType(llvm::Type *type) {
57 if (type->isBFloatTy())
59 if (type->isFloatTy())
61 if (type->isDoubleTy())
63 if (type->isFP128Ty())
65 if (type->isX86_FP80Ty())
67 if (type->isX86_AMXTy())
69 if (type->isPPC_FP128Ty())
71 if (type->isLabelTy())
73 if (type->isMetadataTy())
75 if (type->isTokenTy())
77 llvm_unreachable(
"not a primitive type");
81 Type translate(llvm::ArrayType *type) {
83 type->getNumElements());
87 Type translate(llvm::FunctionType *type) {
89 translateTypes(type->params(), paramTypes);
91 paramTypes, type->isVarArg());
95 Type translate(llvm::IntegerType *type) {
100 Type translate(llvm::PointerType *type) {
105 Type translate(llvm::StructType *type) {
107 if (type->isLiteral() || importStructsAsLiterals) {
108 translateTypes(type->subtypes(), subtypes);
109 return LLVM::LLVMStructType::getLiteral(&context, subtypes,
113 if (type->isOpaque())
114 return LLVM::LLVMStructType::getOpaque(type->getName(), &context);
119 translateTypes(type->subtypes(), subtypes);
120 LLVM::LLVMStructType translated = LLVM::LLVMStructType::getNewIdentified(
121 &context, type->getName(), subtypes, type->isPacked());
122 knownTranslations.try_emplace(type, translated);
127 Type translate(llvm::FixedVectorType *type) {
133 Type translate(llvm::ScalableVectorType *type) {
140 Type translate(llvm::TargetExtType *type) {
142 translateTypes(type->type_params(), typeParams);
151 result.reserve(result.size() + types.size());
152 for (llvm::Type *type : types)
165 bool importStructsAsLiterals;
173 MLIRContext &context,
bool importStructsAsLiterals)
174 :
impl(std::make_unique<detail::TypeFromLLVMIRTranslatorImpl>(
175 context, importStructsAsLiterals)) {}
180 return impl->translateType(type);
TypeFromLLVMIRTranslator(MLIRContext &context, bool importStructsAsLiterals=false)
Type translateType(llvm::Type *type)
Translates the given LLVM IR type to the MLIR LLVM dialect.
~TypeFromLLVMIRTranslator()
Support for translating LLVM IR types to MLIR LLVM dialect types.
Type translateType(llvm::Type *type)
Translates the given type.
TypeFromLLVMIRTranslatorImpl(MLIRContext &context, bool importStructsAsLiterals)
Constructs a class creating types in the given MLIR context.
MLIRContext is the top-level object for a collection of MLIR operations.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...