MLIR  17.0.0git
TypeConverter.h
Go to the documentation of this file.
1 //===- TypeConverter.h - Convert builtin to LLVM dialect types --*- C++ -*-===//
2 //
3 // Part of the LLVM 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 type converter configuration for converting most builtin types to
10 // LLVM dialect types.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_CONVERSION_LLVMCOMMON_TYPECONVERTER_H
15 #define MLIR_CONVERSION_LLVMCOMMON_TYPECONVERTER_H
16 
19 
20 namespace mlir {
21 
22 class DataLayoutAnalysis;
23 class LowerToLLVMOptions;
24 
25 namespace LLVM {
26 class LLVMDialect;
27 class LLVMPointerType;
28 class LLVMFunctionType;
29 class LLVMStructType;
30 } // namespace LLVM
31 
32 /// Conversion from types to the LLVM IR dialect.
34  /// Give structFuncArgTypeConverter access to memref-specific functions.
35  friend LogicalResult
37  SmallVectorImpl<Type> &result);
38 
39 public:
41 
42  /// Create an LLVMTypeConverter using the default LowerToLLVMOptions.
43  /// Optionally takes a data layout analysis to use in conversions.
45  const DataLayoutAnalysis *analysis = nullptr);
46 
47  /// Create an LLVMTypeConverter using custom LowerToLLVMOptions. Optionally
48  /// takes a data layout analysis to use in conversions.
50  const DataLayoutAnalysis *analysis = nullptr);
51 
52  /// Convert a function type. The arguments and results are converted one by
53  /// one and results are packed into a wrapped LLVM IR structure type. `result`
54  /// is populated with argument mapping.
55  Type convertFunctionSignature(FunctionType funcTy, bool isVariadic,
56  SignatureConversion &result);
57 
58  /// Convert a non-empty list of types to be returned from a function into a
59  /// supported LLVM IR type. In particular, if more than one value is
60  /// returned, create an LLVM IR structure type with elements that correspond
61  /// to each of the MLIR types converted with `convertType`.
63 
64  /// Convert a type in the context of the default or bare pointer calling
65  /// convention. Calling convention sensitive types, such as MemRefType and
66  /// UnrankedMemRefType, are converted following the specific rules for the
67  /// calling convention. Calling convention independent types are converted
68  /// following the default LLVM type conversions.
70 
71  /// Promote the bare pointers in 'values' that resulted from memrefs to
72  /// descriptors. 'stdTypes' holds the types of 'values' before the conversion
73  /// to the LLVM-IR dialect (i.e., MemRefType, or any other builtin type).
75  Location loc, ArrayRef<Type> stdTypes,
76  SmallVectorImpl<Value> &values);
77 
78  /// Returns the MLIR context.
80 
81  /// Returns the LLVM dialect.
82  LLVM::LLVMDialect *getDialect() { return llvmDialect; }
83 
84  const LowerToLLVMOptions &getOptions() const { return options; }
85 
86  /// Set the lowering options to `newOptions`. Note: using this after some
87  /// some conversions have been performed can lead to inconsistencies in the
88  /// IR.
90  options = std::move(newOptions);
91  }
92 
93  /// Promote the LLVM representation of all operands including promoting MemRef
94  /// descriptors to stack and use pointers to struct to avoid the complexity
95  /// of the platform-specific C/C++ ABI lowering related to struct argument
96  /// passing.
98  ValueRange operands,
99  OpBuilder &builder);
100 
101  /// Promote the LLVM struct representation of one MemRef descriptor to stack
102  /// and use pointer to struct to avoid the complexity of the platform-specific
103  /// C/C++ ABI lowering related to struct argument passing.
105  OpBuilder &builder);
106 
107  /// Converts the function type to a C-compatible format, in particular using
108  /// pointers to memref descriptors for arguments. Also converts the return
109  /// type to a pointer argument if it is a struct. Returns true if this
110  /// was the case.
111  std::pair<LLVM::LLVMFunctionType, LLVM::LLVMStructType>
112  convertFunctionTypeCWrapper(FunctionType type);
113 
114  /// Returns the data layout to use during and after conversion.
115  const llvm::DataLayout &getDataLayout() { return options.dataLayout; }
116 
117  /// Returns the data layout analysis to query during conversion.
119  return dataLayoutAnalysis;
120  }
121 
122  /// Gets the LLVM representation of the index type. The returned type is an
123  /// integer type with the size configured for this type converter.
124  Type getIndexType();
125 
126  /// Returns true if using opaque pointers was enabled in the lowering options.
127  bool useOpaquePointers() const { return getOptions().useOpaquePointers; }
128 
129  /// Creates an LLVM pointer type with the given element type and address
130  /// space.
131  /// This function is meant to be used in code supporting both typed and opaque
132  /// pointers, as it will create an opaque pointer with the given address space
133  /// if opaque pointers are enabled in the lowering options.
134  LLVM::LLVMPointerType getPointerType(Type elementType,
135  unsigned addressSpace = 0);
136 
137  /// Gets the bitwidth of the index type when converted to LLVM.
138  unsigned getIndexTypeBitwidth() { return options.getIndexBitwidth(); }
139 
140  /// Gets the pointer bitwidth.
141  unsigned getPointerBitwidth(unsigned addressSpace = 0);
142 
143  /// Returns the size of the memref descriptor object in bytes.
144  unsigned getMemRefDescriptorSize(MemRefType type, const DataLayout &layout);
145 
146  /// Returns the size of the unranked memref descriptor object in bytes.
148  const DataLayout &layout);
149 
150  /// Return the LLVM address space corresponding to the memory space of the
151  /// memref type `type` or failure if the memory space cannot be converted to
152  /// an integer.
154 
155  /// Check if a memref type can be converted to a bare pointer.
156  static bool canConvertToBarePtr(BaseMemRefType type);
157 
158 protected:
159  /// Pointer to the LLVM dialect.
160  LLVM::LLVMDialect *llvmDialect;
161 
162 private:
163  /// Convert a function type. The arguments and results are converted one by
164  /// one. Additionally, if the function returns more than one value, pack the
165  /// results into an LLVM IR structure type so that the converted function type
166  /// returns at most one result.
167  Type convertFunctionType(FunctionType type);
168 
169  /// Convert the index type. Uses llvmModule data layout to create an integer
170  /// of the pointer bitwidth.
171  Type convertIndexType(IndexType type);
172 
173  /// Convert an integer type `i*` to `!llvm<"i*">`.
174  Type convertIntegerType(IntegerType type);
175 
176  /// Convert a floating point type: `f16` to `f16`, `f32` to
177  /// `f32` and `f64` to `f64`. `bf16` is not supported
178  /// by LLVM.
179  Type convertFloatType(FloatType type);
180 
181  /// Convert complex number type: `complex<f16>` to `!llvm<"{ half, half }">`,
182  /// `complex<f32>` to `!llvm<"{ float, float }">`, and `complex<f64>` to
183  /// `!llvm<"{ double, double }">`. `complex<bf16>` is not supported.
184  Type convertComplexType(ComplexType type);
185 
186  /// Convert a memref type into an LLVM type that captures the relevant data.
187  Type convertMemRefType(MemRefType type);
188 
189  /// Convert a memref type into a list of LLVM IR types that will form the
190  /// memref descriptor. If `unpackAggregates` is true the `sizes` and `strides`
191  /// arrays in the descriptors are unpacked to individual index-typed elements,
192  /// else they are are kept as rank-sized arrays of index type. In particular,
193  /// the list will contain:
194  /// - two pointers to the memref element type, followed by
195  /// - an index-typed offset, followed by
196  /// - (if unpackAggregates = true)
197  /// - one index-typed size per dimension of the memref, followed by
198  /// - one index-typed stride per dimension of the memref.
199  /// - (if unpackArrregates = false)
200  /// - one rank-sized array of index-type for the size of each dimension
201  /// - one rank-sized array of index-type for the stride of each dimension
202  ///
203  /// For example, memref<?x?xf32> is converted to the following list:
204  /// - `!llvm<"float*">` (allocated pointer),
205  /// - `!llvm<"float*">` (aligned pointer),
206  /// - `i64` (offset),
207  /// - `i64`, `i64` (sizes),
208  /// - `i64`, `i64` (strides).
209  /// These types can be recomposed to a memref descriptor struct.
210  SmallVector<Type, 5> getMemRefDescriptorFields(MemRefType type,
211  bool unpackAggregates);
212 
213  /// Convert an unranked memref type into a list of non-aggregate LLVM IR types
214  /// that will form the unranked memref descriptor. In particular, this list
215  /// contains:
216  /// - an integer rank, followed by
217  /// - a pointer to the memref descriptor struct.
218  /// For example, memref<*xf32> is converted to the following list:
219  /// i64 (rank)
220  /// !llvm<"i8*"> (type-erased pointer).
221  /// These types can be recomposed to a unranked memref descriptor struct.
222  SmallVector<Type, 2> getUnrankedMemRefDescriptorFields();
223 
224  /// Convert an unranked memref type to an LLVM type that captures the
225  /// runtime rank and a pointer to the static ranked memref desc
226  Type convertUnrankedMemRefType(UnrankedMemRefType type);
227 
228  /// Convert a memref type to a bare pointer to the memref element type.
229  Type convertMemRefToBarePtr(BaseMemRefType type);
230 
231  /// Convert a 1D vector type into an LLVM vector type.
232  Type convertVectorType(VectorType type);
233 
234  /// Options for customizing the llvm lowering.
235  LowerToLLVMOptions options;
236 
237  /// Data layout analysis mapping scopes to layouts active in them.
238  const DataLayoutAnalysis *dataLayoutAnalysis;
239 };
240 
241 /// Callback to convert function argument types. It converts a MemRef function
242 /// argument to a list of non-aggregate types containing descriptor
243 /// information, and an UnrankedmemRef function argument to a list containing
244 /// the rank and a pointer to a descriptor struct.
246  Type type,
247  SmallVectorImpl<Type> &result);
248 
249 /// Callback to convert function argument types. It converts MemRef function
250 /// arguments to bare pointers to the MemRef element type.
252  Type type,
253  SmallVectorImpl<Type> &result);
254 
255 } // namespace mlir
256 
257 #endif // MLIR_CONVERSION_LLVMCOMMON_TYPECONVERTER_H
This class provides a shared interface for ranked and unranked memref types.
Definition: BuiltinTypes.h:116
This class implements a pattern rewriter for use with ConversionPatterns.
Stores data layout objects for each operation that specifies the data layout above and below the give...
The main mechanism for performing data layout queries.
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:33
LLVM::LLVMDialect * llvmDialect
Pointer to the LLVM dialect.
unsigned getPointerBitwidth(unsigned addressSpace=0)
Gets the pointer bitwidth.
Type packFunctionResults(TypeRange types)
Convert a non-empty list of types to be returned from a function into a supported LLVM IR type.
LLVM::LLVMDialect * getDialect()
Returns the LLVM dialect.
Definition: TypeConverter.h:82
SmallVector< Value, 4 > promoteOperands(Location loc, ValueRange opOperands, ValueRange operands, OpBuilder &builder)
Promote the LLVM representation of all operands including promoting MemRef descriptors to stack and u...
std::pair< LLVM::LLVMFunctionType, LLVM::LLVMStructType > convertFunctionTypeCWrapper(FunctionType type)
Converts the function type to a C-compatible format, in particular using pointers to memref descripto...
const LowerToLLVMOptions & getOptions() const
Definition: TypeConverter.h:84
const llvm::DataLayout & getDataLayout()
Returns the data layout to use during and after conversion.
const DataLayoutAnalysis * getDataLayoutAnalysis() const
Returns the data layout analysis to query during conversion.
MLIRContext & getContext()
Returns the MLIR context.
Type convertCallingConventionType(Type type)
Convert a type in the context of the default or bare pointer calling convention.
Type getIndexType()
Gets the LLVM representation of the index type.
unsigned getMemRefDescriptorSize(MemRefType type, const DataLayout &layout)
Returns the size of the memref descriptor object in bytes.
void promoteBarePtrsToDescriptors(ConversionPatternRewriter &rewriter, Location loc, ArrayRef< Type > stdTypes, SmallVectorImpl< Value > &values)
Promote the bare pointers in 'values' that resulted from memrefs to descriptors.
LLVM::LLVMPointerType getPointerType(Type elementType, unsigned addressSpace=0)
Creates an LLVM pointer type with the given element type and address space.
Type convertFunctionSignature(FunctionType funcTy, bool isVariadic, SignatureConversion &result)
Convert a function type.
void dangerousSetOptions(LowerToLLVMOptions newOptions)
Set the lowering options to newOptions.
Definition: TypeConverter.h:89
FailureOr< unsigned > getMemRefAddressSpace(BaseMemRefType type)
Return the LLVM address space corresponding to the memory space of the memref type type or failure if...
friend LogicalResult structFuncArgTypeConverter(LLVMTypeConverter &converter, Type type, SmallVectorImpl< Type > &result)
Give structFuncArgTypeConverter access to memref-specific functions.
LLVMTypeConverter(MLIRContext *ctx, const DataLayoutAnalysis *analysis=nullptr)
Create an LLVMTypeConverter using the default LowerToLLVMOptions.
static bool canConvertToBarePtr(BaseMemRefType type)
Check if a memref type can be converted to a bare pointer.
unsigned getIndexTypeBitwidth()
Gets the bitwidth of the index type when converted to LLVM.
Value promoteOneMemRefDescriptor(Location loc, Value operand, OpBuilder &builder)
Promote the LLVM struct representation of one MemRef descriptor to stack and use pointer to struct to...
bool useOpaquePointers() const
Returns true if using opaque pointers was enabled in the lowering options.
unsigned getUnrankedMemRefDescriptorSize(UnrankedMemRefType type, const DataLayout &layout)
Returns the size of the unranked memref descriptor object in bytes.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
Options to control the LLVM lowering.
llvm::DataLayout dataLayout
The data layout of the module to produce.
unsigned getIndexBitwidth() const
Get the index bitwidth.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class helps build Operations.
Definition: Builders.h:202
This class provides all of the information necessary to convert a type signature.
Type conversion class.
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results)
Convert the given type.
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:370
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:93
Include the generated interface declarations.
LogicalResult barePtrFuncArgTypeConverter(LLVMTypeConverter &converter, Type type, SmallVectorImpl< Type > &result)
Callback to convert function argument types.
LogicalResult structFuncArgTypeConverter(LLVMTypeConverter &converter, Type type, SmallVectorImpl< Type > &result)
Callback to convert function argument types.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26