-
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 |
@bors delegate=@Shoruya742 with print comment adressed |
✌️ @Shoruya742, you can now approve this pull request! If @ZuseZ4 told you to " |
63bbaaa
to
48d05aa
Compare
@bors r=ZuseZ4 |
@Shourya742: 🔑 Insufficient privileges: Not in reviewers |
@Shourya742: 🔑 Insufficient privileges: Not in reviewers |
@ZuseZ4 looks like I can't approve. |
Strange, |
…-inline, r=ZuseZ4 add autodiff inline closes: rust-lang#138920 r? `@ZuseZ4`
Rollup of 4 pull requests Successful merges: - rust-lang#139308 (add autodiff inline) - rust-lang#140291 (Correctly display stdout and stderr in case a doctest is failing) - rust-lang#140297 (Update example to use CStr::to_string_lossy) - rust-lang#140339 (session: Cleanup `CanonicalizedPath::new`) r? `@ghost` `@rustbot` modify labels: rollup
…-inline, r=ZuseZ4 add autodiff inline closes: rust-lang#138920 r? ``@ZuseZ4``
48d05aa
to
bbb9f6c
Compare
Hey @ZuseZ4, it looks like there was a pointer mismatch. I updated all vanilla references to use |
Yes let's try that, but also when comparing it to other code it looks like existing functions (e.g. CreateAttrString) accept &str and use @bors try |
⌛ Trying commit bbb9f6c with merge 93ac4be0c9a102e10028aa511237f84031510b8f... |
…nline, r=<try> add autodiff inline closes: rust-lang#138920 r? `@ZuseZ4` try-job: dist-aarch64-linux
Let me check this — I was under the impression that with |
You're right, either solution should work. It's just the question of whether rustc users will need to allocate a CString which is zero terminated, or a Rust String which contains ptr + len. Working with pure Rust types would be nicer, and as I just noticed when checking the build failure, other LLVM wrappers also seem to make use of the ptr+len combination, so I think it would be good if we copy them. In that case you could also remove your CString and replace it with a normal string. |
☀️ Try build successful - checks-actions |
@ZuseZ4, should we go ahead and push this in? I can follow up with another PR to replace CString with a Rust String and then use StringRef. |
closes: #138920
r? @ZuseZ4
try-job: dist-aarch64-linux