Skip to content

Fix compilation for libgccjit12 #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- { gcc: "libgccjit.so", extra: "", env_extra: "", artifacts_branch: "master" }
- { gcc: "libgccjit_without_int128.so", extra: "", env_extra: "", artifacts_branch: "master-without-128bit-integers" }
- { gcc: "libgccjit12.so", extra: "--no-default-features", env_extra: "TEST_FLAGS='-Cpanic=abort -Zpanic-abort-tests'", artifacts_branch: "gcc12" }
#- { gcc: "libgccjit12.so", extra: "--no-default-features", env_extra: "TEST_FLAGS='-Cpanic=abort -Zpanic-abort-tests' CG_RUSTFLAGS='-Clink-arg=-fno-use-linker-plugin'", artifacts_branch: "gcc12" }
commands: [
"--mini-tests",
"--std-tests",
Expand All @@ -46,7 +47,9 @@ jobs:

- name: Install packages
# `llvm-14-tools` is needed to install the `FileCheck` binary which is used for asm tests.
run: sudo apt-get install ninja-build ripgrep llvm-14-tools
run: |
sudo apt-get update
sudo apt-get install ninja-build ripgrep llvm-14-tools

- name: Install libgccjit12
if: matrix.libgccjit_version.gcc == 'libgccjit12.so'
Expand Down
2 changes: 1 addition & 1 deletion build_sysroot/build_sysroot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if [[ "$1" == "--release" ]]; then
RUSTFLAGS="$RUSTFLAGS -Zmir-opt-level=3" cargo build --target $TARGET_TRIPLE --release
else
sysroot_channel='debug'
cargo build --target $TARGET_TRIPLE --features compiler_builtins/c
GCC_EXEC_PREFIX=/usr/lib/gcc/x86_64-linux-gnu/ cargo build --target $TARGET_TRIPLE --features compiler_builtins/c
fi

# Copy files to sysroot
Expand Down
8 changes: 7 additions & 1 deletion src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use std::time::Instant;
use gccjit::{
Context,
FunctionType,
GlobalKind, TargetInfo,
GlobalKind,
};
#[cfg(feature="master")]
use gccjit::TargetInfo;
use rustc_middle::dep_graph;
use rustc_middle::ty::TyCtxt;
#[cfg(feature="master")]
Expand All @@ -21,6 +23,8 @@ use rustc_session::config::DebugInfo;
use rustc_span::Symbol;

use crate::GccContext;
#[cfg(not(feature="master"))]
use crate::TargetInfo;
use crate::builder::Builder;
use crate::context::CodegenCx;

Expand Down Expand Up @@ -89,6 +93,8 @@ pub fn compile_codegen_unit(tcx: TyCtxt<'_>, cgu_name: Symbol, target_info: Arc<
// Instantiate monomorphizations without filling out definitions yet...
//let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str());
let context = Context::default();
//context.add_driver_option("-v");
//context.add_driver_option("-fno-use-linker-plugin");

context.add_command_line_option("-fexceptions");
context.add_driver_option("-fexceptions");
Expand Down
2 changes: 1 addition & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
}

#[cfg(not(feature="master"))]
fn invoke(&mut self, typ: Type<'gcc>, fn_attrs: &CodegenFnAttrs, fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
fn invoke(&mut self, typ: Type<'gcc>, fn_attrs: Option<&CodegenFnAttrs>, fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
let call_site = self.call(typ, fn_attrs, None, func, args, None);
let condition = self.context.new_rvalue_from_int(self.bool_type, 1);
self.llbb().end_with_conditional(None, condition, then, catch);
Expand Down
69 changes: 51 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_target;
extern crate tempfile;

// This prevents duplicating functions and statics that are already part of the host rustc process.
#[allow(unused_extern_crates)]
Expand Down Expand Up @@ -66,7 +67,9 @@ use std::any::Any;
use std::sync::Arc;

use crate::errors::LTONotSupported;
use gccjit::{Context, OptimizationLevel, TargetInfo};
use gccjit::{Context, OptimizationLevel};
#[cfg(feature="master")]
use gccjit::TargetInfo;
use rustc_ast::expand::allocator::AllocatorKind;
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen};
use rustc_codegen_ssa::base::codegen_crate;
Expand Down Expand Up @@ -259,17 +262,54 @@ impl WriteBackendMethods for GccCodegenBackend {
}
}

#[cfg(not(feature="master"))]
#[derive(Debug)]
pub struct TargetInfo {
supports_128bit_integers: bool,
}

#[cfg(not(feature="master"))]
impl TargetInfo {
fn cpu_supports(&self, _feature: &str) -> bool {
false
}

fn supports_128bit_int(&self) -> bool {
self.supports_128bit_integers
}
}

/// This is the entrypoint for a hot plugged rustc_codegen_gccjit
#[no_mangle]
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
// Get the native arch and check whether the target supports 128-bit integers.
let context = Context::default();
let arch = context.get_target_info().arch().unwrap();

// Get the second TargetInfo with the correct CPU features by setting the arch.
let context = Context::default();
context.add_driver_option(&format!("-march={}", arch.to_str().unwrap()));
let target_info = Arc::new(context.get_target_info());
#[cfg(feature="master")]
let target_info = {
// Get the native arch and check whether the target supports 128-bit integers.
let context = Context::default();
let arch = context.get_target_info().arch().unwrap();

// Get the second TargetInfo with the correct CPU features by setting the arch.
let context = Context::default();
context.add_driver_option(&format!("-march={}", arch.to_str().unwrap()));
Arc::new(context.get_target_info())
};

#[cfg(not(feature="master"))]
let target_info = {
use gccjit::CType;
use tempfile::TempDir;

let temp_dir = TempDir::new().expect("cannot create temporary directory");
let temp_file = temp_dir.into_path().join("result.asm");
let check_context = Context::default();
check_context.set_print_errors_to_stderr(false);
let _int128_ty = check_context.new_c_type(CType::UInt128t);
// NOTE: we cannot just call compile() as this would require other files than libgccjit.so.
check_context.compile_to_file(gccjit::OutputKind::Assembler, temp_file.to_str().expect("path to str"));
Arc::new(TargetInfo {
supports_128bit_integers: check_context.get_last_error() == Ok(None),
})
};

Box::new(GccCodegenBackend {
target_info,
Expand Down Expand Up @@ -314,15 +354,8 @@ pub fn target_features(sess: &Session, allow_unstable: bool, target_info: &Arc<T
if sess.is_nightly_build() || allow_unstable || gate.is_none() { Some(feature) } else { None }
},
)
.filter(|_feature| {
#[cfg(feature="master")]
{
target_info.cpu_supports(_feature)
}
#[cfg(not(feature="master"))]
{
false
}
.filter(|feature| {
target_info.cpu_supports(feature)
/*
adx, aes, avx, avx2, avx512bf16, avx512bitalg, avx512bw, avx512cd, avx512dq, avx512er, avx512f, avx512ifma,
avx512pf, avx512vbmi, avx512vbmi2, avx512vl, avx512vnni, avx512vp2intersect, avx512vpopcntdq,
Expand Down