MLIR  22.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 
17 #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 class DataLayout;
32 
33 namespace LLVM {
34 class LLVMDialect;
35 
36 namespace detail {
37 struct LLVMFunctionTypeStorage;
38 struct LLVMPointerTypeStorage;
39 struct LLVMStructTypeStorage;
40 struct LLVMTypeAndSizeStorage;
41 } // namespace detail
42 } // namespace LLVM
43 } // namespace mlir
44 
45 //===----------------------------------------------------------------------===//
46 // ODS-Generated Declarations
47 //===----------------------------------------------------------------------===//
48 
49 #include "mlir/Dialect/LLVMIR/LLVMTypeInterfaces.h.inc"
50 
51 #define GET_TYPEDEF_CLASSES
52 #include "mlir/Dialect/LLVMIR/LLVMTypes.h.inc"
53 
54 namespace mlir {
55 namespace LLVM {
56 
57 //===----------------------------------------------------------------------===//
58 // Trivial types.
59 //===----------------------------------------------------------------------===//
60 
61 // Batch-define trivial types.
62 #define DEFINE_TRIVIAL_LLVM_TYPE(ClassName, TypeName) \
63  class ClassName : public Type::TypeBase<ClassName, Type, TypeStorage> { \
64  public: \
65  using Base::Base; \
66  static constexpr StringLiteral name = TypeName; \
67  }
68 
69 DEFINE_TRIVIAL_LLVM_TYPE(LLVMVoidType, "llvm.void");
70 DEFINE_TRIVIAL_LLVM_TYPE(LLVMTokenType, "llvm.token");
71 DEFINE_TRIVIAL_LLVM_TYPE(LLVMLabelType, "llvm.label");
72 DEFINE_TRIVIAL_LLVM_TYPE(LLVMMetadataType, "llvm.metadata");
73 
74 #undef DEFINE_TRIVIAL_LLVM_TYPE
75 
76 //===----------------------------------------------------------------------===//
77 // Printing and parsing.
78 //===----------------------------------------------------------------------===//
79 
80 namespace detail {
81 /// Parses an LLVM dialect type.
83 
84 /// Prints an LLVM Dialect type.
85 void printType(Type type, AsmPrinter &printer);
86 } // namespace detail
87 
88 /// Parse any MLIR type or a concise syntax for LLVM types.
89 ParseResult parsePrettyLLVMType(AsmParser &p, Type &type);
90 /// Print any MLIR type or a concise syntax for LLVM types.
91 void printPrettyLLVMType(AsmPrinter &p, Type type);
92 
93 //===----------------------------------------------------------------------===//
94 // Utility functions.
95 //===----------------------------------------------------------------------===//
96 
97 /// Returns `true` if the given type is compatible with the LLVM dialect. This
98 /// is an alias to `LLVMDialect::isCompatibleType`.
99 bool isCompatibleType(Type type);
100 
101 /// Returns `true` if the given outer type is compatible with the LLVM dialect
102 /// without checking its potential nested types such as struct elements.
103 bool isCompatibleOuterType(Type type);
104 
105 /// Returns `true` if the given type is a floating-point type compatible with
106 /// the LLVM dialect.
108 
109 /// Returns `true` if the given type is a vector type compatible with the LLVM
110 /// dialect. Compatible types include 1D built-in vector types of built-in
111 /// integers and floating-point values, LLVM dialect fixed vector types of LLVM
112 /// dialect pointers and LLVM dialect scalable vector types.
113 bool isCompatibleVectorType(Type type);
114 
115 /// Returns `true` if the given type is a loadable type compatible with the LLVM
116 /// dialect.
117 bool isLoadableType(Type type);
118 
119 /// Returns true if the given type is supported by atomic operations. All
120 /// integer, float, and pointer types with a power-of-two bitsize and a minimal
121 /// size of 8 bits are supported.
122 bool isTypeCompatibleWithAtomicOp(Type type, const DataLayout &dataLayout);
123 
124 /// Returns the element count of any LLVM-compatible vector type.
125 llvm::ElementCount getVectorNumElements(Type type);
126 
127 /// Returns whether a vector type is scalable or not.
128 bool isScalableVectorType(Type vectorType);
129 
130 /// Creates an LLVM dialect-compatible vector type with the given element type
131 /// and length.
132 Type getVectorType(Type elementType, unsigned numElements,
133  bool isScalable = false);
134 
135 /// Creates an LLVM dialect-compatible vector type with the given element type
136 /// and length.
137 Type getVectorType(Type elementType, const llvm::ElementCount &numElements);
138 
139 /// Returns the size of the given primitive LLVM dialect-compatible type
140 /// (including vectors) in bits, for example, the size of i16 is 16 and
141 /// the size of vector<4xi16> is 64. Returns 0 for non-primitive
142 /// (aggregates such as struct) or types that don't have a size (such as void).
143 llvm::TypeSize getPrimitiveTypeSizeInBits(Type type);
144 
145 /// The positions of different values in the data layout entry for pointers.
146 enum class PtrDLEntryPos { Size = 0, Abi = 1, Preferred = 2, Index = 3 };
147 
148 /// Returns the value that corresponds to named position `pos` from the
149 /// data layout entry `attr` assuming it's a dense integer elements attribute.
150 /// Returns `std::nullopt` if `pos` is not present in the entry.
151 /// Currently only `PtrDLEntryPos::Index` is optional, and all other positions
152 /// may be assumed to be present.
153 std::optional<uint64_t> extractPointerSpecValue(Attribute attr,
154  PtrDLEntryPos pos);
155 
156 } // namespace LLVM
157 } // namespace mlir
158 
159 #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...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
The OpAsmOpInterface, see OpAsmInterface.td for more details.
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:867
llvm::TypeSize getPrimitiveTypeSizeInBits(Type type)
Returns the size of the given primitive LLVM dialect-compatible type (including vectors) in bits,...
Definition: LLVMTypes.cpp:883
void printPrettyLLVMType(AsmPrinter &p, Type type)
Print any MLIR type or a concise syntax for LLVM types.
bool isLoadableType(Type type)
Returns true if the given type is a loadable type compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:822
bool isScalableVectorType(Type vectorType)
Returns whether a vector type is scalable or not.
Definition: LLVMTypes.cpp:861
DEFINE_TRIVIAL_LLVM_TYPE(LLVMVoidType, "llvm.void")
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:839
bool isCompatibleOuterType(Type type)
Returns true if the given outer type is compatible with the LLVM dialect without checking its potenti...
Definition: LLVMTypes.cpp:716
PtrDLEntryPos
The positions of different values in the data layout entry for pointers.
Definition: LLVMTypes.h:146
std::optional< uint64_t > 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:266
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:809
bool isTypeCompatibleWithAtomicOp(Type type, const DataLayout &dataLayout)
Returns true if the given type is supported by atomic operations.
bool isCompatibleFloatingPointType(Type type)
Returns true if the given type is a floating-point type compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:834
llvm::ElementCount getVectorNumElements(Type type)
Returns the element count of any LLVM-compatible vector type.
Definition: LLVMTypes.cpp:853
Include the generated interface declarations.