Skip to content

[4.2] IRGen: Fix type of a global with tail allocated storage #19467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
ResilienceExpansion expansion = getResilienceExpansionForLayout(var);

llvm::Type *storageType;
llvm::Type *castStorageToType = nullptr;
Size fixedSize;
Alignment fixedAlignment;
bool inFixedBuffer = false;
Expand Down Expand Up @@ -2147,6 +2148,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
storageType = Layout->getType();
fixedSize = Layout->getSize();
fixedAlignment = Layout->getAlignment();
castStorageToType = cast<FixedTypeInfo>(ti).getStorageType();
assert(fixedAlignment >= TargetInfo.HeapObjectAlignment);
} else if (isREPLVar || ti.isFixedSize(expansion)) {
// Allocate static storage.
Expand Down Expand Up @@ -2242,7 +2244,9 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
// to a reference to it).
addr = llvm::ConstantExpr::getGetElementPtr(nullptr, gvar, Indices);
}
addr = llvm::ConstantExpr::getBitCast(addr, storageType->getPointerTo());
addr = llvm::ConstantExpr::getBitCast(
addr,
castStorageToType ? castStorageToType : storageType->getPointerTo());
return Address(addr, Alignment(gvar->getAlignment()));
}

Expand Down
26 changes: 23 additions & 3 deletions test/IRGen/static_initializer.sil
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,31 @@ bb0:
// CHECK: [[TMP:%.*]] = call swiftcc %swift.metadata_response @"$S18static_initializer16TestArrayStorageCMa"(i64 0)
// CHECK: [[MD:%[0-9]+]] = extractvalue %swift.metadata_response [[TMP]], 0
// 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))
// CHECK: [[R:%[0-9]+]] = bitcast %swift.refcounted* [[O]] to %T18static_initializer16TestArrayStorageC_tailelems0*
// CHECK: [[R2:%[0-9]+]] = bitcast %T18static_initializer16TestArrayStorageC_tailelems0* [[R]] to %T18static_initializer16TestArrayStorageC*
// CHECK: ret %T18static_initializer16TestArrayStorageC* [[R2]]
// CHECK: [[R:%[0-9]+]] = bitcast %swift.refcounted* [[O]] to %T18static_initializer16TestArrayStorageC*
// CHECK: ret %T18static_initializer16TestArrayStorageC* [[R]]
%0 = global_value @static_array : $TestArrayStorage
%1 = struct $TestArray (%0 : $TestArrayStorage)
return %1 : $TestArray
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc %T18static_initializer16TestArrayStorageC* @phi_nodes(i1, %T18static_initializer16TestArrayStorageC*)
// CHECK: [[T0:%.*]] = call %swift.refcounted* @swift_initStaticObject
// CHECK: [[T1:%.*]] = bitcast %swift.refcounted* [[T0]] to %T18static_initializer16TestArrayStorageC*
// CHECK: br
// CHECK: br
// CHECK: [[T3:%.*]] = phi %T18static_initializer16TestArrayStorageC* [ %1, {{.*}} ], [ [[T1]], {{.*}} ]
// CHECK: ret %T18static_initializer16TestArrayStorageC* [[T3]]
sil @phi_nodes : $@convention(thin) (Builtin.Int1, TestArrayStorage) -> TestArrayStorage {
bb0(%0 : $Builtin.Int1, %1 : $TestArrayStorage):
cond_br %0, bb1, bb2

bb1:
%2 = global_value @static_array : $TestArrayStorage
br bb3(%2 : $TestArrayStorage)

bb2:
br bb3(%1 : $TestArrayStorage)

bb3(%3 : $TestArrayStorage):
return %3 : $TestArrayStorage
}