Skip to content

Commit 8134a8f

Browse files
authored
[mlir] use TypeSize and uint64_t in DataLayout (#72874)
Data layout queries may be issued for types whose size exceeds the range of 32-bit integer as well as for types that don't have a size known at compile time, such as scalable vectors. Use best practices from LLVM IR and adopt `llvm::TypeSize` for size-related queries and `uint64_t` for alignment-related queries. See #72678.
1 parent 49f0070 commit 8134a8f

File tree

21 files changed

+318
-247
lines changed

21 files changed

+318
-247
lines changed

mlir/docs/DataLayout.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ class DataLayout {
7373
public:
7474
explicit DataLayout(DataLayoutOpInterface scope);
7575

76-
unsigned getTypeSize(Type type) const;
77-
unsigned getTypeSizeInBits(Type type) const;
78-
unsigned getTypeABIAlignment(Type type) const;
79-
unsigned getTypePreferredAlignment(Type type) const;
76+
llvm::TypeSize getTypeSize(Type type) const;
77+
llvm::TypeSize getTypeSizeInBits(Type type) const;
78+
uint64_t getTypeABIAlignment(Type type) const;
79+
uint64_t getTypePreferredAlignment(Type type) const;
8080
};
8181
```
8282

mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ class LLVMStructType
186186

187187
/// Hooks for DataLayoutTypeInterface. Should not be called directly. Obtain a
188188
/// DataLayout instance and query it instead.
189-
unsigned getTypeSizeInBits(const DataLayout &dataLayout,
190-
DataLayoutEntryListRef params) const;
189+
llvm::TypeSize getTypeSizeInBits(const DataLayout &dataLayout,
190+
DataLayoutEntryListRef params) const;
191191

192-
unsigned getABIAlignment(const DataLayout &dataLayout,
192+
uint64_t getABIAlignment(const DataLayout &dataLayout,
193193
DataLayoutEntryListRef params) const;
194194

195-
unsigned getPreferredAlignment(const DataLayout &dataLayout,
195+
uint64_t getPreferredAlignment(const DataLayout &dataLayout,
196196
DataLayoutEntryListRef params) const;
197197

198198
bool areCompatible(DataLayoutEntryListRef oldLayout,
@@ -288,7 +288,7 @@ enum class PtrDLEntryPos { Size = 0, Abi = 1, Preferred = 2, Index = 3 };
288288
/// Returns `std::nullopt` if `pos` is not present in the entry.
289289
/// Currently only `PtrDLEntryPos::Index` is optional, and all other positions
290290
/// may be assumed to be present.
291-
std::optional<unsigned> extractPointerSpecValue(Attribute attr,
291+
std::optional<uint64_t> extractPointerSpecValue(Attribute attr,
292292
PtrDLEntryPos pos);
293293

294294
} // namespace LLVM

mlir/include/mlir/Interfaces/DataLayoutInterfaces.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "mlir/IR/DialectInterface.h"
1919
#include "mlir/IR/OpDefinition.h"
2020
#include "llvm/ADT/DenseMap.h"
21+
#include "llvm/Support/TypeSize.h"
2122

2223
namespace mlir {
2324
class DataLayout;
@@ -34,25 +35,25 @@ class ModuleOp;
3435
namespace detail {
3536
/// Default handler for the type size request. Computes results for built-in
3637
/// types and dispatches to the DataLayoutTypeInterface for other types.
37-
unsigned getDefaultTypeSize(Type type, const DataLayout &dataLayout,
38-
DataLayoutEntryListRef params);
38+
llvm::TypeSize getDefaultTypeSize(Type type, const DataLayout &dataLayout,
39+
DataLayoutEntryListRef params);
3940

4041
/// Default handler for the type size in bits request. Computes results for
4142
/// built-in types and dispatches to the DataLayoutTypeInterface for other
4243
/// types.
43-
unsigned getDefaultTypeSizeInBits(Type type, const DataLayout &dataLayout,
44-
DataLayoutEntryListRef params);
44+
llvm::TypeSize getDefaultTypeSizeInBits(Type type, const DataLayout &dataLayout,
45+
DataLayoutEntryListRef params);
4546

46-
/// Default handler for the required alignemnt request. Computes results for
47+
/// Default handler for the required alignment request. Computes results for
4748
/// built-in types and dispatches to the DataLayoutTypeInterface for other
4849
/// types.
49-
unsigned getDefaultABIAlignment(Type type, const DataLayout &dataLayout,
50+
uint64_t getDefaultABIAlignment(Type type, const DataLayout &dataLayout,
5051
ArrayRef<DataLayoutEntryInterface> params);
5152

52-
/// Default handler for the preferred alignemnt request. Computes results for
53+
/// Default handler for the preferred alignment request. Computes results for
5354
/// built-in types and dispatches to the DataLayoutTypeInterface for other
5455
/// types.
55-
unsigned
56+
uint64_t
5657
getDefaultPreferredAlignment(Type type, const DataLayout &dataLayout,
5758
ArrayRef<DataLayoutEntryInterface> params);
5859

@@ -62,7 +63,7 @@ Attribute getDefaultAllocaMemorySpace(DataLayoutEntryInterface entry);
6263

6364
/// Default handler for the stack alignment request. Dispatches to the
6465
/// DataLayoutInterface if specified, otherwise returns the default.
65-
unsigned getDefaultStackAlignment(DataLayoutEntryInterface entry);
66+
uint64_t getDefaultStackAlignment(DataLayoutEntryInterface entry);
6667

6768
/// Given a list of data layout entries, returns a new list containing the
6869
/// entries with keys having the given type ID, i.e. belonging to the same type
@@ -85,6 +86,10 @@ LogicalResult verifyDataLayoutOp(Operation *op);
8586
/// entry verifiers, and then to the verifiers implemented by the relevant type
8687
/// and dialect interfaces for type and identifier keys respectively.
8788
LogicalResult verifyDataLayoutSpec(DataLayoutSpecInterface spec, Location loc);
89+
90+
/// Divides the known min value of the numerator by the denominator and rounds
91+
/// the result up to the next integer. Preserves the scalable flag.
92+
llvm::TypeSize divideCeil(llvm::TypeSize numerator, uint64_t denominator);
8893
} // namespace detail
8994
} // namespace mlir
9095

@@ -156,16 +161,16 @@ class DataLayout {
156161
static DataLayout closest(Operation *op);
157162

158163
/// Returns the size of the given type in the current scope.
159-
unsigned getTypeSize(Type t) const;
164+
llvm::TypeSize getTypeSize(Type t) const;
160165

161166
/// Returns the size in bits of the given type in the current scope.
162-
unsigned getTypeSizeInBits(Type t) const;
167+
llvm::TypeSize getTypeSizeInBits(Type t) const;
163168

164169
/// Returns the required alignment of the given type in the current scope.
165-
unsigned getTypeABIAlignment(Type t) const;
170+
uint64_t getTypeABIAlignment(Type t) const;
166171

167172
/// Returns the preferred of the given type in the current scope.
168-
unsigned getTypePreferredAlignment(Type t) const;
173+
uint64_t getTypePreferredAlignment(Type t) const;
169174

170175
/// Returns the memory space used for AllocaOps.
171176
Attribute getAllocaMemorySpace() const;
@@ -174,7 +179,7 @@ class DataLayout {
174179
/// stack variables should be limited to the natural stack alignment to
175180
/// prevent dynamic stack alignment. Returns zero if the stack alignment is
176181
/// unspecified.
177-
unsigned getStackAlignment() const;
182+
uint64_t getStackAlignment() const;
178183

179184
private:
180185
/// Combined layout spec at the given scope.
@@ -193,16 +198,16 @@ class DataLayout {
193198
Operation *scope;
194199

195200
/// 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;
201+
mutable DenseMap<Type, llvm::TypeSize> sizes;
202+
mutable DenseMap<Type, llvm::TypeSize> bitsizes;
203+
mutable DenseMap<Type, uint64_t> abiAlignments;
204+
mutable DenseMap<Type, uint64_t> preferredAlignments;
200205

201206
/// Cache for alloca memory space.
202207
mutable std::optional<Attribute> allocaMemorySpace;
203208

204209
/// Cache for stack alignment.
205-
mutable std::optional<unsigned> stackAlignment;
210+
mutable std::optional<uint64_t> stackAlignment;
206211
};
207212

208213
} // namespace mlir

mlir/include/mlir/Interfaces/DataLayoutInterfaces.td

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,22 @@ def DataLayoutOpInterface : OpInterface<"DataLayoutOpInterface"> {
213213
/*description=*/"Returns the size of the given type computed using the "
214214
"relevant entries. The data layout object can be used "
215215
"for recursive queries.",
216-
/*retTy=*/"unsigned",
216+
/*retTy=*/"::llvm::TypeSize",
217217
/*methodName=*/"getTypeSize",
218218
/*args=*/(ins "::mlir::Type":$type,
219219
"const ::mlir::DataLayout &":$dataLayout,
220220
"::mlir::DataLayoutEntryListRef":$params),
221221
/*methodBody=*/"",
222222
/*defaultImplementation=*/[{
223-
unsigned bits = ConcreteOp::getTypeSizeInBits(type, dataLayout, params);
224-
return ::llvm::divideCeil(bits, 8);
223+
::llvm::TypeSize bits = ConcreteOp::getTypeSizeInBits(type, dataLayout, params);
224+
return ::mlir::detail::divideCeil(bits, 8u);
225225
}]
226226
>,
227227
StaticInterfaceMethod<
228228
/*description=*/"Returns the size of the given type in bits computed "
229229
"using the relevant entries. The data layout object can "
230230
"be used for recursive queries.",
231-
/*retTy=*/"unsigned",
231+
/*retTy=*/"::llvm::TypeSize",
232232
/*methodName=*/"getTypeSizeInBits",
233233
/*args=*/(ins "::mlir::Type":$type,
234234
"const ::mlir::DataLayout &":$dataLayout,
@@ -243,7 +243,7 @@ def DataLayoutOpInterface : OpInterface<"DataLayoutOpInterface"> {
243243
/*description=*/"Returns the alignment required by the ABI for the given "
244244
"type computed using the relevant entries. The data "
245245
"layout object can be used for recursive queries.",
246-
/*retTy=*/"unsigned",
246+
/*retTy=*/"uint64_t",
247247
/*methodName=*/"getTypeABIAlignment",
248248
/*args=*/(ins "::mlir::Type":$type,
249249
"const ::mlir::DataLayout &":$dataLayout,
@@ -257,7 +257,7 @@ def DataLayoutOpInterface : OpInterface<"DataLayoutOpInterface"> {
257257
/*description=*/"Returns the alignment preferred by the given type "
258258
"computed using the relevant entries. The data layout"
259259
"object can be used for recursive queries.",
260-
/*retTy=*/"unsigned",
260+
/*retTy=*/"uint64_t",
261261
/*methodName=*/"getTypePreferredAlignment",
262262
/*args=*/(ins "::mlir::Type":$type,
263263
"const ::mlir::DataLayout &":$dataLayout,
@@ -284,7 +284,7 @@ def DataLayoutOpInterface : OpInterface<"DataLayoutOpInterface"> {
284284
/*description=*/"Returns the natural stack alignment in bits computed "
285285
"using the relevant entries. The data layout object "
286286
"can be used for recursive queries.",
287-
/*retTy=*/"unsigned",
287+
/*retTy=*/"uint64_t",
288288
/*methodName=*/"getStackAlignment",
289289
/*args=*/(ins "::mlir::DataLayoutEntryInterface":$entry),
290290
/*methodBody=*/"",
@@ -331,35 +331,35 @@ def DataLayoutTypeInterface : TypeInterface<"DataLayoutTypeInterface"> {
331331
let methods = [
332332
InterfaceMethod<
333333
/*description=*/"Returns the size of this type in bytes.",
334-
/*retTy=*/"unsigned",
334+
/*retTy=*/"::llvm::TypeSize",
335335
/*methodName=*/"getTypeSize",
336336
/*args=*/(ins "const ::mlir::DataLayout &":$dataLayout,
337337
"::mlir::DataLayoutEntryListRef":$params),
338338
/*methodBody=*/"",
339339
/*defaultImplementation=*/[{
340-
unsigned bits = $_type.getTypeSizeInBits(dataLayout, params);
341-
return ::llvm::divideCeil(bits, 8);
340+
::llvm::TypeSize bits = $_type.getTypeSizeInBits(dataLayout, params);
341+
return ::mlir::detail::divideCeil(bits, 8u);
342342
}]
343343
>,
344344
InterfaceMethod<
345345
/*description=*/"Returns the size of this type in bits.",
346-
/*retTy=*/"unsigned",
346+
/*retTy=*/"::llvm::TypeSize",
347347
/*methodName=*/"getTypeSizeInBits",
348348
/*args=*/(ins "const ::mlir::DataLayout &":$dataLayout,
349349
"::mlir::DataLayoutEntryListRef":$params)
350350
>,
351351
InterfaceMethod<
352352
/*description=*/"Returns the ABI-required alignment for this type, "
353353
"in bytes",
354-
/*retTy=*/"unsigned",
354+
/*retTy=*/"uint64_t",
355355
/*methodName=*/"getABIAlignment",
356356
/*args=*/(ins "const ::mlir::DataLayout &":$dataLayout,
357357
"::mlir::DataLayoutEntryListRef":$params)
358358
>,
359359
InterfaceMethod<
360360
/*description=*/"Returns the preferred alignment for this type, "
361361
"in bytes.",
362-
/*retTy=*/"unsigned",
362+
/*retTy=*/"uint64_t",
363363
/*methodName=*/"getPreferredAlignment",
364364
/*args=*/(ins "const ::mlir::DataLayout &":$dataLayout,
365365
"::mlir::DataLayoutEntryListRef":$params)

0 commit comments

Comments
 (0)