Skip to content

Commit

Permalink
Auto merge of rust-lang#117313 - GuillaumeGomez:cg_gcc-tests, r=onur-…
Browse files Browse the repository at this point in the history
…ozkan

Run part of `rustc_codegen_gcc`'s tests in CI

Thanks to rust-lang#112701 and `@bjorn3,` it made this much easier.

Also cc `@antoyo.`

r? `@bjorn3`
  • Loading branch information
bors committed Nov 3, 2023
2 parents 2520ca8 + c890dd6 commit 6b9d6de
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ jobs:
os: ubuntu-20.04-4core-16gb
env: {}
- name: x86_64-gnu-llvm-15
env:
ENABLE_GCC_CODEGEN: "1"
os: ubuntu-20.04-16core-64gb
env: {}
- name: x86_64-gnu-tools
os: ubuntu-20.04-16core-64gb
env: {}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_codegen_gcc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ path = "tests/lang_tests_release.rs"
harness = false

[features]
default = ["master"]
master = ["gccjit/master"]

[dependencies]
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_codegen_gcc/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ else
exit 1
fi

HOST_TRIPLE=$(rustc -vV | grep host | cut -d: -f2 | tr -d " ")
HOST_TRIPLE=$($RUSTC -vV | grep host | cut -d: -f2 | tr -d " ")
# TODO: remove $OVERWRITE_TARGET_TRIPLE when config.sh is removed.
TARGET_TRIPLE="${OVERWRITE_TARGET_TRIPLE:-$HOST_TRIPLE}"

Expand Down Expand Up @@ -54,6 +54,10 @@ if [[ -z "$BUILTIN_BACKEND" ]]; then
export RUSTFLAGS="$CG_RUSTFLAGS $linker -Csymbol-mangling-version=v0 -Cdebuginfo=2 $disable_lto_flags -Zcodegen-backend=$(pwd)/target/${CHANNEL:-debug}/librustc_codegen_gcc.$dylib_ext --sysroot $(pwd)/build_sysroot/sysroot $TEST_FLAGS"
else
export RUSTFLAGS="$CG_RUSTFLAGS $linker -Csymbol-mangling-version=v0 -Cdebuginfo=2 $disable_lto_flags -Zcodegen-backend=gcc $TEST_FLAGS -Cpanic=abort"

if [[ ! -z "$RUSTC_SYSROOT" ]]; then
export RUSTFLAGS="$RUSTFLAGS --sysroot $RUSTC_SYSROOT"
fi
fi

# FIXME(antoyo): remove once the atomic shim is gone
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/example/alloc_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern "C" {
}

#[panic_handler]
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
fn panic_handler(_: &core::panic::PanicInfo<'_>) -> ! {
core::intrinsics::abort();
}

Expand Down
18 changes: 14 additions & 4 deletions compiler/rustc_codegen_gcc/example/alloc_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#![no_std]
#![feature(allocator_api, rustc_private)]
#![cfg_attr(any(unix, target_os = "redox"), feature(libc))]

// The minimum alignment guaranteed by the architecture. This value is used to
// add fast paths for low alignment values.
Expand Down Expand Up @@ -48,7 +47,18 @@ mod realloc_fallback {
}
#[cfg(any(unix, target_os = "redox"))]
mod platform {
extern crate libc;
mod libc {
use core::ffi::{c_void, c_int};

#[link(name = "c")]
extern "C" {
pub fn malloc(size: usize) -> *mut c_void;
pub fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void;
pub fn calloc(nmemb: usize, size: usize) -> *mut c_void;
pub fn free(ptr: *mut u8);
pub fn posix_memalign(memptr: *mut *mut c_void, alignment: usize, size: usize) -> c_int;
}
}
use core::ptr;
use MIN_ALIGN;
use System;
Expand Down Expand Up @@ -82,12 +92,12 @@ mod platform {
}
#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
libc::free(ptr as *mut libc::c_void)
libc::free(ptr as *mut _)
}
#[inline]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
libc::realloc(ptr as *mut _, new_size) as *mut u8
} else {
self.realloc_fallback(ptr, layout, new_size)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/example/mod_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extern {}

#[panic_handler]
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
fn panic_handler(_: &core::panic::PanicInfo<'_>) -> ! {
core::intrinsics::abort();
}

Expand Down
1 change: 1 addition & 0 deletions src/bootstrap/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def v(*args):
o("use-libcxx", "llvm.use-libcxx", "build LLVM with libc++")
o("control-flow-guard", "rust.control-flow-guard", "Enable Control Flow Guard")
o("patch-binaries-for-nix", "build.patch-binaries-for-nix", "whether patch binaries for usage with Nix toolchains")
o("new-symbol-mangling", "rust.new-symbol-mangling", "use symbol-mangling-version v0")

v("llvm-cflags", "llvm.cflags", "build LLVM with these extra compiler flags")
v("llvm-cxxflags", "llvm.cxxflags", "build LLVM with these extra compiler flags")
Expand Down
35 changes: 33 additions & 2 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,42 @@ pub struct Std {
/// When using download-rustc, we need to use a new build of `std` for running unit tests of Std itself,
/// but we need to use the downloaded copy of std for linking to rustdoc. Allow this to be overriden by `builder.ensure` from other steps.
force_recompile: bool,
extra_rust_args: &'static [&'static str],
}

impl Std {
pub fn new(compiler: Compiler, target: TargetSelection) -> Self {
Self { target, compiler, crates: Default::default(), force_recompile: false }
Self {
target,
compiler,
crates: Default::default(),
force_recompile: false,
extra_rust_args: &[],
}
}

pub fn force_recompile(compiler: Compiler, target: TargetSelection) -> Self {
Self { target, compiler, crates: Default::default(), force_recompile: true }
Self {
target,
compiler,
crates: Default::default(),
force_recompile: true,
extra_rust_args: &[],
}
}

pub fn new_with_extra_rust_args(
compiler: Compiler,
target: TargetSelection,
extra_rust_args: &'static [&'static str],
) -> Self {
Self {
target,
compiler,
crates: Default::default(),
force_recompile: false,
extra_rust_args,
}
}
}

Expand Down Expand Up @@ -81,6 +108,7 @@ impl Step for Std {
target: run.target,
crates,
force_recompile: false,
extra_rust_args: &[],
});
}

Expand Down Expand Up @@ -188,6 +216,9 @@ impl Step for Std {
if target.is_synthetic() {
cargo.env("RUSTC_BOOTSTRAP_SYNTHETIC_TARGET", "1");
}
for rustflag in self.extra_rust_args.into_iter() {
cargo.rustflag(rustflag);
}

let _guard = builder.msg(
Kind::Build,
Expand Down
121 changes: 121 additions & 0 deletions src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3063,6 +3063,7 @@ impl Step for CodegenCranelift {
// FIXME handle vendoring for source tarballs before removing the --skip-test below
let download_dir = builder.out.join("cg_clif_download");

// FIXME: Uncomment the `prepare` command below once vendoring is implemented.
/*
let mut prepare_cargo = build_cargo();
prepare_cargo.arg("--").arg("prepare").arg("--download-dir").arg(&download_dir);
Expand Down Expand Up @@ -3094,3 +3095,123 @@ impl Step for CodegenCranelift {
builder.run_cmd(BootstrapCommand::from(&mut cmd).fail_fast());
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CodegenGCC {
compiler: Compiler,
target: TargetSelection,
}

impl Step for CodegenGCC {
type Output = ();
const DEFAULT: bool = true;
const ONLY_HOSTS: bool = true;

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.paths(&["compiler/rustc_codegen_gcc"])
}

fn make_run(run: RunConfig<'_>) {
let builder = run.builder;
let host = run.build_triple();
let compiler = run.builder.compiler_for(run.builder.top_stage, host, host);

if builder.doc_tests == DocTests::Only {
return;
}

let triple = run.target.triple;
let target_supported =
if triple.contains("linux") { triple.contains("x86_64") } else { false };
if !target_supported {
builder.info("target not supported by rustc_codegen_gcc. skipping");
return;
}

if builder.remote_tested(run.target) {
builder.info("remote testing is not supported by rustc_codegen_gcc. skipping");
return;
}

if !builder.config.rust_codegen_backends.contains(&INTERNER.intern_str("gcc")) {
builder.info("gcc not in rust.codegen-backends. skipping");
return;
}

builder.ensure(CodegenGCC { compiler, target: run.target });
}

fn run(self, builder: &Builder<'_>) {
let compiler = self.compiler;
let target = self.target;

builder.ensure(compile::Std::new_with_extra_rust_args(
compiler,
target,
&["-Csymbol-mangling-version=v0", "-Cpanic=abort"],
));

// If we're not doing a full bootstrap but we're testing a stage2
// version of libstd, then what we're actually testing is the libstd
// produced in stage1. Reflect that here by updating the compiler that
// we're working with automatically.
let compiler = builder.compiler_for(compiler.stage, compiler.host, target);

let build_cargo = || {
let mut cargo = builder.cargo(
compiler,
Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works
SourceType::InTree,
target,
"run",
);
cargo.current_dir(&builder.src.join("compiler/rustc_codegen_gcc"));
cargo
.arg("--manifest-path")
.arg(builder.src.join("compiler/rustc_codegen_gcc/build_system/Cargo.toml"));
compile::rustc_cargo_env(builder, &mut cargo, target, compiler.stage);

// Avoid incremental cache issues when changing rustc
cargo.env("CARGO_BUILD_INCREMENTAL", "false");
cargo.rustflag("-Cpanic=abort");

cargo
};

builder.info(&format!(
"{} GCC stage{} ({} -> {})",
Kind::Test.description(),
compiler.stage,
&compiler.host,
target
));
let _time = helpers::timeit(&builder);

// FIXME: Uncomment the `prepare` command below once vendoring is implemented.
/*
let mut prepare_cargo = build_cargo();
prepare_cargo.arg("--").arg("prepare");
#[allow(deprecated)]
builder.config.try_run(&mut prepare_cargo.into()).unwrap();
*/

let mut cargo = build_cargo();

cargo
.arg("--")
.arg("test")
.arg("--use-system-gcc")
.arg("--use-backend")
.arg("gcc")
.arg("--out-dir")
.arg(builder.stage_out(compiler, Mode::ToolRustc).join("cg_gcc"))
.arg("--release")
.arg("--no-default-features")
.arg("--mini-tests")
.arg("--std-tests");
cargo.args(builder.config.test_args());

let mut cmd: Command = cargo.into();
builder.run_cmd(BootstrapCommand::from(&mut cmd).fail_fast());
}
}
1 change: 1 addition & 0 deletions src/bootstrap/src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ impl<'a> Builder<'a> {
test::Debuginfo,
test::UiFullDeps,
test::CodegenCranelift,
test::CodegenGCC,
test::Rustdoc,
test::RunCoverageRustdoc,
test::Pretty,
Expand Down
5 changes: 5 additions & 0 deletions src/ci/docker/host-x86_64/x86_64-gnu-llvm-15/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
xz-utils \
nodejs \
mingw-w64 \
libgccjit-12-dev \
&& rm -rf /var/lib/apt/lists/*

# Install powershell (universal package) so we can test x.ps1 on Linux
Expand All @@ -34,6 +35,9 @@ RUN curl -sL "https://github.com/PowerShell/PowerShell/releases/download/v7.3.1/
COPY scripts/sccache.sh /scripts/
RUN sh /scripts/sccache.sh

# Make `libgccjit.so` accessible to the linker.
RUN ln -s /usr/lib/gcc/x86_64-linux-gnu/12/libgccjit.so /usr/lib/x86_64-linux-gnu/libgccjit.so

# We are disabling CI LLVM since this builder is intentionally using a host
# LLVM, rather than the typical src/llvm-project LLVM.
ENV NO_DOWNLOAD_CI_LLVM 1
Expand All @@ -47,6 +51,7 @@ ENV RUST_CONFIGURE_ARGS \
--build=x86_64-unknown-linux-gnu \
--llvm-root=/usr/lib/llvm-15 \
--enable-llvm-link-shared \
$USE_NEW_MANGLING \
--set rust.thin-lto-import-instr-limit=10

COPY host-x86_64/x86_64-gnu-llvm-15/script.sh /tmp/
Expand Down
16 changes: 14 additions & 2 deletions src/ci/docker/host-x86_64/x86_64-gnu-llvm-15/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ set -ex

# Only run the stage 1 tests on merges, not on PR CI jobs.
if [[ -z "${PR_CI_JOB}" ]]; then
../x.py --stage 1 test --skip src/tools/tidy
# When running gcc backend tests, we need to install `libgccjit` and to not run llvm codegen
# tests as it will fail them.
if [[ "${ENABLE_GCC_CODEGEN}" == "1" ]]; then
../x.py --stage 1 test --skip src/tools/tidy --skip tests/codegen
else
../x.py --stage 1 test --skip src/tools/tidy
fi

# Run the `mir-opt` tests again but this time for a 32-bit target.
# This enforces that tests using `// EMIT_MIR_FOR_EACH_BIT_WIDTH` have
Expand All @@ -19,8 +25,14 @@ if [[ -z "${PR_CI_JOB}" ]]; then
../x.py --stage 1 test tests/ui-fulldeps
fi

# When running gcc backend tests, we need to install `libgccjit` and to not run llvm codegen
# tests as it will fail them.
# NOTE: intentionally uses all of `x.py`, `x`, and `x.ps1` to make sure they all work on Linux.
../x.py --stage 2 test --skip src/tools/tidy
if [[ "${ENABLE_GCC_CODEGEN}" == "1" ]]; then
../x.py --stage 2 test --skip src/tools/tidy --skip tests/codegen
else
../x.py --stage 2 test --skip src/tools/tidy
fi

# Run the `mir-opt` tests again but this time for a 32-bit target.
# This enforces that tests using `// EMIT_MIR_FOR_EACH_BIT_WIDTH` have
Expand Down
9 changes: 8 additions & 1 deletion src/ci/docker/host-x86_64/x86_64-gnu-tools/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
sudo \
xz-utils \
tidy \
libgccjit-12-dev \
\
# Install dependencies for chromium browser
gconf-service \
Expand Down Expand Up @@ -61,6 +62,11 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
COPY scripts/sccache.sh /scripts/
RUN sh /scripts/sccache.sh

# Make `libgccjit.so` accessible.
RUN ln -s /usr/lib/gcc/x86_64-linux-gnu/12/libgccjit.so /usr/lib/x86_64-linux-gnu/libgccjit.so
# Fix rustc_codegen_gcc lto issues.
ENV GCC_EXEC_PREFIX="/usr/lib/gcc/"

COPY host-x86_64/x86_64-gnu-tools/checktools.sh /tmp/

RUN curl -sL https://nodejs.org/dist/v14.20.0/node-v14.20.0-linux-x64.tar.xz | tar -xJ
Expand All @@ -81,7 +87,8 @@ RUN npm install -g browser-ui-test@$(head -n 1 /tmp/browser-ui-test.version) --u

ENV RUST_CONFIGURE_ARGS \
--build=x86_64-unknown-linux-gnu \
--save-toolstates=/tmp/toolstate/toolstates.json
--save-toolstates=/tmp/toolstate/toolstates.json \
--enable-new-symbol-mangling

ENV HOST_TARGET x86_64-unknown-linux-gnu

Expand Down
Loading

0 comments on commit 6b9d6de

Please sign in to comment.