Skip to content

Commit 18e6726

Browse files
committed
Address code review comments
1 parent 4ae5d01 commit 18e6726

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,10 @@ mlir::Value ComplexExprEmitter::emitBinDiv(const BinOpInfo &op) {
869869
assert(!cir::MissingFeatures::fastMathFlags());
870870
assert(!cir::MissingFeatures::cgFPOptionsRAII());
871871

872-
// Handle division between Complex LHS and RHS with element type
873-
// floating-point, and also handling if the element type is integer and either
874-
// LHS or RHS is scalar because it will be implicitly casted to ComplexType
875-
// from the frontend
872+
// Handle division between two complex values. In the case of complex integer
873+
// types mixed with scalar integers, the scalar integer type will always be
874+
// promoted to a complex integer value with a zero imaginary component when
875+
// the AST is formed.
876876
if (mlir::isa<cir::ComplexType>(op.lhs.getType()) &&
877877
mlir::isa<cir::ComplexType>(op.rhs.getType())) {
878878
cir::ComplexRangeKind rangeKind =
@@ -881,7 +881,11 @@ mlir::Value ComplexExprEmitter::emitBinDiv(const BinOpInfo &op) {
881881
rangeKind);
882882
}
883883

884+
// The C99 standard (G.5.1) defines division of a complex value by a real
885+
// value in the following simplified form.
884886
if (mlir::isa<cir::ComplexType>(op.lhs.getType())) {
887+
assert(mlir::cast<cir::ComplexType>(op.lhs.getType()).getElementType() ==
888+
op.rhs.getType());
885889
mlir::Value real = builder.createComplexReal(op.loc, op.lhs);
886890
mlir::Value imag = builder.createComplexImag(op.loc, op.lhs);
887891
mlir::Value newReal = builder.createFDiv(op.loc, real, op.rhs);

clang/test/CIR/CodeGen/complex-compound-assignment.cpp

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -534,45 +534,6 @@ void foo8() {
534534
// OGCG: store float %[[RESULT_REAL]], ptr %[[A_REAL_PTR]], align 4
535535
// OGCG: store float %[[RESULT_IMAG]], ptr %[[A_IMAG_PTR]], align 4
536536

537-
#ifndef __cplusplus
538-
void foo9() {
539-
float _Complex a;
540-
float b;
541-
b += a;
542-
}
543-
#endif
544-
545-
// C_CIR: %[[A_ADDR:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["a"]
546-
// C_CIR: %[[B_ADDR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["b"]
547-
// C_CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
548-
// C_CIR: %[[TMP_B:.*]] = cir.load{{.*}} %[[B_ADDR]] : !cir.ptr<!cir.float>, !cir.float
549-
// C_CIR: %[[A_REAL:.*]] = cir.complex.real %[[A_ADDR]] : !cir.complex<!cir.float> -> !cir.float
550-
// C_CIR: %[[A_IMAG:.*]] = cir.complex.imag %[[A_ADDR]] : !cir.complex<!cir.float> -> !cir.float
551-
// C_CIR: %[[NEW_REAL:.*]] = cir.binop(add, %[[TMP_B]], %[[A_REAL]]) : !cir.float
552-
// C_CIR: %[[RESULT:.*]] = cir.complex.create %[[NEW_REAL]], %[[A_IMAG]] : !cir.float -> !cir.complex<!cir.float>
553-
// C_CIR: %[[RESULT_REAL:.*]] = cir.complex.real %[[RESULT]] : !cir.complex<!cir.float> -> !cir.float
554-
// C_CIR: cir.store{{.*}} %[[RESULT_REAL]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float>
555-
556-
// C_LLVM: %[[A_ADDR:.*]] = alloca { float, float }, i64 1, align 4
557-
// C_LLVM: %[[B_ADDR:.*]] = alloca float, i64 1, align 4
558-
// C_LLVM: %[[TMP_A:.*]] = load { float, float }, ptr %[[A_ADDR]], align 4
559-
// C_LLVM: %[[TMP_B:.*]] = load float, ptr %[[B_ADDR]], align 4
560-
// C_LLVM: %[[A_REAL:.*]] = extractvalue { float, float } %[[TMP_A]], 0
561-
// C_LLVM: %[[A_IMAG:.*]] = extractvalue { float, float } %[[TMP_A]], 1
562-
// C_LLVM: %[[NEW_REAL:.*]] = fadd float %[[TMP_B]], %[[A_REAL]]
563-
// C_LLVM: %[[TMP_RESULT:.*]] = insertvalue { float, float } {{.*}}, float %[[NEW_REAL]], 0
564-
// C_LLVM: store float %[[NEW_REAL]], ptr %[[B_ADDR]], align 4
565-
566-
// C_OGCG: %[[A_ADDR:.*]] = alloca { float, float }, align 4
567-
// C_OGCG: %[[B_ADDR:.*]] = alloca float, align 4
568-
// C_OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 0
569-
// C_OGCG: %[[A_REAL:.*]] = load float, ptr %[[A_REAL_PTR]], align 4
570-
// C_OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
571-
// C_OGCG: %[[A_IMAG:.*]] = load float, ptr %[[A_IMAG_PTR]], align 4
572-
// C_OGCG: %[[TMP_B:.*]] = load float, ptr %[[B_ADDR]], align 4
573-
// C_OGCG: %[[ADD_REAL:.*]] = fadd float %[[TMP_B]], %[[A_REAL]]
574-
// C_OGCG: store float %[[ADD_REAL]], ptr %[[B_ADDR]], align 4
575-
576537
void foo10() {
577538
float _Complex a;
578539
float _Complex b;
@@ -612,7 +573,7 @@ void foo10() {
612573
// OGCG: %[[A_REAL:.*]] = load float, ptr %[[A_REAL_PTR]], align 4
613574
// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
614575
// OGCG: %[[A_IMAG:.*]] = load float, ptr %[[A_IMAG_PTR]], align 4
615-
// OGCG: %[[RESULT:.*]] = call noundef <2 x float> @__divsc3(float noundef %[[A_REAL]], float noundef %[[A_IMAG]], float noundef %[[B_REAL]], float noundef %[[B_IMAG]])
576+
// OGCG: %[[RESULT:.*]] = call{{.*}} <2 x float> @__divsc3(float noundef %[[A_REAL]], float noundef %[[A_IMAG]], float noundef %[[B_REAL]], float noundef %[[B_IMAG]])
616577
// OGCG: store <2 x float> %[[RESULT]], ptr %[[RESULT_ADDR]], align 4
617578
// OGCG: %[[RESULT_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[RESULT_ADDR]], i32 0, i32 0
618579
// OGCG: %[[RESULT_REAL:.*]] = load float, ptr %[[RESULT_REAL_PTR]], align 4
@@ -739,3 +700,42 @@ void foo12() {
739700
// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[A_ADDR]], i32 0, i32 1
740701
// OGCG: store i32 %[[RESULT_REAL]], ptr %[[A_REAL_PTR]], align 4
741702
// OGCG: store i32 %[[RESULT_IMAG]], ptr %[[A_IMAG_PTR]], align 4
703+
704+
#ifndef __cplusplus
705+
void foo9() {
706+
float _Complex a;
707+
float b;
708+
b += a;
709+
}
710+
#endif
711+
712+
// C_CIR: %[[A_ADDR:.*]] = cir.alloca !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>, ["a"]
713+
// C_CIR: %[[B_ADDR:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["b"]
714+
// C_CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
715+
// C_CIR: %[[TMP_B:.*]] = cir.load{{.*}} %[[B_ADDR]] : !cir.ptr<!cir.float>, !cir.float
716+
// C_CIR: %[[A_REAL:.*]] = cir.complex.real %[[A_ADDR]] : !cir.complex<!cir.float> -> !cir.float
717+
// C_CIR: %[[A_IMAG:.*]] = cir.complex.imag %[[A_ADDR]] : !cir.complex<!cir.float> -> !cir.float
718+
// C_CIR: %[[NEW_REAL:.*]] = cir.binop(add, %[[TMP_B]], %[[A_REAL]]) : !cir.float
719+
// C_CIR: %[[RESULT:.*]] = cir.complex.create %[[NEW_REAL]], %[[A_IMAG]] : !cir.float -> !cir.complex<!cir.float>
720+
// C_CIR: %[[RESULT_REAL:.*]] = cir.complex.real %[[RESULT]] : !cir.complex<!cir.float> -> !cir.float
721+
// C_CIR: cir.store{{.*}} %[[RESULT_REAL]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float>
722+
723+
// C_LLVM: %[[A_ADDR:.*]] = alloca { float, float }, i64 1, align 4
724+
// C_LLVM: %[[B_ADDR:.*]] = alloca float, i64 1, align 4
725+
// C_LLVM: %[[TMP_A:.*]] = load { float, float }, ptr %[[A_ADDR]], align 4
726+
// C_LLVM: %[[TMP_B:.*]] = load float, ptr %[[B_ADDR]], align 4
727+
// C_LLVM: %[[A_REAL:.*]] = extractvalue { float, float } %[[TMP_A]], 0
728+
// C_LLVM: %[[A_IMAG:.*]] = extractvalue { float, float } %[[TMP_A]], 1
729+
// C_LLVM: %[[NEW_REAL:.*]] = fadd float %[[TMP_B]], %[[A_REAL]]
730+
// C_LLVM: %[[TMP_RESULT:.*]] = insertvalue { float, float } {{.*}}, float %[[NEW_REAL]], 0
731+
// C_LLVM: store float %[[NEW_REAL]], ptr %[[B_ADDR]], align 4
732+
733+
// C_OGCG: %[[A_ADDR:.*]] = alloca { float, float }, align 4
734+
// C_OGCG: %[[B_ADDR:.*]] = alloca float, align 4
735+
// C_OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 0
736+
// C_OGCG: %[[A_REAL:.*]] = load float, ptr %[[A_REAL_PTR]], align 4
737+
// C_OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
738+
// C_OGCG: %[[A_IMAG:.*]] = load float, ptr %[[A_IMAG_PTR]], align 4
739+
// C_OGCG: %[[TMP_B:.*]] = load float, ptr %[[B_ADDR]], align 4
740+
// C_OGCG: %[[ADD_REAL:.*]] = fadd float %[[TMP_B]], %[[A_REAL]]
741+
// C_OGCG: store float %[[ADD_REAL]], ptr %[[B_ADDR]], align 4

0 commit comments

Comments
 (0)