Skip to content

Commit 2c1612c

Browse files
authored
Auto merge of #34743 - badboy:llvm-upgrade, r=eddyb
LLVM upgrade As discussed in https://internals.rust-lang.org/t/need-help-with-emscripten-port/3154/46 I'm trying to update the used LLVM checkout in Rust. I basically took @shepmaster's code and applied it on top (though I did the commits manually, the [original commits have better descriptions](master...avr-rust:avr-support). With these changes I was able to build rustc. `make check` throws one last error on `run-pass/issue-28950.rs`. Output: https://gist.github.com/badboy/bcdd3bbde260860b6159aa49070a9052 I took the metadata changes as is and they seem to work, though it now uses the module in another step. I'm not sure if this is the best and correct way. Things to do: * [x] ~~Make `run-pass/issue-28950.rs` pass~~ unrelated * [x] Find out how the `PositionIndependentExecutable` setting is now used * [x] Is the `llvm::legacy` still the right way to do these things? cc @brson @alexcrichton
2 parents 5ef1e7e + 5d1d247 commit 2c1612c

22 files changed

+241
-392
lines changed

configure

+6
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,12 @@ then
10201020
err "bad LLVM version: $LLVM_VERSION, need >=3.7"
10211021
;;
10221022
esac
1023+
1024+
if "$CFG_LLVM_ROOT/bin/llvm-mc" -help | grep -- "-relocation-model"; then
1025+
msg "found older llvm-mc"
1026+
CFG_LLVM_MC_HAS_RELOCATION_MODEL=1
1027+
putvar CFG_LLVM_MC_HAS_RELOCATION_MODEL
1028+
fi
10231029
fi
10241030

10251031
# Even when the user overrides the choice of CC, still try to detect

mk/platform.mk

+8-1
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,19 @@ define CFG_MAKE_TOOLCHAIN
221221
LLVM_MC_RELOCATION_MODEL="default"
222222
endif
223223

224+
# LLVM changed this flag in 3.9
225+
ifdef CFG_LLVM_MC_HAS_RELOCATION_MODEL
226+
LLVM_MC_RELOC_FLAG := -relocation-model=$$(LLVM_MC_RELOCATION_MODEL)
227+
else
228+
LLVM_MC_RELOC_FLAG := -position-independent
229+
endif
230+
224231
# We're using llvm-mc as our assembler because it supports
225232
# .cfi pseudo-ops on mac
226233
CFG_ASSEMBLE_$(1)=$$(CPP_$(1)) -E $$(2) | \
227234
$$(LLVM_MC_$$(CFG_BUILD)) \
228235
-assemble \
229-
-relocation-model=$$(LLVM_MC_RELOCATION_MODEL) \
236+
$$(LLVM_MC_RELOC_FLAG) \
230237
-filetype=obj \
231238
-triple=$(1) \
232239
-o=$$(1)

src/compiler-rt

Submodule compiler-rt updated 691 files

src/etc/mklldeps.py

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ def runErr(args):
7777
lib = lib.strip()[2:]
7878
elif lib[0] == '-':
7979
lib = lib.strip()[1:]
80+
# If this actually points at a literal file then we're on MSVC which now
81+
# prints full paths, so get just the name of the library and strip off the
82+
# trailing ".lib"
83+
elif os.path.exists(lib):
84+
lib = os.path.basename(lib)[:-4]
85+
elif lib[-4:] == '.lib':
86+
lib = lib[:-4]
8087
f.write("#[link(name = \"" + lib + "\"")
8188
if not llvm_shared and 'LLVM' in lib:
8289
f.write(", kind = \"static\"")

src/librustc_back/target/aarch64_linux_android.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn target() -> TargetResult {
2020
llvm_target: "aarch64-linux-android".to_string(),
2121
target_endian: "little".to_string(),
2222
target_pointer_width: "64".to_string(),
23-
data_layout: "e-m:e-i64:64-i128:128-n32:64-S128".to_string(),
23+
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(),
2424
arch: "aarch64".to_string(),
2525
target_os: "android".to_string(),
2626
target_env: "".to_string(),

src/librustc_back/target/aarch64_unknown_linux_gnu.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn target() -> TargetResult {
1818
target_endian: "little".to_string(),
1919
target_pointer_width: "64".to_string(),
2020
target_env: "gnu".to_string(),
21-
data_layout: "e-m:e-i64:64-i128:128-n32:64-S128".to_string(),
21+
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(),
2222
arch: "aarch64".to_string(),
2323
target_os: "linux".to_string(),
2424
target_vendor: "unknown".to_string(),

src/librustc_llvm/build.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern crate build_helper;
1313

1414
use std::process::Command;
1515
use std::env;
16-
use std::path::PathBuf;
16+
use std::path::{PathBuf, Path};
1717

1818
use build_helper::output;
1919

@@ -135,8 +135,17 @@ fn main() {
135135
&lib[2..]
136136
} else if lib.starts_with("-") {
137137
&lib[1..]
138+
} else if Path::new(lib).exists() {
139+
// On MSVC llvm-config will print the full name to libraries, but
140+
// we're only interested in the name part
141+
let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
142+
name.trim_right_matches(".lib")
143+
} else if lib.ends_with(".lib") {
144+
// Some MSVC libraries just come up with `.lib` tacked on, so chop
145+
// that off
146+
lib.trim_right_matches(".lib")
138147
} else {
139-
continue;
148+
continue
140149
};
141150

142151
// Don't need or want this library, but LLVM's CMake build system
@@ -145,7 +154,7 @@ fn main() {
145154
// library and it otherwise may just pull in extra dependencies on
146155
// libedit which we don't want
147156
if name == "LLVMLineEditor" {
148-
continue;
157+
continue
149158
}
150159

151160
let kind = if name.starts_with("LLVM") {
@@ -165,7 +174,9 @@ fn main() {
165174
let mut cmd = Command::new(&llvm_config);
166175
cmd.arg("--ldflags");
167176
for lib in output(&mut cmd).split_whitespace() {
168-
if is_crossed {
177+
if lib.starts_with("-LIBPATH:") {
178+
println!("cargo:rustc-link-search=native={}", &lib[9..]);
179+
} else if is_crossed {
169180
if lib.starts_with("-L") {
170181
println!("cargo:rustc-link-search=native={}",
171182
lib[2..].replace(&host, &target));

src/librustc_llvm/lib.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl Attributes {
226226

227227
pub fn apply_callsite(&self, idx: usize, callsite: ValueRef) {
228228
unsafe {
229-
LLVMAddCallSiteAttribute(callsite, idx as c_uint, self.regular.bits());
229+
LLVMRustAddCallSiteAttribute(callsite, idx as c_uint, self.regular.bits());
230230
if self.dereferenceable_bytes != 0 {
231231
LLVMAddDereferenceableCallSiteAttr(callsite, idx as c_uint,
232232
self.dereferenceable_bytes);
@@ -1056,7 +1056,7 @@ extern {
10561056
pub fn LLVMSetInstrParamAlignment(Instr: ValueRef,
10571057
index: c_uint,
10581058
align: c_uint);
1059-
pub fn LLVMAddCallSiteAttribute(Instr: ValueRef,
1059+
pub fn LLVMRustAddCallSiteAttribute(Instr: ValueRef,
10601060
index: c_uint,
10611061
Val: uint64_t);
10621062
pub fn LLVMAddDereferenceableCallSiteAttr(Instr: ValueRef,
@@ -1561,7 +1561,7 @@ extern {
15611561
Alignment: c_uint)
15621562
-> ValueRef;
15631563

1564-
pub fn LLVMBuildAtomicCmpXchg(B: BuilderRef,
1564+
pub fn LLVMRustBuildAtomicCmpXchg(B: BuilderRef,
15651565
LHS: ValueRef,
15661566
CMP: ValueRef,
15671567
RHS: ValueRef,
@@ -1591,9 +1591,6 @@ extern {
15911591

15921592
/// Creates target data from a target layout string.
15931593
pub fn LLVMCreateTargetData(StringRep: *const c_char) -> TargetDataRef;
1594-
/// Adds the target data to the given pass manager. The pass manager
1595-
/// references the target data only weakly.
1596-
pub fn LLVMAddTargetData(TD: TargetDataRef, PM: PassManagerRef);
15971594
/// Number of bytes clobbered when doing a Store to *T.
15981595
pub fn LLVMStoreSizeOfType(TD: TargetDataRef, Ty: TypeRef)
15991596
-> c_ulonglong;
@@ -2155,6 +2152,7 @@ extern {
21552152

21562153
pub fn LLVMRustSetComdat(M: ModuleRef, V: ValueRef, Name: *const c_char);
21572154
pub fn LLVMRustUnsetComdat(V: ValueRef);
2155+
pub fn LLVMRustSetModulePIELevel(M: ModuleRef);
21582156
}
21592157

21602158
// LLVM requires symbols from this library, but apparently they're not printed

src/librustc_trans/back/write.rs

+3-23
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use util::fs::link_or_copy;
2424
use errors::{self, Handler, Level, DiagnosticBuilder};
2525
use errors::emitter::Emitter;
2626
use syntax_pos::MultiSpan;
27+
use context::{is_pie_binary, get_reloc_model};
2728

2829
use std::collections::HashMap;
2930
use std::ffi::{CStr, CString};
@@ -154,32 +155,11 @@ fn get_llvm_opt_size(optimize: config::OptLevel) -> llvm::CodeGenOptSize {
154155
}
155156

156157
pub fn create_target_machine(sess: &Session) -> TargetMachineRef {
157-
let reloc_model_arg = match sess.opts.cg.relocation_model {
158-
Some(ref s) => &s[..],
159-
None => &sess.target.target.options.relocation_model[..],
160-
};
161-
let reloc_model = match reloc_model_arg {
162-
"pic" => llvm::RelocPIC,
163-
"static" => llvm::RelocStatic,
164-
"default" => llvm::RelocDefault,
165-
"dynamic-no-pic" => llvm::RelocDynamicNoPic,
166-
_ => {
167-
sess.err(&format!("{:?} is not a valid relocation mode",
168-
sess.opts
169-
.cg
170-
.relocation_model));
171-
sess.abort_if_errors();
172-
bug!();
173-
}
174-
};
158+
let reloc_model = get_reloc_model(sess);
175159

176160
let opt_level = get_llvm_opt_level(sess.opts.optimize);
177161
let use_softfp = sess.opts.cg.soft_float;
178162

179-
let any_library = sess.crate_types.borrow().iter().any(|ty| {
180-
*ty != config::CrateTypeExecutable
181-
});
182-
183163
let ffunction_sections = sess.target.target.options.function_sections;
184164
let fdata_sections = ffunction_sections;
185165

@@ -220,7 +200,7 @@ pub fn create_target_machine(sess: &Session) -> TargetMachineRef {
220200
reloc_model,
221201
opt_level,
222202
use_softfp,
223-
!any_library && reloc_model == llvm::RelocPIC,
203+
is_pie_binary(sess),
224204
ffunction_sections,
225205
fdata_sections,
226206
)

src/librustc_trans/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10831083
failure_order: AtomicOrdering,
10841084
weak: llvm::Bool) -> ValueRef {
10851085
unsafe {
1086-
llvm::LLVMBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src,
1086+
llvm::LLVMRustBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src,
10871087
order, failure_order, weak)
10881088
}
10891089
}

src/librustc_trans/context.rs

+60-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use rustc::ty::subst::{Substs, VecPerParamSpace};
3434
use rustc::ty::{self, Ty, TyCtxt};
3535
use session::config::NoDebugInfo;
3636
use session::Session;
37+
use session::config;
3738
use symbol_map::SymbolMap;
3839
use util::sha2::Sha256;
3940
use util::nodemap::{NodeMap, NodeSet, DefIdMap, FnvHashMap, FnvHashSet};
@@ -322,6 +323,38 @@ impl<'a, 'tcx> Iterator for CrateContextMaybeIterator<'a, 'tcx> {
322323
}
323324
}
324325

326+
pub fn get_reloc_model(sess: &Session) -> llvm::RelocMode {
327+
let reloc_model_arg = match sess.opts.cg.relocation_model {
328+
Some(ref s) => &s[..],
329+
None => &sess.target.target.options.relocation_model[..],
330+
};
331+
332+
match reloc_model_arg {
333+
"pic" => llvm::RelocPIC,
334+
"static" => llvm::RelocStatic,
335+
"default" => llvm::RelocDefault,
336+
"dynamic-no-pic" => llvm::RelocDynamicNoPic,
337+
_ => {
338+
sess.err(&format!("{:?} is not a valid relocation mode",
339+
sess.opts
340+
.cg
341+
.relocation_model));
342+
sess.abort_if_errors();
343+
bug!();
344+
}
345+
}
346+
}
347+
348+
fn is_any_library(sess: &Session) -> bool {
349+
sess.crate_types.borrow().iter().any(|ty| {
350+
*ty != config::CrateTypeExecutable
351+
})
352+
}
353+
354+
pub fn is_pie_binary(sess: &Session) -> bool {
355+
!is_any_library(sess) && get_reloc_model(sess) == llvm::RelocPIC
356+
}
357+
325358
unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
326359
let llcx = llvm::LLVMContextCreate();
327360
let mod_name = CString::new(mod_name).unwrap();
@@ -337,7 +370,25 @@ unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextR
337370
let data_layout = str::from_utf8(CStr::from_ptr(data_layout).to_bytes())
338371
.ok().expect("got a non-UTF8 data-layout from LLVM");
339372

340-
if sess.target.target.data_layout != data_layout {
373+
// Unfortunately LLVM target specs change over time, and right now we
374+
// don't have proper support to work with any more than one
375+
// `data_layout` than the one that is in the rust-lang/rust repo. If
376+
// this compiler is configured against a custom LLVM, we may have a
377+
// differing data layout, even though we should update our own to use
378+
// that one.
379+
//
380+
// As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
381+
// disable this check entirely as we may be configured with something
382+
// that has a different target layout.
383+
//
384+
// Unsure if this will actually cause breakage when rustc is configured
385+
// as such.
386+
//
387+
// FIXME(#34960)
388+
let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
389+
let custom_llvm_used = cfg_llvm_root.trim() != "";
390+
391+
if !custom_llvm_used && sess.target.target.data_layout != data_layout {
341392
bug!("data-layout for builtin `{}` target, `{}`, \
342393
differs from LLVM default, `{}`",
343394
sess.target.target.llvm_target,
@@ -352,6 +403,11 @@ unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextR
352403
let llvm_target = sess.target.target.llvm_target.as_bytes();
353404
let llvm_target = CString::new(llvm_target).unwrap();
354405
llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
406+
407+
if is_pie_binary(sess) {
408+
llvm::LLVMRustSetModulePIELevel(llmod);
409+
}
410+
355411
(llcx, llmod)
356412
}
357413

@@ -558,7 +614,9 @@ impl<'tcx> LocalCrateContext<'tcx> {
558614
&llmod_id[..]);
559615

560616
let dbg_cx = if shared.tcx.sess.opts.debuginfo != NoDebugInfo {
561-
Some(debuginfo::CrateDebugContext::new(llmod))
617+
let dctx = debuginfo::CrateDebugContext::new(llmod);
618+
debuginfo::metadata::compile_unit_metadata(shared, &dctx, shared.tcx.sess);
619+
Some(dctx)
562620
} else {
563621
None
564622
};

0 commit comments

Comments
 (0)