MLIR  18.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 
19 #include "mlir/IR/OpDefinition.h"
20 #include "llvm/ADT/DenseMap.h"
21 
22 namespace mlir {
23 class DataLayout;
24 class DataLayoutEntryInterface;
26 // Using explicit SmallVector size because we cannot infer the size from the
27 // forward declaration, and we need the typedef in the actual declaration.
30 class DataLayoutOpInterface;
31 class DataLayoutSpecInterface;
32 class ModuleOp;
33 
34 namespace detail {
35 /// Default handler for the type size request. Computes results for built-in
36 /// types and dispatches to the DataLayoutTypeInterface for other types.
37 unsigned getDefaultTypeSize(Type type, const DataLayout &dataLayout,
38  DataLayoutEntryListRef params);
39 
40 /// Default handler for the type size in bits request. Computes results for
41 /// built-in types and dispatches to the DataLayoutTypeInterface for other
42 /// types.
43 unsigned getDefaultTypeSizeInBits(Type type, const DataLayout &dataLayout,
44  DataLayoutEntryListRef params);
45 
46 /// Default handler for the required alignemnt request. Computes results for
47 /// built-in types and dispatches to the DataLayoutTypeInterface for other
48 /// types.
49 unsigned getDefaultABIAlignment(Type type, const DataLayout &dataLayout,
51 
52 /// Default handler for the preferred alignemnt request. Computes results for
53 /// built-in types and dispatches to the DataLayoutTypeInterface for other
54 /// types.
55 unsigned
56 getDefaultPreferredAlignment(Type type, const DataLayout &dataLayout,
58 
59 /// Default handler for alloca memory space request. Dispatches to the
60 /// DataLayoutInterface if specified, otherwise returns the default.
61 Attribute getDefaultAllocaMemorySpace(DataLayoutEntryInterface entry);
62 
63 /// Default handler for the stack alignment request. Dispatches to the
64 /// DataLayoutInterface if specified, otherwise returns the default.
65 unsigned getDefaultStackAlignment(DataLayoutEntryInterface entry);
66 
67 /// Given a list of data layout entries, returns a new list containing the
68 /// entries with keys having the given type ID, i.e. belonging to the same type
69 /// class.
71  TypeID typeID);
72 
73 /// Given a list of data layout entries, returns the entry that has the given
74 /// identifier as key, if such an entry exists in the list.
75 DataLayoutEntryInterface
76 filterEntryForIdentifier(DataLayoutEntryListRef entries, StringAttr id);
77 
78 /// Verifies that the operation implementing the data layout interface, or a
79 /// module operation, is valid. This calls the verifier of the spec attribute
80 /// and checks if the layout is compatible with specs attached to the enclosing
81 /// operations.
83 
84 /// Verifies that a data layout spec is valid. This dispatches to individual
85 /// entry verifiers, and then to the verifiers implemented by the relevant type
86 /// and dialect interfaces for type and identifier keys respectively.
87 LogicalResult verifyDataLayoutSpec(DataLayoutSpecInterface spec, Location loc);
88 } // namespace detail
89 } // namespace mlir
90 
91 #include "mlir/Interfaces/DataLayoutAttrInterface.h.inc"
92 #include "mlir/Interfaces/DataLayoutOpInterface.h.inc"
93 #include "mlir/Interfaces/DataLayoutTypeInterface.h.inc"
94 
95 namespace mlir {
96 
97 //===----------------------------------------------------------------------===//
98 // DataLayoutDialectInterface
99 //===----------------------------------------------------------------------===//
100 
101 /// An interface to be implemented by dialects that can have identifiers in the
102 /// data layout specification entries. Provides hooks for verifying the entry
103 /// validity and combining two entries.
105  : public DialectInterface::Base<DataLayoutDialectInterface> {
106 public:
107  DataLayoutDialectInterface(Dialect *dialect) : Base(dialect) {}
108 
109  /// Checks whether the given data layout entry is valid and reports any errors
110  /// at the provided location. Derived classes should override this.
111  virtual LogicalResult verifyEntry(DataLayoutEntryInterface entry,
112  Location loc) const {
113  return success();
114  }
115 
116  /// Default implementation of entry combination that combines identical
117  /// entries and returns null otherwise.
118  static DataLayoutEntryInterface
119  defaultCombine(DataLayoutEntryInterface outer,
120  DataLayoutEntryInterface inner) {
121  if (!outer || outer == inner)
122  return inner;
123  return {};
124  }
125 
126  /// Combines two entries with identifiers that belong to this dialect. Returns
127  /// the combined entry or null if the entries are not compatible. Derived
128  /// classes likely need to reimplement this.
129  virtual DataLayoutEntryInterface
130  combine(DataLayoutEntryInterface outer,
131  DataLayoutEntryInterface inner) const {
132  return defaultCombine(outer, inner);
133  }
134 };
135 
136 //===----------------------------------------------------------------------===//
137 // DataLayout
138 //===----------------------------------------------------------------------===//
139 
140 /// The main mechanism for performing data layout queries. Instances of this
141 /// class can be created for an operation implementing DataLayoutOpInterface.
142 /// Upon construction, a layout spec combining that of the given operation with
143 /// all its ancestors will be computed and used to handle further requests. For
144 /// efficiency, results to all requests will be cached in this object.
145 /// Therefore, if the data layout spec for the scoping operation, or any of the
146 /// enclosing operations, changes, the cache is no longer valid. The user is
147 /// responsible creating a new DataLayout object after any spec change. In debug
148 /// mode, the cache validity is being checked in every request.
149 class DataLayout {
150 public:
151  explicit DataLayout();
152  explicit DataLayout(DataLayoutOpInterface op);
153  explicit DataLayout(ModuleOp op);
154 
155  /// Returns the layout of the closest parent operation carrying layout info.
156  static DataLayout closest(Operation *op);
157 
158  /// Returns the size of the given type in the current scope.
159  unsigned getTypeSize(Type t) const;
160 
161  /// Returns the size in bits of the given type in the current scope.
162  unsigned getTypeSizeInBits(Type t) const;
163 
164  /// Returns the required alignment of the given type in the current scope.
165  unsigned getTypeABIAlignment(Type t) const;
166 
167  /// Returns the preferred of the given type in the current scope.
168  unsigned getTypePreferredAlignment(Type t) const;
169 
170  /// Returns the memory space used for AllocaOps.
172 
173  /// Returns the natural alignment of the stack in bits. Alignment promotion of
174  /// stack variables should be limited to the natural stack alignment to
175  /// prevent dynamic stack alignment. Returns zero if the stack alignment is
176  /// unspecified.
177  unsigned getStackAlignment() const;
178 
179 private:
180  /// Combined layout spec at the given scope.
181  const DataLayoutSpecInterface originalLayout;
182 
183 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
184  /// List of enclosing layout specs.
186 #endif
187 
188  /// Asserts that the cache is still valid. Expensive in debug mode. No-op in
189  /// release mode.
190  void checkValid() const;
191 
192  /// Operation defining the scope of requests.
193  Operation *scope;
194 
195  /// Caches for individual requests.
196  mutable DenseMap<Type, unsigned> sizes;
197  mutable DenseMap<Type, unsigned> bitsizes;
198  mutable DenseMap<Type, unsigned> abiAlignments;
199  mutable DenseMap<Type, unsigned> preferredAlignments;
200 
201  /// Cache for alloca memory space.
202  mutable std::optional<Attribute> allocaMemorySpace;
203 
204  /// Cache for stack alignment.
205  mutable std::optional<unsigned> stackAlignment;
206 };
207 
208 } // namespace mlir
209 
210 #endif // MLIR_INTERFACES_DATALAYOUTINTERFACES_H
Attributes are known-constant values of operations.
Definition: Attributes.h:25
An interface to be implemented by dialects that can have identifiers in the data layout specification...
static DataLayoutEntryInterface defaultCombine(DataLayoutEntryInterface outer, DataLayoutEntryInterface inner)
Default implementation of entry combination that combines identical entries and returns null otherwis...
virtual DataLayoutEntryInterface combine(DataLayoutEntryInterface outer, DataLayoutEntryInterface inner) const
Combines two entries with identifiers that belong to this dialect.
virtual LogicalResult verifyEntry(DataLayoutEntryInterface entry, Location loc) const
Checks whether the given data layout entry is valid and reports any errors at the provided location.
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.
unsigned getTypeABIAlignment(Type t) const
Returns the required alignment of the given type in the current scope.
unsigned getTypeSizeInBits(Type t) const
Returns the size in bits of the given type in the current scope.
unsigned getTypePreferredAlignment(Type t) const
Returns the preferred of the given type in the current scope.
unsigned getStackAlignment() const
Returns the natural alignment of the stack in bits.
unsigned getTypeSize(Type t) const
Returns the size of the given type in the current scope.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:41
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
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:104
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
The base class used for all derived interface types.
Attribute getDefaultAllocaMemorySpace(DataLayoutEntryInterface entry)
Default handler for alloca memory space request.
unsigned getDefaultPreferredAlignment(Type type, const DataLayout &dataLayout, ArrayRef< DataLayoutEntryInterface > params)
Default handler for the preferred alignemnt request.
unsigned getDefaultABIAlignment(Type type, const DataLayout &dataLayout, ArrayRef< DataLayoutEntryInterface > params)
Default handler for the required alignemnt 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...
unsigned getDefaultStackAlignment(DataLayoutEntryInterface entry)
Default handler for the stack alignment request.
unsigned getDefaultTypeSizeInBits(Type type, const DataLayout &dataLayout, DataLayoutEntryListRef params)
Default handler for the type size in bits request.
unsigned getDefaultTypeSize(Type type, const DataLayout &dataLayout, DataLayoutEntryListRef params)
Default handler for the type size 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.
DataLayoutEntryInterface filterEntryForIdentifier(DataLayoutEntryListRef entries, StringAttr id)
Given a list of data layout entries, returns the entry that has the given identifier as key,...
This header declares functions that assist transformations in the MemRef dialect.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26