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::ByteType, llvm::PointerType, llvm::StructType,
40 llvm::FixedVectorType, llvm::ScalableVectorType,
42 [
this](
auto *type) {
return this->translate(type); })
43 .Default([
this](llvm::Type *type) {
44 return translatePrimitiveType(type);
46 knownTranslations.try_emplace(type, translated);
53 Type translatePrimitiveType(llvm::Type *type) {
55 return LLVM::LLVMVoidType::get(&context);
57 return Float16Type::get(&context);
58 if (type->isBFloatTy())
59 return BFloat16Type::get(&context);
60 if (type->isFloatTy())
61 return Float32Type::get(&context);
62 if (type->isDoubleTy())
63 return Float64Type::get(&context);
64 if (type->isFP128Ty())
65 return Float128Type::get(&context);
66 if (type->isX86_FP80Ty())
67 return Float80Type::get(&context);
68 if (type->isX86_AMXTy())
69 return LLVM::LLVMX86AMXType::get(&context);
70 if (type->isPPC_FP128Ty())
71 return LLVM::LLVMPPCFP128Type::get(&context);
72 if (type->isLabelTy())
73 return LLVM::LLVMLabelType::get(&context);
74 if (type->isMetadataTy())
75 return LLVM::LLVMMetadataType::get(&context);
76 if (type->isTokenTy())
77 return TokenType::get(&context);
78 llvm_unreachable(
"not a primitive type");
82 Type translate(llvm::ArrayType *type) {
83 return LLVM::LLVMArrayType::get(
translateType(type->getElementType()),
84 type->getNumElements());
88 Type translate(llvm::FunctionType *type) {
90 translateTypes(type->params(), paramTypes);
91 return LLVM::LLVMFunctionType::get(
translateType(type->getReturnType()),
92 paramTypes, type->isVarArg());
96 Type translate(llvm::IntegerType *type) {
97 return IntegerType::get(&context, type->getBitWidth());
101 Type translate(llvm::ByteType *type) {
102 return LLVM::LLVMByteType::get(&context, type->getBitWidth());
106 Type translate(llvm::PointerType *type) {
107 return LLVM::LLVMPointerType::get(&context, type->getAddressSpace());
111 Type translate(llvm::StructType *type) {
112 SmallVector<Type, 8> subtypes;
113 if (type->isLiteral() || importStructsAsLiterals) {
114 translateTypes(type->subtypes(), subtypes);
115 return LLVM::LLVMStructType::getLiteral(&context, subtypes,
119 if (type->isOpaque())
120 return LLVM::LLVMStructType::getOpaque(type->getName(), &context);
125 translateTypes(type->subtypes(), subtypes);
126 LLVM::LLVMStructType translated = LLVM::LLVMStructType::getNewIdentified(
127 &context, type->getName(), subtypes, type->isPacked());
128 knownTranslations.try_emplace(type, translated);
133 Type translate(llvm::FixedVectorType *type) {
134 return VectorType::get(type->getNumElements(),
139 Type translate(llvm::ScalableVectorType *type) {
140 return VectorType::get(type->getMinNumElements(),
146 Type translate(llvm::TargetExtType *type) {
147 SmallVector<Type> typeParams;
148 translateTypes(type->type_params(), typeParams);
150 return LLVM::LLVMTargetExtType::get(&context, type->getName(), typeParams,
155 void translateTypes(ArrayRef<llvm::Type *> types,
156 SmallVectorImpl<Type> &
result) {
158 for (llvm::Type *type : types)
164 llvm::DenseMap<llvm::Type *, Type> knownTranslations;
167 MLIRContext &context;
171 bool importStructsAsLiterals;
MLIRContext is the top-level object for a collection of MLIR operations.