MLIR  19.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 
18 #include "mlir/IR/BuiltinTypes.h"
20 
21 namespace mlir {
22 
23 class DataLayoutAnalysis;
24 class LowerToLLVMOptions;
25 
26 namespace LLVM {
27 class LLVMDialect;
28 class LLVMPointerType;
29 class LLVMFunctionType;
30 class LLVMStructType;
31 } // namespace LLVM
32 
33 /// Conversion from types to the LLVM IR dialect.
35  /// Give structFuncArgTypeConverter access to memref-specific functions.
36  friend LogicalResult
38  SmallVectorImpl<Type> &result);
39 
40 public:
42 
43  /// Create an LLVMTypeConverter using the default LowerToLLVMOptions.
44  /// Optionally takes a data layout analysis to use in conversions.
46  const DataLayoutAnalysis *analysis = nullptr);
47 
48  /// Create an LLVMTypeConverter using custom LowerToLLVMOptions. Optionally
49  /// takes a data layout analysis to use in conversions.
51  const DataLayoutAnalysis *analysis = nullptr);
52 
53  /// Convert a function type. The arguments and results are converted one by
54  /// one and results are packed into a wrapped LLVM IR structure type. `result`
55  /// is populated with argument mapping.
56  Type convertFunctionSignature(FunctionType funcTy, bool isVariadic,
57  bool useBarePtrCallConv,
58  SignatureConversion &result) const;
59 
60  /// Convert a non-empty list of types to be returned from a function into an
61  /// LLVM-compatible type. In particular, if more than one value is returned,
62  /// create an LLVM dialect structure type with elements that correspond to
63  /// each of the types converted with `convertCallingConventionType`.
65  bool useBarePointerCallConv = false) const;
66 
67  /// Convert a non-empty list of types of values produced by an operation into
68  /// an LLVM-compatible type. In particular, if more than one value is
69  /// produced, create a literal structure with elements that correspond to each
70  /// of the LLVM-compatible types converted with `convertType`.
71  Type packOperationResults(TypeRange types) const;
72 
73  /// Convert a type in the context of the default or bare pointer calling
74  /// convention. Calling convention sensitive types, such as MemRefType and
75  /// UnrankedMemRefType, are converted following the specific rules for the
76  /// calling convention. Calling convention independent types are converted
77  /// following the default LLVM type conversions.
79  bool useBarePointerCallConv = false) const;
80 
81  /// Promote the bare pointers in 'values' that resulted from memrefs to
82  /// descriptors. 'stdTypes' holds the types of 'values' before the conversion
83  /// to the LLVM-IR dialect (i.e., MemRefType, or any other builtin type).
85  Location loc, ArrayRef<Type> stdTypes,
86  SmallVectorImpl<Value> &values) const;
87 
88  /// Returns the MLIR context.
89  MLIRContext &getContext() const;
90 
91  /// Returns the LLVM dialect.
92  LLVM::LLVMDialect *getDialect() const { return llvmDialect; }
93 
94  const LowerToLLVMOptions &getOptions() const { return options; }
95 
96  /// Promote the LLVM representation of all operands including promoting MemRef
97  /// descriptors to stack and use pointers to struct to avoid the complexity
98  /// of the platform-specific C/C++ ABI lowering related to struct argument
99  /// passing.
101  ValueRange operands, OpBuilder &builder,
102  bool useBarePtrCallConv = false) const;
103 
104  /// Promote the LLVM struct representation of one MemRef descriptor to stack
105  /// and use pointer to struct to avoid the complexity of the platform-specific
106  /// C/C++ ABI lowering related to struct argument passing.
108  OpBuilder &builder) const;
109 
110  /// Converts the function type to a C-compatible format, in particular using
111  /// pointers to memref descriptors for arguments. Also converts the return
112  /// type to a pointer argument if it is a struct. Returns true if this
113  /// was the case.
114  std::pair<LLVM::LLVMFunctionType, LLVM::LLVMStructType>
115  convertFunctionTypeCWrapper(FunctionType type) const;
116 
117  /// Returns the data layout to use during and after conversion.
118  const llvm::DataLayout &getDataLayout() const { return options.dataLayout; }
119 
120  /// Returns the data layout analysis to query during conversion.
122  return dataLayoutAnalysis;
123  }
124 
125  /// Gets the LLVM representation of the index type. The returned type is an
126  /// integer type with the size configured for this type converter.
127  Type getIndexType() const;
128 
129  /// Gets the bitwidth of the index type when converted to LLVM.
130  unsigned getIndexTypeBitwidth() const { return options.getIndexBitwidth(); }
131 
132  /// Gets the pointer bitwidth.
133  unsigned getPointerBitwidth(unsigned addressSpace = 0) const;
134 
135  /// Returns the size of the memref descriptor object in bytes.
136  unsigned getMemRefDescriptorSize(MemRefType type,
137  const DataLayout &layout) const;
138 
139  /// Returns the size of the unranked memref descriptor object in bytes.
141  const DataLayout &layout) const;
142 
143  /// Return the LLVM address space corresponding to the memory space of the
144  /// memref type `type` or failure if the memory space cannot be converted to
145  /// an integer.
147 
148  /// Check if a memref type can be converted to a bare pointer.
149  static bool canConvertToBarePtr(BaseMemRefType type);
150 
151 protected:
152  /// Pointer to the LLVM dialect.
153  LLVM::LLVMDialect *llvmDialect;
154 
155  // Recursive structure detection.
156  // We store one entry per thread here, and rely on locking.
158  llvm::sys::SmartRWMutex<true> callStackMutex;
160 
161 private:
162  /// Convert a function type. The arguments and results are converted one by
163  /// one. Additionally, if the function returns more than one value, pack the
164  /// results into an LLVM IR structure type so that the converted function type
165  /// returns at most one result.
166  Type convertFunctionType(FunctionType type) const;
167 
168  /// Convert the index type. Uses llvmModule data layout to create an integer
169  /// of the pointer bitwidth.
170  Type convertIndexType(IndexType type) const;
171 
172  /// Convert an integer type `i*` to `!llvm<"i*">`.
173  Type convertIntegerType(IntegerType type) const;
174 
175  /// Convert a floating point type: `f16` to `f16`, `f32` to
176  /// `f32` and `f64` to `f64`. `bf16` is not supported
177  /// by LLVM. 8-bit float types are converted to 8-bit integers as this is how
178  /// all LLVM backends that support them currently represent them.
179  Type convertFloatType(FloatType type) const;
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) const;
185 
186  /// Convert a memref type into an LLVM type that captures the relevant data.
187  Type convertMemRefType(MemRefType type) const;
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 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) const;
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() const;
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) const;
227 
228  /// Convert a memref type to a bare pointer to the memref element type.
229  Type convertMemRefToBarePtr(BaseMemRefType type) const;
230 
231  /// Convert a 1D vector type into an LLVM vector type.
232  FailureOr<Type> convertVectorType(VectorType type) const;
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:138
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:34
LLVM::LLVMDialect * llvmDialect
Pointer to the LLVM dialect.
llvm::sys::SmartRWMutex< true > callStackMutex
Type packOperationResults(TypeRange types) const
Convert a non-empty list of types of values produced by an operation into an LLVM-compatible type.
LLVM::LLVMDialect * getDialect() const
Returns the LLVM dialect.
Definition: TypeConverter.h:92
Type packFunctionResults(TypeRange types, bool useBarePointerCallConv=false) const
Convert a non-empty list of types to be returned from a function into an LLVM-compatible type.
const LowerToLLVMOptions & getOptions() const
Definition: TypeConverter.h:94
unsigned getUnrankedMemRefDescriptorSize(UnrankedMemRefType type, const DataLayout &layout) const
Returns the size of the unranked memref descriptor object in bytes.
Type convertFunctionSignature(FunctionType funcTy, bool isVariadic, bool useBarePtrCallConv, SignatureConversion &result) const
Convert a function type.
void promoteBarePtrsToDescriptors(ConversionPatternRewriter &rewriter, Location loc, ArrayRef< Type > stdTypes, SmallVectorImpl< Value > &values) const
Promote the bare pointers in 'values' that resulted from memrefs to descriptors.
const DataLayoutAnalysis * getDataLayoutAnalysis() const
Returns the data layout analysis to query during conversion.
DenseMap< uint64_t, std::unique_ptr< SmallVector< Type > > > conversionCallStack
SmallVector< Type > & getCurrentThreadRecursiveStack()
Value promoteOneMemRefDescriptor(Location loc, Value operand, OpBuilder &builder) const
Promote the LLVM struct representation of one MemRef descriptor to stack and use pointer to struct to...
Type convertCallingConventionType(Type type, bool useBarePointerCallConv=false) const
Convert a type in the context of the default or bare pointer calling convention.
SmallVector< Value, 4 > promoteOperands(Location loc, ValueRange opOperands, ValueRange operands, OpBuilder &builder, bool useBarePtrCallConv=false) const
Promote the LLVM representation of all operands including promoting MemRef descriptors to stack and u...
LLVMTypeConverter(MLIRContext *ctx, const DataLayoutAnalysis *analysis=nullptr)
Create an LLVMTypeConverter using the default LowerToLLVMOptions.
unsigned getPointerBitwidth(unsigned addressSpace=0) const
Gets the pointer bitwidth.
FailureOr< unsigned > getMemRefAddressSpace(BaseMemRefType type) const
Return the LLVM address space corresponding to the memory space of the memref type type or failure if...
const llvm::DataLayout & getDataLayout() const
Returns the data layout to use during and after conversion.
static bool canConvertToBarePtr(BaseMemRefType type)
Check if a memref type can be converted to a bare pointer.
MLIRContext & getContext() const
Returns the MLIR context.
unsigned getMemRefDescriptorSize(MemRefType type, const DataLayout &layout) const
Returns the size of the memref descriptor object in bytes.
std::pair< LLVM::LLVMFunctionType, LLVM::LLVMStructType > convertFunctionTypeCWrapper(FunctionType type) const
Converts the function type to a C-compatible format, in particular using pointers to memref descripto...
unsigned getIndexTypeBitwidth() const
Gets the bitwidth of the index type when converted to LLVM.
Type getIndexType() const
Gets the LLVM representation of the index type.
friend LogicalResult structFuncArgTypeConverter(const LLVMTypeConverter &converter, Type type, SmallVectorImpl< Type > &result)
Give structFuncArgTypeConverter access to memref-specific functions.
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:209
This class provides all of the information necessary to convert a type signature.
Type conversion class.
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results) const
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:378
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Include the generated interface declarations.
LogicalResult barePtrFuncArgTypeConverter(const LLVMTypeConverter &converter, Type type, SmallVectorImpl< Type > &result)
Callback to convert function argument types.
LogicalResult structFuncArgTypeConverter(const 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