Skip to content
Open
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
15 changes: 11 additions & 4 deletions include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,7 @@ class ApplyIRLinkage {
IRLinkage IRL;
public:
ApplyIRLinkage(IRLinkage IRL) : IRL(IRL) {}
void to(llvm::GlobalValue *GV, bool definition = true) const {
void to(llvm::GlobalValue *GV, bool nonAliasedDefinition = true) const {
llvm::Module *M = GV->getParent();
const llvm::Triple Triple(M->getTargetTriple());

Expand All @@ -1899,9 +1899,16 @@ class ApplyIRLinkage {
if (Triple.isOSBinFormatELF())
return;

// COMDATs cannot be applied to declarations. If we have a definition,
// apply the COMDAT.
if (definition)
// COMDATs cannot be applied to declarations. Also, definitions that are
// exported through aliases should not have COMDATs, because the alias
// itself might represent an externally visible symbol but such symbols
// are discarded from the symtab when other object files have a COMDAT
// group with the same signature.
//
// If we have a non-aliased definition with ODR-based linkage, attach it
// to a COMDAT group so that duplicate definitions across object files
// can be merged by the linker.
if (nonAliasedDefinition)
if (IRL.Linkage == llvm::GlobalValue::LinkOnceODRLinkage ||
IRL.Linkage == llvm::GlobalValue::WeakODRLinkage)
if (Triple.supportsCOMDAT())
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2201,7 +2201,7 @@ void IRGenModule::emitVTableStubs() {
&Module);
ApplyIRLinkage(canLinkOnce ? IRLinkage::InternalLinkOnceODR
: IRLinkage::Internal)
.to(stub);
.to(stub, /* nonAliasedDefinition */ false);
stub->setAttributes(constructInitialAttributes());
stub->setCallingConv(DefaultCC);
auto *entry = llvm::BasicBlock::Create(getLLVMContext(), "entry", stub);
Expand Down
43 changes: 43 additions & 0 deletions test/IRGen/dead-stub-method-dedup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %empty-directory(%t)
// RUN: split-file --leading-lines %s %t

// Note: Windows uses internal linkage for dead stub methods, which doesn't
// use linkonce_odr or COMDAT groups.
Comment on lines +4 to +5
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain this a bit? Windows definitely uses COMDATs rather aggressively.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe I wasn’t clear, in this case, IRGenModule::emitVTableStubs uses internal linkage instead of linkonce_odr only for COFF, so we don’t attach COMDATs in ApplyIRLinkage::to in the first place.

Copy link
Member

Choose a reason for hiding this comment

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

Can you please update the comment to reflect that please? Yes, LinkOnce ODR doesn't really work on PE/COFF due to the weak linking behaviour.

// UNSUPPORTED: OS=windows-msvc

// Ensure that swift_dead_method_stub is emitted without comdat linkage
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple -parse-as-library -O -module-name A -c -primary-file %t/A.swift %t/B.swift -emit-ir -o - | %FileCheck %s -check-prefix CHECK
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple -parse-as-library -O -module-name A -c %t/A.swift -primary-file %t/B.swift -emit-ir -o - | %FileCheck %s -check-prefix CHECK

// CHECK-LABEL: define {{(linkonce_odr )?}}hidden void @_swift_dead_method_stub(
// CHECK-NOT: comdat
// CHECK: {


// Ensure that link-time deduplication for swift_dead_method_stub works correctly
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple -parse-as-library -O -module-name A -c -primary-file %t/A.swift %t/B.swift -o %t/A.swift.o
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple -parse-as-library -O -module-name A -c %t/A.swift -primary-file %t/B.swift -o %t/B.swift.o
// RUN: %target-build-swift -target %target-swift-5.1-abi-triple %t/B.swift.o %t/A.swift.o -o %t/a.out

//--- A.swift

// Define an open class with a dead vtable entry
class C1 {
private func dead() {}
}

//--- B.swift

class C2: C1 {
// Define another dead vtable entry to ensure that this object file
// also should have dead vtable stub definition
private func beef() {}
}

@main
struct Entry {
static func main() {
_ = C2()
}
}