Skip to content
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

Allow loading of llvm plugins on nightly #86267

Merged
merged 4 commits into from
Jun 26, 2021
Merged

Allow loading of llvm plugins on nightly #86267

merged 4 commits into from
Jun 26, 2021

Conversation

ZuseZ4
Copy link
Contributor

@ZuseZ4 ZuseZ4 commented Jun 13, 2021

Based on a discussion in #82734 / with @wsmoses.
Mainly moves this behind a -Z flag, so it can only be used on nightly,
as requested by @nagisa in #82734 (comment)

This change allows loading of llvm plugins like Enzyme.
Right now it also requires a shared library LLVM build of rustc for symbol resolution.

// test.rs
extern { fn __enzyme_autodiff(_: usize, ...) -> f64; }

fn square(x : f64) -> f64 {
   return x * x;
}

fn main() {
   unsafe {
      println!("Hello, world {} {}!", square(3.0), __enzyme_autodiff(square as usize, 3.0));
   }
}
./rustc test.rs -Z llvm-plugins="./LLVMEnzyme-12.so" -C passes="enzyme"
./test
Hello, world 9 6!

I will try to figure out how to simplify the usage and get this into stable in a later iteration,
but having this on nightly will already help testing further steps.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @varkor (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 13, 2021
@varkor
Copy link
Member

varkor commented Jun 13, 2021

r? @nagisa

@rust-highfive rust-highfive assigned nagisa and unassigned varkor Jun 13, 2021
compiler/rustc_codegen_llvm/src/llvm_util.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_llvm/src/llvm_util.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_llvm/src/llvm_util.rs Outdated Show resolved Hide resolved
@ZuseZ4
Copy link
Contributor Author

ZuseZ4 commented Jun 23, 2021

I think I pushed the second commit from the wrong build dir, but now it should be all right, unless you have some further change requests?
I will look on how we can expose the missing symbols by default so we don't need the shared lib llvm build anymore, but this will be another PR.

let path = Path::new(plugin);
let res = DynamicLibrary::open(path);
match res {
Ok(_) => debug!("configure_llvm: {}", plugin),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: tracing already includes information about functions and modules that the debug call is in. This should perhaps say more about what's being done here precisely:

Suggested change
Ok(_) => debug!("configure_llvm: {}", plugin),
Ok(_) => debug!("LLVM plugin loaded {} ({})", path.display(), plugin),

Or something along those lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I will update it in this case. But how about this section, is it outdated? https://rustc-dev-guide.rust-lang.org/compiler-debugging.html#logging-etiquette-and-conventions

A loosely followed convention is to use debug!("foo(...)") at the start of a function foo and debug!("foo: ...") within the function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I wonder if that convention predates the use of tracing for logging. @oli-obk do you know?

@nagisa
Copy link
Member

nagisa commented Jun 26, 2021

@bors r+ Nice!

@bors
Copy link
Contributor

bors commented Jun 26, 2021

📌 Commit abdd24a has been approved by nagisa

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 26, 2021
@bors
Copy link
Contributor

bors commented Jun 26, 2021

⌛ Testing commit abdd24a with merge a1411de...

@bors
Copy link
Contributor

bors commented Jun 26, 2021

☀️ Test successful - checks-actions
Approved by: nagisa
Pushing a1411de to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 26, 2021
@bors bors merged commit a1411de into rust-lang:master Jun 26, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jun 26, 2021
@bgat
Copy link

bgat commented Apr 25, 2023

I'm trying to load and use my own LLVM plugin from rustc, and getting the following error, even though the very same .so file works when use it via opt:

error: failed to run LLVM passes: Failed to load pass plugin/home/ubuntu/coast/projects/build/DWC/libDWC.so

I'm using the legacy plugin format. Is the above related to the, "Right now it also requires a shared library LLVM build of rustc for symbol resolution" requirement?

@ZuseZ4
Copy link
Contributor Author

ZuseZ4 commented Apr 25, 2023

what do you get from opt --version ?

I don't think that the latest LLVM (which rustc is building or downloading by default) does support legacy plugins at all.
I was actually about to propose to remove this legacy fallback one of these days.
Is there a reason why you can't move to the newPM?

@bgat
Copy link

bgat commented Apr 25, 2023

The plugin I'm using right now was written as a legacy plugin. I was hoping it would Just Work for rustc. :-)

ubuntu@ip-172-31-49-79:~$ opt-15 --version
Ubuntu LLVM version 15.0.6
Optimized build.
Default target: x86_64-pc-linux-gnu
Host CPU: skylake-avx512

@ZuseZ4
Copy link
Contributor Author

ZuseZ4 commented Apr 25, 2023

I think in this case you should probably just take an older rust commit to build. I don't think that you will get a legacy commit working eith an up do date LLVM

@bgat
Copy link

bgat commented Apr 25, 2023

I'll update my plugin to the modern format, and try again. I don't think I can go far enough backwards with rustc to make a difference here. Will let you know.

@CXWorks
Copy link

CXWorks commented Apr 6, 2024

Hi @ZuseZ4 , I'm trying to use my own afl-pass with asan like below:

RUSTFLAGS=" -Z llvm-plugins=./afl-llvm-pass.so -C passes=hello" cargo build --bin xxx

But it seems doesn't work: error: failed to run LLVM passes: unknown pass name 'hello'

@ZuseZ4
Copy link
Contributor Author

ZuseZ4 commented Apr 6, 2024

@CXWorks You should look at newer PRs mentioning LLVM plugins - My original plugin infra iirc got removed and replaced by a newer, since LLVM plugins for the new PassManager work a bit different. You would need to look at those PRs to figure out how to use them. For my own project I moved away from the plugin interface, since it was too limited, so I didn't follow closely.

@CXWorks
Copy link

CXWorks commented Apr 6, 2024

Thanks for your quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants