MLIR 24.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"
19#include <optional>
20
21namespace llvm {
22class ElementCount;
23class TypeSize;
24} // namespace llvm
25
26namespace mlir {
27
28class AsmParser;
29class AsmPrinter;
30class DataLayout;
31
32namespace LLVM {
33class LLVMDialect;
34
35namespace detail {
36struct LLVMFunctionTypeStorage;
37struct LLVMPointerTypeStorage;
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
53namespace mlir {
54namespace 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
68DEFINE_TRIVIAL_LLVM_TYPE(LLVMVoidType, "llvm.void");
69DEFINE_TRIVIAL_LLVM_TYPE(LLVMLabelType, "llvm.label");
70DEFINE_TRIVIAL_LLVM_TYPE(LLVMMetadataType, "llvm.metadata");
71
72#undef DEFINE_TRIVIAL_LLVM_TYPE
73
74//===----------------------------------------------------------------------===//
75// Printing and parsing.
76//===----------------------------------------------------------------------===//
77
78namespace detail {
79/// Parses an LLVM dialect type.
81
82/// Prints an LLVM Dialect type.
83void printType(Type type, AsmPrinter &printer);
84} // namespace detail
85
86/// Parse any MLIR type or a concise syntax for LLVM types.
87ParseResult parsePrettyLLVMType(AsmParser &p, Type &type);
88/// Print any MLIR type or a concise syntax for LLVM types.
90
91//===----------------------------------------------------------------------===//
92// Utility functions.
93//===----------------------------------------------------------------------===//
94
95/// Returns `true` if the given type is compatible with the LLVM dialect. This
96/// is an alias to `LLVMDialect::isCompatibleType`.
97bool isCompatibleType(Type type);
98
99/// Returns `true` if the given outer type is compatible with the LLVM dialect
100/// without checking its potential nested types such as struct elements.
101bool isCompatibleOuterType(Type type);
102
103/// Returns `true` if the given type is a floating-point type compatible with
104/// the LLVM dialect.
106
107/// Returns `true` if the given type is a vector type compatible with the LLVM
108/// dialect. Compatible types include 1D built-in vector types of built-in
109/// integers and floating-point values, LLVM dialect fixed vector types of LLVM
110/// dialect pointers and LLVM dialect scalable vector types.
112
113/// Returns `true` if the given type is a loadable type compatible with the LLVM
114/// dialect.
115bool isLoadableType(Type type);
116
117/// Returns true if the given type is supported by atomic operations. All
118/// integer, float, and pointer types with a power-of-two bitsize and a minimal
119/// size of 8 bits are supported.
120bool isTypeCompatibleWithAtomicOp(Type type, const DataLayout &dataLayout);
121
122/// Returns the element count of any LLVM-compatible vector type.
123llvm::ElementCount getVectorNumElements(Type type);
124
125/// Returns whether a vector type is scalable or not.
126bool isScalableVectorType(Type vectorType);
127
128/// Creates an LLVM dialect-compatible vector type with the given element type
129/// and length.
130Type getVectorType(Type elementType, unsigned numElements,
131 bool isScalable = false);
132
133/// Creates an LLVM dialect-compatible vector type with the given element type
134/// and length.
135Type getVectorType(Type elementType, const llvm::ElementCount &numElements);
136
137/// Returns the size of the given primitive LLVM dialect-compatible type
138/// (including vectors) in bits, for example, the size of i16 is 16 and
139/// the size of vector<4xi16> is 64. Returns 0 for non-primitive
140/// (aggregates such as struct) or types that don't have a size (such as void).
141llvm::TypeSize getPrimitiveTypeSizeInBits(Type type);
142
143/// The positions of different values in the data layout entry for pointers.
144enum class PtrDLEntryPos { Size = 0, Abi = 1, Preferred = 2, Index = 3 };
145
146/// Returns the value that corresponds to named position `pos` from the
147/// data layout entry `attr` assuming it's a dense integer elements attribute.
148/// Returns `std::nullopt` if `pos` is not present in the entry.
149/// Currently only `PtrDLEntryPos::Index` is optional, and all other positions
150/// may be assumed to be present.
151std::optional<uint64_t> extractPointerSpecValue(Attribute attr,
152 PtrDLEntryPos pos);
153
154} // namespace LLVM
155} // namespace mlir
156
157#endif // MLIR_DIALECT_LLVMIR_LLVMTYPES_H_
#define DEFINE_TRIVIAL_LLVM_TYPE(ClassName, TypeName)
Definition LLVMTypes.h:61
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.
llvm::TypeSize getPrimitiveTypeSizeInBits(Type type)
Returns the size of the given primitive LLVM dialect-compatible type (including vectors) in bits,...
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.
bool isScalableVectorType(Type vectorType)
Returns whether a vector type is scalable or not.
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.
bool isCompatibleOuterType(Type type)
Returns true if the given outer type is compatible with the LLVM dialect without checking its potenti...
PtrDLEntryPos
The positions of different values in the data layout entry for pointers.
Definition LLVMTypes.h:144
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'...
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
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.
llvm::ElementCount getVectorNumElements(Type type)
Returns the element count of any LLVM-compatible vector type.
Include the generated interface declarations.
Type storage for LLVM structure types.
Definition TypeDetail.h:61
Common storage used for LLVM dialect types that need an element type and a number: arrays,...
Definition TypeDetail.h:366