Skip to content

[WebAssembly] Don't remove function signatures in ThinLTOBitcodeWriter #126552

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
39 changes: 20 additions & 19 deletions llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,28 +178,29 @@ void simplifyExternals(Module &M) {
FunctionType *EmptyFT =
FunctionType::get(Type::getVoidTy(M.getContext()), false);

for (Function &F : llvm::make_early_inc_range(M)) {
if (F.isDeclaration() && F.use_empty()) {
for (Function &F : llvm::make_early_inc_range(M))
if (F.isDeclaration() && F.use_empty())
F.eraseFromParent();
continue;
}

if (!F.isDeclaration() || F.getFunctionType() == EmptyFT ||
// Changing the type of an intrinsic may invalidate the IR.
F.getName().starts_with("llvm."))
continue;
// Wasm should preserve function signatures for the linker.
if (!Triple(M.getTargetTriple()).isWasm()) {
for (Function &F : llvm::make_early_inc_range(M)) {
if (!F.isDeclaration() || F.getFunctionType() == EmptyFT ||
// Changing the type of an intrinsic may invalidate the IR.
F.getName().starts_with("llvm."))
continue;

Function *NewF =
Function::Create(EmptyFT, GlobalValue::ExternalLinkage,
F.getAddressSpace(), "", &M);
NewF->copyAttributesFrom(&F);
// Only copy function attribtues.
NewF->setAttributes(AttributeList::get(M.getContext(),
AttributeList::FunctionIndex,
F.getAttributes().getFnAttrs()));
NewF->takeName(&F);
F.replaceAllUsesWith(NewF);
F.eraseFromParent();
Function *NewF = Function::Create(EmptyFT, GlobalValue::ExternalLinkage,
F.getAddressSpace(), "", &M);
NewF->copyAttributesFrom(&F);
// Only copy function attribtues.
NewF->setAttributes(AttributeList::get(M.getContext(),
AttributeList::FunctionIndex,
F.getAttributes().getFnAttrs()));
NewF->takeName(&F);
F.replaceAllUsesWith(NewF);
F.eraseFromParent();
}
}

for (GlobalIFunc &I : llvm::make_early_inc_range(M.ifuncs())) {
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/ThinLTO/WebAssembly/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if not "WebAssembly" in config.root.targets:
config.unsupported = True
29 changes: 29 additions & 0 deletions llvm/test/ThinLTO/WebAssembly/thinlto-split-lto-unit.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
; RUN: opt -thinlto-bc -thinlto-split-lto-unit %s -o %t.o
; RUN: llvm-modextract -b -n 0 %t.o -o - | llvm-dis | FileCheck %s --check-prefix=UNIT0
; RUN: llvm-modextract -b -n 1 %t.o -o - | llvm-dis | FileCheck %s --check-prefix=UNIT1

target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
target triple = "wasm32-unknown-unknown"

; After split LTO unit generation, @test_gv will move to UNIT1 (because it has a
; type metadata), and because it refers to @test, @test will be promoted by
; adding its module ID hash to its name, and .lto_set_conditional directive will
; be written in Unit 0.

; UNIT0: module asm ".lto_set_conditional test,test.{{[0-9a-f]+}}"
; UNIT0: define hidden i32 @test.{{[0-9a-f]+}}()

; Unit 1 will contain @test's declaration. The normal ThinLTO split bitcode
; writing removes the signatures from the split unit's declaration, but in Wasm
; you should not do this in order to avoid function signature mismatch in
; linker.

; UNIT1: declare hidden i32 @test.{{[0-9a-f]+}}()

@test_gv = constant ptr @test, align 4, !type !0

define internal i32 @test() {
ret i32 0
}

!0 = !{i64 0, !{}}