clang 22.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}
25
26llvm::Align CIRDataLayout::getAlignment(mlir::Type ty, bool useABIAlign) const {
27 // FIXME(cir): This does not account for differnt address spaces, and relies
28 // on CIR's data layout to give the proper alignment.
30
31 // Fetch type alignment from MLIR's data layout.
32 unsigned align = useABIAlign ? layout.getTypeABIAlignment(ty)
33 : layout.getTypePreferredAlignment(ty);
34 return llvm::Align(align);
35}
36
37// The implementation of this method is provided inline as it is particularly
38// well suited to constant folding when called on a specific Type subclass.
39llvm::TypeSize CIRDataLayout::getTypeSizeInBits(mlir::Type ty) const {
40 assert(cir::isSized(ty) && "Cannot getTypeInfo() on a type that is unsized!");
41
42 if (auto recordTy = llvm::dyn_cast<cir::RecordType>(ty)) {
43 // FIXME(cir): CIR record's data layout implementation doesn't do a good job
44 // of handling unions particularities. We should have a separate union type.
45 return recordTy.getTypeSizeInBits(layout, {});
46 }
47
48 // FIXME(cir): This does not account for different address spaces, and relies
49 // on CIR's data layout to give the proper ABI-specific type width.
51
52 // This is calling mlir::DataLayout::getTypeSizeInBits().
53 return llvm::TypeSize::getFixed(layout.getTypeSizeInBits(ty));
54}
llvm::Align getAlignment(mlir::Type ty, bool abiOrPref) const
Internal helper method that returns requested alignment for type.
mlir::DataLayout layout
Definition: CIRDataLayout.h:28
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::TypeSize getTypeSizeInBits(mlir::Type ty) const
Definition: ABIArgInfo.h:22
bool isSized(mlir::Type ty)
Returns true if the type is a CIR sized type.
Definition: CIRTypes.cpp:24
static bool addressSpace()