From adec1a2e84ba61356e6651f277e72452254699a4 Mon Sep 17 00:00:00 2001 From: kxxt Date: Tue, 9 Apr 2024 02:24:27 +0200 Subject: [PATCH] Convert tests/run-make/cross-lang-lto-riscv-abi to rmake --- src/tools/compiletest/src/header.rs | 1 + src/tools/run-make-support/src/rustc.rs | 12 +++ .../tidy/src/allowed_run_make_makefiles.txt | 1 - .../cross-lang-lto-riscv-abi/Makefile | 24 ------ .../cross-lang-lto-riscv-abi/rmake.rs | 74 +++++++++++++++++++ 5 files changed, 87 insertions(+), 25 deletions(-) delete mode 100644 tests/run-make/cross-lang-lto-riscv-abi/Makefile create mode 100644 tests/run-make/cross-lang-lto-riscv-abi/rmake.rs diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs index e414bc384f1..e832e214efc 100644 --- a/src/tools/compiletest/src/header.rs +++ b/src/tools/compiletest/src/header.rs @@ -819,6 +819,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[ "needs-dynamic-linking", "needs-git-hash", "needs-llvm-components", + "needs-matching-clang", "needs-profiler-support", "needs-relocation-model-pic", "needs-run-enabled", diff --git a/src/tools/run-make-support/src/rustc.rs b/src/tools/run-make-support/src/rustc.rs index 217da36ccc7..65a6d0d2740 100644 --- a/src/tools/run-make-support/src/rustc.rs +++ b/src/tools/run-make-support/src/rustc.rs @@ -86,6 +86,18 @@ impl Rustc { self } + /// This flag defers LTO optimizations to the linker. + pub fn linker_plugin_lto(&mut self, option: &str) -> &mut Self { + self.cmd.arg(format!("-Clinker-plugin-lto={option}")); + self + } + + /// Specify what happens when the code panics. + pub fn panic(&mut self, option: &str) -> &mut Self { + self.cmd.arg(format!("-Cpanic={option}")); + self + } + /// Specify number of codegen units pub fn codegen_units(&mut self, units: usize) -> &mut Self { self.cmd.arg(format!("-Ccodegen-units={units}")); diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 23ecf154334..dfd30d79abc 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -36,7 +36,6 @@ run-make/crate-hash-rustc-version/Makefile run-make/crate-name-priority/Makefile run-make/cross-lang-lto-clang/Makefile run-make/cross-lang-lto-pgo-smoketest/Makefile -run-make/cross-lang-lto-riscv-abi/Makefile run-make/cross-lang-lto-upstream-rlibs/Makefile run-make/cross-lang-lto/Makefile run-make/debug-assertions/Makefile diff --git a/tests/run-make/cross-lang-lto-riscv-abi/Makefile b/tests/run-make/cross-lang-lto-riscv-abi/Makefile deleted file mode 100644 index 5fab076bb1f..00000000000 --- a/tests/run-make/cross-lang-lto-riscv-abi/Makefile +++ /dev/null @@ -1,24 +0,0 @@ -# needs-matching-clang - -# This test makes sure that cross-language LTO works on riscv targets - -include ../tools.mk - -all: riscv64gc-unknown-linux-gnu riscv32imac-unknown-none-elf riscv32gc-unknown-linux-gnu - -define check-target = -@echo "Testing target $(1)" -$(RUSTC) --target $(1) -Clinker-plugin-lto=on -Cpanic=abort --crate-type=rlib -o $(TMPDIR)/libriscv-xlto.a ./riscv-xlto.rs -$(CLANG) -target $(2) -march=$(3) -mabi=$(4) -flto=thin -fuse-ld=lld -L $(TMPDIR) -lriscv-xlto -nostdlib -o $(TMPDIR)/riscv-xlto ./cstart.c -file $(TMPDIR)/riscv-xlto | $(CGREP) "$(5)" -endef - - -riscv64gc-unknown-linux-gnu: - @$(call check-target,$@,riscv64-linux-gnu,rv64gc,lp64d,double-float ABI) - -riscv32imac-unknown-none-elf: - @$(call check-target,$@,riscv32-unknown-elf,rv32imac,ilp32,soft-float ABI) - -riscv32gc-unknown-linux-gnu: - @$(call check-target,$@,riscv32-linux-gnu,rv32gc,ilp32d,double-float ABI) diff --git a/tests/run-make/cross-lang-lto-riscv-abi/rmake.rs b/tests/run-make/cross-lang-lto-riscv-abi/rmake.rs new file mode 100644 index 00000000000..2f13cf17169 --- /dev/null +++ b/tests/run-make/cross-lang-lto-riscv-abi/rmake.rs @@ -0,0 +1,74 @@ +//! Make sure that cross-language LTO works on riscv targets, +//! which requires extra abi metadata to be emitted. +//@ needs-matching-clang +//@ needs-llvm-components riscv +extern crate run_make_support; + +use run_make_support::{bin_name, rustc, tmp_dir}; +use std::{ + env, + path::PathBuf, + process::{Command, Output}, + str, +}; + +fn handle_failed_output(output: Output) { + eprintln!("output status: `{}`", output.status); + eprintln!("=== STDOUT ===\n{}\n\n", String::from_utf8(output.stdout).unwrap()); + eprintln!("=== STDERR ===\n{}\n\n", String::from_utf8(output.stderr).unwrap()); + std::process::exit(1) +} + +fn check_target(target: &str, clang_target: &str, carch: &str, is_double_float: bool) { + eprintln!("Checking target {target}"); + // Rust part + rustc() + .input("riscv-xlto.rs") + .crate_type("rlib") + .target(target) + .panic("abort") + .linker_plugin_lto("on") + .run(); + // C part + let clang = env::var("CLANG").unwrap(); + let mut cmd = Command::new(clang); + let executable = tmp_dir().join("riscv-xlto"); + cmd.arg("-target") + .arg(clang_target) + .arg(format!("-march={carch}")) + .arg(format!("-flto=thin")) + .arg(format!("-fuse-ld=lld")) + .arg("-nostdlib") + .arg("-o") + .arg(&executable) + .arg("cstart.c") + .arg(tmp_dir().join("libriscv_xlto.rlib")); + eprintln!("{cmd:?}"); + let output = cmd.output().unwrap(); + if !output.status.success() { + handle_failed_output(output); + } + // Check that the built binary has correct float abi + let llvm_readobj = + PathBuf::from(env::var("LLVM_BIN_DIR").unwrap()).join(bin_name("llvm-readobj")); + let mut cmd = Command::new(llvm_readobj); + cmd.arg("--file-header").arg(executable); + eprintln!("{cmd:?}"); + let output = cmd.output().unwrap(); + if output.status.success() { + assert!( + !(is_double_float + ^ dbg!(str::from_utf8(&output.stdout).unwrap()) + .contains("EF_RISCV_FLOAT_ABI_DOUBLE")) + ) + } else { + handle_failed_output(output); + } +} + +fn main() { + check_target("riscv64gc-unknown-linux-gnu", "riscv64-linux-gnu", "rv64gc", true); + check_target("riscv64imac-unknown-none-elf", "riscv64-unknown-elf", "rv64imac", false); + check_target("riscv32imac-unknown-none-elf", "riscv32-unknown-elf", "rv32imac", false); + check_target("riscv32gc-unknown-linux-gnu", "riscv32-linux-gnu", "rv32gc", true); +}