Skip to content

Commit 2ad340e

Browse files
committed
upstream rustc_codegen_ssa/rustc_middle changes for enzyme/autodiff
1 parent c528b8c commit 2ad340e

File tree

25 files changed

+444
-26
lines changed

25 files changed

+444
-26
lines changed

Diff for: Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -4233,6 +4233,7 @@ name = "rustc_monomorphize"
42334233
version = "0.0.0"
42344234
dependencies = [
42354235
"rustc_abi",
4236+
"rustc_ast",
42364237
"rustc_attr_parsing",
42374238
"rustc_data_structures",
42384239
"rustc_errors",
@@ -4242,6 +4243,7 @@ dependencies = [
42424243
"rustc_middle",
42434244
"rustc_session",
42444245
"rustc_span",
4246+
"rustc_symbol_mangling",
42454247
"rustc_target",
42464248
"serde",
42474249
"serde_json",

Diff for: compiler/rustc_codegen_llvm/src/builder/autodiff.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ pub(crate) fn differentiate<'ll, 'tcx>(
286286
}
287287

288288
let diag_handler = cgcx.create_dcx();
289-
let (_, cgus) = tcx.collect_and_partition_mono_items(());
289+
let (_, _, cgus) = tcx.collect_and_partition_mono_items(());
290290
let cx = context::CodegenCx::new(tcx, &cgus.first().unwrap(), &module.module_llvm);
291291

292292
// Before dumping the module, we want all the TypeTrees to become part of the module.

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ struct UsageSets<'tcx> {
297297
/// Prepare sets of definitions that are relevant to deciding whether something
298298
/// is an "unused function" for coverage purposes.
299299
fn prepare_usage_sets<'tcx>(tcx: TyCtxt<'tcx>) -> UsageSets<'tcx> {
300-
let (all_mono_items, cgus) = tcx.collect_and_partition_mono_items(());
300+
let (all_mono_items, _, cgus) = tcx.collect_and_partition_mono_items(());
301301

302302
// Obtain a MIR body for each function participating in codegen, via an
303303
// arbitrary instance.

Diff for: compiler/rustc_codegen_ssa/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,6 @@ codegen_ssa_use_cargo_directive = use the `cargo:rustc-link-lib` directive to sp
367367
codegen_ssa_version_script_write_failure = failed to write version script: {$error}
368368
369369
codegen_ssa_visual_studio_not_installed = you may need to install Visual Studio build tools with the "C++ build tools" workload
370+
371+
codegen_ssa_autodiff_without_lto = using the autodiff feature requires using fat-lto
372+

Diff for: compiler/rustc_codegen_ssa/src/assert_module_sources.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>, set_reuse: &dyn Fn(&mut CguReuseTr
4747
}
4848

4949
let available_cgus =
50-
tcx.collect_and_partition_mono_items(()).1.iter().map(|cgu| cgu.name()).collect();
50+
tcx.collect_and_partition_mono_items(()).2.iter().map(|cgu| cgu.name()).collect();
5151

5252
let mut ams = AssertModuleSource {
5353
tcx,

Diff for: compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ fn exported_symbols_provider_local(
293293
// external linkage is enough for monomorphization to be linked to.
294294
let need_visibility = tcx.sess.target.dynamic_linking && !tcx.sess.target.only_cdylib;
295295

296-
let (_, cgus) = tcx.collect_and_partition_mono_items(());
296+
let (_, _, cgus) = tcx.collect_and_partition_mono_items(());
297297

298298
// The symbols created in this loop are sorted below it
299299
#[allow(rustc::potential_query_instability)]

Diff for: compiler/rustc_codegen_ssa/src/back/write.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::sync::mpsc::{Receiver, Sender, channel};
77
use std::{fs, io, mem, str, thread};
88

99
use rustc_ast::attr;
10+
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
1011
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
1112
use rustc_data_structures::jobserver::{self, Acquired};
1213
use rustc_data_structures::memmap::Mmap;
@@ -40,7 +41,7 @@ use tracing::debug;
4041
use super::link::{self, ensure_removed};
4142
use super::lto::{self, SerializedModule};
4243
use super::symbol_export::symbol_name_for_instance_in_crate;
43-
use crate::errors::ErrorCreatingRemarkDir;
44+
use crate::errors::{AutodiffWithoutLto, ErrorCreatingRemarkDir};
4445
use crate::traits::*;
4546
use crate::{
4647
CachedModuleCodegen, CodegenResults, CompiledModule, CrateInfo, ModuleCodegen, ModuleKind,
@@ -118,6 +119,7 @@ pub struct ModuleConfig {
118119
pub merge_functions: bool,
119120
pub emit_lifetime_markers: bool,
120121
pub llvm_plugins: Vec<String>,
122+
pub autodiff: Vec<config::AutoDiff>,
121123
}
122124

123125
impl ModuleConfig {
@@ -266,6 +268,7 @@ impl ModuleConfig {
266268

267269
emit_lifetime_markers: sess.emit_lifetime_markers(),
268270
llvm_plugins: if_regular!(sess.opts.unstable_opts.llvm_plugins.clone(), vec![]),
271+
autodiff: if_regular!(sess.opts.unstable_opts.autodiff.clone(), vec![]),
269272
}
270273
}
271274

@@ -389,6 +392,7 @@ impl<B: WriteBackendMethods> CodegenContext<B> {
389392

390393
fn generate_lto_work<B: ExtraBackendMethods>(
391394
cgcx: &CodegenContext<B>,
395+
autodiff: Vec<AutoDiffItem>,
392396
needs_fat_lto: Vec<FatLtoInput<B>>,
393397
needs_thin_lto: Vec<(String, B::ThinBuffer)>,
394398
import_only_modules: Vec<(SerializedModule<B::ModuleBuffer>, WorkProduct)>,
@@ -399,9 +403,18 @@ fn generate_lto_work<B: ExtraBackendMethods>(
399403
assert!(needs_thin_lto.is_empty());
400404
let module =
401405
B::run_fat_lto(cgcx, needs_fat_lto, import_only_modules).unwrap_or_else(|e| e.raise());
406+
if cgcx.lto == Lto::Fat {
407+
let _config = cgcx.config(ModuleKind::Regular);
408+
todo!("fat LTO with autodiff is not yet implemented");
409+
//module = unsafe { module.autodiff(cgcx, autodiff, config).unwrap() };
410+
}
402411
// We are adding a single work item, so the cost doesn't matter.
403412
vec![(WorkItem::LTO(module), 0)]
404413
} else {
414+
if !autodiff.is_empty() {
415+
let dcx = cgcx.create_dcx();
416+
dcx.handle().emit_fatal(AutodiffWithoutLto {});
417+
}
405418
assert!(needs_fat_lto.is_empty());
406419
let (lto_modules, copy_jobs) = B::run_thin_lto(cgcx, needs_thin_lto, import_only_modules)
407420
.unwrap_or_else(|e| e.raise());
@@ -1021,6 +1034,9 @@ pub(crate) enum Message<B: WriteBackendMethods> {
10211034
/// Sent from a backend worker thread.
10221035
WorkItem { result: Result<WorkItemResult<B>, Option<WorkerFatalError>>, worker_id: usize },
10231036

1037+
/// A vector containing all the AutoDiff tasks that we have to pass to Enzyme.
1038+
AddAutoDiffItems(Vec<AutoDiffItem>),
1039+
10241040
/// The frontend has finished generating something (backend IR or a
10251041
/// post-LTO artifact) for a codegen unit, and it should be passed to the
10261042
/// backend. Sent from the main thread.
@@ -1348,6 +1364,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
13481364

13491365
// This is where we collect codegen units that have gone all the way
13501366
// through codegen and LLVM.
1367+
let mut autodiff_items = Vec::new();
13511368
let mut compiled_modules = vec![];
13521369
let mut compiled_allocator_module = None;
13531370
let mut needs_link = Vec::new();
@@ -1459,9 +1476,13 @@ fn start_executing_work<B: ExtraBackendMethods>(
14591476
let needs_thin_lto = mem::take(&mut needs_thin_lto);
14601477
let import_only_modules = mem::take(&mut lto_import_only_modules);
14611478

1462-
for (work, cost) in
1463-
generate_lto_work(&cgcx, needs_fat_lto, needs_thin_lto, import_only_modules)
1464-
{
1479+
for (work, cost) in generate_lto_work(
1480+
&cgcx,
1481+
autodiff_items.clone(),
1482+
needs_fat_lto,
1483+
needs_thin_lto,
1484+
import_only_modules,
1485+
) {
14651486
let insertion_index = work_items
14661487
.binary_search_by_key(&cost, |&(_, cost)| cost)
14671488
.unwrap_or_else(|e| e);
@@ -1596,6 +1617,10 @@ fn start_executing_work<B: ExtraBackendMethods>(
15961617
main_thread_state = MainThreadState::Idle;
15971618
}
15981619

1620+
Message::AddAutoDiffItems(mut items) => {
1621+
autodiff_items.append(&mut items);
1622+
}
1623+
15991624
Message::CodegenComplete => {
16001625
if codegen_state != Aborted {
16011626
codegen_state = Completed;
@@ -2070,6 +2095,10 @@ impl<B: ExtraBackendMethods> OngoingCodegen<B> {
20702095
drop(self.coordinator.sender.send(Box::new(Message::CodegenComplete::<B>)));
20712096
}
20722097

2098+
pub(crate) fn submit_autodiff_items(&self, items: Vec<AutoDiffItem>) {
2099+
drop(self.coordinator.sender.send(Box::new(Message::<B>::AddAutoDiffItems(items))));
2100+
}
2101+
20732102
pub(crate) fn check_for_errors(&self, sess: &Session) {
20742103
self.shared_emitter_main.check(sess, false);
20752104
}

Diff for: compiler/rustc_codegen_ssa/src/base.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,8 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
620620

621621
// Run the monomorphization collector and partition the collected items into
622622
// codegen units.
623-
let codegen_units = tcx.collect_and_partition_mono_items(()).1;
623+
let (_, autodiff_fncs, codegen_units) = tcx.collect_and_partition_mono_items(());
624+
let autodiff_fncs = autodiff_fncs.to_vec();
624625

625626
// Force all codegen_unit queries so they are already either red or green
626627
// when compile_codegen_unit accesses them. We are not able to re-execute
@@ -691,6 +692,10 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
691692
);
692693
}
693694

695+
if !autodiff_fncs.is_empty() {
696+
ongoing_codegen.submit_autodiff_items(autodiff_fncs);
697+
}
698+
694699
// For better throughput during parallel processing by LLVM, we used to sort
695700
// CGUs largest to smallest. This would lead to better thread utilization
696701
// by, for example, preventing a large CGU from being processed last and
@@ -1051,7 +1056,7 @@ pub(crate) fn provide(providers: &mut Providers) {
10511056
config::OptLevel::SizeMin => config::OptLevel::Default,
10521057
};
10531058

1054-
let (defids, _) = tcx.collect_and_partition_mono_items(cratenum);
1059+
let (defids, _, _) = tcx.collect_and_partition_mono_items(cratenum);
10551060

10561061
let any_for_speed = defids.items().any(|id| {
10571062
let CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);

Diff for: compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+134-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
use std::str::FromStr;
2+
13
use rustc_ast::attr::list_contains_name;
2-
use rustc_ast::{MetaItemInner, attr};
4+
use rustc_ast::expand::autodiff_attrs::{
5+
AutoDiffAttrs, DiffActivity, DiffMode, valid_input_activity, valid_ret_activity,
6+
};
7+
use rustc_ast::{MetaItem, MetaItemInner, attr};
38
use rustc_attr_parsing::{InlineAttr, InstructionSetAttr, OptimizeAttr};
49
use rustc_data_structures::fx::FxHashMap;
510
use rustc_errors::codes::*;
@@ -828,6 +833,133 @@ impl<'a> MixedExportNameAndNoMangleState<'a> {
828833
}
829834
}
830835

836+
/// We now check the #[rustc_autodiff] attributes which we generated from the #[autodiff(...)]
837+
/// macros. There are two forms. The pure one without args to mark primal functions (the functions
838+
/// being differentiated). The other form is #[rustc_autodiff(Mode, ActivityList)] on top of the
839+
/// placeholder functions. We wrote the rustc_autodiff attributes ourself, so this should never
840+
/// panic, unless we introduced a bug when parsing the autodiff macro.
841+
fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
842+
let attrs = tcx.get_attrs(id, sym::rustc_autodiff);
843+
844+
let attrs =
845+
attrs.filter(|attr| attr.name_or_empty() == sym::rustc_autodiff).collect::<Vec<_>>();
846+
847+
// check for exactly one autodiff attribute on placeholder functions.
848+
// There should only be one, since we generate a new placeholder per ad macro.
849+
// TODO: re-enable this. We should fix that rustc_autodiff isn't applied multiple times to the
850+
// source function.
851+
let msg_once = "cg_ssa: implementation bug. Autodiff attribute can only be applied once";
852+
let attr = match attrs.len() {
853+
0 => return AutoDiffAttrs::error(),
854+
1 => attrs.get(0).unwrap(),
855+
_ => {
856+
attrs.get(0).unwrap()
857+
//tcx.dcx().struct_span_err(attrs[1].span, msg_once).with_note("more than one").emit();
858+
//return AutoDiffAttrs::error();
859+
}
860+
};
861+
862+
let list = attr.meta_item_list().unwrap_or_default();
863+
864+
// empty autodiff attribute macros (i.e. `#[autodiff]`) are used to mark source functions
865+
if list.len() == 0 {
866+
return AutoDiffAttrs::source();
867+
}
868+
869+
let [mode, input_activities @ .., ret_activity] = &list[..] else {
870+
tcx.dcx()
871+
.struct_span_err(attr.span, msg_once)
872+
.with_note("Implementation bug in autodiff_attrs. Please report this!")
873+
.emit();
874+
return AutoDiffAttrs::error();
875+
};
876+
let mode = if let MetaItemInner::MetaItem(MetaItem { path: ref p1, .. }) = mode {
877+
p1.segments.first().unwrap().ident
878+
} else {
879+
let msg = "autodiff attribute must contain autodiff mode";
880+
tcx.dcx().struct_span_err(attr.span, msg).with_note("empty argument list").emit();
881+
return AutoDiffAttrs::error();
882+
};
883+
884+
// parse mode
885+
let msg_mode = "mode should be either forward or reverse";
886+
let mode = match mode.as_str() {
887+
"Forward" => DiffMode::Forward,
888+
"Reverse" => DiffMode::Reverse,
889+
"ForwardFirst" => DiffMode::ForwardFirst,
890+
"ReverseFirst" => DiffMode::ReverseFirst,
891+
_ => {
892+
tcx.dcx().struct_span_err(attr.span, msg_mode).with_note("invalid mode").emit();
893+
return AutoDiffAttrs::error();
894+
}
895+
};
896+
897+
// First read the ret symbol from the attribute
898+
let ret_symbol = if let MetaItemInner::MetaItem(MetaItem { path: ref p1, .. }) = ret_activity {
899+
p1.segments.first().unwrap().ident
900+
} else {
901+
let msg = "autodiff attribute must contain the return activity";
902+
tcx.dcx().struct_span_err(attr.span, msg).with_note("missing return activity").emit();
903+
return AutoDiffAttrs::error();
904+
};
905+
906+
// Then parse it into an actual DiffActivity
907+
let msg_unknown_ret_activity = "unknown return activity";
908+
let ret_activity = match DiffActivity::from_str(ret_symbol.as_str()) {
909+
Ok(x) => x,
910+
Err(_) => {
911+
tcx.dcx()
912+
.struct_span_err(attr.span, msg_unknown_ret_activity)
913+
.with_note("invalid return activity")
914+
.emit();
915+
return AutoDiffAttrs::error();
916+
}
917+
};
918+
919+
// Now parse all the intermediate (input) activities
920+
let msg_arg_activity = "autodiff attribute must contain the return activity";
921+
let mut arg_activities: Vec<DiffActivity> = vec![];
922+
for arg in input_activities {
923+
let arg_symbol = if let MetaItemInner::MetaItem(MetaItem { path: ref p2, .. }) = arg {
924+
p2.segments.first().unwrap().ident
925+
} else {
926+
tcx.dcx()
927+
.struct_span_err(attr.span, msg_arg_activity)
928+
.with_note("Implementation bug, please report this!")
929+
.emit();
930+
return AutoDiffAttrs::error();
931+
};
932+
933+
match DiffActivity::from_str(arg_symbol.as_str()) {
934+
Ok(arg_activity) => arg_activities.push(arg_activity),
935+
Err(_) => {
936+
tcx.dcx()
937+
.struct_span_err(attr.span, msg_unknown_ret_activity)
938+
.with_note("invalid input activity")
939+
.emit();
940+
return AutoDiffAttrs::error();
941+
}
942+
}
943+
}
944+
945+
let mut msg = "".to_string();
946+
for &input in &arg_activities {
947+
if !valid_input_activity(mode, input) {
948+
msg = format!("Invalid input activity {} for {} mode", input, mode);
949+
}
950+
}
951+
if !valid_ret_activity(mode, ret_activity) {
952+
msg = format!("Invalid return activity {} for {} mode", ret_activity, mode);
953+
}
954+
if msg != "".to_string() {
955+
tcx.dcx().struct_span_err(attr.span, msg).with_note("invalid activity").emit();
956+
return AutoDiffAttrs::error();
957+
}
958+
959+
AutoDiffAttrs { mode, ret_activity, input_activity: arg_activities }
960+
}
961+
831962
pub(crate) fn provide(providers: &mut Providers) {
832-
*providers = Providers { codegen_fn_attrs, should_inherit_track_caller, ..*providers };
963+
*providers =
964+
Providers { codegen_fn_attrs, should_inherit_track_caller, autodiff_attrs, ..*providers };
833965
}

Diff for: compiler/rustc_codegen_ssa/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ pub(crate) struct CguNotRecorded<'a> {
3939
pub cgu_name: &'a str,
4040
}
4141

42+
#[derive(Diagnostic)]
43+
#[diag(codegen_ssa_autodiff_without_lto)]
44+
pub struct AutodiffWithoutLto;
45+
4246
#[derive(Diagnostic)]
4347
#[diag(codegen_ssa_unknown_reuse_kind)]
4448
pub(crate) struct UnknownReuseKind {

Diff for: compiler/rustc_codegen_ssa/src/traits/write.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
1414
type ModuleBuffer: ModuleBufferMethods;
1515
type ThinData: Send + Sync;
1616
type ThinBuffer: ThinBufferMethods;
17+
//type TypeTree: Clone;
1718

1819
/// Merge all modules into main_module and returning it
1920
fn run_link(
@@ -38,6 +39,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
3839
) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError>;
3940
fn print_pass_timings(&self);
4041
fn print_statistics(&self);
42+
// does enzyme prep work, should do ad too.
4143
unsafe fn optimize(
4244
cgcx: &CodegenContext<Self>,
4345
dcx: DiagCtxtHandle<'_>,

Diff for: compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ fn test_unstable_options_tracking_hash() {
756756
tracked!(allow_features, Some(vec![String::from("lang_items")]));
757757
tracked!(always_encode_mir, true);
758758
tracked!(assume_incomplete_release, true);
759+
tracked!(autodiff, vec![String::from("ad_flags")]);
759760
tracked!(binary_dep_depinfo, true);
760761
tracked!(box_noalias, false);
761762
tracked!(

Diff for: compiler/rustc_middle/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
middle_autodiff_unsafe_inner_const_ref = reading from a `Duplicated` const {$ty} is unsafe
2+
3+
middle_unsupported_union = we don't support unions yet: '{$ty_name}'
4+
15
middle_adjust_for_foreign_abi_error =
26
target architecture {$arch} does not support `extern {$abi}` ABI
37

0 commit comments

Comments
 (0)