Skip to content

Commit 337b23e

Browse files
committed
[CIR] Upstream basic support for sizeof and alignof
1 parent 5f21ee2 commit 337b23e

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
9292

9393
mlir::Value VisitCastExpr(CastExpr *E);
9494

95+
mlir::Value VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *e);
96+
9597
/// Emit a conversion from the specified type to the specified destination
9698
/// type, both of which are CIR scalar types.
9799
/// TODO: do we need ScalarConversionOpts here? Should be done in another
@@ -148,3 +150,27 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
148150
}
149151
return {};
150152
}
153+
154+
/// Return the size or alignment of the type of argument of the sizeof
155+
/// expression as an integer.
156+
mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
157+
const UnaryExprOrTypeTraitExpr *e) {
158+
const QualType typeToSize = e->getTypeOfArgument();
159+
if (e->getKind() == UETT_SizeOf) {
160+
if (const VariableArrayType *variableArrTy =
161+
cgf.getContext().getAsVariableArrayType(typeToSize)) {
162+
cgf.getCIRGenModule().errorNYI(e->getSourceRange(),
163+
"sizeof operator for VariableArrayType",
164+
e->getStmtClassName());
165+
}
166+
} else if (e->getKind() == UETT_OpenMPRequiredSimdAlign) {
167+
cgf.getCIRGenModule().errorNYI(
168+
e->getSourceRange(),
169+
"sizeof operator for Not yet implemented: ", e->getStmtClassName());
170+
}
171+
172+
return builder.create<cir::ConstantOp>(
173+
cgf.getLoc(e->getSourceRange()), cgf.cgm.UInt64Ty,
174+
builder.getAttr<cir::IntAttr>(
175+
cgf.cgm.UInt64Ty, e->EvaluateKnownConstInt(cgf.getContext())));
176+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - 2>&1 | filecheck %s
2+
3+
void foo() {
4+
unsigned long b = sizeof(bool);
5+
// CHECK: cir.const #cir.int<1> : !cir.int<u, 64>
6+
7+
unsigned long i = sizeof(int);
8+
// CHECK: cir.const #cir.int<4> : !cir.int<u, 64>
9+
10+
unsigned long l = sizeof(long);
11+
// CHECK: cir.const #cir.int<8> : !cir.int<u, 64>
12+
13+
unsigned long f = sizeof(float);
14+
// CHECK: cir.const #cir.int<4> : !cir.int<u, 64>
15+
16+
unsigned long d = sizeof(double);
17+
// CHECK: cir.const #cir.int<8> : !cir.int<u, 64>
18+
}
19+
20+
void foo2() {
21+
unsigned long b = alignof(bool);
22+
// CHECK: cir.const #cir.int<1> : !cir.int<u, 64>
23+
24+
unsigned long i = alignof(int);
25+
// CHECK: cir.const #cir.int<4> : !cir.int<u, 64>
26+
27+
unsigned long l = alignof(long);
28+
// CHECK: cir.const #cir.int<8> : !cir.int<u, 64>
29+
30+
unsigned long f = alignof(float);
31+
// CHECK: cir.const #cir.int<4> : !cir.int<u, 64>
32+
33+
unsigned long d = alignof(double);
34+
// CHECK: cir.const #cir.int<8> : !cir.int<u, 64>
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - 2>&1 | FileCheck %s
2+
3+
void foo() {
4+
unsigned long b = sizeof(bool);
5+
// CHECK: store i64 1, ptr {{%.*}}, align 4
6+
7+
unsigned long i = sizeof(int);
8+
// CHECK: store i64 4, ptr {{%.*}}, align 4
9+
10+
unsigned long l = sizeof(long);
11+
// CHECK: store i64 8, ptr {{%.*}}, align 4
12+
13+
unsigned long f = sizeof(float);
14+
// CHECK: store i64 4, ptr {{%.*}}, align 4
15+
16+
unsigned long d = sizeof(double);
17+
// CHECK: store i64 8, ptr {{%.*}}, align 4
18+
}
19+
20+
void foo2() {
21+
unsigned long b = alignof(bool);
22+
// CHECK: store i64 1, ptr {{%.*}}, align 4
23+
24+
unsigned long i = alignof(int);
25+
// CHECK: store i64 4, ptr {{%.*}}, align 4
26+
27+
unsigned long l = alignof(long);
28+
// CHECK: store i64 8, ptr {{%.*}}, align 4
29+
30+
unsigned long f = alignof(float);
31+
// CHECK: store i64 4, ptr {{%.*}}, align 4
32+
33+
unsigned long d = alignof(double);
34+
// CHECK: store i64 8, ptr {{%.*}}, align 4
35+
}

0 commit comments

Comments
 (0)