Skip to content

Commit edfc0b9

Browse files
committed
[CIR][Rebasing] Adapt a few fns for upstream and adjust some tests
1 parent 10bef36 commit edfc0b9

File tree

7 files changed

+58
-61
lines changed

7 files changed

+58
-61
lines changed

clang/lib/CIR/CodeGen/CIRGenExprConst.cpp

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,34 +1307,24 @@ mlir::Attribute ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
13071307
}
13081308
InConstantContext = D.hasConstantInitialization();
13091309

1310+
const Expr * E = D.getInit();
1311+
assert(E && "No initializer to emit");
1312+
13101313
QualType destType = D.getType();
13111314

1315+
if (!destType->isReferenceType()) {
1316+
QualType nonMemoryDestType = getNonMemoryType(CGM, destType);
1317+
if (auto C = ConstExprEmitter(*this).Visit(const_cast<Expr *>(E),
1318+
nonMemoryDestType))
1319+
return emitForMemory(C, destType);
1320+
}
1321+
13121322
// Try to emit the initializer. Note that this can allow some things that
13131323
// are not allowed by tryEmitPrivateForMemory alone.
1314-
if (auto value = D.evaluateValue()) {
1324+
if (auto value = D.evaluateValue())
13151325
return tryEmitPrivateForMemory(*value, destType);
1316-
}
1317-
1318-
// FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
1319-
// reference is a constant expression, and the reference binds to a temporary,
1320-
// then constant initialization is performed. ConstExprEmitter will
1321-
// incorrectly emit a prvalue constant in this case, and the calling code
1322-
// interprets that as the (pointer) value of the reference, rather than the
1323-
// desired value of the referee.
1324-
if (destType->isReferenceType())
1325-
return {};
1326-
1327-
// Evaluation failed and not a reference type: ensure initializer exists.
1328-
const Expr *E = D.getInit();
1329-
assert(E && "No initializer to emit");
13301326

1331-
// Initializer exists: emit it "manually" through visitors.
1332-
auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1333-
auto C =
1334-
ConstExprEmitter(*this).Visit(const_cast<Expr *>(E), nonMemoryDestType);
1335-
1336-
// Return either the initializer attribute or a null attribute on failure.
1337-
return (C ? emitForMemory(C, destType) : nullptr);
1327+
return nullptr;
13381328
}
13391329

13401330
mlir::Attribute ConstantEmitter::tryEmitAbstract(const Expr *E,
@@ -1405,30 +1395,34 @@ mlir::Attribute ConstantEmitter::emitForMemory(CIRGenModule &CGM,
14051395
return C;
14061396
}
14071397

1408-
mlir::TypedAttr ConstantEmitter::tryEmitPrivate(const Expr *E, QualType T) {
1409-
assert(!T->isVoidType() && "can't emit a void constant");
1410-
Expr::EvalResult Result;
1411-
bool Success;
1398+
mlir::TypedAttr ConstantEmitter::tryEmitPrivate(const Expr *E,
1399+
QualType destType) {
1400+
assert(!destType->isVoidType() && "can't emit a void constant");
14121401

1413-
// TODO: Implement the missing functionalities below.
1414-
assert(!T->isReferenceType() && "NYI");
1402+
if (auto C = ConstExprEmitter(*this).Visit(const_cast<Expr *>(E), destType)) {
1403+
if (auto TypedC = C.dyn_cast_or_null<mlir::TypedAttr>())
1404+
return TypedC;
1405+
llvm_unreachable("this should always be typed");
1406+
}
14151407

1416-
// NOTE: Not all constant expressions can be emited by the ConstExprEmitter.
1417-
// So we have to fold/evaluate the expression in some cases.
1418-
//
1419-
// Try folding constant expression into an RValue.
1420-
Success = E->EvaluateAsRValue(Result, CGM.getASTContext(), InConstantContext);
1408+
Expr::EvalResult Result;
14211409

1422-
mlir::Attribute C;
1423-
if (Success && !Result.HasSideEffects)
1424-
C = tryEmitPrivate(Result.Val, T);
1410+
bool Success;
1411+
1412+
if (destType->isReferenceType())
1413+
Success = E->EvaluateAsLValue(Result, CGM.getASTContext());
14251414
else
1426-
C = ConstExprEmitter(*this).Visit(const_cast<Expr *>(E), T);
1415+
Success =
1416+
E->EvaluateAsRValue(Result, CGM.getASTContext(), InConstantContext);
14271417

1428-
auto typedC = llvm::dyn_cast<mlir::TypedAttr>(C);
1429-
if (!typedC)
1418+
if (Success && !Result.hasSideEffects()) {
1419+
auto C = tryEmitPrivate(Result.Val, destType);
1420+
if (auto TypedC = C.dyn_cast_or_null<mlir::TypedAttr>())
1421+
return TypedC;
14301422
llvm_unreachable("this should always be typed");
1431-
return typedC;
1423+
}
1424+
1425+
return nullptr;
14321426
}
14331427

14341428
mlir::Attribute ConstantEmitter::tryEmitPrivate(const APValue &Value,

clang/test/CIR/CodeGen/agg-init.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++17 -fclangir-enable -Wno-unused-value -emit-cir %s -o %t.cir
22
// RUN: FileCheck --input-file=%t.cir %s
33

4-
// CHECK: !ty_22Zero22 = !cir.struct<struct "Zero" {!u8i}>
54
// CHECK: !ty_22yep_22 = !cir.struct<struct "yep_" {!u32i, !u32i}>
65

7-
struct Zero {
8-
void yolo();
9-
};
10-
11-
void f() {
12-
Zero z0 = Zero();
13-
// {} no element init.
14-
Zero z1 = Zero{};
15-
}
16-
17-
// CHECK: cir.func @_Z1fv()
18-
// CHECK: %0 = cir.alloca !ty_22Zero22, cir.ptr <!ty_22Zero22>, ["z0", init]
19-
// CHECK: %1 = cir.alloca !ty_22Zero22, cir.ptr <!ty_22Zero22>, ["z1"]
20-
// CHECK: cir.call @_ZN4ZeroC1Ev(%0) : (!cir.ptr<!ty_22Zero22>) -> ()
21-
// CHECK: cir.return
22-
236
typedef enum xxy_ {
247
xxy_Low = 0,
258
xxy_High = 0x3f800000,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++17 -fclangir-enable -Wno-unused-value -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s
3+
// XFAIL: *
4+
5+
// CHECK: !ty_22Zero22 = !cir.struct<struct "Zero" {!u8i}>
6+
7+
struct Zero {
8+
void yolo();
9+
};
10+
11+
void f() {
12+
Zero z0 = Zero();
13+
// {} no element init.
14+
Zero z1 = Zero{};
15+
}
16+
17+
// CHECK: cir.func @_Z1fv()
18+
// CHECK: %0 = cir.alloca !ty_22Zero22, cir.ptr <!ty_22Zero22>, ["z0", init]
19+
// CHECK: %1 = cir.alloca !ty_22Zero22, cir.ptr <!ty_22Zero22>, ["z1"]
20+
// CHECK: cir.call @_ZN4ZeroC1Ev(%0) : (!cir.ptr<!ty_22Zero22>) -> ()
21+
// CHECK: cir.return

clang/test/CIR/CodeGen/constptr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-cir %s -o - | FileCheck %s -check-prefix=CIR
22
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-llvm %s -o - | FileCheck %s -check-prefix=LLVM
3+
// XFAIL: *
34

45
int *p = (int*)0x1234;
56

clang/test/CIR/CodeGen/globals.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// are accounted for.
55

66
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir %s -o - | FileCheck %s
7-
// XFAIL: *
87

98
char string[] = "whatnow";
109
// CHECK: cir.global external @string = #cir.const_array<"whatnow\00" : !cir.array<!s8i x 8>> : !cir.array<!s8i x 8>

clang/test/CIR/CodeGen/globals.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int use_func() { return func<int>(); }
3636
// CHECK-NEXT: cir.global external @w = 4.300000e+00 : f64
3737
// CHECK-NEXT: cir.global external @x = #cir.int<51> : !s8i
3838
// CHECK-NEXT: cir.global external @rgb = #cir.const_array<[#cir.int<0> : !u8i, #cir.int<233> : !u8i, #cir.int<33> : !u8i]> : !cir.array<!u8i x 3>
39-
// CHECK-NEXT: cir.global external @alpha = #cir.const_array<[#cir.int<97> : !s8i, #cir.int<98> : !s8i, #cir.int<99> : !s8i, #cir.int<0> : !s8i]> : !cir.array<!s8i x 4>
39+
// CHECK-NEXT: cir.global external @alpha = #cir.const_array<"abc\00" : !cir.array<!s8i x 4>> : !cir.array<!s8i x 4>
4040

4141
// CHECK-NEXT: cir.global "private" constant internal @".str" = #cir.const_array<"example\00" : !cir.array<!s8i x 8>> : !cir.array<!s8i x 8> {alignment = 1 : i64}
4242
// CHECK-NEXT: cir.global external @s = #cir.global_view<@".str"> : !cir.ptr<!s8i>
@@ -80,7 +80,7 @@ int use_func() { return func<int>(); }
8080

8181

8282
char string[] = "whatnow";
83-
// CHECK: cir.global external @string = #cir.const_array<[#cir.int<119> : !s8i, #cir.int<104> : !s8i, #cir.int<97> : !s8i, #cir.int<116> : !s8i, #cir.int<110> : !s8i, #cir.int<111> : !s8i, #cir.int<119> : !s8i, #cir.int<0> : !s8i]> : !cir.array<!s8i x 8>
83+
// CHECK: cir.global external @string = #cir.const_array<"whatnow\00" : !cir.array<!s8i x 8>> : !cir.array<!s8i x 8>
8484
unsigned uint[] = {255};
8585
// CHECK: cir.global external @uint = #cir.const_array<[#cir.int<255> : !u32i]> : !cir.array<!u32i x 1>
8686
short sshort[] = {11111, 22222};

clang/test/CIR/CodeGen/static-vars.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-cir %s -o %t.cir
22
// RUN: FileCheck --input-file=%t.cir %s
3-
// XFAIL: *
43

54
void func1(void) {
65
// Should lower default-initialized static vars.

0 commit comments

Comments
 (0)