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->isPPC_FP128Ty())
68 if (type->isLabelTy())
70 if (type->isMetadataTy())
72 if (type->isTokenTy())
74 llvm_unreachable(
"not a primitive type");
78 Type translate(llvm::ArrayType *type) {
80 type->getNumElements());
84 Type translate(llvm::FunctionType *type) {
86 translateTypes(type->params(), paramTypes);
88 paramTypes, type->isVarArg());
92 Type translate(llvm::IntegerType *type) {
97 Type translate(llvm::PointerType *type) {
102 Type translate(llvm::StructType *type) {
104 if (type->isLiteral()) {
105 translateTypes(type->subtypes(), subtypes);
110 if (type->isOpaque())
116 translateTypes(type->subtypes(), subtypes);
118 &context, type->getName(), subtypes, type->isPacked());
119 knownTranslations.try_emplace(type, translated);
124 Type translate(llvm::FixedVectorType *type) {
126 type->getNumElements());
130 Type translate(llvm::ScalableVectorType *type) {
132 translateType(type->getElementType()), type->getMinNumElements());
136 Type translate(llvm::TargetExtType *type) {
138 translateTypes(type->type_params(), typeParams);
147 result.reserve(result.size() + types.size());
148 for (llvm::Type *type : types)
165 :
impl(new detail::TypeFromLLVMIRTranslatorImpl(context)) {}
170 return impl->translateType(type);
LLVM dialect structure type representing a collection of different-typed elements manipulated togethe...
static LLVMStructType getLiteral(MLIRContext *context, ArrayRef< Type > types, bool isPacked=false)
Gets or creates a literal struct with the given body in the provided context.
static LLVMStructType getNewIdentified(MLIRContext *context, StringRef name, ArrayRef< Type > elements, bool isPacked=false)
Gets a new identified struct with the given body.
static LLVMStructType getOpaque(StringRef name, MLIRContext *context)
Gets or creates an intentionally-opaque identified struct.
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...