Skip to content

Commit 8f5faa7

Browse files
committed
[CIR] Track type sizes with module attribute
1 parent 60126e9 commit 8f5faa7

File tree

7 files changed

+98
-16
lines changed

7 files changed

+98
-16
lines changed

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

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

125+
//===----------------------------------------------------------------------===//
126+
// TypeSizesInfoAttr
127+
//===----------------------------------------------------------------------===//
128+
129+
def CIR_TypeSizesInfoAttr : CIR_Attr<"TypeSizesInfo", "type_sizes_info"> {
130+
let summary = "the size of types in bits";
131+
let description = [{
132+
The `cir.type_sizes` attribute is attached to a module, recording lengths
133+
of various types if their names don't include it.
134+
135+
It is worth noticing that size_t and pointers are considered to have the
136+
same length in Clang IR.
137+
138+
Float and double types are represented by cir::SingleType and cir::
139+
DoubleType respectively, whose constructos don't need the type size as an
140+
argument. So their lengths are not stored here.
141+
142+
Examples:
143+
144+
```mlir
145+
// sizeof(int) == 4, sizeof(size_t) == 8
146+
module attributes {
147+
cir.type_sizes = #cir.type_sizes<
148+
char = 8,
149+
int = 32,
150+
size_t = 64
151+
>
152+
} {}
153+
```
154+
}];
155+
156+
let parameters = (ins "unsigned":$char_size,
157+
"unsigned":$int_size,
158+
"unsigned":$size_type_size);
159+
160+
let assemblyFormat = [{
161+
`<`
162+
`char` `=` $char_size `,`
163+
`int` `=` $int_size `,`
164+
`size_t` `=` $size_type_size
165+
`>`
166+
}];
167+
168+
let extraClassDeclaration = [{
169+
unsigned getPointerSize() const { return getSizeTypeSize(); }
170+
171+
mlir::Type getCharType(mlir::MLIRContext *ctx) const {
172+
return cir::IntType::get(ctx, getCharSize(), /*signed=*/true);
173+
}
174+
175+
mlir::Type getUCharType(mlir::MLIRContext *ctx) const {
176+
return cir::IntType::get(ctx, getCharSize(), /*signed=*/false);
177+
}
178+
179+
mlir::Type getIntType(mlir::MLIRContext *ctx) const {
180+
return cir::IntType::get(ctx, getIntSize(), /*signed=*/true);
181+
}
182+
183+
mlir::Type getUIntType(mlir::MLIRContext *ctx) const {
184+
return cir::IntType::get(ctx, getIntSize(), /*signed=*/false);
185+
}
186+
187+
mlir::Type getSizeType(mlir::MLIRContext *ctx) const {
188+
return cir::IntType::get(ctx, getSizeTypeSize(), /*signed=*/false);
189+
}
190+
191+
mlir::Type getPtrDiffType(mlir::MLIRContext *ctx) const {
192+
return cir::IntType::get(ctx, getSizeTypeSize(), /*signed=*/true);
193+
}
194+
}];
195+
}
196+
197+
125198
//===----------------------------------------------------------------------===//
126199
// BoolAttr
127200
//===----------------------------------------------------------------------===//

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 getTypeSizesInfoAttrName() { return "cir.type_sizes_info"; }
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

+15-14
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
112112
openMPRuntime(new CIRGenOpenMPRuntime(*this)),
113113
cudaRuntime(new CIRGenCUDARuntime(*this)) {
114114

115+
unsigned charSize = astContext.getTargetInfo().getCharWidth();
116+
unsigned intSize = astContext.getTargetInfo().getIntWidth();
117+
unsigned sizeTypeSize = astContext.getTargetInfo().getMaxPointerWidth();
118+
119+
auto typeSizeInfo = cir::TypeSizesInfoAttr::get(&mlirContext, charSize,
120+
intSize, sizeTypeSize);
121+
theModule->setAttr(cir::CIRDialect::getTypeSizesInfoAttrName(), typeSizeInfo);
122+
115123
// Initialize CIR signed integer types cache.
116124
SInt8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/true);
117125
SInt16Ty = cir::IntType::get(&getMLIRContext(), 16, /*isSigned=*/true);
@@ -146,19 +154,13 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
146154
astContext.getTargetInfo().getPointerAlign(LangAS::Default))
147155
.getQuantity();
148156
SizeSizeInBytes =
149-
astContext
150-
.toCharUnitsFromBits(astContext.getTargetInfo().getMaxPointerWidth())
157+
astContext.toCharUnitsFromBits(typeSizeInfo.getSizeTypeSize())
151158
.getQuantity();
152159
// TODO: IntAlignInBytes
153-
UCharTy = cir::IntType::get(&getMLIRContext(),
154-
astContext.getTargetInfo().getCharWidth(),
155-
/*isSigned=*/false);
156-
UIntTy = cir::IntType::get(&getMLIRContext(),
157-
astContext.getTargetInfo().getIntWidth(),
158-
/*isSigned=*/false);
159-
UIntPtrTy = cir::IntType::get(&getMLIRContext(),
160-
astContext.getTargetInfo().getMaxPointerWidth(),
161-
/*isSigned=*/false);
160+
UCharTy = typeSizeInfo.getUCharType(&getMLIRContext());
161+
UIntTy = typeSizeInfo.getUIntType(&getMLIRContext());
162+
// In CIRGenTypeCache, UIntPtrTy and SizeType are fields of the same union
163+
UIntPtrTy = typeSizeInfo.getSizeType(&getMLIRContext());
162164
UInt8PtrTy = builder.getPointerTo(UInt8Ty);
163165
UInt8PtrPtrTy = builder.getPointerTo(UInt8PtrTy);
164166
AllocaInt8PtrTy = UInt8PtrTy;
@@ -167,9 +169,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
167169
// TODO: ConstGlobalsPtrTy
168170
CIRAllocaAddressSpace = getTargetCIRGenInfo().getCIRAllocaAddressSpace();
169171

170-
PtrDiffTy = cir::IntType::get(&getMLIRContext(),
171-
astContext.getTargetInfo().getMaxPointerWidth(),
172-
/*isSigned=*/true);
172+
PtrDiffTy = typeSizeInfo.getPtrDiffType(&getMLIRContext());
173173

174174
if (langOpts.OpenCL) {
175175
createOpenCLRuntime();
@@ -197,6 +197,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
197197
cir::LangAttr::get(&mlirContext, lang));
198198
theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
199199
builder.getStringAttr(getTriple().str()));
200+
200201
if (CGO.OptimizationLevel > 0 || CGO.OptimizeSize > 0)
201202
theModule->setAttr(cir::CIRDialect::getOptInfoAttrName(),
202203
cir::OptInfoAttr::get(&mlirContext,

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

+1
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"

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

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ 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_info =
9+
// CHECK-DAG: #cir.type_sizes_info<
10+
// CHECK-DAG: char = 8,
11+
// CHECK-DAG: int = {{16|32}},
12+
// CHECK-DAG: size_t = {{32|64}}
13+
// CHECK-DAG: >
814
// CHECK-DAG: dlti.dl_spec =
915
// CHECK-DAG: #dlti.dl_spec<
1016
// 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)