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) {
54 return LLVM::LLVMVoidType::get(&context);
56 return Float16Type::get(&context);
57 if (type->isBFloatTy())
58 return BFloat16Type::get(&context);
59 if (type->isFloatTy())
60 return Float32Type::get(&context);
61 if (type->isDoubleTy())
62 return Float64Type::get(&context);
63 if (type->isFP128Ty())
64 return Float128Type::get(&context);
65 if (type->isX86_FP80Ty())
66 return Float80Type::get(&context);
67 if (type->isX86_AMXTy())
68 return LLVM::LLVMX86AMXType::get(&context);
69 if (type->isPPC_FP128Ty())
70 return LLVM::LLVMPPCFP128Type::get(&context);
71 if (type->isLabelTy())
72 return LLVM::LLVMLabelType::get(&context);
73 if (type->isMetadataTy())
74 return LLVM::LLVMMetadataType::get(&context);
75 if (type->isTokenTy())
76 return LLVM::LLVMTokenType::get(&context);
77 llvm_unreachable(
"not a primitive type");
81 Type translate(llvm::ArrayType *type) {
82 return LLVM::LLVMArrayType::get(
translateType(type->getElementType()),
83 type->getNumElements());
87 Type translate(llvm::FunctionType *type) {
89 translateTypes(type->params(), paramTypes);
90 return LLVM::LLVMFunctionType::get(
translateType(type->getReturnType()),
91 paramTypes, type->isVarArg());
95 Type translate(llvm::IntegerType *type) {
96 return IntegerType::get(&context, type->getBitWidth());
100 Type translate(llvm::PointerType *type) {
101 return LLVM::LLVMPointerType::get(&context, type->getAddressSpace());
105 Type translate(llvm::StructType *type) {
106 SmallVector<Type, 8> subtypes;
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) {
128 return VectorType::get(type->getNumElements(),
133 Type translate(llvm::ScalableVectorType *type) {
134 return VectorType::get(type->getMinNumElements(),
140 Type translate(llvm::TargetExtType *type) {
141 SmallVector<Type> typeParams;
142 translateTypes(type->type_params(), typeParams);
144 return LLVM::LLVMTargetExtType::get(&context, type->getName(), typeParams,
149 void translateTypes(ArrayRef<llvm::Type *> types,
150 SmallVectorImpl<Type> &
result) {
152 for (llvm::Type *type : types)
158 llvm::DenseMap<llvm::Type *, Type> knownTranslations;
161 MLIRContext &context;
165 bool importStructsAsLiterals;
MLIRContext is the top-level object for a collection of MLIR operations.