Skip to content

Commit 2db4a03

Browse files
Merge pull request swiftlang#66507 from aschwaighofer/fix_some_globals
IRGen: alloc_global and global_addr instructions need to agree on the storage
2 parents 544db5f + 3b06c34 commit 2db4a03

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -primary-file %s -emit-ir | %FileCheck %s
1+
// RUN: %target-swift-frontend -primary-file %s -disable-availability-checking -emit-ir | %FileCheck %s
22

33
// REQUIRES: swift_in_compiler
44
// REQUIRES: PTRSIZE=64
@@ -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)