MLIR  15.0.0git
LLVMTypes.h
Go to the documentation of this file.
1 //===- LLVMDialect.h - MLIR 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 // This file defines the types for the LLVM dialect in MLIR. These MLIR types
10 // correspond to the LLVM IR type system.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_DIALECT_LLVMIR_LLVMTYPES_H_
15 #define MLIR_DIALECT_LLVMIR_LLVMTYPES_H_
16 
17 #include "mlir/IR/Types.h"
19 
20 namespace llvm {
21 class ElementCount;
22 class TypeSize;
23 } // namespace llvm
24 
25 namespace mlir {
26 
27 class AsmParser;
28 class AsmPrinter;
29 
30 namespace LLVM {
31 class LLVMDialect;
32 
33 namespace detail {
34 struct LLVMFunctionTypeStorage;
35 struct LLVMPointerTypeStorage;
36 struct LLVMStructTypeStorage;
37 struct LLVMTypeAndSizeStorage;
38 } // namespace detail
39 } // namespace LLVM
40 } // namespace mlir
41 
42 #include "mlir/Dialect/LLVMIR/LLVMTypeInterfaces.h.inc"
43 
44 namespace mlir {
45 namespace LLVM {
46 
47 //===----------------------------------------------------------------------===//
48 // Trivial types.
49 //===----------------------------------------------------------------------===//
50 
51 // Batch-define trivial types.
52 #define DEFINE_TRIVIAL_LLVM_TYPE(ClassName) \
53  class ClassName : public Type::TypeBase<ClassName, Type, TypeStorage> { \
54  public: \
55  using Base::Base; \
56  }
57 
58 DEFINE_TRIVIAL_LLVM_TYPE(LLVMVoidType);
59 DEFINE_TRIVIAL_LLVM_TYPE(LLVMPPCFP128Type);
60 DEFINE_TRIVIAL_LLVM_TYPE(LLVMX86MMXType);
61 DEFINE_TRIVIAL_LLVM_TYPE(LLVMTokenType);
62 DEFINE_TRIVIAL_LLVM_TYPE(LLVMLabelType);
63 DEFINE_TRIVIAL_LLVM_TYPE(LLVMMetadataType);
64 
65 #undef DEFINE_TRIVIAL_LLVM_TYPE
66 
67 //===----------------------------------------------------------------------===//
68 // LLVMArrayType.
69 //===----------------------------------------------------------------------===//
70 
71 /// LLVM dialect array type. It is an aggregate type representing consecutive
72 /// elements in memory, parameterized by the number of elements and the element
73 /// type.
75  : public Type::TypeBase<LLVMArrayType, Type, detail::LLVMTypeAndSizeStorage,
76  DataLayoutTypeInterface::Trait> {
77 public:
78  /// Inherit base constructors.
79  using Base::Base;
80  using Base::getChecked;
81 
82  /// Checks if the given type can be used inside an array type.
83  static bool isValidElementType(Type type);
84 
85  /// Gets or creates an instance of LLVM dialect array type containing
86  /// `numElements` of `elementType`, in the same context as `elementType`.
87  static LLVMArrayType get(Type elementType, unsigned numElements);
89  Type elementType, unsigned numElements);
90 
91  /// Returns the element type of the array.
92  Type getElementType() const;
93 
94  /// Returns the number of elements in the array type.
95  unsigned getNumElements() const;
96 
97  /// Verifies that the type about to be constructed is well-formed.
99  Type elementType, unsigned numElements);
100 
101  /// Hooks for DataLayoutTypeInterface. Should not be called directly. Obtain a
102  /// DataLayout instance and query it instead.
103  unsigned getTypeSizeInBits(const DataLayout &dataLayout,
104  DataLayoutEntryListRef params) const;
105 
106  unsigned getTypeSize(const DataLayout &dataLayout,
107  DataLayoutEntryListRef params) const;
108 
109  unsigned getABIAlignment(const DataLayout &dataLayout,
110  DataLayoutEntryListRef params) const;
111 
112  unsigned getPreferredAlignment(const DataLayout &dataLayout,
113  DataLayoutEntryListRef params) const;
114 };
115 
116 //===----------------------------------------------------------------------===//
117 // LLVMFunctionType.
118 //===----------------------------------------------------------------------===//
119 
120 /// LLVM dialect function type. It consists of a single return type (unlike MLIR
121 /// which can have multiple), a list of parameter types and can optionally be
122 /// variadic.
124  : public Type::TypeBase<LLVMFunctionType, Type,
125  detail::LLVMFunctionTypeStorage> {
126 public:
127  /// Inherit base constructors.
128  using Base::Base;
129  using Base::getChecked;
130 
131  /// Checks if the given type can be used an argument in a function type.
132  static bool isValidArgumentType(Type type);
133 
134  /// Checks if the given type can be used as a result in a function type.
135  static bool isValidResultType(Type type);
136 
137  /// Returns whether the function is variadic.
138  bool isVarArg() const;
139 
140  /// Gets or creates an instance of LLVM dialect function in the same context
141  /// as the `result` type.
142  static LLVMFunctionType get(Type result, ArrayRef<Type> arguments,
143  bool isVarArg = false);
144  static LLVMFunctionType
145  getChecked(function_ref<InFlightDiagnostic()> emitError, Type result,
146  ArrayRef<Type> arguments, bool isVarArg = false);
147 
148  /// Returns a clone of this function type with the given argument
149  /// and result types.
150  LLVMFunctionType clone(TypeRange inputs, TypeRange results) const;
151 
152  /// Returns the result type of the function.
153  Type getReturnType();
154 
155  /// Returns the result type of the function as an ArrayRef, enabling better
156  /// integration with generic MLIR utilities.
157  ArrayRef<Type> getReturnTypes();
158 
159  /// Returns the number of arguments to the function.
160  unsigned getNumParams();
161 
162  /// Returns `i`-th argument of the function. Asserts on out-of-bounds.
163  Type getParamType(unsigned i);
164 
165  /// Returns a list of argument types of the function.
166  ArrayRef<Type> getParams();
167  ArrayRef<Type> params() { return getParams(); }
168 
169  /// Verifies that the type about to be constructed is well-formed.
171  Type result, ArrayRef<Type> arguments, bool);
172 };
173 
174 //===----------------------------------------------------------------------===//
175 // LLVMPointerType.
176 //===----------------------------------------------------------------------===//
177 
178 /// LLVM dialect pointer type. This type typically represents a reference to an
179 /// object in memory. Pointers may be opaque or parameterized by the element
180 /// type. Both opaque and non-opaque pointers are additionally parameterized by
181 /// the address space.
182 class LLVMPointerType : public Type::TypeBase<LLVMPointerType, Type,
183  detail::LLVMPointerTypeStorage,
184  DataLayoutTypeInterface::Trait> {
185 public:
186  /// Inherit base constructors.
187  using Base::Base;
188 
189  /// Checks if the given type can have a pointer type pointing to it.
190  static bool isValidElementType(Type type);
191 
192  /// Gets or creates an instance of LLVM dialect pointer type pointing to an
193  /// object of `pointee` type in the given address space. The pointer type is
194  /// created in the same context as `pointee`. If the pointee is not provided,
195  /// creates an opaque pointer in the given context and address space.
196  static LLVMPointerType get(MLIRContext *context, unsigned addressSpace = 0);
197  static LLVMPointerType get(Type pointee, unsigned addressSpace = 0);
198  static LLVMPointerType
199  getChecked(function_ref<InFlightDiagnostic()> emitError, Type pointee,
200  unsigned addressSpace = 0);
201  static LLVMPointerType
202  getChecked(function_ref<InFlightDiagnostic()> emitError, MLIRContext *context,
203  unsigned addressSpace = 0);
204 
205  /// Returns the pointed-to type. It may be null if the pointer is opaque.
206  Type getElementType() const;
207 
208  /// Returns `true` if this type is the opaque pointer type, i.e., it has no
209  /// pointed-to type.
210  bool isOpaque() const;
211 
212  /// Returns the address space of the pointer.
213  unsigned getAddressSpace() const;
214 
215  /// Verifies that the type about to be constructed is well-formed.
217  Type pointee, unsigned);
219  MLIRContext *context, unsigned) {
220  return success();
221  }
222 
223  /// Hooks for DataLayoutTypeInterface. Should not be called directly. Obtain a
224  /// DataLayout instance and query it instead.
225  unsigned getTypeSizeInBits(const DataLayout &dataLayout,
226  DataLayoutEntryListRef params) const;
227  unsigned getABIAlignment(const DataLayout &dataLayout,
228  DataLayoutEntryListRef params) const;
229  unsigned getPreferredAlignment(const DataLayout &dataLayout,
230  DataLayoutEntryListRef params) const;
231  bool areCompatible(DataLayoutEntryListRef oldLayout,
232  DataLayoutEntryListRef newLayout) const;
233  LogicalResult verifyEntries(DataLayoutEntryListRef entries,
234  Location loc) const;
235 };
236 
237 //===----------------------------------------------------------------------===//
238 // LLVMStructType.
239 //===----------------------------------------------------------------------===//
240 
241 /// LLVM dialect structure type representing a collection of different-typed
242 /// elements manipulated together. Structured can optionally be packed, meaning
243 /// that their elements immediately follow each other in memory without
244 /// accounting for potential alignment.
245 ///
246 /// Structure types can be identified (named) or literal. Literal structures
247 /// are uniquely represented by the list of types they contain and packedness.
248 /// Literal structure types are immutable after construction.
249 ///
250 /// Identified structures are uniquely represented by their name, a string. They
251 /// have a mutable component, consisting of the list of types they contain,
252 /// the packedness and the opacity bits. Identified structs can be created
253 /// without providing the lists of element types, making them suitable to
254 /// represent recursive, i.e. self-referring, structures. Identified structs
255 /// without body are considered opaque. For such structs, one can set the body.
256 /// Identified structs can be created as intentionally-opaque, implying that the
257 /// caller does not intend to ever set the body (e.g. forward-declarations of
258 /// structs from another module) and wants to disallow further modification of
259 /// the body. For intentionally-opaque structs or non-opaque structs with the
260 /// body, one is not allowed to set another body (however, one can set exactly
261 /// the same body).
262 ///
263 /// Note that the packedness of the struct takes place in uniquing of literal
264 /// structs, but does not in uniquing of identified structs.
266  : public Type::TypeBase<LLVMStructType, Type, detail::LLVMStructTypeStorage,
267  DataLayoutTypeInterface::Trait> {
268 public:
269  /// Inherit base constructors.
270  using Base::Base;
271 
272  /// Checks if the given type can be contained in a structure type.
273  static bool isValidElementType(Type type);
274 
275  /// Gets or creates an identified struct with the given name in the provided
276  /// context. Note that unlike llvm::StructType::create, this function will
277  /// _NOT_ rename a struct in case a struct with the same name already exists
278  /// in the context. Instead, it will just return the existing struct,
279  /// similarly to the rest of MLIR type ::get methods.
280  static LLVMStructType getIdentified(MLIRContext *context, StringRef name);
281  static LLVMStructType
282  getIdentifiedChecked(function_ref<InFlightDiagnostic()> emitError,
283  MLIRContext *context, StringRef name);
284 
285  /// Gets a new identified struct with the given body. The body _cannot_ be
286  /// changed later. If a struct with the given name already exists, renames
287  /// the struct by appending a `.` followed by a number to the name. Renaming
288  /// happens even if the existing struct has the same body.
289  static LLVMStructType getNewIdentified(MLIRContext *context, StringRef name,
290  ArrayRef<Type> elements,
291  bool isPacked = false);
292 
293  /// Gets or creates a literal struct with the given body in the provided
294  /// context.
295  static LLVMStructType getLiteral(MLIRContext *context, ArrayRef<Type> types,
296  bool isPacked = false);
297  static LLVMStructType
298  getLiteralChecked(function_ref<InFlightDiagnostic()> emitError,
299  MLIRContext *context, ArrayRef<Type> types,
300  bool isPacked = false);
301 
302  /// Gets or creates an intentionally-opaque identified struct. Such a struct
303  /// cannot have its body set. To create an opaque struct with a mutable body,
304  /// use `getIdentified`. Note that unlike llvm::StructType::create, this
305  /// function will _NOT_ rename a struct in case a struct with the same name
306  /// already exists in the context. Instead, it will just return the existing
307  /// struct, similarly to the rest of MLIR type ::get methods.
308  static LLVMStructType getOpaque(StringRef name, MLIRContext *context);
309  static LLVMStructType
310  getOpaqueChecked(function_ref<InFlightDiagnostic()> emitError,
311  MLIRContext *context, StringRef name);
312 
313  /// Set the body of an identified struct. Returns failure if the body could
314  /// not be set, e.g. if the struct already has a body or if it was marked as
315  /// intentionally opaque. This might happen in a multi-threaded context when a
316  /// different thread modified the struct after it was created. Most callers
317  /// are likely to assert this always succeeds, but it is possible to implement
318  /// a local renaming scheme based on the result of this call.
319  LogicalResult setBody(ArrayRef<Type> types, bool isPacked);
320 
321  /// Checks if a struct is packed.
322  bool isPacked() const;
323 
324  /// Checks if a struct is identified.
325  bool isIdentified() const;
326 
327  /// Checks if a struct is opaque.
328  bool isOpaque();
329 
330  /// Checks if a struct is initialized.
331  bool isInitialized();
332 
333  /// Returns the name of an identified struct.
334  StringRef getName();
335 
336  /// Returns the list of element types contained in a non-opaque struct.
337  ArrayRef<Type> getBody() const;
338 
339  /// Verifies that the type about to be constructed is well-formed.
341  StringRef, bool);
343  ArrayRef<Type> types, bool);
344 
345  /// Hooks for DataLayoutTypeInterface. Should not be called directly. Obtain a
346  /// DataLayout instance and query it instead.
347  unsigned getTypeSizeInBits(const DataLayout &dataLayout,
348  DataLayoutEntryListRef params) const;
349 
350  unsigned getABIAlignment(const DataLayout &dataLayout,
351  DataLayoutEntryListRef params) const;
352 
353  unsigned getPreferredAlignment(const DataLayout &dataLayout,
354  DataLayoutEntryListRef params) const;
355 
356  bool areCompatible(DataLayoutEntryListRef oldLayout,
357  DataLayoutEntryListRef newLayout) const;
358 
359  LogicalResult verifyEntries(DataLayoutEntryListRef entries,
360  Location loc) const;
361 };
362 
363 //===----------------------------------------------------------------------===//
364 // LLVMFixedVectorType.
365 //===----------------------------------------------------------------------===//
366 
367 /// LLVM dialect fixed vector type, represents a sequence of elements of known
368 /// length that can be processed as one.
370  : public Type::TypeBase<LLVMFixedVectorType, Type,
371  detail::LLVMTypeAndSizeStorage> {
372 public:
373  /// Inherit base constructor.
374  using Base::Base;
375  using Base::getChecked;
376 
377  /// Gets or creates a fixed vector type containing `numElements` of
378  /// `elementType` in the same context as `elementType`.
379  static LLVMFixedVectorType get(Type elementType, unsigned numElements);
380  static LLVMFixedVectorType
381  getChecked(function_ref<InFlightDiagnostic()> emitError, Type elementType,
382  unsigned numElements);
383 
384  /// Checks if the given type can be used in a vector type. This type supports
385  /// only a subset of LLVM dialect types that don't have a built-in
386  /// counter-part, e.g., pointers.
387  static bool isValidElementType(Type type);
388 
389  /// Returns the element type of the vector.
391 
392  /// Returns the number of elements in the fixed vector.
393  unsigned getNumElements();
394 
395  /// Verifies that the type about to be constructed is well-formed.
397  Type elementType, unsigned numElements);
398 };
399 
400 //===----------------------------------------------------------------------===//
401 // LLVMScalableVectorType.
402 //===----------------------------------------------------------------------===//
403 
404 /// LLVM dialect scalable vector type, represents a sequence of elements of
405 /// unknown length that is known to be divisible by some constant. These
406 /// elements can be processed as one in SIMD context.
408  : public Type::TypeBase<LLVMScalableVectorType, Type,
409  detail::LLVMTypeAndSizeStorage> {
410 public:
411  /// Inherit base constructor.
412  using Base::Base;
413  using Base::getChecked;
414 
415  /// Gets or creates a scalable vector type containing a non-zero multiple of
416  /// `minNumElements` of `elementType` in the same context as `elementType`.
417  static LLVMScalableVectorType get(Type elementType, unsigned minNumElements);
419  getChecked(function_ref<InFlightDiagnostic()> emitError, Type elementType,
420  unsigned minNumElements);
421 
422  /// Checks if the given type can be used in a vector type.
423  static bool isValidElementType(Type type);
424 
425  /// Returns the element type of the vector.
427 
428  /// Returns the scaling factor of the number of elements in the vector. The
429  /// vector contains at least the resulting number of elements, or any non-zero
430  /// multiple of this number.
431  unsigned getMinNumElements();
432 
433  /// Verifies that the type about to be constructed is well-formed.
435  Type elementType, unsigned minNumElements);
436 };
437 
438 //===----------------------------------------------------------------------===//
439 // Printing and parsing.
440 //===----------------------------------------------------------------------===//
441 
442 namespace detail {
443 /// Parses an LLVM dialect type.
445 
446 /// Prints an LLVM Dialect type.
447 void printType(Type type, AsmPrinter &printer);
448 } // namespace detail
449 
450 //===----------------------------------------------------------------------===//
451 // Utility functions.
452 //===----------------------------------------------------------------------===//
453 
454 /// Returns `true` if the given type is compatible with the LLVM dialect.
455 bool isCompatibleType(Type type);
456 
457 /// Returns `true` if the given outer type is compatible with the LLVM dialect
458 /// without checking its potential nested types such as struct elements.
459 bool isCompatibleOuterType(Type type);
460 
461 /// Returns `true` if the given type is a floating-point type compatible with
462 /// the LLVM dialect.
464 
465 /// Returns `true` if the given type is a vector type compatible with the LLVM
466 /// dialect. Compatible types include 1D built-in vector types of built-in
467 /// integers and floating-point values, LLVM dialect fixed vector types of LLVM
468 /// dialect pointers and LLVM dialect scalable vector types.
469 bool isCompatibleVectorType(Type type);
470 
471 /// Returns the element type of any vector type compatible with the LLVM
472 /// dialect.
474 
475 /// Returns the element count of any LLVM-compatible vector type.
476 llvm::ElementCount getVectorNumElements(Type type);
477 
478 /// Returns whether a vector type is scalable or not.
480 
481 /// Creates an LLVM dialect-compatible vector type with the given element type
482 /// and length.
483 Type getVectorType(Type elementType, unsigned numElements,
484  bool isScalable = false);
485 
486 /// Creates an LLVM dialect-compatible type with the given element type and
487 /// length.
488 Type getFixedVectorType(Type elementType, unsigned numElements);
489 
490 /// Creates an LLVM dialect-compatible type with the given element type and
491 /// length.
492 Type getScalableVectorType(Type elementType, unsigned numElements);
493 
494 /// Returns the size of the given primitive LLVM dialect-compatible type
495 /// (including vectors) in bits, for example, the size of i16 is 16 and
496 /// the size of vector<4xi16> is 64. Returns 0 for non-primitive
497 /// (aggregates such as struct) or types that don't have a size (such as void).
498 llvm::TypeSize getPrimitiveTypeSizeInBits(Type type);
499 
500 } // namespace LLVM
501 } // namespace mlir
502 
503 #endif // MLIR_DIALECT_LLVMIR_LLVMTYPES_H_
Include the generated interface declarations.
bool isCompatibleFloatingPointType(Type type)
Returns true if the given type is a floating-point type compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:783
llvm::ElementCount getVectorNumElements(Type type)
Returns the element count of any LLVM-compatible vector type.
Definition: LLVMTypes.cpp:813
Type getFixedVectorType(Type elementType, unsigned numElements)
Creates an LLVM dialect-compatible type with the given element type and length.
Definition: LLVMTypes.cpp:856
LLVM dialect scalable vector type, represents a sequence of elements of unknown length that is known ...
Definition: LLVMTypes.h:407
Explicitly register a set of "builtin" types.
Definition: CallGraph.h:221
bool isCompatibleOuterType(Type type)
Returns true if the given outer type is compatible with the LLVM dialect without checking its potenti...
Definition: LLVMTypes.cpp:687
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:311
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity...
Definition: SPIRVOps.cpp:687
LLVM dialect function type.
Definition: LLVMTypes.h:123
void printType(Type type, AsmPrinter &printer)
Prints an LLVM Dialect type.
ArrayRef< Type > params()
Definition: LLVMTypes.h:167
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:48
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:778
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:38
static LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, MLIRContext *context, unsigned)
Definition: LLVMTypes.h:218
Type getScalableVectorType(Type elementType, unsigned numElements)
Creates an LLVM dialect-compatible type with the given element type and length.
Definition: LLVMTypes.cpp:867
#define DEFINE_TRIVIAL_LLVM_TYPE(ClassName)
Definition: LLVMTypes.h:52
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:72
LLVM dialect array type.
Definition: LLVMTypes.h:74
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
Type parseType(DialectAsmParser &parser)
Parses an LLVM dialect type.
LLVM dialect structure type representing a collection of different-typed elements manipulated togethe...
Definition: LLVMTypes.h:265
static int64_t getNumElements(ShapedType type)
Definition: TensorOps.cpp:751
bool isCompatibleVectorType(Type type)
Returns true if the given type is a vector type compatible with the LLVM dialect. ...
Definition: LLVMTypes.cpp:788
static VectorType vectorType(CodeGen &codegen, Type etp)
Constructs vector type.
Utility class for implementing users of storage classes uniqued by a StorageUniquer.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55
LLVM dialect pointer type.
Definition: LLVMTypes.h:182
llvm::TypeSize getPrimitiveTypeSizeInBits(Type type)
Returns the size of the given primitive LLVM dialect-compatible type (including vectors) in bits...
Definition: LLVMTypes.cpp:879
bool isScalableVectorType(Type vectorType)
Returns whether a vector type is scalable or not.
Definition: LLVMTypes.cpp:831
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs, on this operation and any nested operations.
Definition: Verifier.cpp:372
static VectorType getVectorType(Type scalarTy, const VectorizationStrategy *strategy)
Returns the vector type resulting from applying the provided vectorization strategy on the scalar typ...
This base class exposes generic asm printer hooks, usable across the various derived printers...
LLVM dialect fixed vector type, represents a sequence of elements of known length that can be process...
Definition: LLVMTypes.h:369
Type getVectorElementType(Type type)
Returns the element type of any vector type compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:804
The DialectAsmParser has methods for interacting with the asm parser when parsing attributes and type...
The main mechanism for performing data layout queries.