Skip to content

Commit 712f521

Browse files
committed
[CIR] Track size_t and int size with module attributes
1 parent c301b4a commit 712f521

File tree

8 files changed

+67
-2
lines changed

8 files changed

+67
-2
lines changed

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

+31
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,37 @@ def CIR_OptInfoAttr : CIR_Attr<"OptInfo", "opt_info"> {
122122
let genVerifyDecl = 1;
123123
}
124124

125+
//===----------------------------------------------------------------------===//
126+
// TypeSizeAttr
127+
//===----------------------------------------------------------------------===//
128+
129+
def CIR_TypeSizesAttr : CIR_Attr<"TypeSizes", "type_sizes"> {
130+
let summary = "the size of types in bits";
131+
let description = [{
132+
Attached to a module, to share information between CIR generation and
133+
later passes.
134+
135+
Examples:
136+
137+
```mlir
138+
// sizeof(int) == 4, sizeof(size_t) == 8
139+
module attributes {
140+
cir.type_sizes = #cir.type_sizes<
141+
int = 32,
142+
size_t = 64
143+
>
144+
} {}
145+
```
146+
}];
147+
148+
let parameters = (ins "unsigned":$intSize, "unsigned":$sizeTypeSize);
149+
150+
let assemblyFormat = [{
151+
`<` `int` `=` $intSize `,` `size_t` `=` $sizeTypeSize `>`
152+
}];
153+
}
154+
155+
125156
//===----------------------------------------------------------------------===//
126157
// BoolAttr
127158
//===----------------------------------------------------------------------===//

clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class CIRDataLayout {
3535
// The StructType -> StructLayout map.
3636
mutable void *LayoutMap = nullptr;
3737

38+
unsigned intSize;
39+
unsigned sizeTypeSize;
40+
3841
public:
3942
mlir::DataLayout layout;
4043

@@ -106,6 +109,10 @@ class CIRDataLayout {
106109
cir::IntType::get(Ty.getContext(), getPointerTypeSizeInBits(Ty), false);
107110
return IntTy;
108111
}
112+
113+
mlir::Type getIntType(mlir::MLIRContext *ctx) const;
114+
mlir::Type getSizeType(mlir::MLIRContext *ctx) const;
115+
mlir::Type getPtrDiffType(mlir::MLIRContext *ctx) const;
109116
};
110117

111118
/// Used to lazily calculate structure layout information for a target machine,

clang/include/clang/CIR/Dialect/IR/CIRDialect.td

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def CIR_Dialect : Dialect {
3434
// Names of CIR parameter attributes.
3535
static llvm::StringRef getSExtAttrName() { return "cir.signext"; }
3636
static llvm::StringRef getZExtAttrName() { return "cir.zeroext"; }
37+
static llvm::StringRef getTypeSizesAttrName() { return "cir.type_sizes"; }
3738
static llvm::StringRef getSOBAttrName() { return "cir.sob"; }
3839
static llvm::StringRef getLangAttrName() { return "cir.lang"; }
3940
static llvm::StringRef getTripleAttrName() { return "cir.triple"; }

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
197197
cir::LangAttr::get(&mlirContext, lang));
198198
theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
199199
builder.getStringAttr(getTriple().str()));
200+
201+
unsigned sizeTypeSize = astContext.getTargetInfo().getMaxPointerWidth();
202+
unsigned intSize = astContext.getTargetInfo().getIntWidth();
203+
theModule->setAttr(
204+
cir::CIRDialect::getTypeSizesAttrName(),
205+
cir::TypeSizesAttr::get(&mlirContext, intSize, sizeTypeSize));
200206
if (CGO.OptimizationLevel > 0 || CGO.OptimizeSize > 0)
201207
theModule->setAttr(cir::CIRDialect::getOptInfoAttrName(),
202208
cir::OptInfoAttr::get(&mlirContext,

clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
2+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
23
#include "clang/CIR/Dialect/IR/CIRTypes.h"
34
#include "clang/CIR/MissingFeatures.h"
45
#include "llvm/IR/DataLayout.h"
@@ -112,6 +113,12 @@ class StructLayoutMap {
112113

113114
CIRDataLayout::CIRDataLayout(mlir::ModuleOp modOp) : layout{modOp} {
114115
reset(modOp.getDataLayoutSpec());
116+
117+
if (auto attr = modOp->getAttr(cir::CIRDialect::getTypeSizesAttrName())) {
118+
auto typeSizeAttr = mlir::cast<cir::TypeSizesAttr>(attr);
119+
intSize = typeSizeAttr.getIntSize();
120+
sizeTypeSize = typeSizeAttr.getSizeTypeSize();
121+
}
115122
}
116123

117124
void CIRDataLayout::reset(mlir::DataLayoutSpecInterface spec) {
@@ -220,3 +227,15 @@ llvm::TypeSize CIRDataLayout::getTypeSizeInBits(mlir::Type Ty) const {
220227

221228
return llvm::TypeSize::getFixed(layout.getTypeSizeInBits(Ty));
222229
}
230+
231+
mlir::Type CIRDataLayout::getPtrDiffType(mlir::MLIRContext *ctx) const {
232+
return cir::IntType::get(ctx, sizeTypeSize, /*signed=*/true);
233+
}
234+
235+
mlir::Type CIRDataLayout::getSizeType(mlir::MLIRContext *ctx) const {
236+
return cir::IntType::get(ctx, sizeTypeSize, /*signed=*/false);
237+
}
238+
239+
mlir::Type CIRDataLayout::getIntType(mlir::MLIRContext *ctx) const {
240+
return cir::IntType::get(ctx, intSize, /*signed=*/true);
241+
}

clang/test/CIR/CodeGen/attribute-annotate-multiple.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void foo(int i) __attribute__((annotate("withargfunc", "os", 23 )));
1616
void bar() __attribute__((annotate("withargfunc", "os", 22))) {
1717
}
1818

19-
// BEFORE: module @{{.*}}attribute-annotate-multiple.cpp" attributes {cir.lang =
19+
// BEFORE: module @{{.*}}attribute-annotate-multiple.cpp" attributes {{{.*}}cir.lang =
2020

2121
// BEFORE: cir.global external @a = #cir.ptr<null> : !cir.ptr<!cir.double>
2222
// BEFORE-SAME: [#cir.annotation<name = "withargs", args = ["21", 12 : i32]>]

clang/test/CIR/CodeGen/dlti.c

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ void foo() {}
55

66
// CHECK: module @"{{.*}}dlti.c" attributes {
77
// CHECK-DAG: cir.sob = #cir.signed_overflow_behavior<undefined>,
8+
// CHECK-DAG: cir.type_sizes = #cir.type_sizes<int = {{16|32}}, size_t = {{32|64}}>
89
// CHECK-DAG: dlti.dl_spec =
910
// CHECK-DAG: #dlti.dl_spec<
1011
// CHECK-DAG: i16 = dense<16> : vector<2xi64>,

clang/test/CIR/CodeGen/sourcelocation.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int s0(int a, int b) {
1818
// CIR: #loc6 = loc("{{.*}}sourcelocation.cpp":6:19)
1919
// CIR: #loc21 = loc(fused[#loc3, #loc4])
2020
// CIR: #loc22 = loc(fused[#loc5, #loc6])
21-
// CIR: module @"{{.*}}sourcelocation.cpp" attributes {cir.lang = #cir.lang<cxx>, cir.sob = #cir.signed_overflow_behavior<undefined>
21+
// CIR: module @"{{.*}}sourcelocation.cpp" attributes {{{.*}}cir.lang = #cir.lang<cxx>, {{.*}}cir.sob = #cir.signed_overflow_behavior<undefined>
2222
// CIR: cir.func @_Z2s0ii(%arg0: !s32i loc(fused[#loc3, #loc4]), %arg1: !s32i loc(fused[#loc5, #loc6])) -> !s32i
2323
// CIR: %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init] {alignment = 4 : i64} loc(#loc21)
2424
// CIR: %1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init] {alignment = 4 : i64} loc(#loc22)

0 commit comments

Comments
 (0)