Skip to content

Commit 2a3c9f9

Browse files
authored
[CIR] Upstream VisitOpaqueValueExpr support for Complex & Scalar (#157331)
This change adds support for the OpaqueValueExpr for Complex & Scalar Issue: #141365
1 parent 1dc4db8 commit 2a3c9f9

File tree

5 files changed

+215
-4
lines changed

5 files changed

+215
-4
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,30 @@ LValue CIRGenFunction::emitMaterializeTemporaryExpr(
13761376
return makeAddrLValue(object, m->getType(), AlignmentSource::Decl);
13771377
}
13781378

1379+
LValue
1380+
CIRGenFunction::getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e) {
1381+
assert(OpaqueValueMapping::shouldBindAsLValue(e));
1382+
1383+
auto it = opaqueLValues.find(e);
1384+
if (it != opaqueLValues.end())
1385+
return it->second;
1386+
1387+
assert(e->isUnique() && "LValue for a nonunique OVE hasn't been emitted");
1388+
return emitLValue(e->getSourceExpr());
1389+
}
1390+
1391+
RValue
1392+
CIRGenFunction::getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e) {
1393+
assert(!OpaqueValueMapping::shouldBindAsLValue(e));
1394+
1395+
auto it = opaqueRValues.find(e);
1396+
if (it != opaqueRValues.end())
1397+
return it->second;
1398+
1399+
assert(e->isUnique() && "RValue for a nonunique OVE hasn't been emitted");
1400+
return emitAnyExpr(e->getSourceExpr());
1401+
}
1402+
13791403
LValue CIRGenFunction::emitCompoundLiteralLValue(const CompoundLiteralExpr *e) {
13801404
if (e->isFileScope()) {
13811405
cgm.errorNYI(e->getSourceRange(), "emitCompoundLiteralLValue: FileScope");

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,12 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
128128
return emitLoadOfLValue(me);
129129
}
130130
mlir::Value VisitOpaqueValueExpr(OpaqueValueExpr *e) {
131-
cgf.cgm.errorNYI(e->getExprLoc(),
132-
"ComplexExprEmitter VisitOpaqueValueExpr");
133-
return {};
131+
if (e->isGLValue())
132+
return emitLoadOfLValue(cgf.getOrCreateOpaqueLValueMapping(e),
133+
e->getExprLoc());
134+
135+
// Otherwise, assume the mapping is the scalar directly.
136+
return cgf.getOrCreateOpaqueRValueMapping(e).getValue();
134137
}
135138

136139
mlir::Value VisitPseudoObjectExpr(PseudoObjectExpr *e) {
@@ -960,21 +963,32 @@ mlir::Value ComplexExprEmitter::VisitBinComma(const BinaryOperator *e) {
960963

961964
mlir::Value ComplexExprEmitter::VisitAbstractConditionalOperator(
962965
const AbstractConditionalOperator *e) {
963-
mlir::Value condValue = Visit(e->getCond());
964966
mlir::Location loc = cgf.getLoc(e->getSourceRange());
965967

968+
// Bind the common expression if necessary.
969+
CIRGenFunction::OpaqueValueMapping binding(cgf, e);
970+
971+
CIRGenFunction::ConditionalEvaluation eval(cgf);
972+
973+
Expr *cond = e->getCond()->IgnoreParens();
974+
mlir::Value condValue = cgf.evaluateExprAsBool(cond);
975+
966976
return builder
967977
.create<cir::TernaryOp>(
968978
loc, condValue,
969979
/*thenBuilder=*/
970980
[&](mlir::OpBuilder &b, mlir::Location loc) {
981+
eval.beginEvaluation();
971982
mlir::Value trueValue = Visit(e->getTrueExpr());
972983
b.create<cir::YieldOp>(loc, trueValue);
984+
eval.endEvaluation();
973985
},
974986
/*elseBuilder=*/
975987
[&](mlir::OpBuilder &b, mlir::Location loc) {
988+
eval.beginEvaluation();
976989
mlir::Value falseValue = Visit(e->getFalseExpr());
977990
b.create<cir::YieldOp>(loc, falseValue);
991+
eval.endEvaluation();
978992
})
979993
.getResult();
980994
}

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
193193
return emitNullValue(e->getType(), cgf.getLoc(e->getSourceRange()));
194194
}
195195

196+
mlir::Value VisitOpaqueValueExpr(OpaqueValueExpr *e) {
197+
if (e->isGLValue())
198+
return emitLoadOfLValue(cgf.getOrCreateOpaqueLValueMapping(e),
199+
e->getExprLoc());
200+
201+
// Otherwise, assume the mapping is the scalar directly.
202+
return cgf.getOrCreateOpaqueRValueMapping(e).getValue();
203+
}
204+
196205
mlir::Value VisitCastExpr(CastExpr *e);
197206
mlir::Value VisitCallExpr(const CallExpr *e);
198207

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,14 @@ class CIRGenFunction : public CIRGenTypeCache {
706706
Address getAddrOfBitFieldStorage(LValue base, const clang::FieldDecl *field,
707707
mlir::Type fieldType, unsigned index);
708708

709+
/// Given an opaque value expression, return its LValue mapping if it exists,
710+
/// otherwise create one.
711+
LValue getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e);
712+
713+
/// Given an opaque value expression, return its RValue mapping if it exists,
714+
/// otherwise create one.
715+
RValue getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e);
716+
709717
/// Load the value for 'this'. This function is only valid while generating
710718
/// code for an C++ member function.
711719
/// FIXME(cir): this should return a mlir::Value!

clang/test/CIR/CodeGen/opaque.cpp

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
5+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
7+
8+
void foo() {
9+
int a;
10+
int b = 1 ?: a;
11+
}
12+
13+
// CIR: %[[A_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a"]
14+
// CIR: %[[B_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
15+
// CIR: %[[CONST_1:.*]] = cir.const #cir.int<1> : !s32i
16+
// CIR: cir.store{{.*}} %[[CONST_1]], %[[B_ADDR]] : !s32i, !cir.ptr<!s32i>
17+
18+
// LLVM: %[[A_ADDR:.*]] = alloca i32, i64 1, align 4
19+
// LLVM: %[[B_ADDR:.*]] = alloca i32, i64 1, align 4
20+
// LLVM: store i32 1, ptr %[[B_ADDR]], align 4
21+
22+
// OGCG: %[[A_ADDR:.*]] = alloca i32, align 4
23+
// OGCG: %[[B_ADDR:.*]] = alloca i32, align 4
24+
// OGCG: store i32 1, ptr %[[B_ADDR]], align 4
25+
26+
void foo2() {
27+
float _Complex a;
28+
float _Complex b;
29+
float _Complex c = a ?: b;
30+
}
31+
32+
// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["a"]
33+
// CIR: %[[B_ADDR:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["b"]
34+
// CIR: %[[C_ADDR:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["c", init]
35+
// CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
36+
// CIR: %[[A_REAL:.*]] = cir.complex.real %[[TMP_A]] : !cir.complex<!cir.float> -> !cir.float
37+
// CIR: %[[A_IMAG:.*]] = cir.complex.imag %[[TMP_A]] : !cir.complex<!cir.float> -> !cir.float
38+
// CIR: %[[A_REAL_BOOL:.*]] = cir.cast(float_to_bool, %[[A_REAL]] : !cir.float), !cir.bool
39+
// CIR: %[[A_IMAG_BOOL:.*]] = cir.cast(float_to_bool, %[[A_IMAG]] : !cir.float), !cir.bool
40+
// CIR: %[[CONST_TRUE:.*]] = cir.const #true
41+
// CIR: %[[COND:.*]] = cir.select if %[[A_REAL_BOOL]] then %[[CONST_TRUE]] else %[[A_IMAG_BOOL]] : (!cir.bool, !cir.bool, !cir.bool) -> !cir.bool
42+
// CIR: %[[RESULT:.*]] = cir.ternary(%[[COND]], true {
43+
// CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
44+
// CIR: cir.yield %[[TMP_A]] : !cir.complex<!cir.float>
45+
// CIR: }, false {
46+
// CIR: %[[TMP_B:.*]] = cir.load{{.*}} %[[B_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
47+
// CIR: cir.yield %[[TMP_B]] : !cir.complex<!cir.float>
48+
// CIR: }) : (!cir.bool) -> !cir.complex<!cir.float>
49+
// CIR: cir.store{{.*}} %[[RESULT]], %[[C_ADDR]] : !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>
50+
51+
// LLVM: %[[A_ADDR:.*]] = alloca { float, float }, i64 1, align 4
52+
// LLVM: %[[B_ADDR:.*]] = alloca { float, float }, i64 1, align 4
53+
// LLVM: %[[C_ADDR:.*]] = alloca { float, float }, i64 1, align 4
54+
// LLVM: %[[TMP_A:.*]] = load { float, float }, ptr %[[A_ADDR]], align 4
55+
// LLVM: %[[A_REAL:.*]] = extractvalue { float, float } %[[TMP_A]], 0
56+
// LLVM: %[[A_IMAG:.*]] = extractvalue { float, float } %[[TMP_A]], 1
57+
// LLVM: %[[A_REAL_BOOL:.*]] = fcmp une float %[[A_REAL]], 0.000000e+00
58+
// LLVM: %[[A_IMAG_BOOL:.*]] = fcmp une float %[[A_IMAG]], 0.000000e+00
59+
// LLVM: %[[COND:.*]] = or i1 %[[A_REAL_BOOL]], %[[A_IMAG_BOOL]]
60+
// LLVM: br i1 %[[COND]], label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
61+
// LLVM: [[COND_TRUE]]:
62+
// LLVM: %[[TMP_A:.*]] = load { float, float }, ptr %[[A_ADDR]], align 4
63+
// LLVM: br label %[[COND_RESULT:.*]]
64+
// LLVM: [[COND_FALSE]]:
65+
// LLVM: %[[TMP_B:.*]] = load { float, float }, ptr %[[B_ADDR]], align 4
66+
// LLVM: br label %[[COND_RESULT]]
67+
// LLVM: [[COND_RESULT]]:
68+
// LLVM: %[[RESULT:.*]] = phi { float, float } [ %[[TMP_B]], %[[COND_FALSE]] ], [ %[[TMP_A]], %[[COND_TRUE]] ]
69+
// LLVM: br label %[[COND_END:.*]]
70+
// LLVM: [[COND_END]]:
71+
// LLVM: store { float, float } %[[RESULT]], ptr %[[C_ADDR]], align 4
72+
73+
// OGCG: %[[A_ADDR:.*]] = alloca { float, float }, align 4
74+
// OGCG: %[[B_ADDR:.*]] = alloca { float, float }, align 4
75+
// OGCG: %[[C_ADDR:.*]] = alloca { float, float }, align 4
76+
// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 0
77+
// OGCG: %[[A_REAL:.*]] = load float, ptr %[[A_REAL_PTR]], align 4
78+
// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
79+
// OGCG: %[[A_IMAG:.*]] = load float, ptr %[[A_IMAG_PTR]], align 4
80+
// OGCG: %[[A_REAL_BOOL:.*]] = fcmp une float %[[A_REAL]], 0.000000e+00
81+
// OGCG: %[[A_IMAG_BOOL:.*]] = fcmp une float %[[A_IMAG]], 0.000000e+00
82+
// OGCG: %[[COND:.*]] = or i1 %[[A_REAL_BOOL]], %[[A_IMAG_BOOL]]
83+
// OGCG: br i1 %tobool2, label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
84+
// OGCG: [[COND_TRUE]]:
85+
// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 0
86+
// OGCG: %[[A_REAL:.*]] = load float, ptr %[[A_REAL_PTR]], align 4
87+
// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
88+
// OGCG: %[[A_IMAG:.*]] = load float, ptr %[[A_IMAG_PTR]], align 4
89+
// OGCG: br label %[[COND_END:.*]]
90+
// OGCG: [[COND_FALSE]]:
91+
// OGCG: %[[B_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[B_ADDR]], i32 0, i32 0
92+
// OGCG: %[[B_REAL:.*]] = load float, ptr %[[B_REAL_PTR]], align 4
93+
// OGCG: %[[B_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[B_ADDR]], i32 0, i32 1
94+
// OGCG: %[[B_IMAG:.*]] = load float, ptr %[[B_IMAG_PTR]], align 4
95+
// OGCG: br label %[[COND_END]]
96+
// OGCG: [[COND_END]]:
97+
// OGCG: %[[RESULT_REAL:.*]] = phi float [ %[[A_REAL]], %[[COND_TRUE]] ], [ %[[B_REAL]], %[[COND_FALSE]] ]
98+
// OGCG: %[[RESULT_IMAG:.*]] = phi float [ %[[A_IMAG]], %[[COND_TRUE]] ], [ %[[B_IMAG]], %[[COND_FALSE]] ]
99+
// OGCG: %[[C_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[C_ADDR]], i32 0, i32 0
100+
// OGCG: %[[C_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[C_ADDR]], i32 0, i32 1
101+
// OGCG: store float %[[RESULT_REAL]], ptr %[[C_REAL_PTR]], align 4
102+
// OGCG: store float %[[RESULT_IMAG]], ptr %[[C_IMAG_PTR]], align 4
103+
104+
void foo3() {
105+
int a;
106+
int b;
107+
int c = a ?: b;
108+
}
109+
110+
// CIR: %[[A_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a"]
111+
// CIR: %[[B_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b"]
112+
// CIR: %[[C_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["c", init]
113+
// CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!s32i>, !s32i
114+
// CIR: %[[A_BOOL:.*]] = cir.cast(int_to_bool, %[[TMP_A]] : !s32i), !cir.bool
115+
// CIR: %[[RESULT:.*]] = cir.ternary(%[[A_BOOL]], true {
116+
// CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!s32i>, !s32i
117+
// CIR: cir.yield %[[TMP_A]] : !s32i
118+
// CIR: }, false {
119+
// CIR: %[[TMP_B:.*]] = cir.load{{.*}} %[[B_ADDR]] : !cir.ptr<!s32i>, !s32i
120+
// CIR: cir.yield %[[TMP_B]] : !s32i
121+
// CIR: }) : (!cir.bool) -> !s32i
122+
// CIR: cir.store{{.*}} %[[RESULT]], %[[C_ADDR]] : !s32i, !cir.ptr<!s32i>
123+
124+
// LLVM: %[[A_ADDR:.*]] = alloca i32, i64 1, align 4
125+
// LLVM: %[[B_ADDR:.*]] = alloca i32, i64 1, align 4
126+
// LLVM: %[[C_ADDR:.*]] = alloca i32, i64 1, align 4
127+
// LLVM: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4
128+
// LLVM: %[[COND:.*]] = icmp ne i32 %[[TMP_A]], 0
129+
// LLVM: br i1 %[[COND]], label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
130+
// LLVM: [[COND_TRUE]]:
131+
// LLVM: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4
132+
// LLVM: br label %[[COND_RESULT:.*]]
133+
// LLVM: [[COND_FALSE]]:
134+
// LLVM: %[[TMP_B:.*]] = load i32, ptr %[[B_ADDR]], align 4
135+
// LLVM: br label %[[COND_RESULT]]
136+
// LLVM: [[COND_RESULT]]:
137+
// LLVM: %[[RESULT:.*]] = phi i32 [ %[[TMP_B]], %[[COND_FALSE]] ], [ %[[TMP_A]], %[[COND_TRUE]] ]
138+
// LLVM: br label %[[COND_END:.*]]
139+
// LLVM: [[COND_END]]:
140+
// LLVM: store i32 %[[RESULT]], ptr %[[C_ADDR]], align 4
141+
142+
// OGCG: %[[A_ADDR:.*]] = alloca i32, align 4
143+
// OGCG: %[[B_ADDR:.*]] = alloca i32, align 4
144+
// OGCG: %[[C_ADDR:.*]] = alloca i32, align 4
145+
// OGCG: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4
146+
// OGCG: %[[A_BOOL:.*]] = icmp ne i32 %[[TMP_A]], 0
147+
// OGCG: br i1 %[[A_BOOL]], label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
148+
// OGCG: [[COND_TRUE]]:
149+
// OGCG: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4
150+
// OGCG: br label %[[COND_END:.*]]
151+
// OGCG: [[COND_FALSE]]:
152+
// OGCG: %[[TMP_B:.*]] = load i32, ptr %[[B_ADDR]], align 4
153+
// OGCG: br label %[[COND_END]]
154+
// OGCG: [[COND_END]]:
155+
// OGCG: %[[RESULT:.*]] = phi i32 [ %[[TMP_A]], %[[COND_TRUE]] ], [ %[[TMP_B]], %[[COND_FALSE]] ]
156+
// OGCG: store i32 %[[RESULT]], ptr %[[C_ADDR]], align 4

0 commit comments

Comments
 (0)