Skip to content

Commit b647f4b

Browse files
authored
[CIR] Implement CK_LValueToRValueBitCast for ComplexType (#1751)
Implement supporting for CK_LValueToRValueBitCast for ComplexType
1 parent 46686b7 commit b647f4b

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,14 @@ mlir::Value ComplexExprEmitter::emitCast(CastKind CK, Expr *Op,
431431
case CK_LValueBitCast:
432432
llvm_unreachable("NYI");
433433

434-
case CK_LValueToRValueBitCast:
435-
llvm_unreachable("NYI");
434+
case CK_LValueToRValueBitCast: {
435+
LValue SourceLVal = CGF.emitLValue(Op);
436+
Address Addr = SourceLVal.getAddress().withElementType(
437+
Builder, CGF.convertTypeForMem(DestTy));
438+
LValue DestLV = CGF.makeAddrLValue(Addr, DestTy);
439+
DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo());
440+
return emitLoadOfLValue(DestLV, Op->getExprLoc());
441+
}
436442

437443
case CK_BitCast:
438444
case CK_BaseToDerived:

clang/test/CIR/CodeGen/complex-cast.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,29 @@ void complex_to_bool() {
199199

200200
// CHECK: }
201201

202+
struct CX {
203+
double real;
204+
double imag;
205+
};
206+
207+
void lvalue_to_rvalue_bitcast() {
208+
struct CX a;
209+
double _Complex b = __builtin_bit_cast(double _Complex, a);
210+
}
211+
212+
// CHECK-LABEL: @lvalue_to_rvalue_bitcast()
213+
214+
// CIR-BEFORE: %{{.+}} = cir.cast(bitcast, %{{.+}} : !cir.ptr<!rec_CX>), !cir.ptr<!cir.complex<!cir.double>>
215+
216+
// CIR-AFTER: %{{.+}} = cir.cast(bitcast, %{{.+}} : !cir.ptr<!rec_CX>), !cir.ptr<!cir.complex<!cir.double>>
217+
218+
// LLVM: %[[PTR_ADDR:.*]] = alloca %struct.CX, i64 1, align 8
219+
// LLVM: %[[COMPLEX_ADDR:.*]] = alloca { double, double }, i64 1, align 8
220+
// LLVM: %[[PTR_TO_COMPLEX:.*]] = load { double, double }, ptr %[[PTR_ADDR]], align 8
221+
// LLVM: store { double, double } %[[PTR_TO_COMPLEX]], ptr %[[COMPLEX_ADDR]], align 8
222+
223+
// CHECK: }
224+
202225
void complex_to_complex_cast() {
203226
cd = cf;
204227
ci = cs;

0 commit comments

Comments
 (0)