-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Autodiff Upstreaming - rustc_codegen_llvm changes #130060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ use rustc_session::config::{ | |
}; | ||
use rustc_span::{BytePos, InnerSpan, Pos, SpanData, SyntaxContext, sym}; | ||
use rustc_target::spec::{CodeModel, FloatAbi, RelocModel, SanitizerSet, SplitDebuginfo, TlsModel}; | ||
use tracing::debug; | ||
use tracing::{debug, trace}; | ||
|
||
use crate::back::lto::ThinBuffer; | ||
use crate::back::owned_target_machine::OwnedTargetMachine; | ||
|
@@ -537,9 +537,35 @@ pub(crate) unsafe fn llvm_optimize( | |
config: &ModuleConfig, | ||
opt_level: config::OptLevel, | ||
opt_stage: llvm::OptStage, | ||
skip_size_increasing_opts: bool, | ||
) -> Result<(), FatalError> { | ||
let unroll_loops = | ||
opt_level != config::OptLevel::Size && opt_level != config::OptLevel::SizeMin; | ||
// Enzyme: | ||
// The whole point of compiler based AD is to differentiate optimized IR instead of unoptimized | ||
// source code. However, benchmarks show that optimizations increasing the code size | ||
davidtwco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// tend to reduce AD performance. Therefore deactivate them before AD, then differentiate the code | ||
// and finally re-optimize the module, now with all optimizations available. | ||
// FIXME(ZuseZ4): In a future update we could figure out how to only optimize individual functions getting | ||
// differentiated. | ||
|
||
let unroll_loops; | ||
let vectorize_slp; | ||
let vectorize_loop; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Orthogonal, can happen on master: these three could be fields of a struct, as we're passing them around together anyway |
||
|
||
// When we build rustc with enzyme/autodiff support, we want to postpone size-increasing | ||
// optimizations until after differentiation. FIXME(ZuseZ4): Before shipping on nightly, | ||
// we should make this more granular, or at least check that the user has at least one autodiff | ||
// call in their code, to justify altering the compilation pipeline. | ||
if skip_size_increasing_opts && cfg!(llvm_enzyme) { | ||
unroll_loops = false; | ||
vectorize_slp = false; | ||
vectorize_loop = false; | ||
} else { | ||
unroll_loops = | ||
opt_level != config::OptLevel::Size && opt_level != config::OptLevel::SizeMin; | ||
vectorize_slp = config.vectorize_slp; | ||
vectorize_loop = config.vectorize_loop; | ||
} | ||
trace!(?unroll_loops, ?vectorize_slp, ?vectorize_loop); | ||
let using_thin_buffers = opt_stage == llvm::OptStage::PreLinkThinLTO || config.bitcode_needed(); | ||
let pgo_gen_path = get_pgo_gen_path(config); | ||
let pgo_use_path = get_pgo_use_path(config); | ||
|
@@ -603,8 +629,8 @@ pub(crate) unsafe fn llvm_optimize( | |
using_thin_buffers, | ||
config.merge_functions, | ||
unroll_loops, | ||
config.vectorize_slp, | ||
config.vectorize_loop, | ||
vectorize_slp, | ||
vectorize_loop, | ||
config.no_builtins, | ||
config.emit_lifetime_markers, | ||
sanitizer_options.as_ref(), | ||
|
@@ -648,14 +674,29 @@ pub(crate) unsafe fn optimize( | |
unsafe { llvm::LLVMWriteBitcodeToFile(llmod, out.as_ptr()) }; | ||
} | ||
|
||
// FIXME(ZuseZ4): support SanitizeHWAddress and prevent illegal/unsupported opts | ||
|
||
if let Some(opt_level) = config.opt_level { | ||
let opt_stage = match cgcx.lto { | ||
Lto::Fat => llvm::OptStage::PreLinkFatLTO, | ||
Lto::Thin | Lto::ThinLocal => llvm::OptStage::PreLinkThinLTO, | ||
_ if cgcx.opts.cg.linker_plugin_lto.enabled() => llvm::OptStage::PreLinkThinLTO, | ||
_ => llvm::OptStage::PreLinkNoLTO, | ||
}; | ||
return unsafe { llvm_optimize(cgcx, dcx, module, config, opt_level, opt_stage) }; | ||
|
||
// If we know that we will later run AD, then we disable vectorization and loop unrolling | ||
let skip_size_increasing_opts = cfg!(llvm_enzyme); | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return unsafe { | ||
llvm_optimize( | ||
cgcx, | ||
dcx, | ||
module, | ||
config, | ||
opt_level, | ||
opt_stage, | ||
skip_size_increasing_opts, | ||
) | ||
}; | ||
} | ||
Ok(()) | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.