MLIR 23.0.0git
DataLayoutInterfaces.h
Go to the documentation of this file.
1//===- DataLayoutInterfaces.h - Data Layout Interface Decls -----*- 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// Defines the interfaces for the data layout specification, operations to which
10// they can be attached, types subject to data layout and dialects containing
11// data layout entries.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef MLIR_INTERFACES_DATALAYOUTINTERFACES_H
16#define MLIR_INTERFACES_DATALAYOUTINTERFACES_H
17
18#include "mlir/IR/Attributes.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/MapVector.h"
23#include "llvm/Support/TypeSize.h"
24
25namespace mlir {
26class DataLayout;
27class DataLayoutEntryInterface;
28class DLTIQueryInterface;
29class TargetDeviceSpecInterface;
30class TargetSystemSpecInterface;
32// Using explicit SmallVector size because we cannot infer the size from the
33// forward declaration, and we need the typedef in the actual declaration.
37using TargetDeviceSpecEntry = std::pair<StringAttr, TargetDeviceSpecInterface>;
39 ::llvm::MapVector<::mlir::StringAttr, ::mlir::DataLayoutEntryInterface>;
40class DataLayoutOpInterface;
41class DataLayoutSpecInterface;
42class ModuleOp;
43
44namespace detail {
45/// Default handler for the type size request. Computes results for built-in
46/// types and dispatches to the DataLayoutTypeInterface for other types.
47llvm::TypeSize getDefaultTypeSize(Type type, const DataLayout &dataLayout,
49
50/// Default handler for the type size in bits request. Computes results for
51/// built-in types and dispatches to the DataLayoutTypeInterface for other
52/// types.
53llvm::TypeSize getDefaultTypeSizeInBits(Type type, const DataLayout &dataLayout,
55
56/// Default handler for the required alignment request. Computes results for
57/// built-in types and dispatches to the DataLayoutTypeInterface for other
58/// types.
59uint64_t getDefaultABIAlignment(Type type, const DataLayout &dataLayout,
61
62/// Default handler for the preferred alignment request. Computes results for
63/// built-in types and dispatches to the DataLayoutTypeInterface for other
64/// types.
65uint64_t
66getDefaultPreferredAlignment(Type type, const DataLayout &dataLayout,
68
69/// Default handler for the index bitwidth request. Computes the result for
70/// the built-in index type and dispatches to the DataLayoutTypeInterface for
71/// other types.
72std::optional<uint64_t>
73getDefaultIndexBitwidth(Type type, const DataLayout &dataLayout,
75
76/// Default handler for endianness request. Dispatches to the
77/// DataLayoutInterface if specified, otherwise returns the default.
78Attribute getDefaultEndianness(DataLayoutEntryInterface entry);
79
80/// Default handler for the default memory space request. Dispatches to the
81/// DataLayoutInterface if specified, otherwise returns the default.
82Attribute getDefaultMemorySpace(DataLayoutEntryInterface entry);
83
84/// Default handler for alloca memory space request. Dispatches to the
85/// DataLayoutInterface if specified, otherwise returns the default.
86Attribute getDefaultAllocaMemorySpace(DataLayoutEntryInterface entry);
87
88/// Default handler for mangling mode request. Dispatches to the
89/// DataLayoutInterface if specified, otherwise returns the default.
90Attribute getDefaultManglingMode(DataLayoutEntryInterface entry);
91
92/// Default handler for program memory space request. Dispatches to the
93/// DataLayoutInterface if specified, otherwise returns the default.
94Attribute getDefaultProgramMemorySpace(DataLayoutEntryInterface entry);
95
96/// Default handler for global memory space request. Dispatches to the
97/// DataLayoutInterface if specified, otherwise returns the default.
98Attribute getDefaultGlobalMemorySpace(DataLayoutEntryInterface entry);
99
100/// Default handler for the stack alignment request. Dispatches to the
101/// DataLayoutInterface if specified, otherwise returns the default.
102uint64_t getDefaultStackAlignment(DataLayoutEntryInterface entry);
103
104/// Default handler for the function pointer alignment request. Dispatches to
105/// the DataLayoutInterface if specified, otherwise returns the default.
106Attribute getDefaultFunctionPointerAlignment(DataLayoutEntryInterface entry);
107
108/// Default handler for the legal int widths request. Dispatches to the
109/// DataLayoutInterface if specified, otherwise returns the default.
110Attribute getDefaultLegalIntWidths(DataLayoutEntryInterface entry);
111
112/// Returns the value of the property from the specified DataLayoutEntry. If the
113/// property is missing from the entry, returns std::nullopt.
114std::optional<Attribute> getDevicePropertyValue(DataLayoutEntryInterface entry);
115
116/// Given a list of data layout entries, returns a new list containing the
117/// entries with keys having the given type ID, i.e. belonging to the same type
118/// class.
120 TypeID typeID);
121
122/// Given a list of data layout entries, returns the entry that has the given
123/// identifier as key, if such an entry exists in the list.
124DataLayoutEntryInterface
126
127/// Given a list of target device entries, returns the entry that has the given
128/// identifier as key, if such an entry exists in the list.
129TargetDeviceSpecInterface
131
132/// Verifies that the operation implementing the data layout interface, or a
133/// module operation, is valid. This calls the verifier of the spec attribute
134/// and checks if the layout is compatible with specs attached to the enclosing
135/// operations.
136LogicalResult verifyDataLayoutOp(Operation *op);
137
138/// Verifies that a data layout spec is valid. This dispatches to individual
139/// entry verifiers, and then to the verifiers implemented by the relevant type
140/// and dialect interfaces for type and identifier keys respectively.
141LogicalResult verifyDataLayoutSpec(DataLayoutSpecInterface spec, Location loc);
142
143/// Verifies that a target system desc spec is valid. This dispatches to
144/// individual entry verifiers, and then to the verifiers implemented by the
145/// relevant dialect interfaces for identifier keys.
146LogicalResult verifyTargetSystemSpec(TargetSystemSpecInterface spec,
147 Location loc);
148
149/// Divides the known min value of the numerator by the denominator and rounds
150/// the result up to the next integer. Preserves the scalable flag.
151llvm::TypeSize divideCeil(llvm::TypeSize numerator, uint64_t denominator);
152} // namespace detail
153} // namespace mlir
154
155#include "mlir/Interfaces/DataLayoutAttrInterface.h.inc"
156#include "mlir/Interfaces/DataLayoutDialectInterface.h.inc"
157#include "mlir/Interfaces/DataLayoutOpInterface.h.inc"
158#include "mlir/Interfaces/DataLayoutTypeInterface.h.inc"
159
160namespace mlir {
161
162//===----------------------------------------------------------------------===//
163// DataLayout
164//===----------------------------------------------------------------------===//
165
166/// The main mechanism for performing data layout queries. Instances of this
167/// class can be created for an operation implementing DataLayoutOpInterface.
168/// Upon construction, a layout spec combining that of the given operation with
169/// all its ancestors will be computed and used to handle further requests. For
170/// efficiency, results to all requests will be cached in this object.
171/// Therefore, if the data layout spec for the scoping operation, or any of the
172/// enclosing operations, changes, the cache is no longer valid. The user is
173/// responsible creating a new DataLayout object after any spec change. In debug
174/// mode, the cache validity is being checked in every request.
176public:
177 explicit DataLayout();
178 explicit DataLayout(DataLayoutOpInterface op);
179 explicit DataLayout(ModuleOp op);
180
181 /// Returns the layout of the closest parent operation carrying layout info.
182 static DataLayout closest(Operation *op);
183
184 /// Returns the size of the given type in the current scope.
185 llvm::TypeSize getTypeSize(Type t) const;
186
187 /// Returns the size in bits of the given type in the current scope.
188 llvm::TypeSize getTypeSizeInBits(Type t) const;
189
190 /// Returns the required alignment of the given type in the current scope.
191 uint64_t getTypeABIAlignment(Type t) const;
192
193 /// Returns the preferred of the given type in the current scope.
194 uint64_t getTypePreferredAlignment(Type t) const;
195
196 /// Returns the bitwidth that should be used when performing index
197 /// computations for the given pointer-like type in the current scope. If the
198 /// type is not a pointer-like type, it returns std::nullopt.
199 std::optional<uint64_t> getTypeIndexBitwidth(Type t) const;
200
201 /// Returns the specified endianness.
202 Attribute getEndianness() const;
203
204 /// Returns the default memory space used for memory operations.
206
207 /// Returns the memory space used for AllocaOps.
209
210 /// Returns the mangling mode.
212
213 /// Returns the memory space used for program memory operations.
215
216 /// Returns the memory space used for global operations.
218
219 /// Returns the natural alignment of the stack in bits. Alignment promotion of
220 /// stack variables should be limited to the natural stack alignment to
221 /// prevent dynamic stack alignment. Returns zero if the stack alignment is
222 /// unspecified.
223 uint64_t getStackAlignment() const;
224
225 /// Returns function pointer alignment.
227
228 /// Returns the legal int widths.
230
231 /// Returns the value of the specified property if the property is defined for
232 /// the given device ID, otherwise returns std::nullopt.
233 std::optional<Attribute>
234 getDevicePropertyValue(TargetSystemSpecInterface::DeviceID,
235 StringAttr propertyName) const;
236
237private:
238 /// Combined layout spec at the given scope.
239 const DataLayoutSpecInterface originalLayout;
240
241 /// Combined target system desc spec at the given scope.
242 const TargetSystemSpecInterface originalTargetSystemDesc;
243
244#if LLVM_ENABLE_ABI_BREAKING_CHECKS
245 /// List of enclosing layout specs.
247#endif
248
249 /// Asserts that the cache is still valid. Expensive in debug mode. No-op in
250 /// release mode.
251 void checkValid() const;
252
253 /// Operation defining the scope of requests.
254 Operation *scope;
255
256 /// Caches for individual requests.
257 mutable DenseMap<Type, llvm::TypeSize> sizes;
258 mutable DenseMap<Type, llvm::TypeSize> bitsizes;
259 mutable DenseMap<Type, uint64_t> abiAlignments;
260 mutable DenseMap<Type, uint64_t> preferredAlignments;
261 mutable DenseMap<Type, std::optional<uint64_t>> indexBitwidths;
262
263 /// Cache for the endianness.
264 mutable std::optional<Attribute> endianness;
265 /// Cache for the mangling mode.
266 mutable std::optional<Attribute> manglingMode;
267 /// Cache for default, alloca, global, and program memory spaces.
268 mutable std::optional<Attribute> defaultMemorySpace;
269 mutable std::optional<Attribute> allocaMemorySpace;
270 mutable std::optional<Attribute> programMemorySpace;
271 mutable std::optional<Attribute> globalMemorySpace;
272
273 /// Cache for stack alignment.
274 mutable std::optional<uint64_t> stackAlignment;
275 /// Cache for function pointer alignment.
276 mutable std::optional<Attribute> functionPointerAlignment;
277 /// Cache for legal int widths.
278 mutable std::optional<Attribute> legalIntWidths;
279};
280
281} // namespace mlir
282
283#endif // MLIR_INTERFACES_DATALAYOUTINTERFACES_H
Attributes are known-constant values of operations.
Definition Attributes.h:25
The main mechanism for performing data layout queries.
Attribute getAllocaMemorySpace() const
Returns the memory space used for AllocaOps.
static DataLayout closest(Operation *op)
Returns the layout of the closest parent operation carrying layout info.
std::optional< uint64_t > getTypeIndexBitwidth(Type t) const
Returns the bitwidth that should be used when performing index computations for the given pointer-lik...
llvm::TypeSize getTypeSize(Type t) const
Returns the size of the given type in the current scope.
Attribute getManglingMode() const
Returns the mangling mode.
uint64_t getStackAlignment() const
Returns the natural alignment of the stack in bits.
Attribute getProgramMemorySpace() const
Returns the memory space used for program memory operations.
uint64_t getTypePreferredAlignment(Type t) const
Returns the preferred of the given type in the current scope.
Attribute getFunctionPointerAlignment() const
Returns function pointer alignment.
Attribute getGlobalMemorySpace() const
Returns the memory space used for global operations.
uint64_t getTypeABIAlignment(Type t) const
Returns the required alignment of the given type in the current scope.
llvm::TypeSize getTypeSizeInBits(Type t) const
Returns the size in bits of the given type in the current scope.
Attribute getDefaultMemorySpace() const
Returns the default memory space used for memory operations.
Attribute getEndianness() const
Returns the specified endianness.
Attribute getLegalIntWidths() const
Returns the legal int widths.
std::optional< Attribute > getDevicePropertyValue(TargetSystemSpecInterface::DeviceID, StringAttr propertyName) const
Returns the value of the specified property if the property is defined for the given device ID,...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
This class provides an efficient unique identifier for a specific C++ type.
Definition TypeID.h:107
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
AttrTypeReplacer.
Attribute getDefaultAllocaMemorySpace(DataLayoutEntryInterface entry)
Default handler for alloca memory space request.
Attribute getDefaultProgramMemorySpace(DataLayoutEntryInterface entry)
Default handler for program memory space request.
Attribute getDefaultEndianness(DataLayoutEntryInterface entry)
Default handler for endianness request.
std::optional< uint64_t > getDefaultIndexBitwidth(Type type, const DataLayout &dataLayout, ArrayRef< DataLayoutEntryInterface > params)
Default handler for the index bitwidth request.
DataLayoutEntryList filterEntriesForType(DataLayoutEntryListRef entries, TypeID typeID)
Given a list of data layout entries, returns a new list containing the entries with keys having the g...
LogicalResult verifyTargetSystemSpec(TargetSystemSpecInterface spec, Location loc)
Verifies that a target system desc spec is valid.
std::optional< Attribute > getDevicePropertyValue(DataLayoutEntryInterface entry)
Returns the value of the property from the specified DataLayoutEntry.
Attribute getDefaultManglingMode(DataLayoutEntryInterface entry)
Default handler for mangling mode request.
uint64_t getDefaultABIAlignment(Type type, const DataLayout &dataLayout, ArrayRef< DataLayoutEntryInterface > params)
Default handler for the required alignment request.
Attribute getDefaultLegalIntWidths(DataLayoutEntryInterface entry)
Default handler for the legal int widths request.
llvm::TypeSize getDefaultTypeSize(Type type, const DataLayout &dataLayout, DataLayoutEntryListRef params)
Default handler for the type size request.
llvm::TypeSize getDefaultTypeSizeInBits(Type type, const DataLayout &dataLayout, DataLayoutEntryListRef params)
Default handler for the type size in bits request.
uint64_t getDefaultPreferredAlignment(Type type, const DataLayout &dataLayout, ArrayRef< DataLayoutEntryInterface > params)
Default handler for the preferred alignment request.
llvm::TypeSize divideCeil(llvm::TypeSize numerator, uint64_t denominator)
Divides the known min value of the numerator by the denominator and rounds the result up to the next ...
uint64_t getDefaultStackAlignment(DataLayoutEntryInterface entry)
Default handler for the stack alignment request.
Attribute getDefaultGlobalMemorySpace(DataLayoutEntryInterface entry)
Default handler for global memory space request.
Attribute getDefaultMemorySpace(DataLayoutEntryInterface entry)
Default handler for the default memory space request.
LogicalResult verifyDataLayoutOp(Operation *op)
Verifies that the operation implementing the data layout interface, or a module operation,...
LogicalResult verifyDataLayoutSpec(DataLayoutSpecInterface spec, Location loc)
Verifies that a data layout spec is valid.
Attribute getDefaultFunctionPointerAlignment(DataLayoutEntryInterface entry)
Default handler for the function pointer alignment request.
DataLayoutEntryInterface filterEntryForIdentifier(DataLayoutEntryListRef entries, StringAttr id)
Given a list of data layout entries, returns the entry that has the given identifier as key,...
Include the generated interface declarations.
llvm::SmallVector< DataLayoutEntryInterface, 4 > DataLayoutEntryList
llvm::PointerUnion< Type, StringAttr > DataLayoutEntryKey
std::pair< StringAttr, TargetDeviceSpecInterface > TargetDeviceSpecEntry
llvm::ArrayRef< TargetDeviceSpecInterface > TargetDeviceSpecListRef
::llvm::MapVector<::mlir::StringAttr, ::mlir::DataLayoutEntryInterface > DataLayoutIdentifiedEntryMap
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:118
llvm::ArrayRef< DataLayoutEntryInterface > DataLayoutEntryListRef