Skip to content

Commit 026270a

Browse files
authored
Rollup merge of rust-lang#142640 - Sa4dUs:ad-intrinsic, r=ZuseZ4
Implement autodiff using intrinsics This PR aims to move autodiff logic to `autodiff` intrinsic. Allowing us to delete a great part of our frontend code and overall, simplify the compilation pipeline of autodiff functions.
2 parents efcb7bb + cdd4118 commit 026270a

File tree

41 files changed

+660
-1174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+660
-1174
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4309,7 +4309,6 @@ name = "rustc_monomorphize"
43094309
version = "0.0.0"
43104310
dependencies = [
43114311
"rustc_abi",
4312-
"rustc_ast",
43134312
"rustc_data_structures",
43144313
"rustc_errors",
43154314
"rustc_fluent_macro",
@@ -4318,7 +4317,6 @@ dependencies = [
43184317
"rustc_middle",
43194318
"rustc_session",
43204319
"rustc_span",
4321-
"rustc_symbol_mangling",
43224320
"rustc_target",
43234321
"serde",
43244322
"serde_json",

compiler/rustc_builtin_macros/src/autodiff.rs

Lines changed: 130 additions & 305 deletions
Large diffs are not rendered by default.

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ use gccjit::{CType, Context, OptimizationLevel};
9393
#[cfg(feature = "master")]
9494
use gccjit::{TargetInfo, Version};
9595
use rustc_ast::expand::allocator::AllocatorKind;
96-
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
9796
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule};
9897
use rustc_codegen_ssa::back::write::{
9998
CodegenContext, FatLtoInput, ModuleConfig, TargetMachineFactoryFn,
@@ -363,12 +362,7 @@ impl WriteBackendMethods for GccCodegenBackend {
363362
_exported_symbols_for_lto: &[String],
364363
each_linked_rlib_for_lto: &[PathBuf],
365364
modules: Vec<FatLtoInput<Self>>,
366-
diff_functions: Vec<AutoDiffItem>,
367365
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
368-
if !diff_functions.is_empty() {
369-
unimplemented!();
370-
}
371-
372366
back::lto::run_fat(cgcx, each_linked_rlib_for_lto, modules)
373367
}
374368

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@ pub(crate) fn apply_to_callsite(callsite: &Value, idx: AttributePlace, attrs: &[
2828
}
2929
}
3030

31-
pub(crate) fn has_attr(llfn: &Value, idx: AttributePlace, attr: AttributeKind) -> bool {
32-
llvm::HasAttributeAtIndex(llfn, idx, attr)
33-
}
34-
35-
pub(crate) fn has_string_attr(llfn: &Value, name: &str) -> bool {
36-
llvm::HasStringAttribute(llfn, name)
37-
}
38-
39-
pub(crate) fn remove_from_llfn(llfn: &Value, place: AttributePlace, kind: AttributeKind) {
40-
llvm::RemoveRustEnumAttributeAtIndex(llfn, place, kind);
41-
}
42-
43-
pub(crate) fn remove_string_attr_from_llfn(llfn: &Value, name: &str) {
44-
llvm::RemoveStringAttrFromFn(llfn, name);
45-
}
46-
4731
/// Get LLVM attribute for the provided inline heuristic.
4832
#[inline]
4933
fn inline_attr<'ll>(cx: &CodegenCx<'ll, '_>, inline: InlineAttr) -> Option<&'ll Attribute> {

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ use crate::back::write::{
2424
self, CodegenDiagnosticsStage, DiagnosticHandlers, bitcode_section_name, save_temp_bitcode,
2525
};
2626
use crate::errors::{LlvmError, LtoBitcodeFromRlib};
27-
use crate::llvm::AttributePlace::Function;
2827
use crate::llvm::{self, build_string};
29-
use crate::{LlvmCodegenBackend, ModuleLlvm, SimpleCx, attributes};
28+
use crate::{LlvmCodegenBackend, ModuleLlvm, SimpleCx};
3029

3130
/// We keep track of the computed LTO cache keys from the previous
3231
/// session to determine which CGUs we can reuse.
@@ -593,31 +592,6 @@ pub(crate) fn run_pass_manager(
593592
}
594593

595594
if cfg!(llvm_enzyme) && enable_ad && !thin {
596-
let cx =
597-
SimpleCx::new(module.module_llvm.llmod(), &module.module_llvm.llcx, cgcx.pointer_size);
598-
599-
for function in cx.get_functions() {
600-
let enzyme_marker = "enzyme_marker";
601-
if attributes::has_string_attr(function, enzyme_marker) {
602-
// Sanity check: Ensure 'noinline' is present before replacing it.
603-
assert!(
604-
attributes::has_attr(function, Function, llvm::AttributeKind::NoInline),
605-
"Expected __enzyme function to have 'noinline' before adding 'alwaysinline'"
606-
);
607-
608-
attributes::remove_from_llfn(function, Function, llvm::AttributeKind::NoInline);
609-
attributes::remove_string_attr_from_llfn(function, enzyme_marker);
610-
611-
assert!(
612-
!attributes::has_string_attr(function, enzyme_marker),
613-
"Expected function to not have 'enzyme_marker'"
614-
);
615-
616-
let always_inline = llvm::AttributeKind::AlwaysInline.create_attr(cx.llcx);
617-
attributes::apply_to_llfn(function, Function, &[always_inline]);
618-
}
619-
}
620-
621595
let opt_stage = llvm::OptStage::FatLTO;
622596
let stage = write::AutodiffStage::PostAD;
623597
if !config.autodiff.contains(&config::AutoDiff::NoPostopt) {

0 commit comments

Comments
 (0)