Skip to content

Commit b6e9c8e

Browse files
committed
IRGen: alloc_global and global_addr instructions need to agree on the storage
If the storage is opaque we need to project to the underlying buffer. rdar://109636344
1 parent 9991468 commit b6e9c8e

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/IRGen/IRGenSIL.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,10 +2942,9 @@ void IRGenSILFunction::visitAllocGlobalInst(AllocGlobalInst *i) {
29422942

29432943
void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) {
29442944
SILGlobalVariable *var = i->getReferencedGlobal();
2945-
SILType loweredTy = var->getLoweredTypeInContext(getExpansionContext());
2946-
assert(loweredTy == i->getType().getObjectType());
2945+
SILType loweredTy = var->getLoweredType();
29472946
auto &ti = getTypeInfo(loweredTy);
2948-
2947+
29492948
auto expansion = IGM.getResilienceExpansionForLayout(var);
29502949

29512950
// If the variable is empty in all resilience domains that can see it,
@@ -2968,7 +2967,15 @@ void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) {
29682967
// Otherwise, the static storage for the global consists of a fixed-size
29692968
// buffer; project it.
29702969
addr = emitProjectValueInBuffer(*this, loweredTy, addr);
2971-
2970+
2971+
2972+
// Get the address of the type in context.
2973+
SILType loweredTyInContext = var->getLoweredTypeInContext(getExpansionContext());
2974+
auto &tiInContext = getTypeInfo(loweredTyInContext);
2975+
auto ptr = Builder.CreateBitOrPointerCast(addr.getAddress(),
2976+
tiInContext.getStorageType()->getPointerTo());
2977+
addr = Address(ptr, tiInContext.getStorageType(),
2978+
tiInContext.getBestKnownAlignment());
29722979
setLoweredAddress(i, addr);
29732980
}
29742981

test/IRGen/globals.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,18 @@ extension A {
5454
// CHECK: define{{( dllexport)?}}{{( protected)?}} i32 @main(i32 %0, i8** %1) {{.*}} {
5555
// CHECK: store i64 {{.*}}, i64* getelementptr inbounds ([[INT]], [[INT]]* @"$s7globals2g0Sivp", i32 0, i32 0), align 8
5656

57+
// CHECK: [[BUF_PROJ:%.*]] = call {{.*}} @__swift_project_value_buffer({{.*}}s7globals1gQrvp
58+
// CHECK: [[CAST:%.*]] = bitcast {{.*}} [[BUF_PROJ]]
59+
// CHECK: [[CAST2:%.*]] = bitcast {{.*}} [[CAST]]
60+
// CHECK: call void @llvm.memcpy{{.*}}({{.*}}[[CAST2]]
61+
62+
63+
public protocol Some {}
64+
65+
public struct Implementer : Some {
66+
var w = (0, 1, 2, 3, 4)
67+
68+
public init() { }
69+
}
70+
71+
let g : some Some = Implementer()

0 commit comments

Comments
 (0)