Skip to content

[5.9] IRGen: alloc_global and global_addr instructions need to agree on the storage #66560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2939,10 +2939,9 @@ void IRGenSILFunction::visitAllocGlobalInst(AllocGlobalInst *i) {

void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) {
SILGlobalVariable *var = i->getReferencedGlobal();
SILType loweredTy = var->getLoweredTypeInContext(getExpansionContext());
assert(loweredTy == i->getType().getObjectType());
SILType loweredTy = var->getLoweredType();
auto &ti = getTypeInfo(loweredTy);

auto expansion = IGM.getResilienceExpansionForLayout(var);

// If the variable is empty in all resilience domains that can see it,
Expand All @@ -2965,7 +2964,15 @@ void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) {
// Otherwise, the static storage for the global consists of a fixed-size
// buffer; project it.
addr = emitProjectValueInBuffer(*this, loweredTy, addr);



// Get the address of the type in context.
SILType loweredTyInContext = var->getLoweredTypeInContext(getExpansionContext());
auto &tiInContext = getTypeInfo(loweredTyInContext);
auto ptr = Builder.CreateBitOrPointerCast(addr.getAddress(),
tiInContext.getStorageType()->getPointerTo());
addr = Address(ptr, tiInContext.getStorageType(),
tiInContext.getBestKnownAlignment());
setLoweredAddress(i, addr);
}

Expand Down
17 changes: 16 additions & 1 deletion test/IRGen/globals.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -primary-file %s -emit-ir | %FileCheck %s
// RUN: %target-swift-frontend -primary-file %s -disable-availability-checking -emit-ir | %FileCheck %s

// REQUIRES: CPU=x86_64

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

// CHECK: [[BUF_PROJ:%.*]] = call {{.*}} @__swift_project_value_buffer({{.*}}s7globals1gQrvp
// CHECK: [[CAST:%.*]] = bitcast {{.*}} [[BUF_PROJ]]
// CHECK: [[CAST2:%.*]] = bitcast {{.*}} [[CAST]]
// CHECK: call void @llvm.memcpy{{.*}}({{.*}}[[CAST2]]


public protocol Some {}

public struct Implementer : Some {
var w = (0, 1, 2, 3, 4)

public init() { }
}

let g : some Some = Implementer()