1#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
2#include "clang/CIR/Dialect/IR/CIRDialect.h"
3#include "clang/CIR/Dialect/IR/CIRTypes.h"
4#include "clang/CIR/MissingFeatures.h"
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(spec: modOp.getDataLayoutSpec());
16}
17
18void CIRDataLayout::reset(mlir::DataLayoutSpecInterface spec) {
19 bigEndian = false;
20 bitIntMaxAlignBits = cir::IntType::getBitIntMaxAlignBits(params: {});
21 vectorMaxAlignBits = 0;
22 if (spec) {
23 bitIntMaxAlignBits = cir::IntType::getBitIntMaxAlignBits(
24 params: spec.getSpecForType(type: cir::IntType::getTypeID()));
25 vectorMaxAlignBits = cir::VectorType::getMaxAlignBits(
26 params: spec.getSpecForType(type: cir::VectorType::getTypeID()));
27
28 mlir::StringAttr key = mlir::StringAttr::get(
29 context: spec.getContext(), bytes: mlir::DLTIDialect::kDataLayoutEndiannessKey);
30 if (mlir::DataLayoutEntryInterface entry = spec.getSpecForIdentifier(identifier: key))
31 if (auto str = llvm::dyn_cast<mlir::StringAttr>(Val: entry.getValue()))
32 bigEndian = str == mlir::DLTIDialect::kDataLayoutEndiannessBig;
33
34 mlir::StringAttr addrSpKey = mlir::StringAttr::get(
35 context: spec.getContext(), bytes: mlir::DLTIDialect::kDataLayoutProgramMemorySpaceKey);
36 if (mlir::DataLayoutEntryInterface entry =
37 spec.getSpecForIdentifier(identifier: addrSpKey))
38 if (auto val = llvm::dyn_cast<mlir::IntegerAttr>(Val: 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.
46 assert(!cir::MissingFeatures::addressSpace());
47
48 // Fetch type alignment from MLIR's data layout.
49 unsigned align = useABIAlign ? layout.getTypeABIAlignment(t: ty)
50 : layout.getTypePreferredAlignment(t: 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(a: align, b: getTypeLanguageAlignment(ty: member));
59 return align;
60 };
61 return llvm::TypeSwitch<mlir::Type, uint64_t>(ty)
62 .Case(caseFn: [&](cir::IntType intTy) -> uint64_t {
63 if (intTy.isBitInt())
64 return intTy.getBitIntAlignRequirement(maxAlignBits: bitIntMaxAlignBits);
65 return layout.getTypeABIAlignment(t: intTy);
66 })
67 .Case(caseFn: [&](cir::RecordType recTy) -> uint64_t {
68 if (cir::RecordLayoutAttr recLayout =
69 cir::tryGetRecordLayout(mod: module, name: 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(caseFn: [&](cir::VectorType vecTy) -> uint64_t {
78 uint64_t align = layout.getTypeABIAlignment(t: vecTy);
79 if (vectorMaxAlignBits)
80 return std::min(a: align, b: vectorMaxAlignBits / 8);
81 return align;
82 })
83 .Case<cir::ArrayType, cir::ComplexType, cir::MatrixType>(
84 caseFn: [&](auto elementsTy) -> uint64_t {
85 return getTypeLanguageAlignment(ty: elementsTy.getElementType());
86 })
87 .Case(caseFn: [&](cir::BitFieldType bitFieldTy) -> uint64_t {
88 if (mlir::Type storage = bitFieldTy.getStorageType())
89 return getTypeLanguageAlignment(ty: storage);
90 return 1;
91 })
92 .Default(defaultFn: [&](mlir::Type ty) { return layout.getTypeABIAlignment(t: ty); });
93}
94
95// The implementation of this method is provided inline as it is particularly
96// well suited to constant folding when called on a specific Type subclass.
97llvm::TypeSize CIRDataLayout::getTypeSizeInBits(mlir::Type ty) const {
98 assert(cir::isSized(ty) && "Cannot getTypeInfo() on a type that is unsized!");
99
100 if (auto structTy = llvm::dyn_cast<cir::StructType>(Val&: ty))
101 return structTy.getTypeSizeInBits(dataLayout: layout, params: {});
102 if (auto unionTy = llvm::dyn_cast<cir::UnionType>(Val&: ty))
103 return unionTy.getTypeSizeInBits(dataLayout: layout, params: {});
104
105 // FIXME(cir): This does not account for different address spaces, and relies
106 // on CIR's data layout to give the proper ABI-specific type width.
107 assert(!cir::MissingFeatures::addressSpace());
108
109 // This is calling mlir::DataLayout::getTypeSizeInBits().
110 return layout.getTypeSizeInBits(t: ty);
111}
112