Skip to content

Commit ac7c511

Browse files
authored
Merge pull request #19467 from aschwaighofer/fix_global_tail_allocated_storage_4.2
[4.2] IRGen: Fix type of a global with tail allocated storage
2 parents 31abddf + aadb129 commit ac7c511

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
21182118
ResilienceExpansion expansion = getResilienceExpansionForLayout(var);
21192119

21202120
llvm::Type *storageType;
2121+
llvm::Type *castStorageToType = nullptr;
21212122
Size fixedSize;
21222123
Alignment fixedAlignment;
21232124
bool inFixedBuffer = false;
@@ -2147,6 +2148,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
21472148
storageType = Layout->getType();
21482149
fixedSize = Layout->getSize();
21492150
fixedAlignment = Layout->getAlignment();
2151+
castStorageToType = cast<FixedTypeInfo>(ti).getStorageType();
21502152
assert(fixedAlignment >= TargetInfo.HeapObjectAlignment);
21512153
} else if (isREPLVar || ti.isFixedSize(expansion)) {
21522154
// Allocate static storage.
@@ -2242,7 +2244,9 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
22422244
// to a reference to it).
22432245
addr = llvm::ConstantExpr::getGetElementPtr(nullptr, gvar, Indices);
22442246
}
2245-
addr = llvm::ConstantExpr::getBitCast(addr, storageType->getPointerTo());
2247+
addr = llvm::ConstantExpr::getBitCast(
2248+
addr,
2249+
castStorageToType ? castStorageToType : storageType->getPointerTo());
22462250
return Address(addr, Alignment(gvar->getAlignment()));
22472251
}
22482252

test/IRGen/static_initializer.sil

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,31 @@ bb0:
152152
// CHECK: [[TMP:%.*]] = call swiftcc %swift.metadata_response @"$S18static_initializer16TestArrayStorageCMa"(i64 0)
153153
// CHECK: [[MD:%[0-9]+]] = extractvalue %swift.metadata_response [[TMP]], 0
154154
// CHECK: [[O:%[0-9a-z]+]] = call %swift.refcounted* @swift_initStaticObject(%swift.type* [[MD]], %swift.refcounted* getelementptr inbounds (%T18static_initializer16TestArrayStorageC_tailelems0c, %T18static_initializer16TestArrayStorageC_tailelems0c* @static_array, i32 0, i32 1, i32 0))
155-
// CHECK: [[R:%[0-9]+]] = bitcast %swift.refcounted* [[O]] to %T18static_initializer16TestArrayStorageC_tailelems0*
156-
// CHECK: [[R2:%[0-9]+]] = bitcast %T18static_initializer16TestArrayStorageC_tailelems0* [[R]] to %T18static_initializer16TestArrayStorageC*
157-
// CHECK: ret %T18static_initializer16TestArrayStorageC* [[R2]]
155+
// CHECK: [[R:%[0-9]+]] = bitcast %swift.refcounted* [[O]] to %T18static_initializer16TestArrayStorageC*
156+
// CHECK: ret %T18static_initializer16TestArrayStorageC* [[R]]
158157
%0 = global_value @static_array : $TestArrayStorage
159158
%1 = struct $TestArray (%0 : $TestArrayStorage)
160159
return %1 : $TestArray
161160
}
162161

162+
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc %T18static_initializer16TestArrayStorageC* @phi_nodes(i1, %T18static_initializer16TestArrayStorageC*)
163+
// CHECK: [[T0:%.*]] = call %swift.refcounted* @swift_initStaticObject
164+
// CHECK: [[T1:%.*]] = bitcast %swift.refcounted* [[T0]] to %T18static_initializer16TestArrayStorageC*
165+
// CHECK: br
166+
// CHECK: br
167+
// CHECK: [[T3:%.*]] = phi %T18static_initializer16TestArrayStorageC* [ %1, {{.*}} ], [ [[T1]], {{.*}} ]
168+
// CHECK: ret %T18static_initializer16TestArrayStorageC* [[T3]]
169+
sil @phi_nodes : $@convention(thin) (Builtin.Int1, TestArrayStorage) -> TestArrayStorage {
170+
bb0(%0 : $Builtin.Int1, %1 : $TestArrayStorage):
171+
cond_br %0, bb1, bb2
172+
173+
bb1:
174+
%2 = global_value @static_array : $TestArrayStorage
175+
br bb3(%2 : $TestArrayStorage)
176+
177+
bb2:
178+
br bb3(%1 : $TestArrayStorage)
179+
180+
bb3(%3 : $TestArrayStorage):
181+
return %3 : $TestArrayStorage
182+
}

0 commit comments

Comments
 (0)