Skip to content

Commit da7c8d5

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

File tree

8 files changed

+96
-2
lines changed

8 files changed

+96
-2
lines changed

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

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

125+
//===----------------------------------------------------------------------===//
126+
// SizeTypeSizeAttr
127+
//===----------------------------------------------------------------------===//
128+
129+
def CIR_SizeTypeSizeAttr : CIR_Attr<"SizeTypeSize", "size_type_size"> {
130+
let summary = "the size of size_t 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(size_t) == 8
139+
module attributes {cir.size_type_size = #cir.size_type_size<64>} {}
140+
```
141+
}];
142+
143+
let parameters = (ins "unsigned":$size);
144+
145+
let assemblyFormat = [{
146+
`<` $size `>`
147+
}];
148+
}
149+
150+
//===----------------------------------------------------------------------===//
151+
// IntSizeAttr
152+
//===----------------------------------------------------------------------===//
153+
154+
def CIR_IntSizeAttr : CIR_Attr<"IntSize", "int_size"> {
155+
let summary = "the size of int in bits";
156+
let description = [{
157+
Attached to a module, to share information between CIR generation and
158+
later passes.
159+
160+
Examples:
161+
162+
```mlir
163+
// sizeof(int) == 4
164+
module attributes {cir.int_size = #cir.int_size<32>} {}
165+
```
166+
}];
167+
168+
let parameters = (ins "unsigned":$size);
169+
170+
let assemblyFormat = [{
171+
`<` $size `>`
172+
}];
173+
}
174+
175+
125176
//===----------------------------------------------------------------------===//
126177
// BoolAttr
127178
//===----------------------------------------------------------------------===//

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+
int intSize;
39+
int 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

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ 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 getSizeTypeSizeAttrName() { return "cir.size_type_size"; }
38+
static llvm::StringRef getIntSizeAttrName() { return "cir.int_size"; }
3739
static llvm::StringRef getSOBAttrName() { return "cir.sob"; }
3840
static llvm::StringRef getLangAttrName() { return "cir.lang"; }
3941
static llvm::StringRef getTripleAttrName() { return "cir.triple"; }

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
197197
cir::LangAttr::get(&mlirContext, lang));
198198
theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
199199
builder.getStringAttr(getTriple().str()));
200+
theModule->setAttr(
201+
cir::CIRDialect::getSizeTypeSizeAttrName(),
202+
cir::SizeTypeSizeAttr::get(
203+
&mlirContext, astContext.getTargetInfo().getMaxPointerWidth()));
204+
theModule->setAttr(
205+
cir::CIRDialect::getIntSizeAttrName(),
206+
cir::IntSizeAttr::get(&mlirContext,
207+
astContext.getTargetInfo().getIntWidth()));
200208
if (CGO.OptimizationLevel > 0 || CGO.OptimizeSize > 0)
201209
theModule->setAttr(cir::CIRDialect::getOptInfoAttrName(),
202210
cir::OptInfoAttr::get(&mlirContext,

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

+24
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,17 @@ class StructLayoutMap {
112113

113114
CIRDataLayout::CIRDataLayout(mlir::ModuleOp modOp) : layout{modOp} {
114115
reset(modOp.getDataLayoutSpec());
116+
117+
if (auto intSizeAttr =
118+
modOp->getAttr(cir::CIRDialect::getIntSizeAttrName())) {
119+
intSize = mlir::cast<cir::IntSizeAttr>(intSizeAttr).getSize();
120+
}
121+
122+
if (auto sizeTypeSizeAttr =
123+
modOp->getAttr(cir::CIRDialect::getSizeTypeSizeAttrName())) {
124+
sizeTypeSize =
125+
mlir::cast<cir::SizeTypeSizeAttr>(sizeTypeSizeAttr).getSize();
126+
}
115127
}
116128

117129
void CIRDataLayout::reset(mlir::DataLayoutSpecInterface spec) {
@@ -220,3 +232,15 @@ llvm::TypeSize CIRDataLayout::getTypeSizeInBits(mlir::Type Ty) const {
220232

221233
return llvm::TypeSize::getFixed(layout.getTypeSizeInBits(Ty));
222234
}
235+
236+
mlir::Type CIRDataLayout::getPtrDiffType(mlir::MLIRContext *ctx) const {
237+
return cir::IntType::get(ctx, sizeTypeSize, /*signed=*/true);
238+
}
239+
240+
mlir::Type CIRDataLayout::getSizeType(mlir::MLIRContext *ctx) const {
241+
return cir::IntType::get(ctx, sizeTypeSize, /*signed=*/false);
242+
}
243+
244+
mlir::Type CIRDataLayout::getIntType(mlir::MLIRContext *ctx) const {
245+
return cir::IntType::get(ctx, intSize, /*signed=*/true);
246+
}

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

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

66
// CHECK: module @"{{.*}}dlti.c" attributes {
77
// CHECK-DAG: cir.sob = #cir.signed_overflow_behavior<undefined>,
8+
// CHECK-DAG: cir.int_size = #cir.int_size<{{16|32}}>,
9+
// CHECK-DAG: cir.size_type_size = #cir.size_type_size<{{32|64}}>,
810
// CHECK-DAG: dlti.dl_spec =
911
// CHECK-DAG: #dlti.dl_spec<
1012
// 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)