Skip to content

Commit e08fcbb

Browse files
committed
rustc: Work around an upstream wasm ThinLTO bug
This commit implements a workaround for an [upstream LLVM bug][1] where custom sections were accidentally duplicated amongst codegen units when ThinLTO passes were performed. This is due to the fact that custom sections for wasm are stored as metadata nodes which are automatically imported into modules when ThinLTO happens. The fix here is to forcibly delete the metadata node from imported modules before LLVM has a chance to try to copy it over. [1]: https://bugs.llvm.org/show_bug.cgi?id=38184
1 parent 12ed235 commit e08fcbb

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

src/rustllvm/PassWrapper.cpp

+30-1
Original file line numberDiff line numberDiff line change
@@ -1084,11 +1084,40 @@ LLVMRustPrepareThinLTOInternalize(const LLVMRustThinLTOData *Data, LLVMModuleRef
10841084
extern "C" bool
10851085
LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
10861086
Module &Mod = *unwrap(M);
1087+
10871088
const auto &ImportList = Data->ImportLists.lookup(Mod.getModuleIdentifier());
10881089
auto Loader = [&](StringRef Identifier) {
10891090
const auto &Memory = Data->ModuleMap.lookup(Identifier);
10901091
auto &Context = Mod.getContext();
1091-
return getLazyBitcodeModule(Memory, Context, true, true);
1092+
auto MOrErr = getLazyBitcodeModule(Memory, Context, true, true);
1093+
1094+
if (!MOrErr)
1095+
return std::move(MOrErr);
1096+
1097+
// The rest of this closure is a workaround for
1098+
// https://bugs.llvm.org/show_bug.cgi?id=38184 where during ThinLTO imports
1099+
// we accidentally import wasm custom sections into different modules,
1100+
// duplicating them by in the final output artifact.
1101+
//
1102+
// The issue is worked around here by manually removing the
1103+
// `wasm.custom_sections` named metadata node from any imported module. This
1104+
// we know isn't used by any optimization pass so there's no need for it to
1105+
// be imported.
1106+
//
1107+
// Note that the metadata is currently lazily loaded, so we materialize it
1108+
// here before looking up if there's metadata inside. The `FunctionImporter`
1109+
// will immediately materialize metadata anyway after an import, so this
1110+
// shouldn't be a perf hit.
1111+
if (Error Err = (*MOrErr)->materializeMetadata()) {
1112+
Expected<std::unique_ptr<Module>> Ret(std::move(Err));
1113+
return std::move(Ret);
1114+
}
1115+
1116+
auto *WasmCustomSections = (*MOrErr)->getNamedMetadata("wasm.custom_sections");
1117+
if (WasmCustomSections)
1118+
WasmCustomSections->eraseFromParent();
1119+
1120+
return std::move(MOrErr);
10921121
};
10931122
FunctionImporter Importer(Data->Index, Loader);
10941123
Expected<bool> Result = Importer.importFunctions(Mod, ImportList);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
ifeq ($(TARGET),wasm32-unknown-unknown)
4+
all:
5+
$(RUSTC) foo.rs -O --target wasm32-unknown-unknown
6+
$(NODE) foo.js $(TMPDIR)/foo.wasm
7+
else
8+
all:
9+
endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const fs = require('fs');
12+
const process = require('process');
13+
const assert = require('assert');
14+
const buffer = fs.readFileSync(process.argv[2]);
15+
16+
let m = new WebAssembly.Module(buffer);
17+
18+
sections = WebAssembly.Module.customSections(m, "foo");
19+
console.log('section foo', sections);
20+
assert.strictEqual(sections.length, 1, "didn't create `foo` section");
21+
section = new Uint8Array(sections[0]);
22+
console.log('contents', section);
23+
assert.strictEqual(section.length, 4, "didn't concatenate `foo` sections");
24+
25+
process.exit(0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "cdylib"]
12+
#![deny(warnings)]
13+
14+
#[link_section = "foo"]
15+
pub static A: [u8; 2] = [1, 2];
16+
17+
// make sure this is in another CGU
18+
pub mod another {
19+
#[link_section = "foo"]
20+
pub static FOO: [u8; 2] = [3, 4];
21+
22+
pub fn foo() {}
23+
}
24+
25+
#[no_mangle]
26+
pub extern fn foo() {
27+
// This will import `another::foo` through ThinLTO passes, and it better not
28+
// also accidentally import the `FOO` custom section into this module as
29+
// well
30+
another::foo();
31+
}

0 commit comments

Comments
 (0)