Skip to content

Commit b8f4a4a

Browse files
committed
Fix compilation for libgccjit12
1 parent c4e86b6 commit b8f4a4a

File tree

5 files changed

+64
-22
lines changed

5 files changed

+64
-22
lines changed

.github/workflows/ci.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
- { gcc: "libgccjit.so", extra: "", env_extra: "", artifacts_branch: "master" }
2323
- { gcc: "libgccjit_without_int128.so", extra: "", env_extra: "", artifacts_branch: "master-without-128bit-integers" }
2424
- { gcc: "libgccjit12.so", extra: "--no-default-features", env_extra: "TEST_FLAGS='-Cpanic=abort -Zpanic-abort-tests'", artifacts_branch: "gcc12" }
25+
#- { 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" }
2526
commands: [
2627
"--mini-tests",
2728
"--std-tests",
@@ -46,7 +47,9 @@ jobs:
4647

4748
- name: Install packages
4849
# `llvm-14-tools` is needed to install the `FileCheck` binary which is used for asm tests.
49-
run: sudo apt-get install ninja-build ripgrep llvm-14-tools
50+
run: |
51+
sudo apt-get update
52+
sudo apt-get install ninja-build ripgrep llvm-14-tools
5053
5154
- name: Install libgccjit12
5255
if: matrix.libgccjit_version.gcc == 'libgccjit12.so'

build_sysroot/build_sysroot.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if [[ "$1" == "--release" ]]; then
2222
RUSTFLAGS="$RUSTFLAGS -Zmir-opt-level=3" cargo build --target $TARGET_TRIPLE --release
2323
else
2424
sysroot_channel='debug'
25-
cargo build --target $TARGET_TRIPLE --features compiler_builtins/c
25+
GCC_EXEC_PREFIX=/usr/lib/gcc/x86_64-linux-gnu/ strace -f cargo build --target $TARGET_TRIPLE --features compiler_builtins/c
2626
fi
2727

2828
# Copy files to sysroot

src/base.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ use std::time::Instant;
66
use gccjit::{
77
Context,
88
FunctionType,
9-
GlobalKind, TargetInfo,
9+
GlobalKind,
1010
};
11+
#[cfg(feature="master")]
12+
use gccjit::TargetInfo;
1113
use rustc_middle::dep_graph;
1214
use rustc_middle::ty::TyCtxt;
1315
#[cfg(feature="master")]
@@ -21,6 +23,8 @@ use rustc_session::config::DebugInfo;
2123
use rustc_span::Symbol;
2224

2325
use crate::GccContext;
26+
#[cfg(not(feature="master"))]
27+
use crate::TargetInfo;
2428
use crate::builder::Builder;
2529
use crate::context::CodegenCx;
2630

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

9399
context.add_command_line_option("-fexceptions");
94100
context.add_driver_option("-fexceptions");

src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
501501
}
502502

503503
#[cfg(not(feature="master"))]
504-
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> {
504+
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> {
505505
let call_site = self.call(typ, fn_attrs, None, func, args, None);
506506
let condition = self.context.new_rvalue_from_int(self.bool_type, 1);
507507
self.llbb().end_with_conditional(None, condition, then, catch);

src/lib.rs

+51-18
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern crate rustc_middle;
3535
extern crate rustc_session;
3636
extern crate rustc_span;
3737
extern crate rustc_target;
38+
extern crate tempfile;
3839

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

6869
use crate::errors::LTONotSupported;
69-
use gccjit::{Context, OptimizationLevel, TargetInfo};
70+
use gccjit::{Context, OptimizationLevel};
71+
#[cfg(feature="master")]
72+
use gccjit::TargetInfo;
7073
use rustc_ast::expand::allocator::AllocatorKind;
7174
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen};
7275
use rustc_codegen_ssa::base::codegen_crate;
@@ -259,17 +262,54 @@ impl WriteBackendMethods for GccCodegenBackend {
259262
}
260263
}
261264

265+
#[cfg(not(feature="master"))]
266+
#[derive(Debug)]
267+
pub struct TargetInfo {
268+
supports_128bit_integers: bool,
269+
}
270+
271+
#[cfg(not(feature="master"))]
272+
impl TargetInfo {
273+
fn cpu_supports(&self, _feature: &str) -> bool {
274+
false
275+
}
276+
277+
fn supports_128bit_int(&self) -> bool {
278+
self.supports_128bit_integers
279+
}
280+
}
281+
262282
/// This is the entrypoint for a hot plugged rustc_codegen_gccjit
263283
#[no_mangle]
264284
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
265-
// Get the native arch and check whether the target supports 128-bit integers.
266-
let context = Context::default();
267-
let arch = context.get_target_info().arch().unwrap();
268-
269-
// Get the second TargetInfo with the correct CPU features by setting the arch.
270-
let context = Context::default();
271-
context.add_driver_option(&format!("-march={}", arch.to_str().unwrap()));
272-
let target_info = Arc::new(context.get_target_info());
285+
#[cfg(feature="master")]
286+
let target_info = {
287+
// Get the native arch and check whether the target supports 128-bit integers.
288+
let context = Context::default();
289+
let arch = context.get_target_info().arch().unwrap();
290+
291+
// Get the second TargetInfo with the correct CPU features by setting the arch.
292+
let context = Context::default();
293+
context.add_driver_option(&format!("-march={}", arch.to_str().unwrap()));
294+
Arc::new(context.get_target_info())
295+
};
296+
297+
#[cfg(not(feature="master"))]
298+
let target_info = {
299+
use gccjit::CType;
300+
use tempfile::TempDir;
301+
302+
let temp_dir = TempDir::new().expect("cannot create temporary directory");
303+
let temp_file = temp_dir.into_path().join("result.asm");
304+
let check_context = Context::default();
305+
check_context.set_print_errors_to_stderr(false);
306+
let _int128_ty = check_context.new_c_type(CType::UInt128t);
307+
// NOTE: we cannot just call compile() as this would require other files than libgccjit.so.
308+
check_context.compile_to_file(gccjit::OutputKind::Assembler, temp_file.to_str().expect("path to str"));
309+
Arc::new(TargetInfo {
310+
supports_128bit_integers: check_context.get_last_error() == Ok(None),
311+
})
312+
};
273313

274314
Box::new(GccCodegenBackend {
275315
target_info,
@@ -314,15 +354,8 @@ pub fn target_features(sess: &Session, allow_unstable: bool, target_info: &Arc<T
314354
if sess.is_nightly_build() || allow_unstable || gate.is_none() { Some(feature) } else { None }
315355
},
316356
)
317-
.filter(|_feature| {
318-
#[cfg(feature="master")]
319-
{
320-
target_info.cpu_supports(_feature)
321-
}
322-
#[cfg(not(feature="master"))]
323-
{
324-
false
325-
}
357+
.filter(|feature| {
358+
target_info.cpu_supports(feature)
326359
/*
327360
adx, aes, avx, avx2, avx512bf16, avx512bitalg, avx512bw, avx512cd, avx512dq, avx512er, avx512f, avx512ifma,
328361
avx512pf, avx512vbmi, avx512vbmi2, avx512vl, avx512vnni, avx512vp2intersect, avx512vpopcntdq,

0 commit comments

Comments
 (0)