Skip to content

Commit 34c12ae

Browse files
authored
[CIR] Backport LValueBitcast for ComplexType (#1827)
Backport LValueBitcast support for ComplexType from the upstream
1 parent 222929a commit 34c12ae

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,8 @@ LValue CIRGenFunction::emitDeclRefLValue(const DeclRefExpr *E) {
10561056
cir::GlobalOp var = CGM.getOrCreateStaticVarDecl(
10571057
*VD, CGM.getCIRLinkageVarDefinition(VD, /*IsConstant=*/false));
10581058
auto getGlobalOp = builder.createGetGlobal(var);
1059-
auto actualElemTy = llvm::cast<cir::PointerType>(getGlobalOp.getType()).getPointee();
1059+
auto actualElemTy =
1060+
llvm::cast<cir::PointerType>(getGlobalOp.getType()).getPointee();
10601061
addr = Address(getGlobalOp, actualElemTy, getContext().getDeclAlign(VD));
10611062
} else {
10621063
llvm_unreachable("DeclRefExpr for decl not entered in LocalDeclMap?");
@@ -2017,7 +2018,15 @@ LValue CIRGenFunction::emitCastLValue(const CastExpr *E) {
20172018
assert(0 && "NYI");
20182019
}
20192020
case CK_LValueBitCast: {
2020-
assert(0 && "NYI");
2021+
// This must be a reinterpret_cast (or c-style equivalent).
2022+
const auto *ce = cast<ExplicitCastExpr>(E);
2023+
2024+
CGM.emitExplicitCastExprType(ce, this);
2025+
LValue LV = emitLValue(E->getSubExpr());
2026+
Address V = LV.getAddress().withElementType(
2027+
builder, convertTypeForMem(ce->getTypeAsWritten()->getPointeeType()));
2028+
2029+
return makeAddrLValue(V, E->getType(), LV.getBaseInfo(), LV.getTBAAInfo());
20212030
}
20222031
case CK_AddressSpaceConversion: {
20232032
LValue LV = emitLValue(E->getSubExpr());

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,13 @@ mlir::Value ComplexExprEmitter::emitCast(CastKind CK, Expr *Op,
444444
case CK_UserDefinedConversion:
445445
llvm_unreachable("NYI");
446446

447-
case CK_LValueBitCast:
448-
llvm_unreachable("NYI");
447+
case CK_LValueBitCast: {
448+
LValue origLV = CGF.emitLValue(Op);
449+
Address addr =
450+
origLV.getAddress().withElementType(Builder, CGF.convertType(DestTy));
451+
LValue destLV = CGF.makeAddrLValue(addr, DestTy);
452+
return emitLoadOfLValue(destLV, Op->getExprLoc());
453+
}
449454

450455
case CK_LValueToRValueBitCast: {
451456
LValue SourceLVal = CGF.emitLValue(Op);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 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
2+
// 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
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm -o %t.ll %s
4+
// RUN: FileCheck --input-file=%t.ll --check-prefixes=LLVM %s
5+
6+
struct CX {
7+
double real;
8+
double imag;
9+
};
10+
11+
void complex_lvalue_bitcast() {
12+
struct CX a;
13+
(double _Complex &)a = {};
14+
}
15+
16+
// CIR-BEFORE: %{{.*}} = cir.cast(bitcast, %{{.*}} : !cir.ptr<!rec_CX>), !cir.ptr<!cir.complex<!cir.double>>
17+
18+
// CIR-AFTER: %{{.*}} = cir.cast(bitcast, %{{.*}} : !cir.ptr<!rec_CX>), !cir.ptr<!cir.complex<!cir.double>>
19+
20+
// LLVM: %[[A_ADDR:.*]] = alloca %struct.CX, i64 1, align 8
21+
// LLVM: store { double, double } zeroinitializer, ptr %[[A_ADDR]], align 8

0 commit comments

Comments
 (0)