Skip to content

Commit

Permalink
llvm: simplify data layout check
Browse files Browse the repository at this point in the history
Don't skip the inconsistent data layout check for custom LLVMs.

With rust-lang#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>
  • Loading branch information
davidtwco committed Jan 17, 2024
1 parent 16f4b02 commit 96c48cd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 34 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_codegen_llvm/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ codegen_llvm_lto_dylib = lto cannot be used for `dylib` crate type without `-Zdy
codegen_llvm_lto_proc_macro = lto cannot be used for `proc-macro` crate type without `-Zdylib-lto`
codegen_llvm_mismatch_data_layout =
data-layout for target `{$rustc_target}`, `{$rustc_layout}`, differs from LLVM target's `{$llvm_target}` default layout, `{$llvm_layout}`
codegen_llvm_missing_features =
add the missing features in a `target_feature` attribute
Expand Down
38 changes: 9 additions & 29 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
use smallvec::SmallVec;

use libc::c_uint;
use std::borrow::Borrow;
use std::cell::{Cell, RefCell};
use std::ffi::CStr;
use std::str;
Expand Down Expand Up @@ -147,42 +148,21 @@ pub unsafe fn create_module<'ll>(
}

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

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

// Unfortunately LLVM target specs change over time, and right now we
// don't have proper support to work with any more than one
// `data_layout` than the one that is in the rust-lang/rust repo. If
// this compiler is configured against a custom LLVM, we may have a
// differing data layout, even though we should update our own to use
// that one.
//
// As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
// disable this check entirely as we may be configured with something
// that has a different target layout.
//
// Unsure if this will actually cause breakage when rustc is configured
// as such.
//
// FIXME(#34960)
let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
let custom_llvm_used = !cfg_llvm_root.trim().is_empty();

if !custom_llvm_used && target_data_layout != llvm_data_layout {
bug!(
"data-layout for target `{rustc_target}`, `{rustc_layout}`, \
differs from LLVM target's `{llvm_target}` default layout, `{llvm_layout}`",
rustc_target = sess.opts.target_triple,
rustc_layout = target_data_layout,
llvm_target = sess.target.llvm_target,
llvm_layout = llvm_data_layout
);
if target_data_layout != llvm_data_layout {
tcx.dcx().emit_err(crate::errors::MismatchedDataLayout {
rustc_target: sess.opts.target_triple.to_string().as_str(),
rustc_layout: target_data_layout.as_str(),
llvm_target: sess.target.llvm_target.borrow(),
llvm_layout: llvm_data_layout,
});
}
}

Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_codegen_llvm/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,12 @@ pub(crate) struct CopyBitcode {
pub struct UnknownCompression {
pub algorithm: &'static str,
}

#[derive(Diagnostic)]
#[diag(codegen_llvm_mismatch_data_layout)]
pub struct MismatchedDataLayout<'a> {
pub rustc_target: &'a str,
pub rustc_layout: &'a str,
pub llvm_target: &'a str,
pub llvm_layout: &'a str,
}
5 changes: 0 additions & 5 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,16 +1103,11 @@ pub fn rustc_cargo_env(
/// Pass down configuration from the LLVM build into the build of
/// rustc_llvm and rustc_codegen_llvm.
fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
let target_config = builder.config.target_config.get(&target);

if builder.is_rust_llvm(target) {
cargo.env("LLVM_RUSTLLVM", "1");
}
let llvm::LlvmResult { llvm_config, .. } = builder.ensure(llvm::Llvm { target });
cargo.env("LLVM_CONFIG", &llvm_config);
if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
cargo.env("CFG_LLVM_ROOT", s);
}

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

0 comments on commit 96c48cd

Please sign in to comment.