From 9981a82d01dbadfac547ad0c21056ad427f835a9 Mon Sep 17 00:00:00 2001 From: Nate Chandler Date: Thu, 17 Aug 2023 16:44:20 -0700 Subject: [PATCH] [IRGen] Cast fixed-size opaque globals. In https://github.com/apple/swift/pull/66560 , a bug in the lowering of `global_addr` was fixed. Part of that fix was to postpone mapping the type of the global into context until getting the address of the global and projecting the buffer for the out-of-line value; at that point, the type is mapped into context and the address is cast. It introduced an issue for fixed-size globals, however: the type of such globals was not mapped into context; the result was that the lowered value set for the corresponding SIL value would have the wrong type. Fix that by extracting the code which mapped the type into context and cast the address to the appropriate lowered type into a lambda and call that lambda both in both the fixed-size (newly) and non-fixed-size (as before) cases. rdar://114013709 --- lib/IRGen/IRGenSIL.cpp | 23 ++++++++++++++--------- validation-test/IRGen/rdar114013709.swift | 7 +++++++ 2 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 validation-test/IRGen/rdar114013709.swift diff --git a/lib/IRGen/IRGenSIL.cpp b/lib/IRGen/IRGenSIL.cpp index 6bec19b3d5c77..c2386847a4253 100644 --- a/lib/IRGen/IRGenSIL.cpp +++ b/lib/IRGen/IRGenSIL.cpp @@ -2983,9 +2983,22 @@ void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) { Address addr = IGM.getAddrOfSILGlobalVariable(var, ti, NotForDefinition); + // Get the address of the type in context. + auto getAddressInContext = [this, &var](auto addr) -> Address { + 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()); + return addr; + }; + // If the global is fixed-size in all resilience domains that can see it, // we allocated storage for it statically, and there's nothing to do. if (ti.isFixedSize(expansion)) { + addr = getAddressInContext(addr); setLoweredAddress(i, addr); return; } @@ -2993,15 +3006,7 @@ 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()); + addr = getAddressInContext(addr); setLoweredAddress(i, addr); } diff --git a/validation-test/IRGen/rdar114013709.swift b/validation-test/IRGen/rdar114013709.swift new file mode 100644 index 0000000000000..71b1549c0d508 --- /dev/null +++ b/validation-test/IRGen/rdar114013709.swift @@ -0,0 +1,7 @@ +// RUN: %target-swift-frontend -O -primary-file %s -disable-availability-checking -emit-ir | %FileCheck %s + +// CHECK: define{{( dllexport)?}}{{( protected)?}} i32 @main{{.*}} { +// CHECK: store ptr %{{[0-9]+}}, ptr @"$s13rdar1140137091xQrvp" +actor Actor {} +let x: some Actor = Actor() +