Skip to content

Commit b4d274f

Browse files
authored
[CIR] Implement OpaqueValueExpr for Complex in C (llvm#158423)
This change adds support for the OpaqueValueExpr for Complex in C Issue: llvm#141365
1 parent bbcb5f4 commit b4d274f

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,8 @@ mlir::Value CIRGenFunction::evaluateExprAsBool(const Expr *e) {
730730
if (!e->getType()->isAnyComplexType())
731731
return emitScalarConversion(emitScalarExpr(e), e->getType(), boolTy, loc);
732732

733-
cgm.errorNYI(e->getSourceRange(), "evaluateExprAsBool: complex type");
734-
return createDummyValue(getLoc(loc), boolTy);
733+
return emitComplexToScalarConversion(emitComplexExpr(e), e->getType(), boolTy,
734+
loc);
735735
}
736736

737737
LValue CIRGenFunction::emitUnaryOpLValue(const UnaryOperator *e) {

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
131131
if (e->isGLValue())
132132
return emitLoadOfLValue(cgf.getOrCreateOpaqueLValueMapping(e),
133133
e->getExprLoc());
134-
135-
// Otherwise, assume the mapping is the scalar directly.
136-
return cgf.getOrCreateOpaqueRValueMapping(e).getValue();
134+
return cgf.getOrCreateOpaqueRValueMapping(e).getComplexValue();
137135
}
138136

139137
mlir::Value VisitPseudoObjectExpr(PseudoObjectExpr *e) {

clang/test/CIR/CodeGen/opaque.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// RUN: %clang_cc1 -x c -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 -x c -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 -x c -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 foo2() {
9+
float _Complex a;
10+
float _Complex b;
11+
float _Complex c = a ?: b;
12+
}
13+
14+
// CIR: %[[A_ADDR:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["a"]
15+
// CIR: %[[B_ADDR:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["b"]
16+
// CIR: %[[C_ADDR:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["c", init]
17+
// CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
18+
// CIR: %[[A_REAL:.*]] = cir.complex.real %[[TMP_A]] : !cir.complex<!cir.float> -> !cir.float
19+
// CIR: %[[A_IMAG:.*]] = cir.complex.imag %[[TMP_A]] : !cir.complex<!cir.float> -> !cir.float
20+
// CIR: %[[A_REAL_BOOL:.*]] = cir.cast(float_to_bool, %[[A_REAL]] : !cir.float), !cir.bool
21+
// CIR: %[[A_IMAG_BOOL:.*]] = cir.cast(float_to_bool, %[[A_IMAG]] : !cir.float), !cir.bool
22+
// CIR: %[[CONST_TRUE:.*]] = cir.const #true
23+
// CIR: %[[COND:.*]] = cir.select if %[[A_REAL_BOOL]] then %[[CONST_TRUE]] else %[[A_IMAG_BOOL]] : (!cir.bool, !cir.bool, !cir.bool) -> !cir.bool
24+
// CIR: %[[RESULT:.*]] = cir.ternary(%[[COND]], true {
25+
// CIR: cir.yield %[[TMP_A]] : !cir.complex<!cir.float>
26+
// CIR: }, false {
27+
// CIR: %[[TMP_B:.*]] = cir.load{{.*}} %[[B_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
28+
// CIR: cir.yield %[[TMP_B]] : !cir.complex<!cir.float>
29+
// CIR: }) : (!cir.bool) -> !cir.complex<!cir.float>
30+
// CIR: cir.store{{.*}} %[[RESULT]], %[[C_ADDR]] : !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>
31+
32+
// LLVM: %[[A_ADDR:.*]] = alloca { float, float }, i64 1, align 4
33+
// LLVM: %[[B_ADDR:.*]] = alloca { float, float }, i64 1, align 4
34+
// LLVM: %[[C_ADDR:.*]] = alloca { float, float }, i64 1, align 4
35+
// LLVM: %[[TMP_A:.*]] = load { float, float }, ptr %[[A_ADDR]], align 4
36+
// LLVM: %[[A_REAL:.*]] = extractvalue { float, float } %[[TMP_A]], 0
37+
// LLVM: %[[A_IMAG:.*]] = extractvalue { float, float } %[[TMP_A]], 1
38+
// LLVM: %[[A_REAL_BOOL:.*]] = fcmp une float %[[A_REAL]], 0.000000e+00
39+
// LLVM: %[[A_IMAG_BOOL:.*]] = fcmp une float %[[A_IMAG]], 0.000000e+00
40+
// LLVM: %[[COND:.*]] = or i1 %[[A_REAL_BOOL]], %[[A_IMAG_BOOL]]
41+
// LLVM: br i1 %[[COND]], label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
42+
// LLVM: [[COND_TRUE]]:
43+
// LLVM: br label %[[COND_RESULT:.*]]
44+
// LLVM: [[COND_FALSE]]:
45+
// LLVM: %[[TMP_B:.*]] = load { float, float }, ptr %[[B_ADDR]], align 4
46+
// LLVM: br label %[[COND_RESULT]]
47+
// LLVM: [[COND_RESULT]]:
48+
// LLVM: %[[RESULT:.*]] = phi { float, float } [ %[[TMP_B]], %[[COND_FALSE]] ], [ %[[TMP_A]], %[[COND_TRUE]] ]
49+
// LLVM: br label %[[COND_END:.*]]
50+
// LLVM: [[COND_END]]:
51+
// LLVM: store { float, float } %[[RESULT]], ptr %[[C_ADDR]], align 4
52+
53+
// OGCG: %[[A_ADDR:.*]] = alloca { float, float }, align 4
54+
// OGCG: %[[B_ADDR:.*]] = alloca { float, float }, align 4
55+
// OGCG: %[[C_ADDR:.*]] = alloca { float, float }, align 4
56+
// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 0
57+
// OGCG: %[[A_REAL:.*]] = load float, ptr %[[A_REAL_PTR]], align 4
58+
// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
59+
// OGCG: %[[A_IMAG:.*]] = load float, ptr %[[A_IMAG_PTR]], align 4
60+
// OGCG: %[[A_REAL_BOOL:.*]] = fcmp une float %[[A_REAL]], 0.000000e+00
61+
// OGCG: %[[A_IMAG_BOOL:.*]] = fcmp une float %[[A_IMAG]], 0.000000e+00
62+
// OGCG: %[[COND:.*]] = or i1 %[[A_REAL_BOOL]], %[[A_IMAG_BOOL]]
63+
// OGCG: br i1 %tobool2, label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
64+
// OGCG: [[COND_TRUE]]:
65+
// OGCG: br label %[[COND_END:.*]]
66+
// OGCG: [[COND_FALSE]]:
67+
// OGCG: %[[B_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[B_ADDR]], i32 0, i32 0
68+
// OGCG: %[[B_REAL:.*]] = load float, ptr %[[B_REAL_PTR]], align 4
69+
// OGCG: %[[B_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[B_ADDR]], i32 0, i32 1
70+
// OGCG: %[[B_IMAG:.*]] = load float, ptr %[[B_IMAG_PTR]], align 4
71+
// OGCG: br label %[[COND_END]]
72+
// OGCG: [[COND_END]]:
73+
// OGCG: %[[RESULT_REAL:.*]] = phi float [ %[[A_REAL]], %[[COND_TRUE]] ], [ %[[B_REAL]], %[[COND_FALSE]] ]
74+
// OGCG: %[[RESULT_IMAG:.*]] = phi float [ %[[A_IMAG]], %[[COND_TRUE]] ], [ %[[B_IMAG]], %[[COND_FALSE]] ]
75+
// OGCG: %[[C_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[C_ADDR]], i32 0, i32 0
76+
// OGCG: %[[C_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[C_ADDR]], i32 0, i32 1
77+
// OGCG: store float %[[RESULT_REAL]], ptr %[[C_REAL_PTR]], align 4
78+
// OGCG: store float %[[RESULT_IMAG]], ptr %[[C_IMAG_PTR]], align 4

0 commit comments

Comments
 (0)