From b6e9c8e51c157df3fac29a39d1dc262d4dd4bdf8 Mon Sep 17 00:00:00 2001 From: Arnold Schwaighofer Date: Fri, 9 Jun 2023 09:12:37 -0700 Subject: [PATCH 1/2] 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 --- lib/IRGen/IRGenSIL.cpp | 15 +++++++++++---- test/IRGen/globals.swift | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/IRGen/IRGenSIL.cpp b/lib/IRGen/IRGenSIL.cpp index 4b1ffb0b71ea0..dad6ac28e28e0 100644 --- a/lib/IRGen/IRGenSIL.cpp +++ b/lib/IRGen/IRGenSIL.cpp @@ -2942,10 +2942,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, @@ -2968,7 +2967,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); } diff --git a/test/IRGen/globals.swift b/test/IRGen/globals.swift index dce8d83ad2208..43384f563ef3e 100644 --- a/test/IRGen/globals.swift +++ b/test/IRGen/globals.swift @@ -54,3 +54,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() From 3b06c348fa9f6ef2cbe0260e883f8704257696bc Mon Sep 17 00:00:00 2001 From: Arnold Schwaighofer Date: Fri, 9 Jun 2023 15:02:39 -0700 Subject: [PATCH 2/2] Fix test --- test/IRGen/globals.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/IRGen/globals.swift b/test/IRGen/globals.swift index 43384f563ef3e..4c84f3270eec9 100644 --- a/test/IRGen/globals.swift +++ b/test/IRGen/globals.swift @@ -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: swift_in_compiler // REQUIRES: PTRSIZE=64