From 1cc10793f305ce81743a3b74ee4e3d4e8db785d8 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 21 Nov 2024 12:38:14 +0000 Subject: [PATCH] Remove enable_verifier from BackendConfig --- src/base.rs | 14 ++++---------- src/config.rs | 10 ---------- src/driver/aot.rs | 23 ++++++----------------- src/driver/jit.rs | 30 ++++++------------------------ src/lib.rs | 16 +++++++++++++--- 5 files changed, 29 insertions(+), 64 deletions(-) diff --git a/src/base.rs b/src/base.rs index 3c1c1ae66507..043e7c878fb5 100644 --- a/src/base.rs +++ b/src/base.rs @@ -14,9 +14,9 @@ use rustc_middle::ty::adjustment::PointerCoercion; use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv}; use rustc_middle::ty::print::with_no_trimmed_paths; -use crate::BackendConfig; use crate::constant::ConstantCx; use crate::debuginfo::{FunctionDebugContext, TypeDebugContext}; +use crate::enable_verifier; use crate::inline_asm::codegen_naked_asm; use crate::prelude::*; use crate::pretty_clif::CommentWriter; @@ -31,7 +31,6 @@ pub(crate) struct CodegenedFunction { pub(crate) fn codegen_fn<'tcx>( tcx: TyCtxt<'tcx>, - backend_config: &BackendConfig, cx: &mut crate::CodegenCx, type_dbg: &mut TypeDebugContext<'tcx>, cached_func: Function, @@ -164,7 +163,7 @@ pub(crate) fn codegen_fn<'tcx>( } // Verify function - verify_func(tcx, backend_config, &clif_comments, &func); + verify_func(tcx, &clif_comments, &func); Some(CodegenedFunction { symbol_name, func_id, func, clif_comments, func_debug_cx }) } @@ -266,13 +265,8 @@ pub(crate) fn compile_fn( }); } -fn verify_func( - tcx: TyCtxt<'_>, - backend_config: &BackendConfig, - writer: &crate::pretty_clif::CommentWriter, - func: &Function, -) { - if !tcx.sess.verify_llvm_ir() && !backend_config.enable_verifier { +fn verify_func(tcx: TyCtxt<'_>, writer: &crate::pretty_clif::CommentWriter, func: &Function) { + if !enable_verifier(tcx.sess) { return; } diff --git a/src/config.rs b/src/config.rs index 12bce680d9e1..885d7105da3d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -42,14 +42,6 @@ pub struct BackendConfig { /// Defaults to the value of `CG_CLIF_JIT_ARGS`. pub jit_args: Vec, - /// Enable the Cranelift ir verifier for all compilation passes. If not set it will only run - /// once before passing the clif ir to Cranelift for compilation. - /// - /// Defaults to true when the `CG_CLIF_ENABLE_VERIFIER` env var is set to 1 or when cg_clif is - /// compiled with debug assertions enabled or false otherwise. Can be set using - /// `-Cllvm-args=enable_verifier=...`. - pub enable_verifier: bool, - /// Don't cache object files in the incremental cache. Useful during development of cg_clif /// to make it possible to use incremental mode for all analyses performed by rustc without /// caching object files when their content should have been changed by a change to cg_clif. @@ -72,7 +64,6 @@ impl Default for BackendConfig { } } }, - enable_verifier: cfg!(debug_assertions) || bool_env_var("CG_CLIF_ENABLE_VERIFIER"), disable_incr_cache: bool_env_var("CG_CLIF_DISABLE_INCR_CACHE"), } } @@ -95,7 +86,6 @@ impl BackendConfig { if let Some((name, value)) = opt.split_once('=') { match name { "mode" => config.codegen_mode = value.parse()?, - "enable_verifier" => config.enable_verifier = parse_bool(name, value)?, "disable_incr_cache" => config.disable_incr_cache = parse_bool(name, value)?, _ => return Err(format!("Unknown option `{}`", name)), } diff --git a/src/driver/aot.rs b/src/driver/aot.rs index 2baa0f7f6023..8d1f88ba062e 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -322,12 +322,8 @@ fn produce_final_output_artifacts( // These are used in linking steps and will be cleaned up afterward. } -fn make_module( - sess: &Session, - backend_config: &BackendConfig, - name: String, -) -> UnwindModule { - let isa = crate::build_isa(sess, backend_config); +fn make_module(sess: &Session, name: String) -> UnwindModule { + let isa = crate::build_isa(sess); let mut builder = ObjectBuilder::new(isa, name + ".o", cranelift_module::default_libcall_names()).unwrap(); @@ -488,8 +484,7 @@ fn reuse_workproduct_for_cgu( fn module_codegen( tcx: TyCtxt<'_>, - (backend_config, global_asm_config, cgu_name, token): ( - BackendConfig, + (global_asm_config, cgu_name, token): ( Arc, rustc_span::Symbol, ConcurrencyLimiterToken, @@ -500,7 +495,7 @@ fn module_codegen( let cgu = tcx.codegen_unit(cgu_name); let mono_items = cgu.items_in_deterministic_order(tcx); - let mut module = make_module(tcx.sess, &backend_config, cgu_name.as_str().to_string()); + let mut module = make_module(tcx.sess, cgu_name.as_str().to_string()); let mut cx = crate::CodegenCx::new( tcx, @@ -516,7 +511,6 @@ fn module_codegen( MonoItem::Fn(inst) => { if let Some(codegened_function) = crate::base::codegen_fn( tcx, - &backend_config, &mut cx, &mut type_dbg, Function::new(), @@ -648,12 +642,7 @@ pub(crate) fn run_aot( .with_task( dep_node, tcx, - ( - backend_config.clone(), - global_asm_config.clone(), - cgu.name(), - concurrency_limiter.acquire(tcx.dcx()), - ), + (global_asm_config.clone(), cgu.name(), concurrency_limiter.acquire(tcx.dcx())), module_codegen, Some(rustc_middle::dep_graph::hash_result), ) @@ -667,7 +656,7 @@ pub(crate) fn run_aot( modules }); - let mut allocator_module = make_module(tcx.sess, &backend_config, "allocator_shim".to_string()); + let mut allocator_module = make_module(tcx.sess, "allocator_shim".to_string()); let created_alloc_shim = crate::allocator::codegen(tcx, &mut allocator_module); let allocator_module = if created_alloc_shim { diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 1ec4d6b55eb1..ebbbc2697d10 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -20,7 +20,6 @@ use crate::{BackendConfig, CodegenCx, CodegenMode}; struct JitState { jit_module: UnwindModule, - backend_config: BackendConfig, } thread_local! { @@ -60,14 +59,10 @@ impl UnsafeMessage { } } -fn create_jit_module( - tcx: TyCtxt<'_>, - backend_config: &BackendConfig, - hotswap: bool, -) -> (UnwindModule, CodegenCx) { +fn create_jit_module(tcx: TyCtxt<'_>, hotswap: bool) -> (UnwindModule, CodegenCx) { let crate_info = CrateInfo::new(tcx, "dummy_target_cpu".to_string()); - let isa = crate::build_isa(tcx.sess, backend_config); + let isa = crate::build_isa(tcx.sess); let mut jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names()); jit_builder.hotswap(hotswap); crate::compiler_builtins::register_functions_for_jit(&mut jit_builder); @@ -91,11 +86,8 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { tcx.dcx().fatal("can't jit non-executable crate"); } - let (mut jit_module, mut cx) = create_jit_module( - tcx, - &backend_config, - matches!(backend_config.codegen_mode, CodegenMode::JitLazy), - ); + let (mut jit_module, mut cx) = + create_jit_module(tcx, matches!(backend_config.codegen_mode, CodegenMode::JitLazy)); let mut cached_context = Context::new(); let (_, cgus) = tcx.collect_and_partition_mono_items(()); @@ -116,7 +108,6 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { CodegenMode::Jit => { codegen_and_compile_fn( tcx, - &backend_config, &mut cx, &mut cached_context, &mut jit_module, @@ -171,7 +162,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { LAZY_JIT_STATE.with(|lazy_jit_state| { let mut lazy_jit_state = lazy_jit_state.borrow_mut(); assert!(lazy_jit_state.is_none()); - *lazy_jit_state = Some(JitState { jit_module, backend_config }); + *lazy_jit_state = Some(JitState { jit_module }); }); let f: extern "C" fn(c_int, *const *const c_char) -> c_int = @@ -207,7 +198,6 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { pub(crate) fn codegen_and_compile_fn<'tcx>( tcx: TyCtxt<'tcx>, - backend_config: &BackendConfig, cx: &mut crate::CodegenCx, cached_context: &mut Context, module: &mut dyn Module, @@ -224,7 +214,6 @@ pub(crate) fn codegen_and_compile_fn<'tcx>( let cached_func = std::mem::replace(&mut cached_context.func, Function::new()); if let Some(codegened_func) = crate::base::codegen_fn( tcx, - &backend_config, cx, &mut TypeDebugContext::default(), cached_func, @@ -286,14 +275,7 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) -> false, Symbol::intern("dummy_cgu_name"), ); - codegen_and_compile_fn( - tcx, - &lazy_jit_state.backend_config, - &mut cx, - &mut Context::new(), - jit_module, - instance, - ); + codegen_and_compile_fn(tcx, &mut cx, &mut Context::new(), jit_module, instance); assert!(cx.global_asm.is_empty()); jit_module.finalize_definitions(); diff --git a/src/lib.rs b/src/lib.rs index 15163fa715f1..f418224efe97 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,7 @@ extern crate rustc_driver; use std::any::Any; use std::cell::{Cell, RefCell}; +use std::env; use std::sync::Arc; use cranelift_codegen::isa::TargetIsa; @@ -249,6 +250,16 @@ impl CodegenBackend for CraneliftCodegenBackend { } } +/// Determine if the Cranelift ir verifier should run. +/// +/// Returns true when `-Zverify-llvm-ir` is passed, the `CG_CLIF_ENABLE_VERIFIER` env var is set to +/// 1 or when cg_clif is compiled with debug assertions enabled or false otherwise. +fn enable_verifier(sess: &Session) -> bool { + sess.verify_llvm_ir() + || cfg!(debug_assertions) + || env::var("CG_CLIF_ENABLE_VERIFIER").as_deref() == Ok("1") +} + fn target_triple(sess: &Session) -> target_lexicon::Triple { // FIXME(madsmtm): Use `sess.target.llvm_target` once target-lexicon supports unversioned macOS. // See @@ -258,15 +269,14 @@ fn target_triple(sess: &Session) -> target_lexicon::Triple { } } -fn build_isa(sess: &Session, backend_config: &BackendConfig) -> Arc { +fn build_isa(sess: &Session) -> Arc { use target_lexicon::BinaryFormat; let target_triple = crate::target_triple(sess); let mut flags_builder = settings::builder(); flags_builder.enable("is_pic").unwrap(); - let enable_verifier = - if sess.verify_llvm_ir() || backend_config.enable_verifier { "true" } else { "false" }; + let enable_verifier = if enable_verifier(sess) { "true" } else { "false" }; flags_builder.set("enable_verifier", enable_verifier).unwrap(); flags_builder.set("regalloc_checker", enable_verifier).unwrap();