Skip to content

Commit 46652dd

Browse files
committed
llvm: simplify data layout check
Don't skip the inconsistent data layout check for custom LLVMs. With #118708, all targets will have a simple test that would trigger this check if LLVM's data layouts do change - so data layouts would be corrected during the LLVM upgrade. Therefore, with builtin targets, this check won't trigger with our LLVM because each target will have been confirmed to work. With non-builtin targets, this check is probably useful to have because you can change the data layout in your target and if its wrong then that could lead to bugs. When using a custom LLVM, the same justification makes sense for non-builtin targets as with our LLVM, the user can update their target to match their LLVM and that's probably a good thing to do. However, with a custom LLVM, the user cannot change the builtin target data layouts if they don't match - though given that the compiler's data layout is used for layout computation and a bunch of other things - you could get some bugs because of the mismatch and probably want to know about that. `CFG_LLVM_ROOT` was also always set during local development with `download-ci-llvm` so this bug would never trigger locally. Signed-off-by: David Wood <david@davidtw.co>
1 parent c485ee7 commit 46652dd

File tree

9 files changed

+57
-38
lines changed

9 files changed

+57
-38
lines changed

compiler/rustc_codegen_llvm/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ codegen_llvm_lto_dylib = lto cannot be used for `dylib` crate type without `-Zdy
3939
4040
codegen_llvm_lto_proc_macro = lto cannot be used for `proc-macro` crate type without `-Zdylib-lto`
4141
42+
codegen_llvm_mismatch_data_layout =
43+
data-layout for target `{$rustc_target}`, `{$rustc_layout}`, differs from LLVM target's `{$llvm_target}` default layout, `{$llvm_layout}`
44+
4245
codegen_llvm_missing_features =
4346
add the missing features in a `target_feature` attribute
4447

compiler/rustc_codegen_llvm/src/context.rs

+9-29
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
3434
use smallvec::SmallVec;
3535

3636
use libc::c_uint;
37+
use std::borrow::Borrow;
3738
use std::cell::{Cell, RefCell};
3839
use std::ffi::CStr;
3940
use std::str;
@@ -147,42 +148,21 @@ pub unsafe fn create_module<'ll>(
147148
}
148149

149150
// Ensure the data-layout values hardcoded remain the defaults.
150-
if sess.target.is_builtin {
151-
// tm is disposed by its drop impl
151+
{
152152
let tm = crate::back::write::create_informational_target_machine(tcx.sess);
153153
llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, &tm);
154154

155155
let llvm_data_layout = llvm::LLVMGetDataLayoutStr(llmod);
156156
let llvm_data_layout = str::from_utf8(CStr::from_ptr(llvm_data_layout).to_bytes())
157157
.expect("got a non-UTF8 data-layout from LLVM");
158158

159-
// Unfortunately LLVM target specs change over time, and right now we
160-
// don't have proper support to work with any more than one
161-
// `data_layout` than the one that is in the rust-lang/rust repo. If
162-
// this compiler is configured against a custom LLVM, we may have a
163-
// differing data layout, even though we should update our own to use
164-
// that one.
165-
//
166-
// As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
167-
// disable this check entirely as we may be configured with something
168-
// that has a different target layout.
169-
//
170-
// Unsure if this will actually cause breakage when rustc is configured
171-
// as such.
172-
//
173-
// FIXME(#34960)
174-
let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
175-
let custom_llvm_used = !cfg_llvm_root.trim().is_empty();
176-
177-
if !custom_llvm_used && target_data_layout != llvm_data_layout {
178-
bug!(
179-
"data-layout for target `{rustc_target}`, `{rustc_layout}`, \
180-
differs from LLVM target's `{llvm_target}` default layout, `{llvm_layout}`",
181-
rustc_target = sess.opts.target_triple,
182-
rustc_layout = target_data_layout,
183-
llvm_target = sess.target.llvm_target,
184-
llvm_layout = llvm_data_layout
185-
);
159+
if target_data_layout != llvm_data_layout {
160+
tcx.dcx().emit_err(crate::errors::MismatchedDataLayout {
161+
rustc_target: sess.opts.target_triple.to_string().as_str(),
162+
rustc_layout: target_data_layout.as_str(),
163+
llvm_target: sess.target.llvm_target.borrow(),
164+
llvm_layout: llvm_data_layout,
165+
});
186166
}
187167
}
188168

compiler/rustc_codegen_llvm/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,12 @@ pub(crate) struct CopyBitcode {
244244
pub struct UnknownCompression {
245245
pub algorithm: &'static str,
246246
}
247+
248+
#[derive(Diagnostic)]
249+
#[diag(codegen_llvm_mismatch_data_layout)]
250+
pub struct MismatchedDataLayout<'a> {
251+
pub rustc_target: &'a str,
252+
pub rustc_layout: &'a str,
253+
pub llvm_target: &'a str,
254+
pub llvm_layout: &'a str,
255+
}

src/bootstrap/src/core/build_steps/compile.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1103,16 +1103,11 @@ pub fn rustc_cargo_env(
11031103
/// Pass down configuration from the LLVM build into the build of
11041104
/// rustc_llvm and rustc_codegen_llvm.
11051105
fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
1106-
let target_config = builder.config.target_config.get(&target);
1107-
11081106
if builder.is_rust_llvm(target) {
11091107
cargo.env("LLVM_RUSTLLVM", "1");
11101108
}
11111109
let llvm::LlvmResult { llvm_config, .. } = builder.ensure(llvm::Llvm { target });
11121110
cargo.env("LLVM_CONFIG", &llvm_config);
1113-
if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
1114-
cargo.env("CFG_LLVM_ROOT", s);
1115-
}
11161111

11171112
// Some LLVM linker flags (-L and -l) may be needed to link `rustc_llvm`. Its build script
11181113
// expects these to be passed via the `LLVM_LINKER_FLAGS` env variable, separated by

src/tools/tidy/src/ui_tests.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
2424

2525
const EXTENSION_EXCEPTION_PATHS: &[&str] = &[
2626
"tests/ui/asm/named-asm-labels.s", // loading an external asm file to test named labels lint
27-
"tests/ui/check-cfg/my-awesome-platform.json", // testing custom targets with cfgs
28-
"tests/ui/commandline-argfile-badutf8.args", // passing args via a file
29-
"tests/ui/commandline-argfile.args", // passing args via a file
27+
"tests/ui/codegen/mismatched-data-layout.json", // testing mismatched data layout w/ custom targets
28+
"tests/ui/check-cfg/my-awesome-platform.json", // testing custom targets with cfgs
29+
"tests/ui/commandline-argfile-badutf8.args", // passing args via a file
30+
"tests/ui/commandline-argfile.args", // passing args via a file
3031
"tests/ui/crate-loading/auxiliary/libfoo.rlib", // testing loading a manually created rlib
3132
"tests/ui/include-macros/data.bin", // testing including data with the include macros
3233
"tests/ui/include-macros/file.txt", // testing including data with the include macros

tests/run-make/target-specs/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ all:
99
$(RUSTC) -Z unstable-options --target=my-awesome-platform.json --print target-spec-json > $(TMPDIR)/test-platform.json && $(RUSTC) -Z unstable-options --target=$(TMPDIR)/test-platform.json --print target-spec-json | diff -q $(TMPDIR)/test-platform.json -
1010
$(RUSTC) foo.rs --target=definitely-not-builtin-target 2>&1 | $(CGREP) 'may not set is_builtin'
1111
$(RUSTC) foo.rs --target=endianness-mismatch 2>&1 | $(CGREP) '"data-layout" claims architecture is little-endian'
12-
$(RUSTC) foo.rs --target=mismatching-data-layout --crate-type=lib
12+
$(RUSTC) foo.rs --target=mismatching-data-layout --crate-type=lib 2>&1 | $(CGREP) 'data-layout for target'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"llvm-target": "x86_64-unknown-none-gnu",
3+
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
4+
"arch": "x86_64",
5+
"target-endian": "little",
6+
"target-pointer-width": "64",
7+
"target-c-int-width": "32",
8+
"os": "unknown",
9+
"linker-flavor": "ld.lld",
10+
"linker": "rust-lld",
11+
"executables": true
12+
}
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This test checks that data layout mismatches emit an error.
2+
//
3+
// build-fail
4+
// needs-llvm-components: x86
5+
// compile-flags: --crate-type=lib --target={{src-base}}/codegen/mismatched-data-layout.json -Z unstable-options
6+
// error-pattern: differs from LLVM target's
7+
// normalize-stderr-test: "`, `[A-Za-z0-9-:]*`" -> "`, `normalized data layout`"
8+
// normalize-stderr-test: "layout, `[A-Za-z0-9-:]*`" -> "layout, `normalized data layout`"
9+
10+
#![feature(lang_items, no_core, auto_traits)]
11+
#![no_core]
12+
13+
#[lang = "sized"]
14+
trait Sized {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: data-layout for target `mismatched-data-layout-7814813422914914169`, `normalized data layout`, differs from LLVM target's `x86_64-unknown-none-gnu` default layout, `normalized data layout`
2+
3+
error: aborting due to 1 previous error
4+

0 commit comments

Comments
 (0)