MLIR: lib/Target/LLVMIR/TypeToLLVM.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

13

14 #include "llvm/ADT/TypeSwitch.h"

15 #include "llvm/IR/DataLayout.h"

16 #include "llvm/IR/DerivedTypes.h"

17 #include "llvm/IR/Type.h"

18

19 using namespace mlir;

20

21 namespace mlir {

22 namespace LLVM {

23 namespace detail {

24

26 public:

27

29

30

32

33 if (knownTranslations.count(type))

34 return knownTranslations.lookup(type);

35

36

37 llvm::Type *translated =

39 .Case([this](LLVM::LLVMVoidType) {

40 return llvm::Type::getVoidTy(context);

41 })

42 .Case(

43 [this](Float16Type) { return llvm::Type::getHalfTy(context); })

44 .Case([this](BFloat16Type) {

45 return llvm::Type::getBFloatTy(context);

46 })

47 .Case(

48 [this](Float32Type) { return llvm::Type::getFloatTy(context); })

49 .Case([this](Float64Type) {

50 return llvm::Type::getDoubleTy(context);

51 })

52 .Case([this](Float80Type) {

53 return llvm::Type::getX86_FP80Ty(context);

54 })

55 .Case([this](Float128Type) {

56 return llvm::Type::getFP128Ty(context);

57 })

58 .Case([this](LLVM::LLVMPPCFP128Type) {

59 return llvm::Type::getPPC_FP128Ty(context);

60 })

61 .Case([this](LLVM::LLVMTokenType) {

62 return llvm::Type::getTokenTy(context);

63 })

64 .Case([this](LLVM::LLVMLabelType) {

65 return llvm::Type::getLabelTy(context);

66 })

67 .Case([this](LLVM::LLVMMetadataType) {

68 return llvm::Type::getMetadataTy(context);

69 })

70 .Case([this](LLVM::LLVMX86AMXType) {

71 return llvm::Type::getX86_AMXTy(context);

72 })

73 .Case<LLVM::LLVMArrayType, IntegerType, LLVM::LLVMFunctionType,

74 LLVM::LLVMPointerType, LLVM::LLVMStructType, VectorType,

75 LLVM::LLVMTargetExtType>(

76 [this](auto type) { return this->translate(type); })

77 .Default([](Type t) -> llvm::Type * {

78 llvm_unreachable("unknown LLVM dialect type");

79 });

80

81

82 knownTranslations.try_emplace(type, translated);

83 return translated;

84 }

85

86 private:

87

88 llvm::Type *translate(LLVM::LLVMArrayType type) {

90 type.getNumElements());

91 }

92

93

94 llvm::Type *translate(LLVM::LLVMFunctionType type) {

96 translateTypes(type.getParams(), paramTypes);

98 paramTypes, type.isVarArg());

99 }

100

101

102 llvm::Type *translate(IntegerType type) {

104 }

105

106

107 llvm::Type *translate(LLVM::LLVMPointerType type) {

109 }

110

111

112

113

114 llvm::Type *translate(LLVM::LLVMStructType type) {

116 if (!type.isIdentified()) {

117 translateTypes(type.getBody(), subtypes);

119 }

120

121 llvm::StructType *structType =

122 llvm::StructType::create(context, type.getName());

123

124

125 knownTranslations.try_emplace(type, structType);

126 if (type.isOpaque())

127 return structType;

128

129 translateTypes(type.getBody(), subtypes);

130 structType->setBody(subtypes, type.isPacked());

131 return structType;

132 }

133

134

135 llvm::Type *translate(VectorType type) {

137 "expected compatible with LLVM vector type");

138 if (type.isScalable())

140 type.getNumElements());

142 type.getNumElements());

143 }

144

145

146 llvm::Type *translate(LLVM::LLVMTargetExtType type) {

148 translateTypes(type.getTypeParams(), typeParams);

150 type.getIntParams());

151 }

152

153

156 result.reserve(result.size() + types.size());

157 for (auto type : types)

159 }

160

161

162 llvm::LLVMContext &context;

163

164

165

166

167

169 };

170 }

171 }

172 }

173

175 : impl(new detail::TypeToLLVMIRTranslatorImpl(context)) {}

176

178

180 return impl->translateType(type);

181 }

182

184 Type type, const llvm::DataLayout &layout) {

185 return layout.getPrefTypeAlign(translateType(type)).value();

186 }

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...