Skip to content

Commit e58ebac

Browse files
committed
Remove unused code
1 parent f0348d9 commit e58ebac

File tree

7 files changed

+22
-190
lines changed

7 files changed

+22
-190
lines changed

compiler/rustc_builtin_macros/src/autodiff.rs

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ mod llvm_enzyme {
262262
};
263263

264264
let has_ret = has_ret(&sig.decl.output);
265-
let sig_span = ecx.with_call_site_ctxt(sig.span);
266265

267266
// create TokenStream from vec elemtents:
268267
// meta_item doesn't have a .tokens field
@@ -331,24 +330,13 @@ mod llvm_enzyme {
331330
}
332331
let span = ecx.with_def_site_ctxt(expand_span);
333332

334-
let n_active: u32 = x
335-
.input_activity
336-
.iter()
337-
.filter(|a| **a == DiffActivity::Active || **a == DiffActivity::ActiveOnly)
338-
.count() as u32;
339-
let (d_sig, new_args, idents, errored) = gen_enzyme_decl(ecx, &sig, &x, span);
333+
let (d_sig, idents, errored) = gen_enzyme_decl(ecx, &sig, &x, span);
340334

341-
// TODO(Sa4dUs): Remove this and all the related logic
342335
let d_body = gen_enzyme_body(
343336
ecx,
344-
&x,
345-
n_active,
346-
&sig,
347337
&d_sig,
348338
primal,
349-
&new_args,
350339
span,
351-
sig_span,
352340
idents,
353341
errored,
354342
first_ident(&meta_item_vec[0]),
@@ -361,7 +349,7 @@ mod llvm_enzyme {
361349
defaultness: ast::Defaultness::Final,
362350
sig: d_sig,
363351
ident: first_ident(&meta_item_vec[0]),
364-
generics: generics.clone(),
352+
generics,
365353
contract: None,
366354
body: Some(d_body),
367355
define_opaque: None,
@@ -542,7 +530,7 @@ mod llvm_enzyme {
542530
vec![
543531
Ident::from_str("std"),
544532
Ident::from_str("intrinsics"),
545-
Ident::from_str("enzyme_autodiff"),
533+
Ident::with_dummy_span(sym::enzyme_autodiff),
546534
],
547535
);
548536
let call_expr = ecx.expr_call(
@@ -555,7 +543,7 @@ mod llvm_enzyme {
555543
}
556544

557545
// Generate turbofish expression from fn name and generics
558-
// Given `foo` and `<A, B, C>`, gen `foo::<A, B, C>`
546+
// Given `foo` and `<A, B, C>` params, gen `foo::<A, B, C>`
559547
fn gen_turbofish_expr(
560548
ecx: &ExtCtxt<'_>,
561549
ident: Ident,
@@ -597,43 +585,27 @@ mod llvm_enzyme {
597585

598586
// Will generate a body of the type:
599587
// ```
600-
// {
601-
// unsafe {
602-
// asm!("NOP");
603-
// }
604-
// ::core::hint::black_box(primal(args));
605-
// ::core::hint::black_box((args, ret));
606-
// <This part remains to be done by following function>
588+
// primal(args);
589+
// std::intrinsics::enzyme_autodiff(primal, diff, (args))
607590
// }
608591
// ```
609592
fn init_body_helper(
610593
ecx: &ExtCtxt<'_>,
611594
span: Span,
612595
primal: Ident,
613-
_new_names: &[String],
614-
_sig_span: Span,
615-
new_decl_span: Span,
616596
idents: &[Ident],
617597
errored: bool,
618598
generics: &Generics,
619-
) -> (P<ast::Block>, P<ast::Expr>, P<ast::Expr>, P<ast::Expr>) {
620-
let blackbox_path = ecx.std_path(&[sym::hint, sym::black_box]);
621-
let blackbox_call_expr = ecx.expr_path(ecx.path(span, blackbox_path));
599+
) -> P<ast::Block> {
622600
let primal_call = gen_primal_call(ecx, span, primal, idents, generics);
623-
let black_box_primal_call = ecx.expr_call(
624-
new_decl_span,
625-
blackbox_call_expr.clone(),
626-
thin_vec![primal_call.clone()],
627-
);
628-
629601
let mut body = ecx.block(span, ThinVec::new());
630602

631603
// This uses primal args which won't be available if we errored before
632604
if !errored {
633605
body.stmts.push(ecx.stmt_semi(primal_call.clone()));
634606
}
635607

636-
(body, primal_call, black_box_primal_call, blackbox_call_expr)
608+
body
637609
}
638610

639611
/// We only want this function to type-check, since we will replace the body
@@ -646,14 +618,9 @@ mod llvm_enzyme {
646618
/// from optimizing any arguments away.
647619
fn gen_enzyme_body(
648620
ecx: &ExtCtxt<'_>,
649-
_x: &AutoDiffAttrs,
650-
_n_active: u32,
651-
_sig: &ast::FnSig,
652621
d_sig: &ast::FnSig,
653622
primal: Ident,
654-
new_names: &[String],
655623
span: Span,
656-
sig_span: Span,
657624
idents: Vec<Ident>,
658625
errored: bool,
659626
diff_ident: Ident,
@@ -664,17 +631,7 @@ mod llvm_enzyme {
664631

665632
// Add a call to the primal function to prevent it from being inlined
666633
// and call `enzyme_autodiff` intrinsic (this also covers the return type)
667-
let (mut body, _primal_call, _bb_primal_call, _bb_call_expr) = init_body_helper(
668-
ecx,
669-
span,
670-
primal,
671-
new_names,
672-
sig_span,
673-
new_decl_span,
674-
&idents,
675-
errored,
676-
generics,
677-
);
634+
let mut body = init_body_helper(ecx, span, primal, &idents, errored, generics);
678635

679636
body.stmts.push(call_enzyme_autodiff(
680637
ecx,
@@ -771,7 +728,7 @@ mod llvm_enzyme {
771728
sig: &ast::FnSig,
772729
x: &AutoDiffAttrs,
773730
span: Span,
774-
) -> (ast::FnSig, Vec<String>, Vec<Ident>, bool) {
731+
) -> (ast::FnSig, Vec<Ident>, bool) {
775732
let dcx = ecx.sess.dcx();
776733
let has_ret = has_ret(&sig.decl.output);
777734
let sig_args = sig.decl.inputs.len() + if has_ret { 1 } else { 0 };
@@ -783,7 +740,7 @@ mod llvm_enzyme {
783740
found: num_activities,
784741
});
785742
// This is not the right signature, but we can continue parsing.
786-
return (sig.clone(), vec![], vec![], true);
743+
return (sig.clone(), vec![], true);
787744
}
788745
assert!(sig.decl.inputs.len() == x.input_activity.len());
789746
assert!(has_ret == x.has_ret_activity());
@@ -826,7 +783,7 @@ mod llvm_enzyme {
826783

827784
if errors {
828785
// This is not the right signature, but we can continue parsing.
829-
return (sig.clone(), new_inputs, idents, true);
786+
return (sig.clone(), idents, true);
830787
}
831788

832789
let unsafe_activities = x
@@ -1034,7 +991,7 @@ mod llvm_enzyme {
1034991
}
1035992
let d_sig = FnSig { header: d_header, decl: d_decl, span };
1036993
trace!("Generated signature: {:?}", d_sig);
1037-
(d_sig, new_inputs, idents, false)
994+
(d_sig, idents, false)
1038995
}
1039996
}
1040997

compiler/rustc_codegen_llvm/src/builder/autodiff.rs

Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,18 @@
11
use std::ptr;
22

3-
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, AutoDiffItem, DiffActivity, DiffMode};
4-
use rustc_codegen_ssa::ModuleCodegen;
5-
use rustc_codegen_ssa::back::write::ModuleConfig;
3+
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
64
use rustc_codegen_ssa::common::TypeKind;
75
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, BuilderMethods};
8-
use rustc_errors::FatalError;
96
use rustc_middle::bug;
10-
use tracing::{debug, trace};
7+
use tracing::debug;
118

12-
use crate::back::write::llvm_err;
139
use crate::builder::{Builder, PlaceRef, UNNAMED};
1410
use crate::context::SimpleCx;
1511
use crate::declare::declare_simple_fn;
16-
use crate::errors::{AutoDiffWithoutEnable, LlvmError};
1712
use crate::llvm::AttributePlace::Function;
1813
use crate::llvm::{Metadata, True, Type};
1914
use crate::value::Value;
20-
use crate::{CodegenContext, LlvmCodegenBackend, ModuleLlvm, attributes, llvm};
21-
22-
fn _get_params(fnc: &Value) -> Vec<&Value> {
23-
let param_num = llvm::LLVMCountParams(fnc) as usize;
24-
let mut fnc_args: Vec<&Value> = vec![];
25-
fnc_args.reserve(param_num);
26-
unsafe {
27-
llvm::LLVMGetParams(fnc, fnc_args.as_mut_ptr());
28-
fnc_args.set_len(param_num);
29-
}
30-
fnc_args
31-
}
32-
33-
fn _has_sret(fnc: &Value) -> bool {
34-
let num_args = llvm::LLVMCountParams(fnc) as usize;
35-
if num_args == 0 {
36-
false
37-
} else {
38-
unsafe { llvm::LLVMRustHasAttributeAtIndex(fnc, 0, llvm::AttributeKind::StructRet) }
39-
}
40-
}
15+
use crate::{attributes, llvm};
4116

4217
// When we call the `__enzyme_autodiff` or `__enzyme_fwddiff` function, we need to pass all the
4318
// original inputs, as well as metadata and the additional shadow arguments.
@@ -294,62 +269,3 @@ pub(crate) fn generate_enzyme_call<'ll, 'tcx>(
294269

295270
builder.store_to_place(call, dest.val);
296271
}
297-
298-
pub(crate) fn differentiate<'ll>(
299-
module: &'ll ModuleCodegen<ModuleLlvm>,
300-
cgcx: &CodegenContext<LlvmCodegenBackend>,
301-
diff_items: Vec<AutoDiffItem>,
302-
_config: &ModuleConfig,
303-
) -> Result<(), FatalError> {
304-
// TODO(Sa4dUs): delete all this logic
305-
for item in &diff_items {
306-
trace!("{}", item);
307-
}
308-
309-
let diag_handler = cgcx.create_dcx();
310-
311-
let cx = SimpleCx::new(module.module_llvm.llmod(), module.module_llvm.llcx, cgcx.pointer_size);
312-
313-
// First of all, did the user try to use autodiff without using the -Zautodiff=Enable flag?
314-
if !diff_items.is_empty()
315-
&& !cgcx.opts.unstable_opts.autodiff.contains(&rustc_session::config::AutoDiff::Enable)
316-
{
317-
return Err(diag_handler.handle().emit_almost_fatal(AutoDiffWithoutEnable));
318-
}
319-
320-
// Here we replace the placeholder code with the actual autodiff code, which calls Enzyme.
321-
for item in diff_items.iter() {
322-
let name = item.source.clone();
323-
let fn_def: Option<&llvm::Value> = cx.get_function(&name);
324-
let Some(_fn_def) = fn_def else {
325-
return Err(llvm_err(
326-
diag_handler.handle(),
327-
LlvmError::PrepareAutoDiff {
328-
src: item.source.clone(),
329-
target: item.target.clone(),
330-
error: "could not find source function".to_owned(),
331-
},
332-
));
333-
};
334-
debug!(?item.target);
335-
let fn_target: Option<&llvm::Value> = cx.get_function(&item.target);
336-
let Some(_fn_target) = fn_target else {
337-
return Err(llvm_err(
338-
diag_handler.handle(),
339-
LlvmError::PrepareAutoDiff {
340-
src: item.source.clone(),
341-
target: item.target.clone(),
342-
error: "could not find target function".to_owned(),
343-
},
344-
));
345-
};
346-
347-
// generate_enzyme_call(&cx, fn_def, fn_target, item.attrs.clone());
348-
}
349-
350-
// FIXME(ZuseZ4): support SanitizeHWAddress and prevent illegal/unsupported opts
351-
352-
trace!("done with differentiate()");
353-
354-
Ok(())
355-
}

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
3737
}
3838
}
3939

40+
// TODO(Sa4dUs): we will need to reintroduce these errors somewhere
41+
/*
4042
#[derive(Diagnostic)]
4143
#[diag(codegen_llvm_autodiff_without_lto)]
4244
pub(crate) struct AutoDiffWithoutLTO;
4345
4446
#[derive(Diagnostic)]
4547
#[diag(codegen_llvm_autodiff_without_enable)]
4648
pub(crate) struct AutoDiffWithoutEnable;
49+
*/
4750

4851
#[derive(Diagnostic)]
4952
#[diag(codegen_llvm_lto_disallowed)]

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ use std::mem::ManuallyDrop;
2626
use back::owned_target_machine::OwnedTargetMachine;
2727
use back::write::{create_informational_target_machine, create_target_machine};
2828
use context::SimpleCx;
29-
use errors::{AutoDiffWithoutLTO, ParseTargetMachineConfig};
29+
use errors::ParseTargetMachineConfig;
3030
use llvm_util::target_config;
3131
use rustc_ast::expand::allocator::AllocatorKind;
32-
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
3332
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
3433
use rustc_codegen_ssa::back::write::{
3534
CodegenContext, FatLtoInput, ModuleConfig, TargetMachineFactoryConfig, TargetMachineFactoryFn,
@@ -43,7 +42,7 @@ use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
4342
use rustc_middle::ty::TyCtxt;
4443
use rustc_middle::util::Providers;
4544
use rustc_session::Session;
46-
use rustc_session::config::{Lto, OptLevel, OutputFilenames, PrintKind, PrintRequest};
45+
use rustc_session::config::{OptLevel, OutputFilenames, PrintKind, PrintRequest};
4746
use rustc_span::Symbol;
4847

4948
mod back {
@@ -227,19 +226,6 @@ impl WriteBackendMethods for LlvmCodegenBackend {
227226
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
228227
(module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod()))
229228
}
230-
/// Generate autodiff rules
231-
fn autodiff(
232-
cgcx: &CodegenContext<Self>,
233-
module: &ModuleCodegen<Self::Module>,
234-
diff_fncs: Vec<AutoDiffItem>,
235-
config: &ModuleConfig,
236-
) -> Result<(), FatalError> {
237-
if cgcx.lto != Lto::Fat {
238-
let dcx = cgcx.create_dcx();
239-
return Err(dcx.handle().emit_almost_fatal(AutoDiffWithoutLTO));
240-
}
241-
builder::autodiff::differentiate(module, cgcx, diff_fncs, config)
242-
}
243229
}
244230

245231
impl LlvmCodegenBackend {

compiler/rustc_codegen_ssa/src/back/lto.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use std::ffi::CString;
22
use std::sync::Arc;
33

4-
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
54
use rustc_data_structures::memmap::Mmap;
65
use rustc_errors::FatalError;
76

87
use super::write::CodegenContext;
98
use crate::ModuleCodegen;
10-
use crate::back::write::ModuleConfig;
119
use crate::traits::*;
1210

1311
pub struct ThinModule<B: WriteBackendMethods> {
@@ -78,23 +76,6 @@ impl<B: WriteBackendMethods> LtoModuleCodegen<B> {
7876
LtoModuleCodegen::Thin(ref m) => m.cost(),
7977
}
8078
}
81-
82-
/// Run autodiff on Fat LTO module
83-
pub fn autodiff(
84-
self,
85-
cgcx: &CodegenContext<B>,
86-
diff_fncs: Vec<AutoDiffItem>,
87-
config: &ModuleConfig,
88-
) -> Result<LtoModuleCodegen<B>, FatalError> {
89-
match &self {
90-
LtoModuleCodegen::Fat(module) => {
91-
B::autodiff(cgcx, &module, diff_fncs, config)?;
92-
}
93-
_ => panic!("autodiff called with non-fat LTO module"),
94-
}
95-
96-
Ok(self)
97-
}
9879
}
9980

10081
pub enum SerializedModule<M: ModuleBufferMethods> {

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,8 @@ fn generate_lto_work<B: ExtraBackendMethods>(
408408

409409
if !needs_fat_lto.is_empty() {
410410
assert!(needs_thin_lto.is_empty());
411-
let mut module =
411+
let module =
412412
B::run_fat_lto(cgcx, needs_fat_lto, import_only_modules).unwrap_or_else(|e| e.raise());
413-
if cgcx.lto == Lto::Fat && !autodiff.is_empty() {
414-
let config = cgcx.config(ModuleKind::Regular);
415-
module = module.autodiff(cgcx, autodiff, config).unwrap_or_else(|e| e.raise());
416-
}
417413
// We are adding a single work item, so the cost doesn't matter.
418414
vec![(WorkItem::LTO(module), 0)]
419415
} else {

0 commit comments

Comments
 (0)