Skip to content
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

IRGen: Fix type of a global with tail allocated storage #19397

Merged
merged 2 commits into from Sep 21, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/IRGen/GenDecl.cpp
Expand Up @@ -1739,6 +1739,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 @@ -1768,6 +1769,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
storageType = Layout->getType();
fixedSize = Layout->getSize();
fixedAlignment = Layout->getAlignment();
castStorageToType = cast<FixedTypeInfo>(ti).getStorageType();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I missed something in the logic here. But why did you introduce a new variable 'castStorageToType' and not just assign 'storageType' here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see

assert(fixedAlignment >= TargetInfo.HeapObjectAlignment);
} else if (isREPLVar || ti.isFixedSize(expansion)) {
// Allocate static storage.
Expand Down Expand Up @@ -1863,7 +1865,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
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
}