14 #include "llvm/ADT/TypeSwitch.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/Type.h"
32 if (knownTranslations.count(type))
33 return knownTranslations.lookup(type);
37 .Case<llvm::ArrayType, llvm::FunctionType, llvm::IntegerType,
38 llvm::PointerType, llvm::StructType, llvm::FixedVectorType,
39 llvm::ScalableVectorType, llvm::TargetExtType>(
40 [
this](
auto *type) {
return this->translate(type); })
41 .Default([
this](llvm::Type *type) {
42 return translatePrimitiveType(type);
44 knownTranslations.try_emplace(type, translated);
51 Type translatePrimitiveType(llvm::Type *type) {
56 if (type->isBFloatTy())
58 if (type->isFloatTy())
60 if (type->isDoubleTy())
62 if (type->isFP128Ty())
64 if (type->isX86_FP80Ty())
66 if (type->isX86_AMXTy())
68 if (type->isPPC_FP128Ty())
70 if (type->isLabelTy())
72 if (type->isMetadataTy())
74 if (type->isTokenTy())
76 llvm_unreachable(
"not a primitive type");
80 Type translate(llvm::ArrayType *type) {
82 type->getNumElements());
86 Type translate(llvm::FunctionType *type) {
88 translateTypes(type->params(), paramTypes);
90 paramTypes, type->isVarArg());
94 Type translate(llvm::IntegerType *type) {
99 Type translate(llvm::PointerType *type) {
104 Type translate(llvm::StructType *type) {
106 if (type->isLiteral()) {
107 translateTypes(type->subtypes(), subtypes);
108 return LLVM::LLVMStructType::getLiteral(&context, subtypes,
112 if (type->isOpaque())
113 return LLVM::LLVMStructType::getOpaque(type->getName(), &context);
118 translateTypes(type->subtypes(), subtypes);
119 LLVM::LLVMStructType translated = LLVM::LLVMStructType::getNewIdentified(
120 &context, type->getName(), subtypes, type->isPacked());
121 knownTranslations.try_emplace(type, translated);
126 Type translate(llvm::FixedVectorType *type) {
128 type->getNumElements());
132 Type translate(llvm::ScalableVectorType *type) {
134 translateType(type->getElementType()), type->getMinNumElements());
138 Type translate(llvm::TargetExtType *type) {
140 translateTypes(type->type_params(), typeParams);
149 result.reserve(result.size() + types.size());
150 for (llvm::Type *type : types)
167 :
impl(new detail::TypeFromLLVMIRTranslatorImpl(context)) {}
172 return impl->translateType(type);
Type translateType(llvm::Type *type)
Translates the given LLVM IR type to the MLIR LLVM dialect.
~TypeFromLLVMIRTranslator()
TypeFromLLVMIRTranslator(MLIRContext &context)
Support for translating LLVM IR types to MLIR LLVM dialect types.
TypeFromLLVMIRTranslatorImpl(MLIRContext &context)
Constructs a class creating types in the given MLIR context.
Type translateType(llvm::Type *type)
Translates the given type.
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...
Type getFixedVectorType(Type elementType, unsigned numElements)
Creates an LLVM dialect-compatible type with the given element type and length.
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...