clang 23.0.0git
CIRDataLayout.cpp
Go to the documentation of this file.
4
5using namespace cir;
6
7//===----------------------------------------------------------------------===//
8// DataLayout Class Implementation
9//===----------------------------------------------------------------------===//
10
11CIRDataLayout::CIRDataLayout(mlir::ModuleOp modOp) : layout(modOp) {
12 reset(modOp.getDataLayoutSpec());
13}
14
15void CIRDataLayout::reset(mlir::DataLayoutSpecInterface spec) {
16 bigEndian = false;
17 if (spec) {
18 mlir::StringAttr key = mlir::StringAttr::get(
19 spec.getContext(), mlir::DLTIDialect::kDataLayoutEndiannessKey);
20 if (mlir::DataLayoutEntryInterface entry = spec.getSpecForIdentifier(key))
21 if (auto str = llvm::dyn_cast<mlir::StringAttr>(entry.getValue()))
22 bigEndian = str == mlir::DLTIDialect::kDataLayoutEndiannessBig;
23
24 mlir::StringAttr addrSpKey = mlir::StringAttr::get(
25 spec.getContext(), mlir::DLTIDialect::kDataLayoutProgramMemorySpaceKey);
26 if (mlir::DataLayoutEntryInterface entry =
27 spec.getSpecForIdentifier(addrSpKey))
28 if (auto val = llvm::dyn_cast<mlir::IntegerAttr>(entry.getValue()))
29 programAddrSpace = val.getInt();
30 }
31}
32
33llvm::Align CIRDataLayout::getAlignment(mlir::Type ty, bool useABIAlign) const {
34 // FIXME(cir): This does not account for differnt address spaces, and relies
35 // on CIR's data layout to give the proper alignment.
37
38 // Fetch type alignment from MLIR's data layout.
39 unsigned align = useABIAlign ? layout.getTypeABIAlignment(ty)
40 : layout.getTypePreferredAlignment(ty);
41 return llvm::Align(align);
42}
43
44// The implementation of this method is provided inline as it is particularly
45// well suited to constant folding when called on a specific Type subclass.
46llvm::TypeSize CIRDataLayout::getTypeSizeInBits(mlir::Type ty) const {
47 assert(cir::isSized(ty) && "Cannot getTypeInfo() on a type that is unsized!");
48
49 if (auto recordTy = llvm::dyn_cast<cir::RecordType>(ty)) {
50 // FIXME(cir): CIR record's data layout implementation doesn't do a good job
51 // of handling unions particularities. We should have a separate union type.
52 return recordTy.getTypeSizeInBits(layout, {});
53 }
54
55 // FIXME(cir): This does not account for different address spaces, and relies
56 // on CIR's data layout to give the proper ABI-specific type width.
58
59 // This is calling mlir::DataLayout::getTypeSizeInBits().
60 return llvm::TypeSize::getFixed(layout.getTypeSizeInBits(ty));
61}
mlir::DataLayout layout
void reset(mlir::DataLayoutSpecInterface spec)
Parse a data layout string (with fallback to default values).
CIRDataLayout(mlir::ModuleOp modOp)
Constructs a DataLayout the module's data layout attribute.
llvm::Align getAlignment(mlir::Type ty, bool useABIAlign) const
Internal helper method that returns requested alignment for type.
llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const
bool isSized(mlir::Type ty)
Returns true if the type is a CIR sized type.
Definition CIRTypes.cpp:33
static bool addressSpace()