Skip to content

Commit 9372173

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 9372173

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lib/IRGen/IRGenSIL.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,25 +2958,30 @@ 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 =
2964+
var->getLoweredTypeInContext(getExpansionContext());
2965+
auto &tiInContext = getTypeInfo(loweredTyInContext);
2966+
auto ptr = Builder.CreateBitOrPointerCast(
2967+
addr.getAddress(), tiInContext.getStorageType()->getPointerTo());
2968+
addr = Address(ptr, tiInContext.getStorageType(),
2969+
tiInContext.getBestKnownAlignment());
2970+
return addr;
2971+
};
2972+
29612973
// If the global is fixed-size in all resilience domains that can see it,
29622974
// we allocated storage for it statically, and there's nothing to do.
29632975
if (ti.isFixedSize(expansion)) {
2976+
addr = getAddressInContext(addr);
29642977
setLoweredAddress(i, addr);
29652978
return;
29662979
}
29672980

29682981
// Otherwise, the static storage for the global consists of a fixed-size
29692982
// buffer; project it.
29702983
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());
2984+
addr = getAddressInContext(addr);
29802985
setLoweredAddress(i, addr);
29812986
}
29822987

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)