MLIR  21.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 
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, TypeName) \
62  class ClassName : public Type::TypeBase<ClassName, Type, TypeStorage> { \
63  public: \
64  using Base::Base; \
65  static constexpr StringLiteral name = TypeName; \
66  }
67 
68 DEFINE_TRIVIAL_LLVM_TYPE(LLVMVoidType, "llvm.void");
69 DEFINE_TRIVIAL_LLVM_TYPE(LLVMTokenType, "llvm.token");
70 DEFINE_TRIVIAL_LLVM_TYPE(LLVMLabelType, "llvm.label");
71 DEFINE_TRIVIAL_LLVM_TYPE(LLVMMetadataType, "llvm.metadata");
72 
73 #undef DEFINE_TRIVIAL_LLVM_TYPE
74 
75 //===----------------------------------------------------------------------===//
76 // Printing and parsing.
77 //===----------------------------------------------------------------------===//
78 
79 namespace detail {
80 /// Parses an LLVM dialect type.
82 
83 /// Prints an LLVM Dialect type.
84 void printType(Type type, AsmPrinter &printer);
85 } // namespace detail
86 
87 /// Parse any MLIR type or a concise syntax for LLVM types.
88 ParseResult parsePrettyLLVMType(AsmParser &p, Type &type);
89 /// Print any MLIR type or a concise syntax for LLVM types.
90 void printPrettyLLVMType(AsmPrinter &p, Type type);
91 
92 //===----------------------------------------------------------------------===//
93 // Utility functions.
94 //===----------------------------------------------------------------------===//
95 
96 /// Returns `true` if the given type is compatible with the LLVM dialect. This
97 /// is an alias to `LLVMDialect::isCompatibleType`.
98 bool isCompatibleType(Type type);
99 
100 /// Returns `true` if the given outer type is compatible with the LLVM dialect
101 /// without checking its potential nested types such as struct elements.
102 bool isCompatibleOuterType(Type type);
103 
104 /// Returns `true` if the given type is a floating-point type compatible with
105 /// the LLVM dialect.
107 
108 /// Returns `true` if the given type is a vector type compatible with the LLVM
109 /// dialect. Compatible types include 1D built-in vector types of built-in
110 /// integers and floating-point values, LLVM dialect fixed vector types of LLVM
111 /// dialect pointers and LLVM dialect scalable vector types.
112 bool isCompatibleVectorType(Type type);
113 
114 /// Returns the element count of any LLVM-compatible vector type.
115 llvm::ElementCount getVectorNumElements(Type type);
116 
117 /// Returns whether a vector type is scalable or not.
118 bool isScalableVectorType(Type vectorType);
119 
120 /// Creates an LLVM dialect-compatible vector type with the given element type
121 /// and length.
122 Type getVectorType(Type elementType, unsigned numElements,
123  bool isScalable = false);
124 
125 /// Creates an LLVM dialect-compatible vector type with the given element type
126 /// and length.
127 Type getVectorType(Type elementType, const llvm::ElementCount &numElements);
128 
129 /// Returns the size of the given primitive LLVM dialect-compatible type
130 /// (including vectors) in bits, for example, the size of i16 is 16 and
131 /// the size of vector<4xi16> is 64. Returns 0 for non-primitive
132 /// (aggregates such as struct) or types that don't have a size (such as void).
133 llvm::TypeSize getPrimitiveTypeSizeInBits(Type type);
134 
135 /// The positions of different values in the data layout entry for pointers.
136 enum class PtrDLEntryPos { Size = 0, Abi = 1, Preferred = 2, Index = 3 };
137 
138 /// Returns the value that corresponds to named position `pos` from the
139 /// data layout entry `attr` assuming it's a dense integer elements attribute.
140 /// Returns `std::nullopt` if `pos` is not present in the entry.
141 /// Currently only `PtrDLEntryPos::Index` is optional, and all other positions
142 /// may be assumed to be present.
143 std::optional<uint64_t> extractPointerSpecValue(Attribute attr,
144  PtrDLEntryPos pos);
145 
146 } // namespace LLVM
147 } // namespace mlir
148 
149 #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 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:841
llvm::TypeSize getPrimitiveTypeSizeInBits(Type type)
Returns the size of the given primitive LLVM dialect-compatible type (including vectors) in bits,...
Definition: LLVMTypes.cpp:857
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:835
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:814
bool isCompatibleOuterType(Type type)
Returns true if the given outer type is compatible with the LLVM dialect without checking its potenti...
Definition: LLVMTypes.cpp:705
PtrDLEntryPos
The positions of different values in the data layout entry for pointers.
Definition: LLVMTypes.h:136
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:796
bool isCompatibleFloatingPointType(Type type)
Returns true if the given type is a floating-point type compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:809
llvm::ElementCount getVectorNumElements(Type type)
Returns the element count of any LLVM-compatible vector type.
Definition: LLVMTypes.cpp:827
Include the generated interface declarations.