MLIR 24.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"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/MapVector.h"
22#include "llvm/Support/TypeSize.h"
23
24namespace mlir {
25class DataLayout;
26class DataLayoutEntryInterface;
27class DLTIQueryInterface;
28class TargetDeviceSpecInterface;
29class TargetSystemSpecInterface;
31// Using explicit SmallVector size because we cannot infer the size from the
32// forward declaration, and we need the typedef in the actual declaration.
36using TargetDeviceSpecEntry = std::pair<StringAttr, TargetDeviceSpecInterface>;
39class DataLayoutOpInterface;
40class DataLayoutSpecInterface;
41class ModuleOp;
42
43namespace detail {
44/// Default handler for the type size request. Computes results for built-in
45/// types and dispatches to the DataLayoutTypeInterface for other types.
46llvm::TypeSize getDefaultTypeSize(Type type, const DataLayout &dataLayout,
48
49/// Default handler for the type size in bits request. Computes results for
50/// built-in types and dispatches to the DataLayoutTypeInterface for other
51/// types.
52llvm::TypeSize getDefaultTypeSizeInBits(Type type, const DataLayout &dataLayout,
54
55/// Default handler for the required alignment request. Computes results for
56/// built-in types and dispatches to the DataLayoutTypeInterface for other
57/// types.
58uint64_t getDefaultABIAlignment(Type type, const DataLayout &dataLayout,
60
61/// Default handler for the preferred alignment request. Computes results for
62/// built-in types and dispatches to the DataLayoutTypeInterface for other
63/// types.
64uint64_t
65getDefaultPreferredAlignment(Type type, const DataLayout &dataLayout,
67
68/// Default handler for the index bitwidth request. Computes the result for
69/// the built-in index type and dispatches to the DataLayoutTypeInterface for
70/// other types.
71std::optional<uint64_t>
72getDefaultIndexBitwidth(Type type, const DataLayout &dataLayout,
74
75/// Default handler for endianness request. Dispatches to the
76/// DataLayoutInterface if specified, otherwise returns the default.
77Attribute getDefaultEndianness(DataLayoutEntryInterface entry);
78
79/// Default handler for the default memory space request. Dispatches to the
80/// DataLayoutInterface if specified, otherwise returns the default.
81Attribute getDefaultMemorySpace(DataLayoutEntryInterface entry);
82
83/// Default handler for alloca memory space request. Dispatches to the
84/// DataLayoutInterface if specified, otherwise returns the default.
85Attribute getDefaultAllocaMemorySpace(DataLayoutEntryInterface entry);
86
87/// Default handler for mangling mode request. Dispatches to the
88/// DataLayoutInterface if specified, otherwise returns the default.
89Attribute getDefaultManglingMode(DataLayoutEntryInterface entry);
90
91/// Default handler for program memory space request. Dispatches to the
92/// DataLayoutInterface if specified, otherwise returns the default.
93Attribute getDefaultProgramMemorySpace(DataLayoutEntryInterface entry);
94
95/// Default handler for global memory space request. Dispatches to the
96/// DataLayoutInterface if specified, otherwise returns the default.
97Attribute getDefaultGlobalMemorySpace(DataLayoutEntryInterface entry);
98
99/// Default handler for the stack alignment request. Dispatches to the
100/// DataLayoutInterface if specified, otherwise returns the default.
101uint64_t getDefaultStackAlignment(DataLayoutEntryInterface entry);
102
103/// Default handler for the function pointer alignment request. Dispatches to
104/// the DataLayoutInterface if specified, otherwise returns the default.
105Attribute getDefaultFunctionPointerAlignment(DataLayoutEntryInterface entry);
106
107/// Default handler for the legal int widths request. Dispatches to the
108/// DataLayoutInterface if specified, otherwise returns the default.
109Attribute getDefaultLegalIntWidths(DataLayoutEntryInterface entry);
110
111/// Returns the value of the property from the specified DataLayoutEntry. If the
112/// property is missing from the entry, returns std::nullopt.
113std::optional<Attribute> getDevicePropertyValue(DataLayoutEntryInterface entry);
114
115/// Given a list of data layout entries, returns a new list containing the
116/// entries with keys having the given type ID, i.e. belonging to the same type
117/// class.
119 TypeID typeID);
120
121/// Given a list of data layout entries, returns the entry that has the given
122/// identifier as key, if such an entry exists in the list.
123DataLayoutEntryInterface
125
126/// Given a list of target device entries, returns the entry that has the given
127/// identifier as key, if such an entry exists in the list.
128TargetDeviceSpecInterface
130
131/// Verifies that the operation implementing the data layout interface, or a
132/// module operation, is valid. This calls the verifier of the spec attribute
133/// and checks if the layout is compatible with specs attached to the enclosing
134/// operations.
135LogicalResult verifyDataLayoutOp(Operation *op);
136
137/// Verifies that a data layout spec is valid. This dispatches to individual
138/// entry verifiers, and then to the verifiers implemented by the relevant type
139/// and dialect interfaces for type and identifier keys respectively.
140LogicalResult verifyDataLayoutSpec(DataLayoutSpecInterface spec, Location loc);
141
142/// Verifies that a target system desc spec is valid. This dispatches to
143/// individual entry verifiers, and then to the verifiers implemented by the
144/// relevant dialect interfaces for identifier keys.
145LogicalResult verifyTargetSystemSpec(TargetSystemSpecInterface spec,
146 Location loc);
147
148/// Divides the known min value of the numerator by the denominator and rounds
149/// the result up to the next integer. Preserves the scalable flag.
150llvm::TypeSize divideCeil(llvm::TypeSize numerator, uint64_t denominator);
151} // namespace detail
152} // namespace mlir
153
154#include "mlir/Interfaces/DataLayoutAttrInterface.h.inc"
155#include "mlir/Interfaces/DataLayoutDialectInterface.h.inc"
156#include "mlir/Interfaces/DataLayoutOpInterface.h.inc"
157#include "mlir/Interfaces/DataLayoutTypeInterface.h.inc"
158
159namespace mlir {
160
161//===----------------------------------------------------------------------===//
162// DataLayout
163//===----------------------------------------------------------------------===//
164
165/// The main mechanism for performing data layout queries. Instances of this
166/// class can be created for an operation implementing DataLayoutOpInterface.
167/// Upon construction, a layout spec combining that of the given operation with
168/// all its ancestors will be computed and used to handle further requests. For
169/// efficiency, results to all requests will be cached in this object.
170/// Therefore, if the data layout spec for the scoping operation, or any of the
171/// enclosing operations, changes, the cache is no longer valid. The user is
172/// responsible creating a new DataLayout object after any spec change. In debug
173/// mode, the cache validity is being checked in every request.
175public:
176 explicit DataLayout();
177 explicit DataLayout(DataLayoutOpInterface op);
178 explicit DataLayout(ModuleOp op);
179
180 /// Returns the layout of the closest parent operation carrying layout info.
181 static DataLayout closest(Operation *op);
182
183 /// Returns true if the layout in the current scope has an entry whose key is
184 /// exactly the given type. This includes entries from enclosing scopes.
185 /// A type can have a known size even if this returns false.
186 bool hasExactTypeEntry(Type type) const;
187
188 /// Returns the size of the given type in the current scope.
189 llvm::TypeSize getTypeSize(Type t) const;
190
191 /// Returns the size in bits of the given type in the current scope.
192 llvm::TypeSize getTypeSizeInBits(Type t) const;
193
194 /// Returns the offset in bytes between successive objects of the given type
195 /// in the current scope, that is, its size rounded up to its ABI alignment.
196 llvm::TypeSize getTypeAllocSize(Type t) const;
197
198 /// Returns the required alignment of the given type in the current scope.
199 uint64_t getTypeABIAlignment(Type t) const;
200
201 /// Returns the preferred of the given type in the current scope.
202 uint64_t getTypePreferredAlignment(Type t) const;
203
204 /// Returns the bitwidth that should be used when performing index
205 /// computations for the given pointer-like type in the current scope. If the
206 /// type is not a pointer-like type, it returns std::nullopt.
207 std::optional<uint64_t> getTypeIndexBitwidth(Type t) const;
208
209 /// Returns the specified endianness.
210 Attribute getEndianness() const;
211
212 /// Returns the default memory space used for memory operations.
214
215 /// Returns the memory space used for AllocaOps.
217
218 /// Returns the mangling mode.
220
221 /// Returns the memory space used for program memory operations.
223
224 /// Returns the memory space used for global operations.
226
227 /// Returns the natural alignment of the stack in bits. Alignment promotion of
228 /// stack variables should be limited to the natural stack alignment to
229 /// prevent dynamic stack alignment. Returns zero if the stack alignment is
230 /// unspecified.
231 uint64_t getStackAlignment() const;
232
233 /// Returns function pointer alignment.
235
236 /// Returns the legal int widths.
238
239 /// Returns the value of the specified property if the property is defined for
240 /// the given device ID, otherwise returns std::nullopt.
241 std::optional<Attribute>
242 getDevicePropertyValue(TargetSystemSpecInterface::DeviceID,
243 StringAttr propertyName) const;
244
245private:
246 /// Combined layout spec at the given scope.
247 const DataLayoutSpecInterface originalLayout;
248
249 /// Combined target system desc spec at the given scope.
250 const TargetSystemSpecInterface originalTargetSystemDesc;
251
252#if LLVM_ENABLE_ABI_BREAKING_CHECKS
253 /// List of enclosing layout specs.
255#endif
256
257 /// Asserts that the cache is still valid. Expensive in debug mode. No-op in
258 /// release mode.
259 void checkValid() const;
260
261 /// Operation defining the scope of requests.
262 Operation *scope;
263
264 /// Caches for individual requests.
265 mutable DenseMap<Type, bool> typeEntries;
266 mutable DenseMap<Type, llvm::TypeSize> sizes;
267 mutable DenseMap<Type, llvm::TypeSize> bitsizes;
268 mutable DenseMap<Type, uint64_t> abiAlignments;
269 mutable DenseMap<Type, uint64_t> preferredAlignments;
270 mutable DenseMap<Type, std::optional<uint64_t>> indexBitwidths;
271
272 /// Cache for the endianness.
273 mutable std::optional<Attribute> endianness;
274 /// Cache for the mangling mode.
275 mutable std::optional<Attribute> manglingMode;
276 /// Cache for default, alloca, global, and program memory spaces.
277 mutable std::optional<Attribute> defaultMemorySpace;
278 mutable std::optional<Attribute> allocaMemorySpace;
279 mutable std::optional<Attribute> programMemorySpace;
280 mutable std::optional<Attribute> globalMemorySpace;
281
282 /// Cache for stack alignment.
283 mutable std::optional<uint64_t> stackAlignment;
284 /// Cache for function pointer alignment.
285 mutable std::optional<Attribute> functionPointerAlignment;
286 /// Cache for legal int widths.
287 mutable std::optional<Attribute> legalIntWidths;
288};
289
290} // namespace mlir
291
292#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.
llvm::TypeSize getTypeAllocSize(Type t) const
Returns the offset in bytes between successive objects of the given type in the current scope,...
uint64_t getStackAlignment() const
Returns the natural alignment of the stack in bits.
bool hasExactTypeEntry(Type type) const
Returns true if the layout in the current scope has an entry whose key is exactly the given type.
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:87
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:123
llvm::ArrayRef< DataLayoutEntryInterface > DataLayoutEntryListRef