clang 24.0.0git
CIRDataLayout.cpp
Go to the documentation of this file.
5#include "llvm/ADT/TypeSwitch.h"
6
7using namespace cir;
8
9//===----------------------------------------------------------------------===//
10// DataLayout Class Implementation
11//===----------------------------------------------------------------------===//
12
13CIRDataLayout::CIRDataLayout(mlir::ModuleOp modOp)
14 : module(modOp), layout(modOp) {
15 reset(modOp.getDataLayoutSpec());
16}
17
18void CIRDataLayout::reset(mlir::DataLayoutSpecInterface spec) {
19 bigEndian = false;
20 bitIntMaxAlignBits = cir::IntType::getBitIntMaxAlignBits({});
21 vectorMaxAlignBits = 0;
22 if (spec) {
23 bitIntMaxAlignBits = cir::IntType::getBitIntMaxAlignBits(
24 spec.getSpecForType(cir::IntType::getTypeID()));
25 vectorMaxAlignBits = cir::VectorType::getMaxAlignBits(
26 spec.getSpecForType(cir::VectorType::getTypeID()));
27
28 mlir::StringAttr key = mlir::StringAttr::get(
29 spec.getContext(), mlir::DLTIDialect::kDataLayoutEndiannessKey);
30 if (mlir::DataLayoutEntryInterface entry = spec.getSpecForIdentifier(key))
31 if (auto str = llvm::dyn_cast<mlir::StringAttr>(entry.getValue()))
32 bigEndian = str == mlir::DLTIDialect::kDataLayoutEndiannessBig;
33
34 mlir::StringAttr addrSpKey = mlir::StringAttr::get(
35 spec.getContext(), mlir::DLTIDialect::kDataLayoutProgramMemorySpaceKey);
36 if (mlir::DataLayoutEntryInterface entry =
37 spec.getSpecForIdentifier(addrSpKey))
38 if (auto val = llvm::dyn_cast<mlir::IntegerAttr>(entry.getValue()))
39 programAddrSpace = val.getUInt();
40 }
41}
42
43llvm::Align CIRDataLayout::getAlignment(mlir::Type ty, bool useABIAlign) const {
44 // FIXME(cir): This does not account for differnt address spaces, and relies
45 // on CIR's data layout to give the proper alignment.
47
48 // Fetch type alignment from MLIR's data layout.
49 unsigned align = useABIAlign ? layout.getTypeABIAlignment(ty)
50 : layout.getTypePreferredAlignment(ty);
51 return llvm::Align(align);
52}
53
54uint64_t CIRDataLayout::getTypeLanguageAlignment(mlir::Type ty) const {
55 auto maxMemberAlignment = [&](llvm::ArrayRef<mlir::Type> members) {
56 uint64_t align = 1;
57 for (mlir::Type member : members)
58 align = std::max(align, getTypeLanguageAlignment(member));
59 return align;
60 };
61 return llvm::TypeSwitch<mlir::Type, uint64_t>(ty)
62 .Case([&](cir::IntType intTy) -> uint64_t {
63 if (intTy.isBitInt())
64 return intTy.getBitIntAlignRequirement(bitIntMaxAlignBits);
65 return layout.getTypeABIAlignment(intTy);
66 })
67 .Case([&](cir::RecordType recTy) -> uint64_t {
68 if (cir::RecordLayoutAttr recLayout =
69 cir::tryGetRecordLayout(module, recTy.getName()))
70 return recLayout.getRecordAlign();
71 // A record without a layout entry, such as one in hand-written CIR.
72 assert(recTy.isComplete() && "Cannot get layout of incomplete records");
73 if (recTy.getPacked())
74 return 1;
75 return maxMemberAlignment(recTy.getMembers());
76 })
77 .Case([&](cir::VectorType vecTy) -> uint64_t {
78 // An SVE data vector is 16-byte aligned and an SVE predicate is
79 // 2-byte aligned, regardless of its length.
80 if (vecTy.getKind() == cir::VectorAbiKind::SVEData)
81 return 16;
82 if (vecTy.getKind() == cir::VectorAbiKind::SVEPredicate)
83 return 2;
84 uint64_t align = layout.getTypeABIAlignment(vecTy);
85 if (vectorMaxAlignBits)
86 return std::min(align, vectorMaxAlignBits / 8);
87 return align;
88 })
89 .Case<cir::ArrayType, cir::ComplexType, cir::MatrixType>(
90 [&](auto elementsTy) -> uint64_t {
91 return getTypeLanguageAlignment(elementsTy.getElementType());
92 })
93 .Case([&](cir::BitFieldType bitFieldTy) -> uint64_t {
94 if (mlir::Type storage = bitFieldTy.getStorageType())
95 return getTypeLanguageAlignment(storage);
96 return 1;
97 })
98 .Default([&](mlir::Type ty) { return layout.getTypeABIAlignment(ty); });
99}
100
101// The implementation of this method is provided inline as it is particularly
102// well suited to constant folding when called on a specific Type subclass.
103llvm::TypeSize CIRDataLayout::getTypeSizeInBits(mlir::Type ty) const {
104 assert(cir::isSized(ty) && "Cannot getTypeInfo() on a type that is unsized!");
105
106 if (auto structTy = llvm::dyn_cast<cir::StructType>(ty))
107 return structTy.getTypeSizeInBits(layout, {});
108 if (auto unionTy = llvm::dyn_cast<cir::UnionType>(ty))
109 return unionTy.getTypeSizeInBits(layout, {});
110
111 // FIXME(cir): This does not account for different address spaces, and relies
112 // on CIR's data layout to give the proper ABI-specific type width.
114
115 // This is calling mlir::DataLayout::getTypeSizeInBits().
116 return layout.getTypeSizeInBits(ty);
117}
uint64_t getTypeLanguageAlignment(mlir::Type ty) const
Returns the alignment, in bytes, that the source language requires for an object of type ty.
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
C++ view class that accepts both !cir.struct and !cir.union types.
Definition CIRTypes.h:153
bool isComplete() const
Definition CIRTypes.h:172
llvm::ArrayRef< mlir::Type > getMembers() const
Definition CIRTypes.cpp:653
bool getPacked() const
Definition CIRTypes.cpp:668
mlir::StringAttr getName() const
Definition CIRTypes.cpp:658
RecordLayoutAttr tryGetRecordLayout(mlir::ModuleOp mod, mlir::StringAttr name)
Same lookup as getRecordLayout, but returns a null attribute instead of asserting when the record has...
bool isSized(mlir::Type ty)
Returns true if the type is a CIR sized type.
Definition CIRTypes.cpp:36
static bool addressSpace()