Skip to content

Commit 20cfc9d

Browse files
committed
Auto merge of #58408 - alexcrichton:update-llvm, r=michaelwoerister
rustc: Update LLVM, remove dead wasm code This commit updates the LLVM branch to the rebased version of the upstream release/8.x branch. This includes a wasm patch which means that the `rewrite_imports` pass in rustc is no longer needed (yay!) and we can instead rely on `wasm-import-module`, an attribute we're already emitting, to take care of all the work.
2 parents 350674b + 3206400 commit 20cfc9d

File tree

9 files changed

+13
-153
lines changed

9 files changed

+13
-153
lines changed

config.toml.example

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104
# The value specified here will be passed as `-DLLVM_USE_LINKER` to CMake.
105105
#use-linker = "lld"
106106

107+
# Whether or not to specify `-DLLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN=YES`
108+
#allow-old-toolchain = false
107109

108110
# =============================================================================
109111
# General build configuration options

src/bootstrap/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub struct Config {
7878
pub llvm_link_jobs: Option<u32>,
7979
pub llvm_version_suffix: Option<String>,
8080
pub llvm_use_linker: Option<String>,
81+
pub llvm_allow_old_toolchain: Option<bool>,
8182

8283
pub lld_enabled: bool,
8384
pub lldb_enabled: bool,
@@ -263,6 +264,7 @@ struct Llvm {
263264
ldflags: Option<String>,
264265
use_libcxx: Option<bool>,
265266
use_linker: Option<String>,
267+
allow_old_toolchain: Option<bool>,
266268
}
267269

268270
#[derive(Deserialize, Default, Clone)]
@@ -530,6 +532,7 @@ impl Config {
530532
config.llvm_ldflags = llvm.ldflags.clone();
531533
set(&mut config.llvm_use_libcxx, llvm.use_libcxx);
532534
config.llvm_use_linker = llvm.use_linker.clone();
535+
config.llvm_allow_old_toolchain = llvm.allow_old_toolchain.clone();
533536
}
534537

535538
if let Some(ref rust) = toml.rust {

src/bootstrap/native.rs

+4
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ impl Step for Llvm {
238238
cfg.define("LLVM_USE_LINKER", linker);
239239
}
240240

241+
if let Some(true) = builder.config.llvm_allow_old_toolchain {
242+
cfg.define("LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN", "YES");
243+
}
244+
241245
if let Some(ref python) = builder.config.python {
242246
cfg.define("PYTHON_EXECUTABLE", python);
243247
}

src/ci/docker/dist-x86_64-netbsd/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ ENV \
3333

3434
ENV HOSTS=x86_64-unknown-netbsd
3535

36-
ENV RUST_CONFIGURE_ARGS --enable-extended --disable-docs
36+
ENV RUST_CONFIGURE_ARGS --enable-extended --disable-docs \
37+
--set llvm.allow-old-toolchain
3738
ENV SCRIPT python2.7 ../x.py dist --host $HOSTS --target $HOSTS

src/librustc_codegen_llvm/back/link.rs

-1
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,6 @@ fn link_natively(sess: &Session,
698698
}
699699

700700
if sess.opts.target_triple.triple() == "wasm32-unknown-unknown" {
701-
wasm::rewrite_imports(&out_filename, &codegen_results.crate_info.wasm_imports);
702701
wasm::add_producer_section(
703702
&out_filename,
704703
&sess.edition().to_string(),

src/librustc_codegen_llvm/back/wasm.rs

-126
Original file line numberDiff line numberDiff line change
@@ -2,116 +2,11 @@ use std::fs;
22
use std::path::Path;
33
use std::str;
44

5-
use rustc_data_structures::fx::FxHashMap;
65
use serialize::leb128;
76

87
// https://webassembly.github.io/spec/core/binary/modules.html#binary-importsec
9-
const WASM_IMPORT_SECTION_ID: u8 = 2;
108
const WASM_CUSTOM_SECTION_ID: u8 = 0;
119

12-
const WASM_EXTERNAL_KIND_FUNCTION: u8 = 0;
13-
const WASM_EXTERNAL_KIND_TABLE: u8 = 1;
14-
const WASM_EXTERNAL_KIND_MEMORY: u8 = 2;
15-
const WASM_EXTERNAL_KIND_GLOBAL: u8 = 3;
16-
17-
/// Rewrite the module imports are listed from in a wasm module given the field
18-
/// name to module name mapping in `import_map`.
19-
///
20-
/// LLVM 6 which we're using right now doesn't have the ability to configure the
21-
/// module a wasm symbol is import from. Rather all imported symbols come from
22-
/// the bland `"env"` module unconditionally. Furthermore we'd *also* need
23-
/// support in LLD for preserving these import modules, which it unfortunately
24-
/// currently does not.
25-
///
26-
/// This function is intended as a hack for now where we manually rewrite the
27-
/// wasm output by LLVM to have the correct import modules listed. The
28-
/// `#[link(wasm_import_module = "...")]` attribute in Rust translates to the
29-
/// module that each symbol is imported from, so here we manually go through the
30-
/// wasm file, decode it, rewrite imports, and then rewrite the wasm module.
31-
///
32-
/// Support for this was added to LLVM in
33-
/// https://github.com/llvm-mirror/llvm/commit/0f32e1365, although support still
34-
/// needs to be added, tracked at https://bugs.llvm.org/show_bug.cgi?id=37168
35-
pub fn rewrite_imports(path: &Path, import_map: &FxHashMap<String, String>) {
36-
if import_map.is_empty() {
37-
return
38-
}
39-
40-
let wasm = fs::read(path).expect("failed to read wasm output");
41-
let mut ret = WasmEncoder::new();
42-
ret.data.extend(&wasm[..8]);
43-
44-
// skip the 8 byte wasm/version header
45-
for (id, raw) in WasmSections(WasmDecoder::new(&wasm[8..])) {
46-
ret.byte(id);
47-
if id == WASM_IMPORT_SECTION_ID {
48-
info!("rewriting import section");
49-
let data = rewrite_import_section(
50-
&mut WasmDecoder::new(raw),
51-
import_map,
52-
);
53-
ret.bytes(&data);
54-
} else {
55-
info!("carry forward section {}, {} bytes long", id, raw.len());
56-
ret.bytes(raw);
57-
}
58-
}
59-
60-
fs::write(path, &ret.data).expect("failed to write wasm output");
61-
62-
fn rewrite_import_section(
63-
wasm: &mut WasmDecoder<'_>,
64-
import_map: &FxHashMap<String, String>,
65-
)
66-
-> Vec<u8>
67-
{
68-
let mut dst = WasmEncoder::new();
69-
let n = wasm.u32();
70-
dst.u32(n);
71-
info!("rewriting {} imports", n);
72-
for _ in 0..n {
73-
rewrite_import_entry(wasm, &mut dst, import_map);
74-
}
75-
return dst.data
76-
}
77-
78-
fn rewrite_import_entry(wasm: &mut WasmDecoder<'_>,
79-
dst: &mut WasmEncoder,
80-
import_map: &FxHashMap<String, String>) {
81-
// More info about the binary format here is available at:
82-
// https://webassembly.github.io/spec/core/binary/modules.html#import-section
83-
//
84-
// Note that you can also find the whole point of existence of this
85-
// function here, where we map the `module` name to a different one if
86-
// we've got one listed.
87-
let module = wasm.str();
88-
let field = wasm.str();
89-
let new_module = if module == "env" {
90-
import_map.get(field).map(|s| &**s).unwrap_or(module)
91-
} else {
92-
module
93-
};
94-
info!("import rewrite ({} => {}) / {}", module, new_module, field);
95-
dst.str(new_module);
96-
dst.str(field);
97-
let kind = wasm.byte();
98-
dst.byte(kind);
99-
match kind {
100-
WASM_EXTERNAL_KIND_FUNCTION => dst.u32(wasm.u32()),
101-
WASM_EXTERNAL_KIND_TABLE => {
102-
dst.byte(wasm.byte()); // element_type
103-
dst.limits(wasm.limits());
104-
}
105-
WASM_EXTERNAL_KIND_MEMORY => dst.limits(wasm.limits()),
106-
WASM_EXTERNAL_KIND_GLOBAL => {
107-
dst.byte(wasm.byte()); // content_type
108-
dst.bool(wasm.bool()); // mutable
109-
}
110-
b => panic!("unknown kind: {}", b),
111-
}
112-
}
113-
}
114-
11510
/// Adds or augment the existing `producers` section to encode information about
11611
/// the Rust compiler used to produce the wasm file.
11712
pub fn add_producer_section(
@@ -266,15 +161,6 @@ impl<'a> WasmDecoder<'a> {
266161
let len = self.u32();
267162
str::from_utf8(self.skip(len as usize)).unwrap()
268163
}
269-
270-
fn bool(&mut self) -> bool {
271-
self.byte() == 1
272-
}
273-
274-
fn limits(&mut self) -> (u32, Option<u32>) {
275-
let has_max = self.bool();
276-
(self.u32(), if has_max { Some(self.u32()) } else { None })
277-
}
278164
}
279165

280166
struct WasmEncoder {
@@ -302,16 +188,4 @@ impl WasmEncoder {
302188
fn str(&mut self, val: &str) {
303189
self.bytes(val.as_bytes())
304190
}
305-
306-
fn bool(&mut self, b: bool) {
307-
self.byte(b as u8);
308-
}
309-
310-
fn limits(&mut self, limits: (u32, Option<u32>)) {
311-
self.bool(limits.1.is_some());
312-
self.u32(limits.0);
313-
if let Some(c) = limits.1 {
314-
self.u32(c);
315-
}
316-
}
317191
}

src/librustc_codegen_ssa/base.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use crate::{ModuleCodegen, ModuleKind, CachedModuleCodegen};
1717

1818
use rustc::dep_graph::cgu_reuse_tracker::CguReuse;
19-
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
19+
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
2020
use rustc::middle::lang_items::StartFnLangItem;
2121
use rustc::middle::weak_lang_items;
2222
use rustc::mir::mono::{Stats, CodegenUnitNameBuilder};
@@ -816,21 +816,11 @@ impl CrateInfo {
816816
used_crates_dynamic: cstore::used_crates(tcx, LinkagePreference::RequireDynamic),
817817
used_crates_static: cstore::used_crates(tcx, LinkagePreference::RequireStatic),
818818
used_crate_source: Default::default(),
819-
wasm_imports: Default::default(),
820819
lang_item_to_crate: Default::default(),
821820
missing_lang_items: Default::default(),
822821
};
823822
let lang_items = tcx.lang_items();
824823

825-
let load_wasm_items = tcx.sess.crate_types.borrow()
826-
.iter()
827-
.any(|c| *c != config::CrateType::Rlib) &&
828-
tcx.sess.opts.target_triple.triple() == "wasm32-unknown-unknown";
829-
830-
if load_wasm_items {
831-
info.load_wasm_imports(tcx, LOCAL_CRATE);
832-
}
833-
834824
let crates = tcx.crates();
835825

836826
let n_crates = crates.len();
@@ -858,9 +848,6 @@ impl CrateInfo {
858848
if tcx.is_no_builtins(cnum) {
859849
info.is_no_builtins.insert(cnum);
860850
}
861-
if load_wasm_items {
862-
info.load_wasm_imports(tcx, cnum);
863-
}
864851
let missing = tcx.missing_lang_items(cnum);
865852
for &item in missing.iter() {
866853
if let Ok(id) = lang_items.require(item) {
@@ -879,15 +866,6 @@ impl CrateInfo {
879866

880867
return info
881868
}
882-
883-
fn load_wasm_imports(&mut self, tcx: TyCtxt<'_, '_, '_>, cnum: CrateNum) {
884-
self.wasm_imports.extend(tcx.wasm_import_module_map(cnum).iter().map(|(&id, module)| {
885-
let instance = Instance::mono(tcx, id);
886-
let import_name = tcx.symbol_name(instance);
887-
888-
(import_name.to_string(), module.clone())
889-
}));
890-
}
891869
}
892870

893871
fn is_codegened_item(tcx: TyCtxt<'_, '_, '_>, id: DefId) -> bool {

src/librustc_codegen_ssa/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ pub struct CrateInfo {
138138
pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
139139
pub used_crates_static: Vec<(CrateNum, LibSource)>,
140140
pub used_crates_dynamic: Vec<(CrateNum, LibSource)>,
141-
pub wasm_imports: FxHashMap<String, String>,
142141
pub lang_item_to_crate: FxHashMap<LangItem, CrateNum>,
143142
pub missing_lang_items: FxHashMap<CrateNum, Vec<LangItem>>,
144143
}

src/llvm-project

Submodule llvm-project updated 463 files

0 commit comments

Comments
 (0)