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](TokenType) {
return llvm::Type::getTokenTy(context); })
62 .Case([
this](LLVM::LLVMLabelType) {
63 return llvm::Type::getLabelTy(context);
65 .Case([
this](LLVM::LLVMMetadataType) {
66 return llvm::Type::getMetadataTy(context);
68 .Case([
this](LLVM::LLVMX86AMXType) {
69 return llvm::Type::getX86_AMXTy(context);
71 .Case<LLVM::LLVMArrayType, LLVM::LLVMByteType, IntegerType,
72 LLVM::LLVMFunctionType, LLVM::LLVMPointerType,
73 LLVM::LLVMStructType, VectorType, LLVM::LLVMTargetExtType,
74 PtrLikeTypeInterface>(
75 [
this](
auto type) {
return this->translate(type); })
76 .DefaultUnreachable(
"unknown LLVM dialect type");
79 knownTranslations.try_emplace(type, translated);
85 llvm::Type *translate(LLVM::LLVMArrayType type) {
86 return llvm::ArrayType::get(
translateType(type.getElementType()),
87 type.getNumElements());
91 llvm::Type *translate(LLVM::LLVMFunctionType type) {
93 translateTypes(type.getParams(), paramTypes);
94 return llvm::FunctionType::get(
translateType(type.getReturnType()),
95 paramTypes, type.isVarArg());
99 llvm::Type *translate(LLVM::LLVMByteType type) {
100 return llvm::Type::getByteNTy(context, type.getBitWidth());
104 llvm::Type *translate(IntegerType type) {
105 return llvm::IntegerType::get(context, type.getWidth());
109 llvm::Type *translate(LLVM::LLVMPointerType type) {
110 return llvm::PointerType::get(context, type.getAddressSpace());
116 llvm::Type *translate(LLVM::LLVMStructType type) {
117 SmallVector<llvm::Type *, 8> subtypes;
118 if (!type.isIdentified()) {
119 translateTypes(type.getBody(), subtypes);
120 return llvm::StructType::get(context, subtypes, type.isPacked());
123 llvm::StructType *structType =
124 llvm::StructType::create(context, type.getName());
127 knownTranslations.try_emplace(type, structType);
131 translateTypes(type.getBody(), subtypes);
132 structType->setBody(subtypes, type.isPacked());
137 llvm::Type *translate(VectorType type) {
139 "expected compatible with LLVM vector type");
140 if (type.isScalable())
141 return llvm::ScalableVectorType::get(
translateType(type.getElementType()),
142 type.getNumElements());
143 return llvm::FixedVectorType::get(
translateType(type.getElementType()),
144 type.getNumElements());
148 llvm::Type *translate(LLVM::LLVMTargetExtType type) {
149 SmallVector<llvm::Type *> typeParams;
150 translateTypes(type.getTypeParams(), typeParams);
151 return llvm::TargetExtType::get(context, type.getExtTypeName(), typeParams,
152 type.getIntParams());
156 llvm::Type *translate(PtrLikeTypeInterface type) {
158 dyn_cast<LLVM::LLVMAddrSpaceAttrInterface>(type.getMemorySpace());
159 assert(memSpace &&
"expected pointer with an LLVM address space");
160 assert(!type.hasPtrMetadata() &&
"expected pointer without metadata");
161 return llvm::PointerType::get(context, memSpace.getAddressSpace());
165 void translateTypes(ArrayRef<Type> types,
166 SmallVectorImpl<llvm::Type *> &
result) {
168 for (
auto type : types)
173 llvm::LLVMContext &context;
179 llvm::DenseMap<Type, llvm::Type *> knownTranslations;