From aee4ec9f6159165554fd0aa53c7e5cd2453242f4 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Mon, 21 Jul 2025 21:01:50 +0200 Subject: [PATCH 1/2] [CIR] Implement CK_LValueToRValueBitCast for ComplexType --- clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp | 10 ++++++++-- clang/test/CIR/CodeGen/complex-cast.c | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp index 5e8f2d9b55d1..f55a3a08ca25 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp @@ -431,8 +431,14 @@ mlir::Value ComplexExprEmitter::emitCast(CastKind CK, Expr *Op, case CK_LValueBitCast: llvm_unreachable("NYI"); - case CK_LValueToRValueBitCast: - llvm_unreachable("NYI"); + case CK_LValueToRValueBitCast: { + LValue SourceLVal = CGF.emitLValue(Op); + Address Addr = SourceLVal.getAddress().withElementType( + Builder, CGF.convertTypeForMem(DestTy)); + LValue DestLV = CGF.makeAddrLValue(Addr, DestTy); + DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo()); + return emitLoadOfLValue(DestLV, Op->getExprLoc()); + } case CK_BitCast: case CK_BaseToDerived: diff --git a/clang/test/CIR/CodeGen/complex-cast.c b/clang/test/CIR/CodeGen/complex-cast.c index 95789e5abd4a..49b260609c00 100644 --- a/clang/test/CIR/CodeGen/complex-cast.c +++ b/clang/test/CIR/CodeGen/complex-cast.c @@ -199,6 +199,24 @@ void complex_to_bool() { // CHECK: } +void lvalue_to_rvalue_bitcast() { + void *ptr; + int _Complex complex = __builtin_bit_cast(int _Complex, ptr); +} + +// CHECK-LABEL: @lvalue_to_rvalue_bitcast() + +// CIR-BEFORE: %{{.+}} = cir.cast(bitcast, %{{.+}} : !cir.ptr>), !cir.ptr> + +// CIR-AFTER: %{{.+}} = cir.cast(bitcast, %{{.+}} : !cir.ptr>), !cir.ptr> + +// LLVM: %[[PTR_ADDR:.*]] = alloca ptr, i64 1, align 8 +// LLVM: %[[COMPLEX_ADDR:.*]] = alloca { i32, i32 }, i64 1, align 4 +// LLVM: %[[PTR_TO_COMPLEX:.*]] = load { i32, i32 }, ptr %[[PTR_ADDR]], align 8 +// LLVM: store { i32, i32 } %[[PTR_TO_COMPLEX]], ptr %[[COMPLEX_ADDR]], align 4 + +// CHECK: } + void promotion() { cd = cf + cf; } From c3fe7638958289cc020e3d78a3400ded083c54f7 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Fri, 25 Jul 2025 21:02:12 +0200 Subject: [PATCH 2/2] Update the test snippets --- clang/test/CIR/CodeGen/complex-cast.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/clang/test/CIR/CodeGen/complex-cast.c b/clang/test/CIR/CodeGen/complex-cast.c index 49b260609c00..19e6961ad929 100644 --- a/clang/test/CIR/CodeGen/complex-cast.c +++ b/clang/test/CIR/CodeGen/complex-cast.c @@ -199,21 +199,26 @@ void complex_to_bool() { // CHECK: } +struct CX { + double real; + double imag; +}; + void lvalue_to_rvalue_bitcast() { - void *ptr; - int _Complex complex = __builtin_bit_cast(int _Complex, ptr); + struct CX a; + double _Complex b = __builtin_bit_cast(double _Complex, a); } // CHECK-LABEL: @lvalue_to_rvalue_bitcast() -// CIR-BEFORE: %{{.+}} = cir.cast(bitcast, %{{.+}} : !cir.ptr>), !cir.ptr> +// CIR-BEFORE: %{{.+}} = cir.cast(bitcast, %{{.+}} : !cir.ptr), !cir.ptr> -// CIR-AFTER: %{{.+}} = cir.cast(bitcast, %{{.+}} : !cir.ptr>), !cir.ptr> +// CIR-AFTER: %{{.+}} = cir.cast(bitcast, %{{.+}} : !cir.ptr), !cir.ptr> -// LLVM: %[[PTR_ADDR:.*]] = alloca ptr, i64 1, align 8 -// LLVM: %[[COMPLEX_ADDR:.*]] = alloca { i32, i32 }, i64 1, align 4 -// LLVM: %[[PTR_TO_COMPLEX:.*]] = load { i32, i32 }, ptr %[[PTR_ADDR]], align 8 -// LLVM: store { i32, i32 } %[[PTR_TO_COMPLEX]], ptr %[[COMPLEX_ADDR]], align 4 +// LLVM: %[[PTR_ADDR:.*]] = alloca %struct.CX, i64 1, align 8 +// LLVM: %[[COMPLEX_ADDR:.*]] = alloca { double, double }, i64 1, align 8 +// LLVM: %[[PTR_TO_COMPLEX:.*]] = load { double, double }, ptr %[[PTR_ADDR]], align 8 +// LLVM: store { double, double } %[[PTR_TO_COMPLEX]], ptr %[[COMPLEX_ADDR]], align 8 // CHECK: }