Skip to content

Commit 98c5078

Browse files
authored
Rollup merge of rust-lang#120062 - davidtwco:llvm-data-layout-check, r=wesleywiser
llvm: change data layout bug to an error and make it trigger more Fixes rust-lang#33446. Don't skip the inconsistent data layout check for custom LLVMs or non-built-in targets. With rust-lang#118708, all targets will have a simple test that would trigger this error if LLVM's data layouts do change - so data layouts would be corrected during the LLVM upgrade. Therefore, with builtin targets, this error won't happen with our LLVM because each target will have been confirmed to work. With non-builtin targets, this error is probably useful to have because you can change the data layout in your target and if it is 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. I'm not sure if this is something that people do and is okay, but I doubt it? `CFG_LLVM_ROOT` was also always set during local development with `download-ci-llvm` so this bug would never trigger locally. In rust-lang#33446, two points are raised: - In the issue itself, changing this from a `bug!` to a proper error is what is suggested, by using `isCompatibleDataLayout` from LLVM, but that function still just does the same thing that we do and check for equality, so I've avoided the additional code necessary to do that FFI call. - `@Mark-Simulacrum` suggests a different check is necessary to maintain backwards compatibility with old LLVM versions. I don't know how often this comes up, but we can do that with some simple string manipulation + LLVM version checks as happens already for LLVM 17 just above this diff.
2 parents 976692f + 46652dd commit 98c5078

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;
@@ -155,42 +156,21 @@ pub unsafe fn create_module<'ll>(
155156
}
156157

157158
// Ensure the data-layout values hardcoded remain the defaults.
158-
if sess.target.is_builtin {
159-
// tm is disposed by its drop impl
159+
{
160160
let tm = crate::back::write::create_informational_target_machine(tcx.sess);
161161
llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, &tm);
162162

163163
let llvm_data_layout = llvm::LLVMGetDataLayoutStr(llmod);
164164
let llvm_data_layout = str::from_utf8(CStr::from_ptr(llvm_data_layout).to_bytes())
165165
.expect("got a non-UTF8 data-layout from LLVM");
166166

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

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
@@ -1113,16 +1113,11 @@ pub fn rustc_cargo_env(
11131113
/// Pass down configuration from the LLVM build into the build of
11141114
/// rustc_llvm and rustc_codegen_llvm.
11151115
fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
1116-
let target_config = builder.config.target_config.get(&target);
1117-
11181116
if builder.is_rust_llvm(target) {
11191117
cargo.env("LLVM_RUSTLLVM", "1");
11201118
}
11211119
let llvm::LlvmResult { llvm_config, .. } = builder.ensure(llvm::Llvm { target });
11221120
cargo.env("LLVM_CONFIG", &llvm_config);
1123-
if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
1124-
cargo.env("CFG_LLVM_ROOT", s);
1125-
}
11261121

11271122
// Some LLVM linker flags (-L and -l) may be needed to link `rustc_llvm`. Its build script
11281123
// 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)