Skip to content

Commit 54ec521

Browse files
authored
[CIR] Upstream __builtin_cimag for ComplexType (#147808)
Upstream __builtin_cimag support for ComplexType #141365
1 parent 0354867 commit 54ec521

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
128128
return RValue::get(real);
129129
}
130130

131+
case Builtin::BI__builtin_cimag:
132+
case Builtin::BI__builtin_cimagf:
133+
case Builtin::BI__builtin_cimagl:
134+
case Builtin::BIcimag:
135+
case Builtin::BIcimagf:
136+
case Builtin::BIcimagl: {
137+
mlir::Value complex = emitComplexExpr(e->getArg(0));
138+
mlir::Value imag = builder.createComplexImag(loc, complex);
139+
return RValue::get(imag);
140+
}
141+
131142
case Builtin::BI__builtin_clrsb:
132143
case Builtin::BI__builtin_clrsbl:
133144
case Builtin::BI__builtin_clrsbll:

clang/test/CIR/CodeGen/complex-builtins.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,28 @@ void foo2() {
5858
// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { double, double }, ptr %[[COMPLEX]], i32 0, i32 1
5959
// OGCG: %[[A_IMAG:.*]] = load double, ptr %[[A_IMAG_PTR]], align 8
6060
// OGCG: store double %[[A_REAL]], ptr %[[INIT]], align 8
61+
62+
void foo3() {
63+
double _Complex a;
64+
double imag = __builtin_cimag(a);
65+
}
66+
67+
// CIR: %[[COMPLEX:.*]] = cir.alloca !cir.complex<!cir.double>, !cir.ptr<!cir.complex<!cir.double>>, ["a"]
68+
// CIR: %[[INIT:.*]] = cir.alloca !cir.double, !cir.ptr<!cir.double>, ["imag", init]
69+
// CIR: %[[TMP:.*]] = cir.load{{.*}} %[[COMPLEX]] : !cir.ptr<!cir.complex<!cir.double>>, !cir.complex<!cir.double>
70+
// CIR: %[[IMAG:.*]] = cir.complex.imag %[[TMP]] : !cir.complex<!cir.double> -> !cir.double
71+
// CIR: cir.store{{.*}} %[[IMAG]], %[[INIT]] : !cir.double, !cir.ptr<!cir.double>
72+
73+
// LLVM: %[[COMPLEX:.*]] = alloca { double, double }, i64 1, align 8
74+
// LLVM: %[[INIT:.*]] = alloca double, i64 1, align 8
75+
// LLVM: %[[TMP:.*]] = load { double, double }, ptr %[[COMPLEX]], align 8
76+
// LLVM: %[[IMAG:.*]] = extractvalue { double, double } %[[TMP]], 1
77+
// LLVM: store double %[[IMAG]], ptr %[[INIT]], align 8
78+
79+
// OGCG: %[[COMPLEX:.*]] = alloca { double, double }, align 8
80+
// OGCG: %[[INIT:.*]] = alloca double, align 8
81+
// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { double, double }, ptr %[[COMPLEX]], i32 0, i32 0
82+
// OGCG: %[[A_REAL:.*]] = load double, ptr %[[A_REAL_PTR]], align 8
83+
// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { double, double }, ptr %[[COMPLEX]], i32 0, i32 1
84+
// OGCG: %[[A_IMAG:.*]] = load double, ptr %[[A_IMAG_PTR]], align 8
85+
// OGCG: store double %[[A_IMAG]], ptr %[[INIT]], align 8

0 commit comments

Comments
 (0)