MLIR  17.0.0git
LLVMTypes.h
Go to the documentation of this file.
1 //===- LLVMTypes.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 
18 #include "mlir/IR/Types.h"
20 #include <optional>
21 
22 namespace llvm {
23 class ElementCount;
24 class TypeSize;
25 } // namespace llvm
26 
27 namespace mlir {
28 
29 class AsmParser;
30 class AsmPrinter;
31 
32 namespace LLVM {
33 class LLVMDialect;
34 
35 namespace detail {
36 struct LLVMFunctionTypeStorage;
37 struct LLVMPointerTypeStorage;
38 struct LLVMStructTypeStorage;
39 struct LLVMTypeAndSizeStorage;
40 } // namespace detail
41 } // namespace LLVM
42 } // namespace mlir
43 
44 //===----------------------------------------------------------------------===//
45 // ODS-Generated Declarations
46 //===----------------------------------------------------------------------===//
47 
48 #include "mlir/Dialect/LLVMIR/LLVMTypeInterfaces.h.inc"
49 
50 #define GET_TYPEDEF_CLASSES
51 #include "mlir/Dialect/LLVMIR/LLVMTypes.h.inc"
52 
53 namespace mlir {
54 namespace LLVM {
55 
56 //===----------------------------------------------------------------------===//
57 // Trivial types.
58 //===----------------------------------------------------------------------===//
59 
60 // Batch-define trivial types.
61 #define DEFINE_TRIVIAL_LLVM_TYPE(ClassName) \
62  class ClassName : public Type::TypeBase<ClassName, Type, TypeStorage> { \
63  public: \
64  using Base::Base; \
65  }
66 
68 DEFINE_TRIVIAL_LLVM_TYPE(LLVMPPCFP128Type);
69 DEFINE_TRIVIAL_LLVM_TYPE(LLVMX86MMXType);
70 DEFINE_TRIVIAL_LLVM_TYPE(LLVMTokenType);
71 DEFINE_TRIVIAL_LLVM_TYPE(LLVMLabelType);
72 DEFINE_TRIVIAL_LLVM_TYPE(LLVMMetadataType);
73 
74 #undef DEFINE_TRIVIAL_LLVM_TYPE
75 
76 //===----------------------------------------------------------------------===//
77 // LLVMStructType.
78 //===----------------------------------------------------------------------===//
79 
80 /// LLVM dialect structure type representing a collection of different-typed
81 /// elements manipulated together. Structured can optionally be packed, meaning
82 /// that their elements immediately follow each other in memory without
83 /// accounting for potential alignment.
84 ///
85 /// Structure types can be identified (named) or literal. Literal structures
86 /// are uniquely represented by the list of types they contain and packedness.
87 /// Literal structure types are immutable after construction.
88 ///
89 /// Identified structures are uniquely represented by their name, a string. They
90 /// have a mutable component, consisting of the list of types they contain,
91 /// the packedness and the opacity bits. Identified structs can be created
92 /// without providing the lists of element types, making them suitable to
93 /// represent recursive, i.e. self-referring, structures. Identified structs
94 /// without body are considered opaque. For such structs, one can set the body.
95 /// Identified structs can be created as intentionally-opaque, implying that the
96 /// caller does not intend to ever set the body (e.g. forward-declarations of
97 /// structs from another module) and wants to disallow further modification of
98 /// the body. For intentionally-opaque structs or non-opaque structs with the
99 /// body, one is not allowed to set another body (however, one can set exactly
100 /// the same body).
101 ///
102 /// Note that the packedness of the struct takes place in uniquing of literal
103 /// structs, but does not in uniquing of identified structs.
105  : public Type::TypeBase<LLVMStructType, Type, detail::LLVMStructTypeStorage,
106  DataLayoutTypeInterface::Trait,
107  SubElementTypeInterface::Trait,
108  TypeTrait::IsMutable> {
109 public:
110  /// Inherit base constructors.
111  using Base::Base;
112 
113  /// Checks if the given type can be contained in a structure type.
114  static bool isValidElementType(Type type);
115 
116  /// Gets or creates an identified struct with the given name in the provided
117  /// context. Note that unlike llvm::StructType::create, this function will
118  /// _NOT_ rename a struct in case a struct with the same name already exists
119  /// in the context. Instead, it will just return the existing struct,
120  /// similarly to the rest of MLIR type ::get methods.
121  static LLVMStructType getIdentified(MLIRContext *context, StringRef name);
122  static LLVMStructType
124  MLIRContext *context, StringRef name);
125 
126  /// Gets a new identified struct with the given body. The body _cannot_ be
127  /// changed later. If a struct with the given name already exists, renames
128  /// the struct by appending a `.` followed by a number to the name. Renaming
129  /// happens even if the existing struct has the same body.
130  static LLVMStructType getNewIdentified(MLIRContext *context, StringRef name,
131  ArrayRef<Type> elements,
132  bool isPacked = false);
133 
134  /// Gets or creates a literal struct with the given body in the provided
135  /// context.
136  static LLVMStructType getLiteral(MLIRContext *context, ArrayRef<Type> types,
137  bool isPacked = false);
138  static LLVMStructType
140  MLIRContext *context, ArrayRef<Type> types,
141  bool isPacked = false);
142 
143  /// Gets or creates an intentionally-opaque identified struct. Such a struct
144  /// cannot have its body set. To create an opaque struct with a mutable body,
145  /// use `getIdentified`. Note that unlike llvm::StructType::create, this
146  /// function will _NOT_ rename a struct in case a struct with the same name
147  /// already exists in the context. Instead, it will just return the existing
148  /// struct, similarly to the rest of MLIR type ::get methods.
149  static LLVMStructType getOpaque(StringRef name, MLIRContext *context);
150  static LLVMStructType
152  MLIRContext *context, StringRef name);
153 
154  /// Set the body of an identified struct. Returns failure if the body could
155  /// not be set, e.g. if the struct already has a body or if it was marked as
156  /// intentionally opaque. This might happen in a multi-threaded context when a
157  /// different thread modified the struct after it was created. Most callers
158  /// are likely to assert this always succeeds, but it is possible to implement
159  /// a local renaming scheme based on the result of this call.
161 
162  /// Checks if a struct is packed.
163  bool isPacked() const;
164 
165  /// Checks if a struct is identified.
166  bool isIdentified() const;
167 
168  /// Checks if a struct is opaque.
169  bool isOpaque();
170 
171  /// Checks if a struct is initialized.
172  bool isInitialized();
173 
174  /// Returns the name of an identified struct.
175  StringRef getName();
176 
177  /// Returns the list of element types contained in a non-opaque struct.
178  ArrayRef<Type> getBody() const;
179 
180  /// Verifies that the type about to be constructed is well-formed.
182  StringRef, bool);
184  ArrayRef<Type> types, bool);
185 
186  /// Hooks for DataLayoutTypeInterface. Should not be called directly. Obtain a
187  /// DataLayout instance and query it instead.
188  unsigned getTypeSizeInBits(const DataLayout &dataLayout,
189  DataLayoutEntryListRef params) const;
190 
191  unsigned getABIAlignment(const DataLayout &dataLayout,
192  DataLayoutEntryListRef params) const;
193 
194  unsigned getPreferredAlignment(const DataLayout &dataLayout,
195  DataLayoutEntryListRef params) const;
196 
197  bool areCompatible(DataLayoutEntryListRef oldLayout,
198  DataLayoutEntryListRef newLayout) const;
199 
201  Location loc) const;
202 
203  void walkImmediateSubElements(function_ref<void(Attribute)> walkAttrsFn,
204  function_ref<void(Type)> walkTypesFn) const;
206  ArrayRef<Type> replTypes) const;
207 };
208 
209 //===----------------------------------------------------------------------===//
210 // Printing and parsing.
211 //===----------------------------------------------------------------------===//
212 
213 namespace detail {
214 /// Parses an LLVM dialect type.
216 
217 /// Prints an LLVM Dialect type.
218 void printType(Type type, AsmPrinter &printer);
219 } // namespace detail
220 
221 /// Parse any MLIR type or a concise syntax for LLVM types.
223 /// Print any MLIR type or a concise syntax for LLVM types.
224 void printPrettyLLVMType(AsmPrinter &p, Type type);
225 
226 //===----------------------------------------------------------------------===//
227 // Utility functions.
228 //===----------------------------------------------------------------------===//
229 
230 /// Returns `true` if the given type is compatible with the LLVM dialect. This
231 /// is an alias to `LLVMDialect::isCompatibleType`.
232 bool isCompatibleType(Type type);
233 
234 /// Returns `true` if the given outer type is compatible with the LLVM dialect
235 /// without checking its potential nested types such as struct elements.
236 bool isCompatibleOuterType(Type type);
237 
238 /// Returns `true` if the given type is a floating-point type compatible with
239 /// the LLVM dialect.
241 
242 /// Returns `true` if the given type is a vector type compatible with the LLVM
243 /// dialect. Compatible types include 1D built-in vector types of built-in
244 /// integers and floating-point values, LLVM dialect fixed vector types of LLVM
245 /// dialect pointers and LLVM dialect scalable vector types.
246 bool isCompatibleVectorType(Type type);
247 
248 /// Returns the element type of any vector type compatible with the LLVM
249 /// dialect.
251 
252 /// Returns the element count of any LLVM-compatible vector type.
253 llvm::ElementCount getVectorNumElements(Type type);
254 
255 /// Returns whether a vector type is scalable or not.
256 bool isScalableVectorType(Type vectorType);
257 
258 /// Creates an LLVM dialect-compatible vector type with the given element type
259 /// and length.
260 Type getVectorType(Type elementType, unsigned numElements,
261  bool isScalable = false);
262 
263 /// Creates an LLVM dialect-compatible vector type with the given element type
264 /// and length.
265 Type getVectorType(Type elementType, const llvm::ElementCount &numElements);
266 
267 /// Creates an LLVM dialect-compatible type with the given element type and
268 /// length.
269 Type getFixedVectorType(Type elementType, unsigned numElements);
270 
271 /// Creates an LLVM dialect-compatible type with the given element type and
272 /// length.
273 Type getScalableVectorType(Type elementType, unsigned numElements);
274 
275 /// Returns the size of the given primitive LLVM dialect-compatible type
276 /// (including vectors) in bits, for example, the size of i16 is 16 and
277 /// the size of vector<4xi16> is 64. Returns 0 for non-primitive
278 /// (aggregates such as struct) or types that don't have a size (such as void).
279 llvm::TypeSize getPrimitiveTypeSizeInBits(Type type);
280 
281 /// The positions of different values in the data layout entry for pointers.
282 enum class PtrDLEntryPos { Size = 0, Abi = 1, Preferred = 2, Index = 3 };
283 
284 /// Returns the value that corresponds to named position `pos` from the
285 /// data layout entry `attr` assuming it's a dense integer elements attribute.
286 /// Returns `std::nullopt` if `pos` is not present in the entry.
287 /// Currently only `PtrDLEntryPos::Index` is optional, and all other positions
288 /// may be assumed to be present.
289 std::optional<unsigned> extractPointerSpecValue(Attribute attr,
290  PtrDLEntryPos pos);
291 
292 } // namespace LLVM
293 } // namespace mlir
294 
295 #endif // MLIR_DIALECT_LLVMIR_LLVMTYPES_H_
This base class exposes generic asm parser hooks, usable across the various derived parsers.
This base class exposes generic asm printer hooks, usable across the various derived printers.
Attributes are known-constant values of operations.
Definition: Attributes.h:25
The main mechanism for performing data layout queries.
The DialectAsmParser has methods for interacting with the asm parser when parsing attributes and type...
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:308
LLVM dialect structure type representing a collection of different-typed elements manipulated togethe...
Definition: LLVMTypes.h:108
static LLVMStructType getLiteralChecked(function_ref< InFlightDiagnostic()> emitError, MLIRContext *context, ArrayRef< Type > types, bool isPacked=false)
Definition: LLVMTypes.cpp:453
LogicalResult setBody(ArrayRef< Type > types, bool isPacked)
Set the body of an identified struct.
Definition: LLVMTypes.cpp:469
unsigned getTypeSizeInBits(const DataLayout &dataLayout, DataLayoutEntryListRef params) const
Hooks for DataLayoutTypeInterface.
Definition: LLVMTypes.cpp:505
static bool isValidElementType(Type type)
Checks if the given type can be contained in a structure type.
Definition: LLVMTypes.cpp:414
unsigned getPreferredAlignment(const DataLayout &dataLayout, DataLayoutEntryListRef params) const
Definition: LLVMTypes.cpp:582
StringRef getName()
Returns the name of an identified struct.
Definition: LLVMTypes.cpp:483
static LLVMStructType getOpaqueChecked(function_ref< InFlightDiagnostic()> emitError, MLIRContext *context, StringRef name)
Definition: LLVMTypes.cpp:464
LogicalResult verifyEntries(DataLayoutEntryListRef entries, Location loc) const
Definition: LLVMTypes.cpp:616
bool isPacked() const
Checks if a struct is packed.
Definition: LLVMTypes.cpp:476
static LLVMStructType getIdentifiedChecked(function_ref< InFlightDiagnostic()> emitError, MLIRContext *context, StringRef name)
Definition: LLVMTypes.cpp:424
ArrayRef< Type > getBody() const
Returns the list of element types contained in a non-opaque struct.
Definition: LLVMTypes.cpp:484
bool isInitialized()
Checks if a struct is initialized.
Definition: LLVMTypes.cpp:482
void walkImmediateSubElements(function_ref< void(Attribute)> walkAttrsFn, function_ref< void(Type)> walkTypesFn) const
Definition: LLVMTypes.cpp:646
bool isOpaque()
Checks if a struct is opaque.
Definition: LLVMTypes.cpp:478
static LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, StringRef, bool)
Verifies that the type about to be constructed is well-formed.
Definition: LLVMTypes.cpp:489
bool areCompatible(DataLayoutEntryListRef oldLayout, DataLayoutEntryListRef newLayout) const
Definition: LLVMTypes.cpp:593
unsigned getABIAlignment(const DataLayout &dataLayout, DataLayoutEntryListRef params) const
Definition: LLVMTypes.cpp:575
static LLVMStructType getLiteral(MLIRContext *context, ArrayRef< Type > types, bool isPacked=false)
Gets or creates a literal struct with the given body in the provided context.
Definition: LLVMTypes.cpp:447
static LLVMStructType getNewIdentified(MLIRContext *context, StringRef name, ArrayRef< Type > elements, bool isPacked=false)
Gets a new identified struct with the given body.
Definition: LLVMTypes.cpp:430
static LLVMStructType getIdentified(MLIRContext *context, StringRef name)
Gets or creates an identified struct with the given name in the provided context.
Definition: LLVMTypes.cpp:419
static LLVMStructType getOpaque(StringRef name, MLIRContext *context)
Gets or creates an intentionally-opaque identified struct.
Definition: LLVMTypes.cpp:459
Type replaceImmediateSubElements(ArrayRef< Attribute > replAttrs, ArrayRef< Type > replTypes) const
Definition: LLVMTypes.cpp:653
bool isIdentified() const
Checks if a struct is identified.
Definition: LLVMTypes.cpp:477
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:64
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:56
This class represents success/failure for parsing-like operations that find it important to chain tog...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
Utility class for implementing users of storage classes uniqued by a StorageUniquer.
Include the generated interface declarations.
Definition: CallGraph.h:229
void printType(Type type, AsmPrinter &printer)
Prints an LLVM Dialect type.
Type parseType(DialectAsmParser &parser)
Parses an LLVM dialect type.
Type getVectorType(Type elementType, unsigned numElements, bool isScalable=false)
Creates an LLVM dialect-compatible vector type with the given element type and length.
Definition: LLVMTypes.cpp:908
llvm::TypeSize getPrimitiveTypeSizeInBits(Type type)
Returns the size of the given primitive LLVM dialect-compatible type (including vectors) in bits,...
Definition: LLVMTypes.cpp:955
void printPrettyLLVMType(AsmPrinter &p, Type type)
Print any MLIR type or a concise syntax for LLVM types.
bool isScalableVectorType(Type vectorType)
Returns whether a vector type is scalable or not.
Definition: LLVMTypes.cpp:898
std::optional< unsigned > extractPointerSpecValue(Attribute attr, PtrDLEntryPos pos)
Returns the value that corresponds to named position pos from the data layout entry attr assuming it'...
Definition: LLVMTypes.cpp:267
ParseResult parsePrettyLLVMType(AsmParser &p, Type &type)
Parse any MLIR type or a concise syntax for LLVM types.
bool isCompatibleVectorType(Type type)
Returns true if the given type is a vector type compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:855
bool isCompatibleOuterType(Type type)
Returns true if the given outer type is compatible with the LLVM dialect without checking its potenti...
Definition: LLVMTypes.cpp:742
Type getFixedVectorType(Type elementType, unsigned numElements)
Creates an LLVM dialect-compatible type with the given element type and length.
Definition: LLVMTypes.cpp:932
PtrDLEntryPos
The positions of different values in the data layout entry for pointers.
Definition: LLVMTypes.h:282
DEFINE_TRIVIAL_LLVM_TYPE(LLVMVoidType)
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:837
Type getScalableVectorType(Type elementType, unsigned numElements)
Creates an LLVM dialect-compatible type with the given element type and length.
Definition: LLVMTypes.cpp:943
bool isCompatibleFloatingPointType(Type type)
Returns true if the given type is a floating-point type compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:850
llvm::ElementCount getVectorNumElements(Type type)
Returns the element count of any LLVM-compatible vector type.
Definition: LLVMTypes.cpp:880
Type getVectorElementType(Type type)
Returns the element type of any vector type compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:871
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26