Skip to content

Commit 5a94ee4

Browse files
authored
[CIR] [Codegen] Implement LValue emission for global variable declaration of reference type (#1899)
Fixes #1803
1 parent 3de2824 commit 5a94ee4

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,8 @@ static LValue emitGlobalVarDeclLValue(CIRGenFunction &CGF, const Expr *E,
935935
}
936936
LValue LV;
937937
if (VD->getType()->isReferenceType())
938-
assert(0 && "NYI");
938+
LV = CGF.emitLoadOfReferenceLValue(Addr, CGF.getLoc(E->getExprLoc()),
939+
VD->getType(), AlignmentSource::Decl);
939940
else
940941
LV = CGF.makeAddrLValue(Addr, T, AlignmentSource::Decl);
941942
assert(!cir::MissingFeatures::setObjCGCLValueClass() && "NYI");
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O2 -fclangir -emit-cir -o - %s | FileCheck %s -check-prefix=CIR
2+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O2 -fclangir -emit-llvm -o - %s | FileCheck %s -check-prefix=LLVM
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O2 -emit-llvm -o - %s | FileCheck %s -check-prefix=OGCG
4+
5+
// CIR: !rec_A = !cir.record<class "A" {!s32i} #cir.record.decl.ast>
6+
// CIR: cir.global "private" constant external @_ZN1B1AE : !cir.ptr<!rec_A> {alignment = 8 : i64}
7+
8+
// LLVM: @_ZN1B1AE = external local_unnamed_addr constant ptr, align 8
9+
// OGCG: @_ZN1B1AE = external local_unnamed_addr constant ptr, align 8
10+
class A { int p = 1;};
11+
class B {
12+
public:
13+
static A &A;
14+
};
15+
A& ref() {
16+
// CIR-LABEL: _Z3refv
17+
// CIR: [[GLOBAL:%.*]] = cir.get_global @_ZN1B1AE : !cir.ptr<!cir.ptr<!rec_A>>
18+
// CIR: [[LD1:%.*]] = cir.load [[GLOBAL]] : !cir.ptr<!cir.ptr<!rec_A>>, !cir.ptr<!rec_A>
19+
// CIR: cir.store align(8) [[LD1]], [[ALLOCA:%.*]] : !cir.ptr<!rec_A>, !cir.ptr<!cir.ptr<!rec_A>>
20+
// CIR: [[LD2:%.*]] = cir.load [[ALLOCA:%.*]]: !cir.ptr<!cir.ptr<!rec_A>>, !cir.ptr<!rec_A>
21+
// CIR: cir.return [[LD2]] : !cir.ptr<!rec_A>
22+
23+
// LLVM-LABEL: _Z3refv
24+
// LLVM: [[LD:%.*]] = load ptr, ptr @_ZN1B1AE
25+
// LLVM-NEXT: ret ptr [[LD]]
26+
27+
// OGCG-LABEL: _Z3refv
28+
// OGCG: [[LD:%.*]] = load ptr, ptr @_ZN1B1AE
29+
// OGCG-NEXT: ret ptr [[LD]]
30+
return B::A;
31+
}

0 commit comments

Comments
 (0)