Skip to content

Commit 1203575

Browse files
committed
add option to disable extra tt generation, fmt
1 parent 012062a commit 1203575

File tree

8 files changed

+39
-15
lines changed

8 files changed

+39
-15
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,9 @@ pub(crate) unsafe fn differentiate(
11131113
let name = CString::new(item.source.clone()).unwrap();
11141114
let fn_def: &llvm::Value =
11151115
unsafe { llvm::LLVMGetNamedFunction(llmod, name.as_ptr()).unwrap() };
1116-
crate::builder::add_tt2(llmod, llcx, fn_def, tt);
1116+
if !ad.contains(&AutoDiff::NoTypeTrees) {
1117+
crate::builder::add_tt2(llmod, llcx, fn_def, tt);
1118+
}
11171119

11181120
// Before dumping the module, we also might want to add dummy functions, which will
11191121
// trigger the LLVMEnzyme pass to run on them, if we invoke the opt binary.

compiler/rustc_codegen_llvm/src/builder.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ use crate::abi::FnAbiLlvmExt;
3232
use crate::attributes;
3333
use crate::common::Funclet;
3434
use crate::context::CodegenCx;
35-
use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, True};
35+
use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, Metadata, True};
3636
use crate::type_::Type;
3737
use crate::type_of::LayoutLlvmExt;
3838
use crate::typetree::to_enzyme_typetree;
3939
use crate::value::Value;
40-
use crate::llvm::Metadata;
4140

4241
pub(crate) fn add_tt2<'ll>(
4342
llmod: &'ll llvm::Module,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::str::FromStr;
22

33
use rustc_ast::expand::autodiff_attrs::{
4-
AutoDiffAttrs, DiffActivity, DiffMode, valid_ret_activity, valid_input_activity,
4+
AutoDiffAttrs, DiffActivity, DiffMode, valid_input_activity, valid_ret_activity,
55
};
6-
use rustc_ast::{MetaItemInner, MetaItem, MetaItemKind, ast, attr};
6+
use rustc_ast::{MetaItem, MetaItemInner, MetaItemKind, ast, attr};
77
use rustc_attr::{InlineAttr, InstructionSetAttr, OptimizeAttr, list_contains_name};
88
use rustc_data_structures::fx::FxHashMap;
99
use rustc_errors::codes::*;
@@ -807,7 +807,7 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> AutoDiffAttrs {
807807
attrs.get(0).unwrap()
808808
//tcx.dcx().struct_span_err(attrs[1].span, msg_once).with_note("more than one").emit();
809809
//return AutoDiffAttrs::error();
810-
},
810+
}
811811
};
812812

813813
let list = attr.meta_item_list().unwrap_or_default();

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast::expand::typetree::{FncTree, TypeTree};
22
use rustc_middle::ty::layout::HasTyCtxt;
33
use rustc_middle::ty::{self, Ty, TyCtxt, typetree_from};
44
use rustc_middle::{bug, span_bug};
5-
use rustc_session::config::OptLevel;
5+
use rustc_session::config::{AutoDiff, OptLevel};
66
use rustc_span::{Span, sym};
77
use rustc_target::abi::WrappingRange;
88
use rustc_target::abi::call::{FnAbi, PassMode};
@@ -26,8 +26,15 @@ fn copy_intrinsic<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
2626
) {
2727
let tcx: TyCtxt<'_> = bx.cx().tcx();
2828
let tt: TypeTree = typetree_from(tcx, ty);
29-
let fnc_tree: FncTree =
30-
FncTree { args: vec![tt.clone(), tt.clone(), TypeTree::all_ints()], ret: TypeTree::new() };
29+
let ad = &tcx.sess.opts.unstable_opts.autodiff;
30+
let fnc_tree: Option<FncTree> = if ad.contains(&AutoDiff::NoTypeTrees) {
31+
None
32+
} else {
33+
Some(FncTree {
34+
args: vec![tt.clone(), tt.clone(), TypeTree::all_ints()],
35+
ret: TypeTree::new(),
36+
})
37+
};
3138

3239
let layout = bx.layout_of(ty);
3340
let size = layout.size;
@@ -36,9 +43,9 @@ fn copy_intrinsic<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
3643
let flags = if volatile { MemFlags::VOLATILE } else { MemFlags::empty() };
3744
trace!("copy: mir ty: {:?}, enzyme tt: {:?}", ty, fnc_tree);
3845
if allow_overlap {
39-
bx.memmove(dst, align, src, align, size, flags, Some(fnc_tree));
46+
bx.memmove(dst, align, src, align, size, flags, fnc_tree);
4047
} else {
41-
bx.memcpy(dst, align, src, align, size, flags, Some(fnc_tree));
48+
bx.memcpy(dst, align, src, align, size, flags, fnc_tree);
4249
}
4350
}
4451

@@ -52,15 +59,22 @@ fn memset_intrinsic<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
5259
) {
5360
let tcx: TyCtxt<'_> = bx.cx().tcx();
5461
let tt: TypeTree = typetree_from(tcx, ty);
55-
let fnc_tree: FncTree =
56-
FncTree { args: vec![tt.clone(), tt.clone(), TypeTree::all_ints()], ret: TypeTree::new() };
62+
let ad = &tcx.sess.opts.unstable_opts.autodiff;
63+
let fnc_tree: Option<FncTree> = if ad.contains(&AutoDiff::NoTypeTrees) {
64+
None
65+
} else {
66+
Some(FncTree {
67+
args: vec![tt.clone(), tt.clone(), TypeTree::all_ints()],
68+
ret: TypeTree::new(),
69+
})
70+
};
5771

5872
let layout = bx.layout_of(ty);
5973
let size = layout.size;
6074
let align = layout.align.abi;
6175
let size = bx.mul(bx.const_usize(size.bytes()), count);
6276
let flags = if volatile { MemFlags::VOLATILE } else { MemFlags::empty() };
63-
bx.memset(dst, val, size, align, flags, Some(fnc_tree));
77+
bx.memset(dst, val, size, align, flags, fnc_tree);
6478
}
6579

6680
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {

compiler/rustc_monomorphize/src/partitioning.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ use rustc_middle::util::Providers;
120120
use rustc_session::CodegenUnits;
121121
use rustc_session::config::{DumpMonoStatsFormat, SwitchWithOptPath};
122122
use rustc_span::symbol::Symbol;
123-
use rustc_target::spec::SymbolVisibility;
124123
use rustc_symbol_mangling::symbol_name_for_instance_in_crate;
124+
use rustc_target::spec::SymbolVisibility;
125125
use tracing::{debug, trace};
126126

127127
use crate::collector::{self, MonoItemCollectionStrategy, UsageMap};

compiler/rustc_session/src/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ pub enum AutoDiff {
221221
/// TypeTree options
222222
/// TODO: Figure out how to let users construct these,
223223
/// or whether we want to leave this option in the first place.
224+
NoTypeTrees,
224225
TTWidth(u64),
225226
TTDepth(u64),
226227

compiler/rustc_session/src/options.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,7 @@ mod parse {
10141014
"PrintModAfterOpts" => AutoDiff::PrintModAfterOpts,
10151015
"PrintModAfterEnzyme" => AutoDiff::PrintModAfterEnzyme,
10161016
"LooseTypes" => AutoDiff::LooseTypes,
1017+
"NoTypeTree" => AutoDiff::NoTypeTrees,
10171018
"OPT" => AutoDiff::OPT,
10181019
"NoModOptAfter" => AutoDiff::NoModOptAfter,
10191020
"EnableFncOpt" => AutoDiff::EnableFncOpt,

compiler/rustc_session/src/session.rs

+7
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ pub struct Session {
160160
/// Data about code being compiled, gathered during compilation.
161161
pub code_stats: CodeStats,
162162

163+
/// Tracks if `-Z autodiff="NoTypeTrees"` is specified.
164+
pub emit_type_trees: bool,
165+
163166
/// Tracks fuel info if `-zfuel=crate=n` is specified.
164167
optimization_fuel: Lock<OptimizationFuel>,
165168

@@ -1090,6 +1093,9 @@ pub fn build_session(
10901093
});
10911094
let print_fuel = AtomicU64::new(0);
10921095

1096+
let emit_type_trees =
1097+
!sopts.unstable_opts.autodiff.contains(&crate::config::AutoDiff::NoTypeTrees);
1098+
10931099
let prof = SelfProfilerRef::new(
10941100
self_profiler,
10951101
sopts.unstable_opts.time_passes.then(|| sopts.unstable_opts.time_passes_format),
@@ -1115,6 +1121,7 @@ pub fn build_session(
11151121
incr_comp_session: RwLock::new(IncrCompSession::NotInitialized),
11161122
prof,
11171123
code_stats: Default::default(),
1124+
emit_type_trees,
11181125
optimization_fuel,
11191126
print_fuel,
11201127
jobserver: jobserver::client(),

0 commit comments

Comments
 (0)