Skip to content

Commit fe5977d

Browse files
authored
[CIR] Backport VisitOpaqueValueExpr support for Complex (#1902)
Backport fixes in VisitAbstractConditionalOperator to handle OpaqueValueExpr from the upstream llvm/llvm-project#157331
1 parent 46de857 commit fe5977d

File tree

2 files changed

+164
-1
lines changed

2 files changed

+164
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,21 +332,32 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
332332

333333
mlir::Value
334334
VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
335-
mlir::Value condValue = Visit(E->getCond());
336335
mlir::Location loc = CGF.getLoc(E->getSourceRange());
337336

337+
// Bind the common expression if necessary.
338+
CIRGenFunction::OpaqueValueMapping binding(CGF, E);
339+
340+
CIRGenFunction::ConditionalEvaluation eval(CGF);
341+
342+
Expr *cond = E->getCond()->IgnoreParens();
343+
mlir::Value condValue = CGF.evaluateExprAsBool(cond);
344+
338345
return Builder
339346
.create<cir::TernaryOp>(
340347
loc, condValue,
341348
/*thenBuilder=*/
342349
[&](mlir::OpBuilder &b, mlir::Location loc) {
350+
eval.begin(CGF);
343351
mlir::Value trueValue = Visit(E->getTrueExpr());
344352
b.create<cir::YieldOp>(loc, trueValue);
353+
eval.end(CGF);
345354
},
346355
/*elseBuilder=*/
347356
[&](mlir::OpBuilder &b, mlir::Location loc) {
357+
eval.begin(CGF);
348358
mlir::Value falseValue = Visit(E->getFalseExpr());
349359
b.create<cir::YieldOp>(loc, falseValue);
360+
eval.end(CGF);
350361
})
351362
.getResult();
352363
}

clang/test/CIR/CodeGen/opaque.cpp

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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: store { float, float } %[[RESULT]], ptr %[[C_ADDR]], align 4
70+
71+
// OGCG: %[[A_ADDR:.*]] = alloca { float, float }, align 4
72+
// OGCG: %[[B_ADDR:.*]] = alloca { float, float }, align 4
73+
// OGCG: %[[C_ADDR:.*]] = alloca { float, float }, align 4
74+
// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 0
75+
// OGCG: %[[A_REAL:.*]] = load float, ptr %[[A_REAL_PTR]], align 4
76+
// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
77+
// OGCG: %[[A_IMAG:.*]] = load float, ptr %[[A_IMAG_PTR]], align 4
78+
// OGCG: %[[A_REAL_BOOL:.*]] = fcmp une float %[[A_REAL]], 0.000000e+00
79+
// OGCG: %[[A_IMAG_BOOL:.*]] = fcmp une float %[[A_IMAG]], 0.000000e+00
80+
// OGCG: %[[COND:.*]] = or i1 %[[A_REAL_BOOL]], %[[A_IMAG_BOOL]]
81+
// OGCG: br i1 %tobool2, label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
82+
// OGCG: [[COND_TRUE]]:
83+
// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 0
84+
// OGCG: %[[A_REAL:.*]] = load float, ptr %[[A_REAL_PTR]], align 4
85+
// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
86+
// OGCG: %[[A_IMAG:.*]] = load float, ptr %[[A_IMAG_PTR]], align 4
87+
// OGCG: br label %[[COND_END:.*]]
88+
// OGCG: [[COND_FALSE]]:
89+
// OGCG: %[[B_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[B_ADDR]], i32 0, i32 0
90+
// OGCG: %[[B_REAL:.*]] = load float, ptr %[[B_REAL_PTR]], align 4
91+
// OGCG: %[[B_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[B_ADDR]], i32 0, i32 1
92+
// OGCG: %[[B_IMAG:.*]] = load float, ptr %[[B_IMAG_PTR]], align 4
93+
// OGCG: br label %[[COND_END]]
94+
// OGCG: [[COND_END]]:
95+
// OGCG: %[[RESULT_REAL:.*]] = phi float [ %[[A_REAL]], %[[COND_TRUE]] ], [ %[[B_REAL]], %[[COND_FALSE]] ]
96+
// OGCG: %[[RESULT_IMAG:.*]] = phi float [ %[[A_IMAG]], %[[COND_TRUE]] ], [ %[[B_IMAG]], %[[COND_FALSE]] ]
97+
// OGCG: %[[C_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[C_ADDR]], i32 0, i32 0
98+
// OGCG: %[[C_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[C_ADDR]], i32 0, i32 1
99+
// OGCG: store float %[[RESULT_REAL]], ptr %[[C_REAL_PTR]], align 4
100+
// OGCG: store float %[[RESULT_IMAG]], ptr %[[C_IMAG_PTR]], align 4
101+
102+
void foo3() {
103+
int a;
104+
int b;
105+
int c = a ?: b;
106+
}
107+
108+
// CIR: %[[A_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a"]
109+
// CIR: %[[B_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b"]
110+
// CIR: %[[C_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["c", init]
111+
// CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!s32i>, !s32i
112+
// CIR: %[[A_BOOL:.*]] = cir.cast(int_to_bool, %[[TMP_A]] : !s32i), !cir.bool
113+
// CIR: %[[RESULT:.*]] = cir.ternary(%[[A_BOOL]], true {
114+
// CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!s32i>, !s32i
115+
// CIR: cir.yield %[[TMP_A]] : !s32i
116+
// CIR: }, false {
117+
// CIR: %[[TMP_B:.*]] = cir.load{{.*}} %[[B_ADDR]] : !cir.ptr<!s32i>, !s32i
118+
// CIR: cir.yield %[[TMP_B]] : !s32i
119+
// CIR: }) : (!cir.bool) -> !s32i
120+
// CIR: cir.store{{.*}} %[[RESULT]], %[[C_ADDR]] : !s32i, !cir.ptr<!s32i>
121+
122+
// LLVM: %[[A_ADDR:.*]] = alloca i32, i64 1, align 4
123+
// LLVM: %[[B_ADDR:.*]] = alloca i32, i64 1, align 4
124+
// LLVM: %[[C_ADDR:.*]] = alloca i32, i64 1, align 4
125+
// LLVM: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4
126+
// LLVM: %[[COND:.*]] = icmp ne i32 %[[TMP_A]], 0
127+
// LLVM: br i1 %[[COND]], label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
128+
// LLVM: [[COND_TRUE]]:
129+
// LLVM: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4
130+
// LLVM: br label %[[COND_RESULT:.*]]
131+
// LLVM: [[COND_FALSE]]:
132+
// LLVM: %[[TMP_B:.*]] = load i32, ptr %[[B_ADDR]], align 4
133+
// LLVM: br label %[[COND_RESULT]]
134+
// LLVM: [[COND_RESULT]]:
135+
// LLVM: %[[RESULT:.*]] = phi i32 [ %[[TMP_B]], %[[COND_FALSE]] ], [ %[[TMP_A]], %[[COND_TRUE]] ]
136+
// LLVM: store i32 %[[RESULT]], ptr %[[C_ADDR]], align 4
137+
138+
// OGCG: %[[A_ADDR:.*]] = alloca i32, align 4
139+
// OGCG: %[[B_ADDR:.*]] = alloca i32, align 4
140+
// OGCG: %[[C_ADDR:.*]] = alloca i32, align 4
141+
// OGCG: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4
142+
// OGCG: %[[A_BOOL:.*]] = icmp ne i32 %[[TMP_A]], 0
143+
// OGCG: br i1 %[[A_BOOL]], label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
144+
// OGCG: [[COND_TRUE]]:
145+
// OGCG: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4
146+
// OGCG: br label %[[COND_END:.*]]
147+
// OGCG: [[COND_FALSE]]:
148+
// OGCG: %[[TMP_B:.*]] = load i32, ptr %[[B_ADDR]], align 4
149+
// OGCG: br label %[[COND_END]]
150+
// OGCG: [[COND_END]]:
151+
// OGCG: %[[RESULT:.*]] = phi i32 [ %[[TMP_A]], %[[COND_TRUE]] ], [ %[[TMP_B]], %[[COND_FALSE]] ]
152+
// OGCG: store i32 %[[RESULT]], ptr %[[C_ADDR]], align 4

0 commit comments

Comments
 (0)