Skip to content

Commit f253e67

Browse files
committed
update rustc_middle, rustc_monomorphize, and (partly) cg_ssa
1 parent 51e0b65 commit f253e67

File tree

13 files changed

+425
-15
lines changed

13 files changed

+425
-15
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
409409
/// All items participating in code generation together with (instrumented)
410410
/// items inlined into them.
411411
fn codegenned_and_inlined_items(tcx: TyCtxt<'_>) -> DefIdSet {
412-
let (items, cgus) = tcx.collect_and_partition_mono_items(());
412+
let (items, _, cgus) = tcx.collect_and_partition_mono_items(());
413413
let mut visited = DefIdSet::default();
414414
let mut result = items.clone();
415415

compiler/rustc_codegen_ssa/src/assert_module_sources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>, set_reuse: &dyn Fn(&mut CguReuseTr
4646
}
4747

4848
let available_cgus =
49-
tcx.collect_and_partition_mono_items(()).1.iter().map(|cgu| cgu.name()).collect();
49+
tcx.collect_and_partition_mono_items(()).2.iter().map(|cgu| cgu.name()).collect();
5050

5151
let mut ams = AssertModuleSource {
5252
tcx,

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ fn exported_symbols_provider_local(
331331
// external linkage is enough for monomorphization to be linked to.
332332
let need_visibility = tcx.sess.target.dynamic_linking && !tcx.sess.target.only_cdylib;
333333

334-
let (_, cgus) = tcx.collect_and_partition_mono_items(());
334+
let (_, _, cgus) = tcx.collect_and_partition_mono_items(());
335335

336336
for (mono_item, data) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
337337
if data.linkage != Linkage::External {

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
594594

595595
// Run the monomorphization collector and partition the collected items into
596596
// codegen units.
597-
let codegen_units = tcx.collect_and_partition_mono_items(()).1;
597+
let codegen_units = tcx.collect_and_partition_mono_items(()).2;
598598

599599
// Force all codegen_unit queries so they are already either red or green
600600
// when compile_codegen_unit accesses them. We are not able to re-execute
@@ -980,7 +980,7 @@ pub fn provide(providers: &mut Providers) {
980980
config::OptLevel::SizeMin => config::OptLevel::Default,
981981
};
982982

983-
let (defids, _) = tcx.collect_and_partition_mono_items(cratenum);
983+
let (defids, _, _) = tcx.collect_and_partition_mono_items(cratenum);
984984

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

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 173 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use rustc_ast::{ast, attr, MetaItemKind, NestedMetaItem};
1+
use rustc_ast::{ast, attr, MetaItem, MetaItemKind, NestedMetaItem};
2+
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
23
use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr};
34
use rustc_errors::struct_span_err;
45
use rustc_hir as hir;
@@ -14,6 +15,8 @@ use rustc_span::symbol::Ident;
1415
use rustc_span::{sym, Span};
1516
use rustc_target::spec::{abi, SanitizerSet};
1617

18+
use std::str::FromStr;
19+
1720
use crate::errors;
1821
use crate::target_features::from_target_feature;
1922
use crate::{
@@ -689,6 +692,174 @@ fn check_link_name_xor_ordinal(
689692
}
690693
}
691694

695+
fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
696+
//let attrs = tcx.get_attrs(id, sym::autodiff_into);
697+
let attrs = tcx.get_attrs(id, sym::autodiff);
698+
699+
let attrs = attrs
700+
.into_iter()
701+
.filter(|attr| attr.name_or_empty() == sym::autodiff)
702+
//.filter(|attr| attr.name_or_empty() == sym::autodiff_into)
703+
.collect::<Vec<_>>();
704+
if attrs.len() > 0 {
705+
dbg!("autodiff_attrs len = > 0: {}", attrs.len());
706+
}
707+
708+
// check for exactly one autodiff attribute on extern block
709+
let msg_once = "autodiff attribute can only be applied once";
710+
let attr = match &attrs[..] {
711+
&[] => return AutoDiffAttrs::inactive(),
712+
&[elm] => elm,
713+
x => {
714+
tcx.sess
715+
.struct_span_err(x[1].span, msg_once)
716+
.span_label(x[1].span, "more than one")
717+
.emit();
718+
719+
return AutoDiffAttrs::inactive();
720+
}
721+
};
722+
723+
let list = attr.meta_item_list().unwrap_or_default();
724+
725+
// empty autodiff attribute macros (i.e. `#[autodiff]`) are used to mark source functions
726+
if list.len() == 0 {
727+
return AutoDiffAttrs {
728+
mode: DiffMode::Source,
729+
ret_activity: DiffActivity::None,
730+
input_activity: Vec::new(),
731+
};
732+
}
733+
734+
let msg_ad_mode = "autodiff attribute must contain autodiff mode";
735+
let mode = match &list[0] {
736+
NestedMetaItem::MetaItem(MetaItem { path: ref p2, kind: MetaItemKind::Word, .. }) => {
737+
p2.segments.first().unwrap().ident
738+
}
739+
_ => {
740+
tcx.sess
741+
.struct_span_err(attr.span, msg_ad_mode)
742+
.span_label(attr.span, "empty argument list")
743+
.emit();
744+
745+
return AutoDiffAttrs::inactive();
746+
}
747+
};
748+
749+
// parse mode
750+
let msg_mode = "mode should be either forward or reverse";
751+
let mode = match mode.as_str() {
752+
//map(|x| x.as_str()) {
753+
"Forward" => DiffMode::Forward,
754+
"Reverse" => DiffMode::Reverse,
755+
_ => {
756+
tcx.sess
757+
.struct_span_err(attr.span, msg_mode)
758+
.span_label(attr.span, "invalid mode")
759+
.emit();
760+
761+
return AutoDiffAttrs::inactive();
762+
}
763+
};
764+
765+
let msg_ret_activity = "autodiff attribute must contain the return activity";
766+
let ret_symbol = match &list[1] {
767+
NestedMetaItem::MetaItem(MetaItem { path: ref p2, kind: MetaItemKind::Word, .. }) => {
768+
p2.segments.first().unwrap().ident
769+
}
770+
_ => {
771+
tcx.sess
772+
.struct_span_err(attr.span, msg_ret_activity)
773+
.span_label(attr.span, "missing return activity")
774+
.emit();
775+
776+
return AutoDiffAttrs::inactive();
777+
}
778+
};
779+
780+
let msg_unknown_ret_activity = "unknown return activity";
781+
let ret_activity = match DiffActivity::from_str(ret_symbol.as_str()) {
782+
Ok(x) => x,
783+
Err(_) => {
784+
tcx.sess
785+
.struct_span_err(attr.span, msg_unknown_ret_activity)
786+
.span_label(attr.span, "invalid return activity")
787+
.emit();
788+
789+
return AutoDiffAttrs::inactive();
790+
}
791+
};
792+
793+
let msg_arg_activity = "autodiff attribute must contain the return activity";
794+
let mut arg_activities: Vec<DiffActivity> = vec![];
795+
for arg in &list[2..] {
796+
let arg_symbol = match arg {
797+
NestedMetaItem::MetaItem(MetaItem {
798+
path: ref p2, kind: MetaItemKind::Word, ..
799+
}) => p2.segments.first().unwrap().ident,
800+
_ => {
801+
tcx.sess
802+
.struct_span_err(
803+
attr.span, msg_arg_activity,
804+
)
805+
.span_label(attr.span, "missing return activity")
806+
.emit();
807+
808+
return AutoDiffAttrs::inactive();
809+
}
810+
};
811+
812+
match DiffActivity::from_str(arg_symbol.as_str()) {
813+
Ok(arg_activity) => arg_activities.push(arg_activity),
814+
Err(_) => {
815+
tcx.sess
816+
.struct_span_err(attr.span, msg_unknown_ret_activity)
817+
.span_label(attr.span, "invalid input activity")
818+
.emit();
819+
820+
return AutoDiffAttrs::inactive();
821+
}
822+
}
823+
}
824+
825+
let msg_fwd_incompatible_ret = "Forward Mode is incompatible with Active ret";
826+
let msg_fwd_incompatible_arg = "Forward Mode is incompatible with Active ret";
827+
let msg_rev_incompatible_arg = "Reverse Mode is only compatible with Active, None, or Const ret";
828+
if mode == DiffMode::Forward {
829+
if ret_activity == DiffActivity::Active {
830+
tcx.sess
831+
.struct_span_err(attr.span, msg_fwd_incompatible_ret)
832+
.span_label(attr.span, "invalid return activity")
833+
.emit();
834+
return AutoDiffAttrs::inactive();
835+
}
836+
if arg_activities.iter().filter(|&x| *x == DiffActivity::Active).count() > 0 {
837+
tcx.sess
838+
.struct_span_err(attr.span, msg_fwd_incompatible_arg)
839+
.span_label(attr.span, "invalid input activity")
840+
.emit();
841+
return AutoDiffAttrs::inactive();
842+
}
843+
}
844+
845+
if mode == DiffMode::Reverse {
846+
if ret_activity == DiffActivity::Duplicated
847+
|| ret_activity == DiffActivity::DuplicatedNoNeed
848+
{
849+
tcx.sess
850+
.struct_span_err(
851+
attr.span, msg_rev_incompatible_arg,
852+
)
853+
.span_label(attr.span, "invalid return activity")
854+
.emit();
855+
return AutoDiffAttrs::inactive();
856+
}
857+
}
858+
859+
AutoDiffAttrs { mode, ret_activity, input_activity: arg_activities }
860+
}
861+
692862
pub fn provide(providers: &mut Providers) {
693-
*providers = Providers { codegen_fn_attrs, should_inherit_track_caller, ..*providers };
863+
*providers =
864+
Providers { codegen_fn_attrs, should_inherit_track_caller, autodiff_attrs, ..*providers };
694865
}

compiler/rustc_middle/src/arena.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ macro_rules! arena_types {
8888
[] upvars_mentioned: rustc_data_structures::fx::FxIndexMap<rustc_hir::HirId, rustc_hir::Upvar>,
8989
[] object_safety_violations: rustc_middle::traits::ObjectSafetyViolation,
9090
[] codegen_unit: rustc_middle::mir::mono::CodegenUnit<'tcx>,
91+
[] autodiff_item: rustc_ast::expand::autodiff_attrs::AutoDiffItem,
9192
[decode] attribute: rustc_ast::Attribute,
9293
[] name_set: rustc_data_structures::unord::UnordSet<rustc_span::symbol::Symbol>,
9394
[] ordered_name_set: rustc_data_structures::fx::FxIndexSet<rustc_span::symbol::Symbol>,

compiler/rustc_middle/src/query/erase.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ impl<T0, T1> EraseType for (&'_ T0, &'_ [T1]) {
200200
type Result = [u8; size_of::<(&'static (), &'static [()])>()];
201201
}
202202

203+
impl<T0, T1, T2> EraseType for (&'_ T0, &'_ [T1], &'_ [T2]) {
204+
type Result = [u8; size_of::<(&'static (), &'static [()], &'static [()])>()];
205+
}
206+
203207
macro_rules! trivial {
204208
($($ty:ty),+ $(,)?) => {
205209
$(

compiler/rustc_middle/src/query/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use crate::ty::{GenericArg, GenericArgsRef};
5555
use rustc_arena::TypedArena;
5656
use rustc_ast as ast;
5757
use rustc_ast::expand::{allocator::AllocatorKind, StrippedCfgItem};
58+
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, AutoDiffItem};
5859
use rustc_attr as attr;
5960
use rustc_data_structures::fingerprint::Fingerprint;
6061
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
@@ -1227,6 +1228,13 @@ rustc_queries! {
12271228
separate_provide_extern
12281229
}
12291230

1231+
/// The list autodiff extern functions in current crate
1232+
query autodiff_attrs(def_id: DefId) -> &'tcx AutoDiffAttrs {
1233+
desc { |tcx| "computing autodiff attributes of `{}`", tcx.def_path_str(def_id) }
1234+
arena_cache
1235+
cache_on_disk_if { def_id.is_local() }
1236+
}
1237+
12301238
query asm_target_features(def_id: DefId) -> &'tcx FxIndexSet<Symbol> {
12311239
desc { |tcx| "computing target features for inline asm of `{}`", tcx.def_path_str(def_id) }
12321240
}
@@ -1880,7 +1888,7 @@ rustc_queries! {
18801888
separate_provide_extern
18811889
}
18821890

1883-
query collect_and_partition_mono_items(_: ()) -> (&'tcx DefIdSet, &'tcx [CodegenUnit<'tcx>]) {
1891+
query collect_and_partition_mono_items(_: ()) -> (&'tcx DefIdSet, &'tcx [AutoDiffItem], &'tcx [CodegenUnit<'tcx>]) {
18841892
eval_always
18851893
desc { "collect_and_partition_mono_items" }
18861894
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ pub struct ResolverAstLowering {
195195
pub node_id_to_def_id: NodeMap<LocalDefId>,
196196
pub def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
197197

198+
/// Mapping of autodiff function IDs
199+
pub autodiff_map: FxHashMap<LocalDefId, LocalDefId>,
198200
pub trait_map: NodeMap<Vec<hir::TraitCandidate>>,
199201
/// List functions and methods for which lifetime elision was successful.
200202
pub lifetime_elision_allowed: FxHashSet<ast::NodeId>,

compiler/rustc_monomorphize/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
# tidy-alphabetical-start
8+
rustc_ast = { path = "../rustc_ast" }
89
rustc_data_structures = { path = "../rustc_data_structures" }
910
rustc_errors = { path = "../rustc_errors" }
1011
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
@@ -13,6 +14,7 @@ rustc_macros = { path = "../rustc_macros" }
1314
rustc_middle = { path = "../rustc_middle" }
1415
rustc_session = { path = "../rustc_session" }
1516
rustc_span = { path = "../rustc_span" }
17+
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
1618
rustc_target = { path = "../rustc_target" }
1719
serde = "1"
1820
serde_json = "1"

0 commit comments

Comments
 (0)