From 75f86e6e2e07c40825e5c7e2f63537efff74a207 Mon Sep 17 00:00:00 2001 From: Manuel Drehwald Date: Sat, 12 Apr 2025 00:50:41 -0400 Subject: fix LooseTypes flag and PrintMod behaviour, add debug helper --- compiler/rustc_codegen_llvm/src/back/write.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'compiler/rustc_codegen_llvm/src/back/write.rs') diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 76d431a4975..f60bc052a12 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -565,6 +565,9 @@ pub(crate) unsafe fn llvm_optimize( let consider_ad = cfg!(llvm_enzyme) && config.autodiff.contains(&config::AutoDiff::Enable); let run_enzyme = autodiff_stage == AutodiffStage::DuringAD; + let print_before_enzyme = config.autodiff.contains(&config::AutoDiff::PrintModBefore); + let print_after_enzyme = config.autodiff.contains(&config::AutoDiff::PrintModAfter); + let print_passes = config.autodiff.contains(&config::AutoDiff::PrintPasses); let unroll_loops; let vectorize_slp; let vectorize_loop; @@ -663,6 +666,9 @@ pub(crate) unsafe fn llvm_optimize( config.no_builtins, config.emit_lifetime_markers, run_enzyme, + print_before_enzyme, + print_after_enzyme, + print_passes, sanitizer_options.as_ref(), pgo_gen_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()), pgo_use_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()), -- cgit 1.4.1-3-g733a5 From 31578dc587ecb0a26bc487ce3999032065b8dfb5 Mon Sep 17 00:00:00 2001 From: Manuel Drehwald Date: Sat, 12 Apr 2025 00:51:16 -0400 Subject: fix "could not find source function" error by preventing function merging before AD --- compiler/rustc_codegen_llvm/src/back/write.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'compiler/rustc_codegen_llvm/src/back/write.rs') diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index f60bc052a12..5e46fb1a1f5 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -568,6 +568,7 @@ pub(crate) unsafe fn llvm_optimize( let print_before_enzyme = config.autodiff.contains(&config::AutoDiff::PrintModBefore); let print_after_enzyme = config.autodiff.contains(&config::AutoDiff::PrintModAfter); let print_passes = config.autodiff.contains(&config::AutoDiff::PrintPasses); + let merge_functions; let unroll_loops; let vectorize_slp; let vectorize_loop; @@ -576,12 +577,14 @@ pub(crate) unsafe fn llvm_optimize( // optimizations until after differentiation. Our pipeline is thus: (opt + enzyme), (full opt). // We therefore have two calls to llvm_optimize, if autodiff is used. if consider_ad && autodiff_stage != AutodiffStage::PostAD { + merge_functions = false; unroll_loops = false; vectorize_slp = false; vectorize_loop = false; } else { unroll_loops = opt_level != config::OptLevel::Size && opt_level != config::OptLevel::SizeMin; + merge_functions = config.merge_functions; vectorize_slp = config.vectorize_slp; vectorize_loop = config.vectorize_loop; } @@ -659,7 +662,7 @@ pub(crate) unsafe fn llvm_optimize( thin_lto_buffer, config.emit_thin_lto, config.emit_thin_lto_summary, - config.merge_functions, + merge_functions, unroll_loops, vectorize_slp, vectorize_loop, -- cgit 1.4.1-3-g733a5 From 5ea9125f378a14fac8c60fe2f0e0993e7af2284b Mon Sep 17 00:00:00 2001 From: Manuel Drehwald Date: Sat, 12 Apr 2025 01:09:14 -0400 Subject: update documentation --- compiler/rustc_codegen_llvm/src/back/write.rs | 5 +++++ compiler/rustc_codegen_llvm/src/builder/autodiff.rs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'compiler/rustc_codegen_llvm/src/back/write.rs') diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 5e46fb1a1f5..3cfd739b70c 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -576,6 +576,11 @@ pub(crate) unsafe fn llvm_optimize( // When we build rustc with enzyme/autodiff support, we want to postpone size-increasing // optimizations until after differentiation. Our pipeline is thus: (opt + enzyme), (full opt). // We therefore have two calls to llvm_optimize, if autodiff is used. + // + // We also must disable merge_functions, since autodiff placeholder/dummy bodies tend to be + // identical. We run opts before AD, so there is a chance that LLVM will merge our dummies. + // In that case, we lack some dummy bodies and can't replace them with the real AD code anymore. + // We then would need to abort compilation. This was especially common in test cases. if consider_ad && autodiff_stage != AutodiffStage::PostAD { merge_functions = false; unroll_loops = false; diff --git a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs index 5e7ef27143b..f5023e0ca5a 100644 --- a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs +++ b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs @@ -445,7 +445,7 @@ pub(crate) fn differentiate<'ll>( return Err(diag_handler.handle().emit_almost_fatal(AutoDiffWithoutEnable)); } - // Before dumping the module, we want all the TypeTrees to become part of the module. + // Here we replace the placeholder code with the actual autodiff code, which calls Enzyme. for item in diff_items.iter() { let name = item.source.clone(); let fn_def: Option<&llvm::Value> = cx.get_function(&name); -- cgit 1.4.1-3-g733a5