My Project
ConvertStandardToLLVM.h
Go to the documentation of this file.
1 //===- ConvertStandardToLLVM.h - Convert to the LLVM dialect ----*- C++ -*-===//
2 //
3 // Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Provides a dialect conversion targeting the LLVM IR dialect. By default, it
10 // converts Standard ops and types and provides hooks for dialect-specific
11 // extensions to the conversion.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_CONVERSION_STANDARDTOLLVM_CONVERTSTANDARDTOLLVM_H
16 #define MLIR_CONVERSION_STANDARDTOLLVM_CONVERTSTANDARDTOLLVM_H
17 
19 
20 namespace llvm {
21 class IntegerType;
22 class LLVMContext;
23 class Module;
24 class Type;
25 } // namespace llvm
26 
27 namespace mlir {
28 
29 class UnrankedMemRefType;
30 
31 namespace LLVM {
32 class LLVMDialect;
33 class LLVMType;
34 } // namespace LLVM
35 
38 public:
39  using TypeConverter::convertType;
40 
42 
45  Type convertType(Type t) override;
46 
50  LLVM::LLVMType convertFunctionSignature(FunctionType type, bool isVariadic,
51  SignatureConversion &result);
52 
57  Type packFunctionResults(ArrayRef<Type> types);
58 
60  llvm::LLVMContext &getLLVMContext();
61 
63  LLVM::LLVMDialect *getDialect() { return llvmDialect; }
64 
68  SmallVector<Value, 4> promoteMemRefDescriptors(Location loc,
69  ValueRange opOperands,
70  ValueRange operands,
71  OpBuilder &builder);
72 
76  Value promoteOneMemRefDescriptor(Location loc, Value operand,
77  OpBuilder &builder);
78 
79 protected:
81  llvm::Module *module;
83 
84 private:
85  Type convertStandardType(Type type);
86 
87  // Convert a function type. The arguments and results are converted one by
88  // one. Additionally, if the function returns more than one value, pack the
89  // results into an LLVM IR structure type so that the converted function type
90  // returns at most one result.
91  Type convertFunctionType(FunctionType type);
92 
93  // Convert the index type. Uses llvmModule data layout to create an integer
94  // of the pointer bitwidth.
95  Type convertIndexType(IndexType type);
96 
97  // Convert an integer type `i*` to `!llvm<"i*">`.
98  Type convertIntegerType(IntegerType type);
99 
100  // Convert a floating point type: `f16` to `!llvm.half`, `f32` to
101  // `!llvm.float` and `f64` to `!llvm.double`. `bf16` is not supported
102  // by LLVM.
103  Type convertFloatType(FloatType type);
104 
105  // Convert a memref type into an LLVM type that captures the relevant data.
106  // For statically-shaped memrefs, the resulting type is a pointer to the
107  // (converted) memref element type. For dynamically-shaped memrefs, the
108  // resulting type is an LLVM structure type that contains:
109  // 1. a pointer to the (converted) memref element type
110  // 2. as many index types as memref has dynamic dimensions.
111  Type convertMemRefType(MemRefType type);
112 
113  // Convert an unranked memref type to an LLVM type that captures the
114  // runtime rank and a pointer to the static ranked memref desc
115  Type convertUnrankedMemRefType(UnrankedMemRefType type);
116 
117  // Convert a 1D vector type into an LLVM vector type.
118  Type convertVectorType(VectorType type);
119 
120  // Get the LLVM representation of the index type based on the bitwidth of the
121  // pointer as defined by the data layout of the module.
122  LLVM::LLVMType getIndexType();
123 
124  // Extract an LLVM IR dialect type.
125  LLVM::LLVMType unwrap(Type type);
126 };
127 
131 public:
133  explicit StructBuilder(Value v);
135  static StructBuilder undef(OpBuilder &builder, Location loc,
136  Type descriptorType);
137 
138  /*implicit*/ operator Value() { return value; }
139 
140 protected:
141  // LLVM value
143  // Cached struct type.
145 
146 protected:
148  Value extractPtr(OpBuilder &builder, Location loc, unsigned pos);
150  void setPtr(OpBuilder &builder, Location loc, unsigned pos, Value ptr);
151 };
156 public:
158  explicit MemRefDescriptor(Value descriptor);
160  static MemRefDescriptor undef(OpBuilder &builder, Location loc,
161  Type descriptorType);
165  static MemRefDescriptor fromStaticShape(OpBuilder &builder, Location loc,
166  LLVMTypeConverter &typeConverter,
167  MemRefType type, Value memory);
168 
170  Value allocatedPtr(OpBuilder &builder, Location loc);
172  void setAllocatedPtr(OpBuilder &builder, Location loc, Value ptr);
173 
175  Value alignedPtr(OpBuilder &builder, Location loc);
176 
178  void setAlignedPtr(OpBuilder &builder, Location loc, Value ptr);
179 
181  Value offset(OpBuilder &builder, Location loc);
182 
184  void setOffset(OpBuilder &builder, Location loc, Value offset);
185  void setConstantOffset(OpBuilder &builder, Location loc, uint64_t offset);
186 
188  Value size(OpBuilder &builder, Location loc, unsigned pos);
189 
191  void setSize(OpBuilder &builder, Location loc, unsigned pos, Value size);
192  void setConstantSize(OpBuilder &builder, Location loc, unsigned pos,
193  uint64_t size);
194 
196  Value stride(OpBuilder &builder, Location loc, unsigned pos);
197 
199  void setStride(OpBuilder &builder, Location loc, unsigned pos, Value stride);
200  void setConstantStride(OpBuilder &builder, Location loc, unsigned pos,
201  uint64_t stride);
202 
204  LLVM::LLVMType getElementType();
205 
206 private:
207  // Cached index type.
208  Type indexType;
209 };
210 
212 public:
214  explicit UnrankedMemRefDescriptor(Value descriptor);
216  static UnrankedMemRefDescriptor undef(OpBuilder &builder, Location loc,
217  Type descriptorType);
218 
220  Value rank(OpBuilder &builder, Location loc);
222  void setRank(OpBuilder &builder, Location loc, Value value);
224  Value memRefDescPtr(OpBuilder &builder, Location loc);
226  void setMemRefDescPtr(OpBuilder &builder, Location loc, Value value);
227 };
232 public:
233  LLVMOpLowering(StringRef rootOpName, MLIRContext *context,
234  LLVMTypeConverter &lowering, PatternBenefit benefit = 1);
235 
236 protected:
237  // Back-reference to the lowering class, used to call type and function
238  // conversions accounting for potential extensions.
240 };
241 
242 } // namespace mlir
243 
244 #endif // MLIR_CONVERSION_STANDARDTOLLVM_CONVERTSTANDARDTOLLVM_H
Definition: InferTypeOpInterface.cpp:20
llvm::Module * module
LLVM IR module used to parse/create types.
Definition: ConvertStandardToLLVM.h:81
Definition: PassRegistry.cpp:413
Integer types can have arbitrary bitwidth up to a large fixed limit.
Definition: StandardTypes.h:82
Definition: Attributes.h:139
Definition: DialectConversion.h:151
Function types map from a list of inputs to a list of results.
Definition: Types.h:190
LLVMTypeConverter & lowering
Definition: ConvertStandardToLLVM.h:239
Definition: StandardTypes.h:113
Definition: Location.h:52
Definition: DialectConversion.h:45
Definition: LLVM.h:37
Definition: ConvertStandardToLLVM.h:155
Definition: ConvertStandardToLLVM.h:130
Definition: PatternMatch.h:29
Definition: ConvertStandardToLLVM.h:211
Definition: StandardTypes.h:390
Type structType
Definition: ConvertStandardToLLVM.h:144
Definition: Types.h:84
Definition: Value.h:38
Definition: LLVM.h:35
Value value
Definition: ConvertStandardToLLVM.h:142
Definition: StandardTypes.h:256
LLVM::LLVMDialect * llvmDialect
Definition: ConvertStandardToLLVM.h:82
Definition: StandardTypes.h:444
Definition: DialectConversion.h:39
Conversion from types in the Standard dialect to the LLVM IR dialect.
Definition: ConvertStandardToLLVM.h:37
Definition: MLIRContext.h:34
Definition: ConvertStandardToLLVM.h:231
Definition: StandardTypes.h:70
Definition: Builders.h:158
Definition: OperationSupport.h:640
Definition: LLVMDialect.h:157
LLVM::LLVMDialect * getDialect()
Returns the LLVM dialect.
Definition: ConvertStandardToLLVM.h:63
Definition: LLVMDialect.h:44