Skip to content

Commit e9115f6

Browse files
AmrDeveloperlanza
authored andcommitted
[CIR] CompoundAssignment ComplexType to ScalarType (#1777)
Implement CompoundAssignment from ComplexType to ScalarType
1 parent 9354dc1 commit e9115f6

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,3 +993,12 @@ mlir::Value CIRGenFunction::emitComplexPrePostIncDec(const UnaryOperator *E,
993993
mlir::Value CIRGenFunction::emitLoadOfComplex(LValue src, SourceLocation loc) {
994994
return ComplexExprEmitter(*this).emitLoadOfLValue(src, loc);
995995
}
996+
997+
LValue CIRGenFunction::emitScalarCompoundAssignWithComplex(
998+
const CompoundAssignOperator *E, mlir::Value &Result) {
999+
CompoundFunc Op = getComplexOp(E->getOpcode());
1000+
RValue Val;
1001+
LValue Ret = ComplexExprEmitter(*this).emitCompoundAssignLValue(E, Op, Val);
1002+
Result = Val.getScalarVal();
1003+
return Ret;
1004+
}

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2214,7 +2214,7 @@ LValue ScalarExprEmitter::emitCompoundAssignLValue(
22142214
BinOpInfo OpInfo;
22152215

22162216
if (E->getComputationResultType()->isAnyComplexType())
2217-
assert(0 && "not implemented");
2217+
return CGF.emitScalarCompoundAssignWithComplex(E, Result);
22182218

22192219
// Emit the RHS first. __block variables need to have the rhs evaluated
22202220
// first, plus this should improve codegen a little.

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,6 +1937,8 @@ class CIRGenFunction : public CIRGenTypeCache {
19371937

19381938
LValue emitComplexAssignmentLValue(const BinaryOperator *E);
19391939
LValue emitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
1940+
LValue emitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
1941+
mlir::Value &Result);
19401942

19411943
/// Emit the computation of the specified expression of complex type,
19421944
/// returning the result.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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=C_CIR
3+
4+
// RUN: %clang_cc1 -x c -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
5+
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=C_LLVM
6+
7+
// RUN: %clang_cc1 -x c -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
8+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=C_OGCG
9+
10+
#ifndef __cplusplus
11+
void foo() {
12+
float _Complex a;
13+
float b;
14+
b += a;
15+
}
16+
#endif
17+
18+
// C_CIR: %[[A_ADDR:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["a"]
19+
// C_CIR: %[[B_ADDR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["b"]
20+
// C_CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
21+
// C_CIR: %[[TMP_B:.*]] = cir.load{{.*}} %[[B_ADDR]] : !cir.ptr<!cir.float>, !cir.float
22+
// C_CIR: %[[CONST_ZERO:.*]] = cir.const #cir.fp<0.000000e+00> : !cir.float
23+
// C_CIR: %[[COMPLEX_B:.*]] = cir.complex.create %[[TMP_B]], %[[CONST_ZERO]] : !cir.float -> !cir.complex<!cir.float>
24+
// C_CIR: %[[B_REAL:.*]] = cir.complex.real %[[COMPLEX_B]] : !cir.complex<!cir.float> -> !cir.float
25+
// C_CIR: %[[B_IMAG:.*]] = cir.complex.imag %[[COMPLEX_B]] : !cir.complex<!cir.float> -> !cir.float
26+
// C_CIR: %[[A_REAL:.*]] = cir.complex.real %[[TMP_A]] : !cir.complex<!cir.float> -> !cir.float
27+
// C_CIR: %[[A_IMAG:.*]] = cir.complex.imag %[[TMP_A]] : !cir.complex<!cir.float> -> !cir.float
28+
// C_CIR: %[[ADD_REAL:.*]] = cir.binop(add, %[[B_REAL]], %[[A_REAL]]) : !cir.float
29+
// C_CIR: %[[ADD_IMAG:.*]] = cir.binop(add, %[[B_IMAG]], %[[A_IMAG]]) : !cir.float
30+
// C_CIR: %[[RESULT_COMPLEX:.*]] = cir.complex.create %[[ADD_REAL]], %[[ADD_IMAG]] : !cir.float -> !cir.complex<!cir.float>
31+
// C_CIR: %[[RESULT_REAL:.*]] = cir.complex.real %[[RESULT_COMPLEX]] : !cir.complex<!cir.float> -> !cir.float
32+
// C_CIR: cir.store{{.*}} %[[RESULT_REAL]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float>
33+
34+
// C_LLVM: %[[A_ADDR:.*]] = alloca { float, float }, i64 1, align 4
35+
// C_LLVM: %[[B_ADDR:.*]] = alloca float, i64 1, align 4
36+
// C_LLVM: %[[TMP_A:.*]] = load { float, float }, ptr %[[A_ADDR]], align 4
37+
// C_LLVM: %[[TMP_B:.*]] = load float, ptr %[[B_ADDR]], align 4
38+
// C_LLVM: %[[TMP_B_COMPLEX:.*]] = insertvalue { float, float } {{.*}}, float %[[TMP_B]], 0
39+
// C_LLVM: %[[B_COMPLEX:.*]] = insertvalue { float, float } %[[TMP_B_COMPLEX]], float 0.000000e+00, 1
40+
// C_LLVM: %[[B_REAL:.*]] = extractvalue { float, float } %[[B_COMPLEX]], 0
41+
// C_LLVM: %[[B_IMAG:.*]] = extractvalue { float, float } %[[B_COMPLEX]], 1
42+
// C_LLVM: %[[A_REAL:.*]] = extractvalue { float, float } %[[TMP_A]], 0
43+
// C_LLVM: %[[A_IMAG:.*]] = extractvalue { float, float } %[[TMP_A]], 1
44+
// C_LLVM: %[[ADD_REAL:.*]] = fadd float %[[B_REAL]], %[[A_REAL]]
45+
// C_LLVM: %[[ADD_IMAG:.*]] = fadd float %[[B_IMAG]], %[[A_IMAG]]
46+
// C_LLVM: %[[TMP_RESULT_COMPLEX:.*]] = insertvalue { float, float } {{.*}}, float %[[ADD_REAL]], 0
47+
// C_LLVM: %[[RESULT_COMPLEX:.*]] = insertvalue { float, float } %[[TMP_RESULT_COMPLEX]], float %[[ADD_IMAG]], 1
48+
// C_LLVM: %[[RESULT_REAL:.*]] = extractvalue { float, float } %[[RESULT_COMPLEX]], 0
49+
// C_LLVM: store float %[[RESULT_REAL]], ptr %[[B_ADDR]], align 4
50+
51+
// C_OGCG: %[[A_ADDR:.*]] = alloca { float, float }, align 4
52+
// C_OGCG: %[[B_ADDR:.*]] = alloca float, align 4
53+
// C_OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 0
54+
// C_OGCG: %[[A_REAL:.*]] = load float, ptr %[[A_REAL_PTR]], align 4
55+
// C_OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
56+
// C_OGCG: %[[A_IMAG:.*]] = load float, ptr %[[A_IMAG_PTR]], align 4
57+
// C_OGCG: %[[TMP_B:.*]] = load float, ptr %[[B_ADDR]], align 4
58+
// C_OGCG: %[[ADD_REAL:.*]] = fadd float %[[TMP_B]], %[[A_REAL]]
59+
// C_OGCG: store float %[[ADD_REAL]], ptr %[[B_ADDR]], align 4

0 commit comments

Comments
 (0)