Skip to content

[CIR] Use ComplexRealOp for RValue __real__ operator #1689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2050,9 +2050,11 @@ mlir::Value ScalarExprEmitter::VisitReal(const UnaryOperator *E) {
// If it's an l-value, load through the appropriate subobject l-value.
// Note that we have to ask E because Op might be an l-value that
// this won't work for, e.g. an Obj-C property.
if (E->isGLValue())
return CGF.emitLoadOfLValue(CGF.emitLValue(E), E->getExprLoc())
.getScalarVal();
if (E->isGLValue()) {
mlir::Location Loc = CGF.getLoc(E->getExprLoc());
mlir::Value Complex = CGF.emitComplexExpr(Op);
return CGF.builder.createComplexReal(Loc, Complex);
}
// Otherwise, calculate and project.
llvm_unreachable("NYI");
}
Expand Down
50 changes: 40 additions & 10 deletions clang/test/CIR/CodeGen/complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,25 +261,55 @@ void extract_real() {

// CHECK-BEFORE: cir.func
// CHECK-BEFORE: %[[#C_PTR:]] = cir.get_global @c : !cir.ptr<!cir.complex<!cir.double>>
// CHECK-BEFORE-NEXT: %[[#REAL_PTR:]] = cir.complex.real_ptr %[[#C_PTR]] : !cir.ptr<!cir.complex<!cir.double>> -> !cir.ptr<!cir.double>
// CHECK-BEFORE-NEXT: %{{.+}} = cir.load{{.*}} %[[#REAL_PTR]] : !cir.ptr<!cir.double>, !cir.double
// CHECK-BEFORE-NEXT: %[[COMPLEX:.*]] = cir.load{{.*}} %[[#C_PTR]] : !cir.ptr<!cir.complex<!cir.double>>, !cir.complex<!cir.double>
// CHECK-BEFORE-NEXT: %[[#REAL:]] = cir.complex.real %[[COMPLEX]] : !cir.complex<!cir.double> -> !cir.double
// CHECK-BEFORE: %[[#CI_PTR:]] = cir.get_global @ci : !cir.ptr<!cir.complex<!s32i>>
// CHECK-BEFORE-NEXT: %[[#REAL_PTR:]] = cir.complex.real_ptr %[[#CI_PTR]] : !cir.ptr<!cir.complex<!s32i>> -> !cir.ptr<!s32i>
// CHECK-BEFORE-NEXT: %{{.+}} = cir.load{{.*}} %[[#REAL_PTR]] : !cir.ptr<!s32i>, !s32i
// CHECK-BEFORE-NEXT: %[[COMPLEX:.*]] = cir.load{{.*}} %[[#CI_PTR]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
// CHECK-BEFORE-NEXT: %[[#REAL:]] = cir.complex.real %[[COMPLEX]] : !cir.complex<!s32i> -> !s32i
// CHECK-BEFORE: }

// CHECK-AFTER: cir.func
// CHECK-AFTER: %[[#C_PTR:]] = cir.get_global @c : !cir.ptr<!cir.complex<!cir.double>>
// CHECK-AFTER-NEXT: %[[#REAL_PTR:]] = cir.complex.real_ptr %[[#C_PTR]] : !cir.ptr<!cir.complex<!cir.double>> -> !cir.ptr<!cir.double>
// CHECK-AFTER-NEXT: %{{.+}} = cir.load{{.*}} %[[#REAL_PTR]] : !cir.ptr<!cir.double>, !cir.double
// CHECK-AFTER-NEXT: %[[COMPLEX:.*]] = cir.load{{.*}} %[[#C_PTR]] : !cir.ptr<!cir.complex<!cir.double>>, !cir.complex<!cir.double>
// CHECK-AFTER-NEXT: %[[#REAL:]] = cir.complex.real %[[COMPLEX]] : !cir.complex<!cir.double> -> !cir.double
// CHECK-AFTER: %[[#CI_PTR:]] = cir.get_global @ci : !cir.ptr<!cir.complex<!s32i>>
// CHECK-AFTER-NEXT: %[[#REAL_PTR:]] = cir.complex.real_ptr %[[#CI_PTR]] : !cir.ptr<!cir.complex<!s32i>> -> !cir.ptr<!s32i>
// CHECK-AFTER-NEXT: %{{.+}} = cir.load{{.*}} %[[#REAL_PTR]] : !cir.ptr<!s32i>, !s32i
// CHECK-AFTER-NEXT: %[[COMPLEX:.*]] = cir.load{{.*}} %[[#CI_PTR]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
// CHECK-AFTER-NEXT: %[[#REAL:]] = cir.complex.real %[[COMPLEX]] : !cir.complex<!s32i> -> !s32i
// CHECK-AFTER: }

// LLVM: define dso_local void @extract_real()
// LLVM: %{{.+}} = load double, ptr @c, align 8
// LLVM: %{{.+}} = load i32, ptr @ci, align 4
// LLVM: %[[COMPLEX_D:.*]] = load { double, double }, ptr @c, align 8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not great that we're loading the whole complex value when we only need one component, but hopefully the optimizer will do something about that. If necessary, we can create a CIR-specific transform.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, we can check if it is handled by the optimiser, or we can create our own transform. I will create a PR for __imag__ :D

// LLVM: %[[R1:.*]] = extractvalue { double, double } %[[COMPLEX_D]], 0
// LLVM: %[[COMPLEX_I:.*]] = load { i32, i32 }, ptr @ci, align 4
// LLVM: %[[R2:.*]] = extractvalue { i32, i32 } %[[COMPLEX_I]], 0
// LLVM: }

int extract_real_and_add(int _Complex a, int _Complex b) {
return __real__ a + __real__ b;
}

// CHECK-BEFORE: cir.func
// CHECK-BEFORE: %[[COMPLEX_A:.*]] = cir.load{{.*}} {{.*}} : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
// CHECK-BEFORE-NEXT: %[[REAL_A:.*]] = cir.complex.real %[[COMPLEX_A]] : !cir.complex<!s32i> -> !s32i
// CHECK-BEFORE-NEXT: %[[COMPLEX_B:.*]] = cir.load{{.*}} {{.*}} : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
// CHECK-BEFORE-NEXT: %[[REAL_B:.*]] = cir.complex.real %[[COMPLEX_B]] : !cir.complex<!s32i> -> !s32i
// CHECK-BEFORE-NEXT: %[[ADD:.*]] = cir.binop(add, %[[REAL_A]], %[[REAL_B]]) nsw : !s32i
// CHECK-BEFORE: }

// CHECK-AFTER: cir.func
// CHECK-AFTER: %[[COMPLEX_A:.*]] = cir.load{{.*}} {{.*}} : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
// CHECK-AFTER-NEXT: %[[REAL_A:.*]] = cir.complex.real %[[COMPLEX_A]] : !cir.complex<!s32i> -> !s32i
// CHECK-AFTER-NEXT: %[[COMPLEX_B:.*]] = cir.load{{.*}} {{.*}} : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
// CHECK-AFTER-NEXT: %[[REAL_B:.*]] = cir.complex.real %[[COMPLEX_B]] : !cir.complex<!s32i> -> !s32i
// CHECK-AFTER-NEXT: %[[ADD:.*]] = cir.binop(add, %[[REAL_A]], %[[REAL_B]]) nsw : !s32i
// CHECK-AFTER: }

// LLVM: define dso_local i32 @extract_real_and_add
// LLVM: %[[COMPLEX_A:.*]] = load { i32, i32 }, ptr {{.*}}, align 4
// LLVM: %[[REAL_A:.*]] = extractvalue { i32, i32 } %[[COMPLEX_A]], 0
// LLVM: %[[COMPLEX_B:.*]] = load { i32, i32 }, ptr {{.*}}, align 4
// LLVM: %[[REAL_B:.*]] = extractvalue { i32, i32 } %[[COMPLEX_B]], 0
// LLVM: %10 = add nsw i32 %[[REAL_A]], %[[REAL_B]]
// LLVM: }

void imag_ptr() {
Expand Down
Loading