14 #include "llvm/ADT/TypeSwitch.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/Type.h"
33 if (knownTranslations.count(type))
34 return knownTranslations.lookup(type);
37 llvm::Type *translated =
39 .Case([
this](LLVM::LLVMVoidType) {
40 return llvm::Type::getVoidTy(context);
43 [
this](Float16Type) {
return llvm::Type::getHalfTy(context); })
44 .Case([
this](BFloat16Type) {
45 return llvm::Type::getBFloatTy(context);
48 [
this](Float32Type) {
return llvm::Type::getFloatTy(context); })
49 .Case([
this](Float64Type) {
50 return llvm::Type::getDoubleTy(context);
52 .Case([
this](Float80Type) {
53 return llvm::Type::getX86_FP80Ty(context);
55 .Case([
this](Float128Type) {
56 return llvm::Type::getFP128Ty(context);
58 .Case([
this](LLVM::LLVMPPCFP128Type) {
59 return llvm::Type::getPPC_FP128Ty(context);
61 .Case([
this](LLVM::LLVMTokenType) {
62 return llvm::Type::getTokenTy(context);
64 .Case([
this](LLVM::LLVMLabelType) {
65 return llvm::Type::getLabelTy(context);
67 .Case([
this](LLVM::LLVMMetadataType) {
68 return llvm::Type::getMetadataTy(context);
70 .Case([
this](LLVM::LLVMX86AMXType) {
71 return llvm::Type::getX86_AMXTy(context);
73 .Case<LLVM::LLVMArrayType, IntegerType, LLVM::LLVMFunctionType,
75 LLVM::LLVMFixedVectorType, LLVM::LLVMScalableVectorType,
76 VectorType, LLVM::LLVMTargetExtType>(
77 [
this](
auto type) {
return this->translate(type); })
78 .Default([](
Type t) -> llvm::Type * {
79 llvm_unreachable(
"unknown LLVM dialect type");
83 knownTranslations.try_emplace(type, translated);
89 llvm::Type *translate(LLVM::LLVMArrayType type) {
91 type.getNumElements());
95 llvm::Type *translate(LLVM::LLVMFunctionType type) {
97 translateTypes(type.getParams(), paramTypes);
99 paramTypes, type.isVarArg());
103 llvm::Type *translate(IntegerType type) {
108 llvm::Type *translate(LLVM::LLVMPointerType type) {
118 translateTypes(type.
getBody(), subtypes);
122 llvm::StructType *structType =
123 llvm::StructType::create(context, type.
getName());
126 knownTranslations.try_emplace(type, structType);
130 translateTypes(type.
getBody(), subtypes);
131 structType->setBody(subtypes, type.
isPacked());
136 llvm::Type *translate(VectorType type) {
138 "expected compatible with LLVM vector type");
139 if (type.isScalable())
141 type.getNumElements());
143 type.getNumElements());
147 llvm::Type *translate(LLVM::LLVMFixedVectorType type) {
149 type.getNumElements());
153 llvm::Type *translate(LLVM::LLVMScalableVectorType type) {
155 type.getMinNumElements());
159 llvm::Type *translate(LLVM::LLVMTargetExtType type) {
161 translateTypes(type.getTypeParams(), typeParams);
163 type.getIntParams());
169 result.reserve(result.size() + types.size());
170 for (
auto type : types)
175 llvm::LLVMContext &context;
188 :
impl(new detail::TypeToLLVMIRTranslatorImpl(context)) {}
193 return impl->translateType(type);
197 Type type,
const llvm::DataLayout &layout) {
198 return layout.getPrefTypeAlign(translateType(type)).value();
LLVM dialect structure type representing a collection of different-typed elements manipulated togethe...
StringRef getName()
Returns the name of an identified struct.
bool isPacked() const
Checks if a struct is packed.
ArrayRef< Type > getBody() const
Returns the list of element types contained in a non-opaque struct.
bool isOpaque()
Checks if a struct is opaque.
bool isIdentified() const
Checks if a struct is identified.
unsigned getPreferredAlignment(Type type, const llvm::DataLayout &layout)
Returns the preferred alignment for the type given the data layout.
llvm::Type * translateType(Type type)
Translates the given MLIR LLVM dialect type to LLVM IR.
TypeToLLVMIRTranslator(llvm::LLVMContext &context)
~TypeToLLVMIRTranslator()
Support for translating MLIR LLVM dialect types to LLVM IR.
TypeToLLVMIRTranslatorImpl(llvm::LLVMContext &context)
Constructs a class creating types in the given LLVM context.
llvm::Type * translateType(Type type)
Translates a single type.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isCompatibleVectorType(Type type)
Returns true if the given type is a vector type compatible with the LLVM dialect.
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...