From c4edf34b51388289592c0c90cb1f50380a32f24e Mon Sep 17 00:00:00 2001 From: Manuel Drehwald Date: Fri, 26 Jul 2024 20:08:03 -0400 Subject: [PATCH] Now correctly catching not-fat-lto errors --- compiler/rustc_codegen_llvm/messages.ftl | 2 ++ compiler/rustc_codegen_llvm/src/errors.rs | 5 +++++ compiler/rustc_codegen_llvm/src/lib.rs | 7 ++++++- compiler/rustc_codegen_ssa/messages.ftl | 2 ++ compiler/rustc_codegen_ssa/src/back/lto.rs | 2 +- compiler/rustc_codegen_ssa/src/back/write.rs | 10 +++++----- compiler/rustc_codegen_ssa/src/errors.rs | 4 ++++ 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_codegen_llvm/messages.ftl b/compiler/rustc_codegen_llvm/messages.ftl index bcf4d0096d5f3..23872d7fa1dcd 100644 --- a/compiler/rustc_codegen_llvm/messages.ftl +++ b/compiler/rustc_codegen_llvm/messages.ftl @@ -63,6 +63,8 @@ codegen_llvm_run_passes_with_llvm_err = failed to run LLVM passes: {$llvm_err} codegen_llvm_prepare_autodiff = failed to prepare AutoDiff: src: {$src}, target: {$target}, {$error} codegen_llvm_prepare_autodiff_with_llvm_err = failed to prepare AutoDiff: {$llvm_err}, src: {$src}, target: {$target}, {$error} +codegen_llvm_autodiff_without_lto = using the autodiff feature requires using fat-lto + codegen_llvm_sanitizer_memtag_requires_mte = `-Zsanitizer=memtag` requires `-Ctarget-feature=+mte` diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 129e692fd7fa4..7b7ee00959bdb 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -97,6 +97,11 @@ pub(crate) struct DlltoolFailImportLibrary<'a> { #[note] pub(crate) struct DynamicLinkingWithLTO; +#[derive(Diagnostic)] +#[diag(codegen_llvm_autodiff_without_lto)] +#[note] +pub(crate) struct AutoDiffWithoutLTO; + pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>); impl IntoDiagnostic<'_, G> for ParseTargetMachineConfig<'_> { diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index a6b625139ef22..7506bbe436785 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -25,10 +25,11 @@ extern crate rustc_macros; #[macro_use] extern crate tracing; +use rustc_session::config::Lto; use back::owned_target_machine::OwnedTargetMachine; use back::write::{create_informational_target_machine, create_target_machine}; -use errors::ParseTargetMachineConfig; +use errors::{ParseTargetMachineConfig, AutoDiffWithoutLTO}; #[allow(unused_imports)] use llvm::TypeTree; @@ -268,6 +269,10 @@ impl WriteBackendMethods for LlvmCodegenBackend { typetrees: FxHashMap, config: &ModuleConfig, ) -> Result<(), FatalError> { + if cgcx.lto != Lto::Fat { + let dcx = cgcx.create_dcx(); + return Err(dcx.emit_almost_fatal(AutoDiffWithoutLTO{})); + } unsafe { back::write::differentiate(module, cgcx, diff_fncs, typetrees, config) } } diff --git a/compiler/rustc_codegen_ssa/messages.ftl b/compiler/rustc_codegen_ssa/messages.ftl index 5881c6236ece6..44e7324c829a3 100644 --- a/compiler/rustc_codegen_ssa/messages.ftl +++ b/compiler/rustc_codegen_ssa/messages.ftl @@ -330,3 +330,5 @@ codegen_ssa_use_cargo_directive = use the `cargo:rustc-link-lib` directive to sp codegen_ssa_version_script_write_failure = failed to write version script: {$error} codegen_ssa_visual_studio_not_installed = you may need to install Visual Studio build tools with the "C++ build tools" workload + +codegen_ssa_autodiff_without_lto = using the autodiff feature requires using fat-lto diff --git a/compiler/rustc_codegen_ssa/src/back/lto.rs b/compiler/rustc_codegen_ssa/src/back/lto.rs index f78b74c9545be..2d8e230e1976b 100644 --- a/compiler/rustc_codegen_ssa/src/back/lto.rs +++ b/compiler/rustc_codegen_ssa/src/back/lto.rs @@ -90,7 +90,7 @@ impl LtoModuleCodegen { LtoModuleCodegen::Fat { ref module, .. } => { B::autodiff(cgcx, &module, diff_fncs, typetrees, config)?; } - _ => panic!("autodiff called with non-fat LTO module"), + _ => panic!("Unreachable? Autodiff called with non-fat LTO module"), } Ok(self) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 0a9117072b7ac..fda8330ea8f11 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -35,7 +35,7 @@ use rustc_span::symbol::sym; use rustc_span::{BytePos, FileName, InnerSpan, Pos, Span}; use rustc_target::spec::{MergeFunctions, SanitizerSet}; -use crate::errors::ErrorCreatingRemarkDir; +use crate::errors::{ErrorCreatingRemarkDir, AutodiffWithoutLto}; use std::any::Any; use std::borrow::Cow; use std::fs; @@ -382,9 +382,6 @@ fn generate_lto_work( import_only_modules: Vec<(SerializedModule, WorkProduct)>, ) -> Vec<(WorkItem, u64)> { let _prof_timer = cgcx.prof.generic_activity("codegen_generate_lto_work"); - //let error_msg = format!("Found {} Functions, but {} TypeTrees", autodiff.len(), typetrees.len()); - // Don't assert yet, bc. apparently we add them later. - //assert!(autodiff.len() == typetrees.len(), "{}", error_msg); if !needs_fat_lto.is_empty() { assert!(needs_thin_lto.is_empty()); @@ -397,7 +394,10 @@ fn generate_lto_work( // We are adding a single work item, so the cost doesn't matter. vec![(WorkItem::LTO(module), 0)] } else { - assert!(autodiff.is_empty()); + if !autodiff.is_empty() { + let dcx = cgcx.create_dcx(); + dcx.emit_fatal(AutodiffWithoutLto{}); + } assert!(needs_fat_lto.is_empty()); let (lto_modules, copy_jobs) = B::run_thin_lto(cgcx, needs_thin_lto, import_only_modules) .unwrap_or_else(|e| e.raise()); diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 2b628d2aa69b7..42fb45a6f42aa 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -35,6 +35,10 @@ pub struct CguNotRecorded<'a> { pub cgu_name: &'a str, } +#[derive(Diagnostic)] +#[diag(codegen_ssa_autodiff_without_lto)] +pub struct AutodiffWithoutLto; + #[derive(Diagnostic)] #[diag(codegen_ssa_unknown_reuse_kind)] pub struct UnknownReuseKind {