Skip to content

Commit 213bcd3

Browse files
committed
[IRGen] Cast fixed-size opaque globals.
In #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
1 parent a189be3 commit 213bcd3

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/IRGen/IRGenSIL.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,25 +2958,29 @@ void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) {
29582958
Address addr = IGM.getAddrOfSILGlobalVariable(var, ti,
29592959
NotForDefinition);
29602960

2961+
// Get the address of the type in context.
2962+
auto getAddressInContext = [this, &var](auto addr) -> Address {
2963+
SILType loweredTyInContext = var->getLoweredTypeInContext(getExpansionContext());
2964+
auto &tiInContext = getTypeInfo(loweredTyInContext);
2965+
auto ptr = Builder.CreateBitOrPointerCast(addr.getAddress(),
2966+
tiInContext.getStorageType()->getPointerTo());
2967+
addr = Address(ptr, tiInContext.getStorageType(),
2968+
tiInContext.getBestKnownAlignment());
2969+
return addr;
2970+
};
2971+
29612972
// If the global is fixed-size in all resilience domains that can see it,
29622973
// we allocated storage for it statically, and there's nothing to do.
29632974
if (ti.isFixedSize(expansion)) {
2975+
addr = getAddressInContext(addr);
29642976
setLoweredAddress(i, addr);
29652977
return;
29662978
}
29672979

29682980
// Otherwise, the static storage for the global consists of a fixed-size
29692981
// buffer; project it.
29702982
addr = emitProjectValueInBuffer(*this, loweredTy, addr);
2971-
2972-
2973-
// Get the address of the type in context.
2974-
SILType loweredTyInContext = var->getLoweredTypeInContext(getExpansionContext());
2975-
auto &tiInContext = getTypeInfo(loweredTyInContext);
2976-
auto ptr = Builder.CreateBitOrPointerCast(addr.getAddress(),
2977-
tiInContext.getStorageType()->getPointerTo());
2978-
addr = Address(ptr, tiInContext.getStorageType(),
2979-
tiInContext.getBestKnownAlignment());
2983+
addr = getAddressInContext(addr);
29802984
setLoweredAddress(i, addr);
29812985
}
29822986

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-swift-frontend -O -primary-file %s -disable-availability-checking -emit-ir | %FileCheck %s
2+
3+
// CHECK: define{{( dllexport)?}}{{( protected)?}} i32 @main{{.*}} {
4+
// CHECK: store %swift.refcounted* %{{[0-9]+}},
5+
// CHECK-SAME: %swift.refcounted**
6+
// CHECK-SAME: bitcast (
7+
// CHECK-SAME: %T13rdar1140137095ActorC** @"$s13rdar1140137091xQrvp"
8+
// CHECK-SAME: to %swift.refcounted**)
9+
actor Actor {}
10+
let x: some Actor = Actor()
11+

0 commit comments

Comments
 (0)