Skip to content

Commit a1411de

Browse files
committed
Auto merge of rust-lang#86267 - ZuseZ4:master, r=nagisa
Allow loading of llvm plugins on nightly Based on a discussion in rust-lang#82734 / with `@wsmoses.` Mainly moves [this](wsmoses@0149bc4) behind a -Z flag, so it can only be used on nightly, as requested by `@nagisa` in rust-lang#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. ```rust // 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.
2 parents 3ddb78a + abdd24a commit a1411de

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

compiler/rustc_codegen_llvm/src/llvm_util.rs

+14
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ use crate::{llvm, llvm_util};
33
use libc::c_int;
44
use rustc_codegen_ssa::target_features::supported_target_features;
55
use rustc_data_structures::fx::FxHashSet;
6+
use rustc_metadata::dynamic_lib::DynamicLibrary;
67
use rustc_middle::bug;
78
use rustc_session::config::PrintRequest;
89
use rustc_session::Session;
910
use rustc_span::symbol::Symbol;
1011
use rustc_target::spec::{MergeFunctions, PanicStrategy};
1112
use std::ffi::{CStr, CString};
13+
use tracing::debug;
1214

15+
use std::mem;
16+
use std::path::Path;
1317
use std::ptr;
1418
use std::slice;
1519
use std::str;
@@ -129,6 +133,16 @@ unsafe fn configure_llvm(sess: &Session) {
129133

130134
llvm::LLVMInitializePasses();
131135

136+
for plugin in &sess.opts.debugging_opts.llvm_plugins {
137+
let path = Path::new(plugin);
138+
let res = DynamicLibrary::open(path);
139+
match res {
140+
Ok(_) => debug!("LLVM plugin loaded succesfully {} ({})", path.display(), plugin),
141+
Err(e) => bug!("couldn't load plugin: {}", e),
142+
}
143+
mem::forget(res);
144+
}
145+
132146
rustc_llvm::initialize_available_targets();
133147

134148
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr());

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ fn test_debugging_options_tracking_hash() {
715715
tracked!(instrument_coverage, Some(InstrumentCoverage::All));
716716
tracked!(instrument_mcount, true);
717717
tracked!(link_only, true);
718+
tracked!(llvm_plugins, vec![String::from("plugin_name")]);
718719
tracked!(merge_functions, Some(MergeFunctions::Disabled));
719720
tracked!(mir_emit_retag, true);
720721
tracked!(mir_opt_level, Some(4));

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,8 @@ options! {
11291129
"link native libraries in the linker invocation (default: yes)"),
11301130
link_only: bool = (false, parse_bool, [TRACKED],
11311131
"link the `.rlink` file generated by `-Z no-link` (default: no)"),
1132+
llvm_plugins: Vec<String> = (Vec::new(), parse_list, [TRACKED],
1133+
"a list LLVM plugins to enable (space separated)"),
11321134
llvm_time_trace: bool = (false, parse_bool, [UNTRACKED],
11331135
"generate JSON tracing data file from LLVM data (default: no)"),
11341136
ls: bool = (false, parse_bool, [UNTRACKED],

0 commit comments

Comments
 (0)