Skip to content

[CIR] Backport LValueBitcast for ComplexType #1827

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
Aug 15, 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
13 changes: 11 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,8 @@ LValue CIRGenFunction::emitDeclRefLValue(const DeclRefExpr *E) {
cir::GlobalOp var = CGM.getOrCreateStaticVarDecl(
*VD, CGM.getCIRLinkageVarDefinition(VD, /*IsConstant=*/false));
auto getGlobalOp = builder.createGetGlobal(var);
auto actualElemTy = llvm::cast<cir::PointerType>(getGlobalOp.getType()).getPointee();
auto actualElemTy =
llvm::cast<cir::PointerType>(getGlobalOp.getType()).getPointee();
addr = Address(getGlobalOp, actualElemTy, getContext().getDeclAlign(VD));
} else {
llvm_unreachable("DeclRefExpr for decl not entered in LocalDeclMap?");
Expand Down Expand Up @@ -2017,7 +2018,15 @@ LValue CIRGenFunction::emitCastLValue(const CastExpr *E) {
assert(0 && "NYI");
}
case CK_LValueBitCast: {
assert(0 && "NYI");
// This must be a reinterpret_cast (or c-style equivalent).
const auto *ce = cast<ExplicitCastExpr>(E);

CGM.emitExplicitCastExprType(ce, this);
LValue LV = emitLValue(E->getSubExpr());
Address V = LV.getAddress().withElementType(
builder, convertTypeForMem(ce->getTypeAsWritten()->getPointeeType()));

return makeAddrLValue(V, E->getType(), LV.getBaseInfo(), LV.getTBAAInfo());
}
case CK_AddressSpaceConversion: {
LValue LV = emitLValue(E->getSubExpr());
Expand Down
9 changes: 7 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,13 @@ mlir::Value ComplexExprEmitter::emitCast(CastKind CK, Expr *Op,
case CK_UserDefinedConversion:
llvm_unreachable("NYI");

case CK_LValueBitCast:
llvm_unreachable("NYI");
case CK_LValueBitCast: {
LValue origLV = CGF.emitLValue(Op);
Address addr =
origLV.getAddress().withElementType(Builder, CGF.convertType(DestTy));
LValue destLV = CGF.makeAddrLValue(addr, DestTy);
return emitLoadOfLValue(destLV, Op->getExprLoc());
}

case CK_LValueToRValueBitCast: {
LValue SourceLVal = CGF.emitLValue(Op);
Expand Down
21 changes: 21 additions & 0 deletions clang/test/CIR/CodeGen/complex-cast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -x c++ -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-canonicalize -o %t.cir %s 2>&1 | FileCheck --check-prefix=CIR-BEFORE %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -x c++ -fclangir -emit-cir -mmlir --mlir-print-ir-after=cir-canonicalize -o %t.cir %s 2>&1 | FileCheck --check-prefix=CIR-AFTER %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm -o %t.ll %s
// RUN: FileCheck --input-file=%t.ll --check-prefixes=LLVM %s

struct CX {
double real;
double imag;
};

void complex_lvalue_bitcast() {
struct CX a;
(double _Complex &)a = {};
}

// CIR-BEFORE: %{{.*}} = cir.cast(bitcast, %{{.*}} : !cir.ptr<!rec_CX>), !cir.ptr<!cir.complex<!cir.double>>

// CIR-AFTER: %{{.*}} = cir.cast(bitcast, %{{.*}} : !cir.ptr<!rec_CX>), !cir.ptr<!cir.complex<!cir.double>>

// LLVM: %[[A_ADDR:.*]] = alloca %struct.CX, i64 1, align 8
// LLVM: store { double, double } zeroinitializer, ptr %[[A_ADDR]], align 8
Loading