Skip to content

Commit a5a0d99

Browse files
committed
Fix VisitAbstractConditionalOperator for ComplexType
1 parent 31d11cd commit a5a0d99

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,21 +963,32 @@ mlir::Value ComplexExprEmitter::VisitBinComma(const BinaryOperator *e) {
963963

964964
mlir::Value ComplexExprEmitter::VisitAbstractConditionalOperator(
965965
const AbstractConditionalOperator *e) {
966-
mlir::Value condValue = Visit(e->getCond());
967966
mlir::Location loc = cgf.getLoc(e->getSourceRange());
968967

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+
969976
return builder
970977
.create<cir::TernaryOp>(
971978
loc, condValue,
972979
/*thenBuilder=*/
973980
[&](mlir::OpBuilder &b, mlir::Location loc) {
981+
eval.beginEvaluation();
974982
mlir::Value trueValue = Visit(e->getTrueExpr());
975983
b.create<cir::YieldOp>(loc, trueValue);
984+
eval.endEvaluation();
976985
},
977986
/*elseBuilder=*/
978987
[&](mlir::OpBuilder &b, mlir::Location loc) {
988+
eval.beginEvaluation();
979989
mlir::Value falseValue = Visit(e->getFalseExpr());
980990
b.create<cir::YieldOp>(loc, falseValue);
991+
eval.endEvaluation();
981992
})
982993
.getResult();
983994
}

clang/test/CIR/CodeGen/opaque.cpp

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,81 @@ void foo() {
2525

2626
void foo2() {
2727
float _Complex a;
28-
float b = 1.0f ?: __real__ a;
28+
float _Complex b;
29+
float _Complex c = a ?: b;
2930
}
3031

3132
// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["a"]
32-
// CIR: %[[B_ADDR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["b", init]
33-
// CIR: %[[CONST_1:.*]] = cir.const #cir.fp<1.000000e+00> : !cir.float
34-
// CIR: cir.store{{.*}} %[[CONST_1]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float>
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>>
3550

3651
// LLVM: %[[A_ADDR:.*]] = alloca { float, float }, i64 1, align 4
37-
// LLVM: %[[B_ADDR:.*]] = alloca float, i64 1, align 4
38-
// LLVM: store float 1.000000e+00, ptr %[[B_ADDR]], 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
3972

4073
// OGCG: %[[A_ADDR:.*]] = alloca { float, float }, align 4
41-
// OGCG: %[[B_ADDR:.*]] = alloca float, align 4
42-
// OGCG: store float 1.000000e+00, ptr %[[B_ADDR]], 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
43103

44104
void foo3() {
45105
int a;

0 commit comments

Comments
 (0)