Skip to content

Commit bfef7f3

Browse files
Merge pull request #66560 from aschwaighofer/fix_some_globals_5.9
[5.9] IRGen: alloc_global and global_addr instructions need to agree on the storage
2 parents 7e24b8b + e8011ee commit bfef7f3

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
@@ -2939,10 +2939,9 @@ void IRGenSILFunction::visitAllocGlobalInst(AllocGlobalInst *i) {
29392939

29402940
void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) {
29412941
SILGlobalVariable *var = i->getReferencedGlobal();
2942-
SILType loweredTy = var->getLoweredTypeInContext(getExpansionContext());
2943-
assert(loweredTy == i->getType().getObjectType());
2942+
SILType loweredTy = var->getLoweredType();
29442943
auto &ti = getTypeInfo(loweredTy);
2945-
2944+
29462945
auto expansion = IGM.getResilienceExpansionForLayout(var);
29472946

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

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: CPU=x86_64
44

@@ -53,3 +53,18 @@ extension A {
5353
// CHECK: define{{( dllexport)?}}{{( protected)?}} i32 @main(i32 %0, i8** %1) {{.*}} {
5454
// CHECK: store i64 {{.*}}, i64* getelementptr inbounds ([[INT]], [[INT]]* @"$s7globals2g0Sivp", i32 0, i32 0), align 8
5555

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

0 commit comments

Comments
 (0)