Skip to content

Commit 35d882f

Browse files
committed
Raise an error if people use autodiff without -Zautodiff=Enable
1 parent 755bec9 commit 35d882f

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
codegen_llvm_autodiff_without_lto = using the autodiff feature requires using fat-lto
2+
codegen_llvm_autodiff_without_enable = using the autodiff feature requires -Z autodiff=Enable
23
34
codegen_llvm_copy_bitcode = failed to copy bitcode to object file: {$err}
45

compiler/rustc_codegen_llvm/src/builder/autodiff.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::back::write::llvm_err;
1010
use crate::builder::SBuilder;
1111
use crate::context::SimpleCx;
1212
use crate::declare::declare_simple_fn;
13-
use crate::errors::LlvmError;
13+
use crate::errors::{AutoDiffWithoutEnable, LlvmError};
1414
use crate::llvm::AttributePlace::Function;
1515
use crate::llvm::{Metadata, True};
1616
use crate::value::Value;
@@ -288,6 +288,14 @@ pub(crate) fn differentiate<'ll>(
288288
let diag_handler = cgcx.create_dcx();
289289
let cx = SimpleCx { llmod: module.module_llvm.llmod(), llcx: module.module_llvm.llcx };
290290

291+
// First of all, did the user try to use autodiff without using the -Zautodiff=Enable flag?
292+
if !diff_items.is_empty()
293+
&& !cgcx.opts.unstable_opts.autodiff.contains(&rustc_session::config::AutoDiff::Enable)
294+
{
295+
let dcx = cgcx.create_dcx();
296+
return Err(dcx.handle().emit_almost_fatal(AutoDiffWithoutEnable));
297+
}
298+
291299
// Before dumping the module, we want all the TypeTrees to become part of the module.
292300
for item in diff_items.iter() {
293301
let name = item.source.clone();

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,12 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
9292

9393
#[derive(Diagnostic)]
9494
#[diag(codegen_llvm_autodiff_without_lto)]
95-
#[note]
9695
pub(crate) struct AutoDiffWithoutLTO;
9796

97+
#[derive(Diagnostic)]
98+
#[diag(codegen_llvm_autodiff_without_enable)]
99+
pub(crate) struct AutoDiffWithoutEnable;
100+
98101
#[derive(Diagnostic)]
99102
#[diag(codegen_llvm_lto_disallowed)]
100103
pub(crate) struct LtoDisallowed;

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ fn generate_lto_work<B: ExtraBackendMethods>(
405405
B::run_fat_lto(cgcx, needs_fat_lto, import_only_modules).unwrap_or_else(|e| e.raise());
406406
if cgcx.lto == Lto::Fat && !autodiff.is_empty() {
407407
let config = cgcx.config(ModuleKind::Regular);
408-
module = unsafe { module.autodiff(cgcx, autodiff, config).unwrap() };
408+
module = unsafe { module.autodiff(cgcx, autodiff, config).unwrap_or_else(|e| e.raise()) };
409409
}
410410
// We are adding a single work item, so the cost doesn't matter.
411411
vec![(WorkItem::LTO(module), 0)]

0 commit comments

Comments
 (0)