Skip to content

Commit 5dbd701

Browse files
committed
single commit with new opt2 impl
1 parent c1db4dc commit 5dbd701

File tree

39 files changed

+1129
-58
lines changed

39 files changed

+1129
-58
lines changed

Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -4121,6 +4121,7 @@ dependencies = [
41214121
name = "rustc_monomorphize"
41224122
version = "0.0.0"
41234123
dependencies = [
4124+
"rustc_ast",
41244125
"rustc_data_structures",
41254126
"rustc_errors",
41264127
"rustc_fluent_macro",
@@ -4129,6 +4130,7 @@ dependencies = [
41294130
"rustc_middle",
41304131
"rustc_session",
41314132
"rustc_span",
4133+
"rustc_symbol_mangling",
41324134
"rustc_target",
41334135
"serde",
41344136
"serde_json",

compiler/rustc_ast/src/expand/autodiff_attrs.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use std::fmt::{self, Display, Formatter};
77
use std::str::FromStr;
88

9-
use crate::expand::typetree::TypeTree;
109
use crate::expand::{Decodable, Encodable, HashStable_Generic};
1110
use crate::ptr::P;
1211
use crate::{Ty, TyKind};
@@ -79,10 +78,6 @@ pub struct AutoDiffItem {
7978
/// The name of the function being generated
8079
pub target: String,
8180
pub attrs: AutoDiffAttrs,
82-
/// Describe the memory layout of input types
83-
pub inputs: Vec<TypeTree>,
84-
/// Describe the memory layout of the output type
85-
pub output: TypeTree,
8681
}
8782
#[derive(Clone, Eq, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
8883
pub struct AutoDiffAttrs {
@@ -266,18 +261,14 @@ impl AutoDiffAttrs {
266261
self,
267262
source: String,
268263
target: String,
269-
inputs: Vec<TypeTree>,
270-
output: TypeTree,
271264
) -> AutoDiffItem {
272-
AutoDiffItem { source, target, inputs, output, attrs: self }
265+
AutoDiffItem { source, target, attrs: self }
273266
}
274267
}
275268

276269
impl fmt::Display for AutoDiffItem {
277270
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
278271
write!(f, "Differentiating {} -> {}", self.source, self.target)?;
279-
write!(f, " with attributes: {:?}", self.attrs)?;
280-
write!(f, " with inputs: {:?}", self.inputs)?;
281-
write!(f, " with output: {:?}", self.output)
272+
write!(f, " with attributes: {:?}", self.attrs)
282273
}
283274
}

compiler/rustc_codegen_llvm/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ codegen_llvm_prepare_thin_lto_module_with_llvm_err = failed to prepare thin LTO
5151
codegen_llvm_run_passes = failed to run LLVM passes
5252
codegen_llvm_run_passes_with_llvm_err = failed to run LLVM passes: {$llvm_err}
5353
54+
codegen_llvm_prepare_autodiff = failed to prepare AutoDiff: src: {$src}, target: {$target}, {$error}
55+
codegen_llvm_prepare_autodiff_with_llvm_err = failed to prepare AutoDiff: {$llvm_err}, src: {$src}, target: {$target}, {$error}
56+
codegen_llvm_autodiff_without_lto = using the autodiff feature requires using fat-lto
57+
5458
codegen_llvm_sanitizer_memtag_requires_mte =
5559
`-Zsanitizer=memtag` requires `-Ctarget-feature=+mte`
5660

compiler/rustc_codegen_llvm/src/attributes.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Set and unset common attributes on LLVM values.
22
3+
use rustc_ast::expand::autodiff_attrs::AutoDiffAttrs;
34
use rustc_attr::{InlineAttr, InstructionSetAttr, OptimizeAttr};
45
use rustc_codegen_ssa::traits::*;
56
use rustc_hir::def_id::DefId;
@@ -332,6 +333,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
332333
instance: ty::Instance<'tcx>,
333334
) {
334335
let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id());
336+
let autodiff_attrs: &AutoDiffAttrs = cx.tcx.autodiff_attrs(instance.def_id());
335337

336338
let mut to_add = SmallVec::<[_; 16]>::new();
337339

@@ -349,6 +351,8 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
349351
let inline =
350352
if codegen_fn_attrs.inline == InlineAttr::None && instance.def.requires_inline(cx.tcx) {
351353
InlineAttr::Hint
354+
} else if autodiff_attrs.is_active() {
355+
InlineAttr::Never
352356
} else {
353357
codegen_fn_attrs.inline
354358
};

compiler/rustc_codegen_llvm/src/back/lto.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,11 @@ pub(crate) fn run_pass_manager(
616616
}
617617
let opt_stage = if thin { llvm::OptStage::ThinLTO } else { llvm::OptStage::FatLTO };
618618
let opt_level = config.opt_level.unwrap_or(config::OptLevel::No);
619-
write::llvm_optimize(cgcx, dcx, module, config, opt_level, opt_stage)?;
619+
620+
// We will run this again with different values in the context of automatic differentiation.
621+
let first_run = true;
622+
debug!("running llvm pm opt pipeline");
623+
write::llvm_optimize(cgcx, dcx, module, config, opt_level, opt_stage, first_run)?;
620624
}
621625
debug!("lto done");
622626
Ok(())
@@ -723,7 +727,11 @@ pub(crate) unsafe fn optimize_thin_module(
723727
let llcx = unsafe { llvm::LLVMRustContextCreate(cgcx.fewer_names) };
724728
let llmod_raw = parse_module(llcx, module_name, thin_module.data(), dcx)? as *const _;
725729
let mut module = ModuleCodegen {
726-
module_llvm: ModuleLlvm { llmod_raw, llcx, tm: ManuallyDrop::new(tm) },
730+
module_llvm: ModuleLlvm {
731+
llmod_raw,
732+
llcx,
733+
tm: ManuallyDrop::new(tm),
734+
},
727735
name: thin_module.name().to_string(),
728736
kind: ModuleKind::Regular,
729737
};

0 commit comments

Comments
 (0)