-
Notifications
You must be signed in to change notification settings - Fork 13.3k
add autodiff inline #139308
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
base: master
Are you sure you want to change the base?
add autodiff inline #139308
Conversation
Thank you for looking into this! There are two things to improve here, first we want to write that in safer Rust, and second this currently adds As a rule of thumb I often noticed during my autodiff work, that if you want to add something to an LLVM component, then rustc likely already has wrappers for it. If you want to get something out of an LLVM component, chances are you have to write those safe wrappers, since rustc usually just lowers things. Here is an example I found after using rg (ripgrep) to find inline related matches in rustc_codegen_llvm:
SimpleCx and CodegenCx are safe wrappers around llvm modules to help you handle things safely. The CodegenCx is more powerful, but also needs more inputs to be constructed. You probably won't be able to generate a full CodegenCx, since you don't have a TypeContext (tcx) available. Instead search for the SimpleCx, you should be able to create one instead. In the next steps we can then use the SimpleCx, to safely replace the 4 LLVM functions which you are currently introducing. (The way I usually find safe wrappers systematically is by first looking up the llvm function I need, then searching for existing uses (wrappers) in rustc, and then going up the wrapper chain till I find one that I can use.) |
☔ The latest upstream changes (presumably #139396) made this pull request unmergeable. Please resolve the merge conflicts. |
5fddbe3
to
530b565
Compare
@rustbot review |
Thanks, that looks pretty good now. I'm a bit worried about regressing this, though, since just missing an inline will probably not be noticed immediately. So a |
Hello @ZuseZ4, I’ve locally rebased my PR onto #139700, and in the generated IR, I’m seeing the following: ; Function Attrs: alwaysinline noinline
declare double @__enzyme_autodiff_ZN6inline8d_square17h021c74e92c259cdeE(...) local_unnamed_addr #8 This looks correct to me. I used |
oh, the test is helpful. I'm not sure what LLVM would do, but I think specifying Can you (before adding alwaysinline) go through fn attrs, and assert that there is a noinline attr, with a comment that this check isn't strictly necessary, but just a guard to remind us if this code path ever changes? Then remove the no inline attr before adding always inpine. That should make it more robust. |
2b58c16
to
b149ed4
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
ce3e200
to
4a1b3b1
Compare
Hi @ZuseZ4, I added |
@rustbot review |
Ok, I've experimented a bit. In general, your As to why your function didn't remove the attribute, I'm not fully sure, but I remember that I ran into the same bug a while ago. I ended up copying me old code here: https://github.com/EnzymeAD/rust/blob/322f2226c1f672c9b5e934b15d255ae0d66bd0e2/compiler/rustc_codegen_llvm/src/back/write.rs#L1195 I pushed my code here. Sorry that it's quite messy, I cherry picked both my flags PR and your code (and messed up the cherry picking on top of that). Also, it's not using any pretty wrappers, but it hopefully shows how to remove the attribute. Either in this or a follow-up PR we should also be a bit more precise, since e.g. the |
Ok, I think also remembered again why your approach didn't work. |
That did the trick— |
Thanks a lot for the thorough explanation!. The marker-based approach you added is a big improvement. Totally agree that relying on string matching for |
This comment has been minimized.
This comment has been minimized.
e1266af
to
752ebf9
Compare
Overall looks good, just left some nits. Can you clean up the history now, maybe bring it down to 2-3 commits (or just squash it all if you don't want to deal with it)? |
[DO NOT MERGE] start building enzyme on x86_64-gnu-llvm-{19|20} builders My goal is to put this in CI on April 26, to have a week to land some of the outstanding PRs: rust-lang#139700 rust-lang#139308 rust-lang#139557 rust-lang#140030 rust-lang#140049 The autodiff flags PR should land first, but otherwise they don't overlap and are mostly ready, so it shouldn't be too hard to land them. In the meantime, I'll experiment here with some builders. r? `@oli-obk` Tracking: - rust-lang#124509 try-job: x86_64-gnu-llvm-19 try-job: x86_64-gnu-llvm-20
[DO NOT MERGE] start building enzyme on x86_64-gnu-llvm-{19|20} builders My goal is to put this in CI on April 26, to have a week to land some of the outstanding PRs: rust-lang#139700 rust-lang#139308 rust-lang#139557 rust-lang#140030 rust-lang#140049 The autodiff flags PR should land first, but otherwise they don't overlap and are mostly ready, so it shouldn't be too hard to land them. In the meantime, I'll experiment here with some builders. r? `@oli-obk` Tracking: - rust-lang#124509 try-job: dist-x86_64-linux
752ebf9
to
215f60f
Compare
closes: #138920
r? @ZuseZ4