From 4dbdcd1c5cc5f2e61495af95d81b1fc588506aa4 Mon Sep 17 00:00:00 2001 From: Manuel Drehwald Date: Sun, 13 Jun 2021 18:23:01 +0200 Subject: [PATCH 1/4] allow loading of llvm plugins on nightly --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 10 ++++++++++ compiler/rustc_interface/src/tests.rs | 1 + compiler/rustc_session/src/options.rs | 2 ++ 3 files changed, 13 insertions(+) diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 0dd3d2ae15bca..a15edaf513858 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -129,6 +129,16 @@ unsafe fn configure_llvm(sess: &Session) { llvm::LLVMInitializePasses(); + for plugin in &sess.opts.debugging_opts.llvm_plugins { + let path = CString::new(plugin.as_bytes()).unwrap(); + let res = libc::dlopen(path.as_ptr(), libc::RTLD_LAZY | libc::RTLD_GLOBAL); + if res.is_null() { + println!("{}", CStr::from_ptr(libc::dlerror()).to_string_lossy().into_owned()); + } + println!("{:p}", res); + println!("{}", plugin); + } + rustc_llvm::initialize_available_targets(); llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr()); diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index 5d8a6084f2e0b..89adb34919057 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -707,6 +707,7 @@ fn test_debugging_options_tracking_hash() { tracked!(instrument_coverage, Some(InstrumentCoverage::All)); tracked!(instrument_mcount, true); tracked!(link_only, true); + tracked!(llvm_plugins, vec![String::from("plugin_name")]); tracked!(merge_functions, Some(MergeFunctions::Disabled)); tracked!(mir_emit_retag, true); tracked!(mir_opt_level, Some(4)); diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 1946bfd78cc38..ca0a4ecd81c0f 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1122,6 +1122,8 @@ options! { "link native libraries in the linker invocation (default: yes)"), link_only: bool = (false, parse_bool, [TRACKED], "link the `.rlink` file generated by `-Z no-link` (default: no)"), + llvm_plugins: Vec = (Vec::new(), parse_list, [TRACKED], + "a list LLVM plugins to enable (space separated)"), llvm_time_trace: bool = (false, parse_bool, [UNTRACKED], "generate JSON tracing data file from LLVM data (default: no)"), ls: bool = (false, parse_bool, [UNTRACKED], From 9f406ce2c799cd39173f43cff8288674710f91d7 Mon Sep 17 00:00:00 2001 From: Manuel Drehwald Date: Mon, 21 Jun 2021 01:38:25 +0200 Subject: [PATCH 2/4] addressing feedback --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index a15edaf513858..1972e96d10eb9 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -130,13 +130,12 @@ unsafe fn configure_llvm(sess: &Session) { llvm::LLVMInitializePasses(); for plugin in &sess.opts.debugging_opts.llvm_plugins { - let path = CString::new(plugin.as_bytes()).unwrap(); - let res = libc::dlopen(path.as_ptr(), libc::RTLD_LAZY | libc::RTLD_GLOBAL); - if res.is_null() { - println!("{}", CStr::from_ptr(libc::dlerror()).to_string_lossy().into_owned()); + let path = path::Path::new(plugin); + let res = DynamicLibrary::open(path); + match res { + Ok(_) => debug!("configure_llvm: {}", plugin), + Err(e) => bug!("couldn't load plugin: {}", e), } - println!("{:p}", res); - println!("{}", plugin); } rustc_llvm::initialize_available_targets(); From f454aab3d6951aefbdec39fd99b473cfc0579acc Mon Sep 17 00:00:00 2001 From: Manuel Drehwald Date: Wed, 23 Jun 2021 04:26:14 +0200 Subject: [PATCH 3/4] Add missing use --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 1972e96d10eb9..52d88df5b3fd0 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -3,13 +3,16 @@ use crate::{llvm, llvm_util}; use libc::c_int; use rustc_codegen_ssa::target_features::supported_target_features; use rustc_data_structures::fx::FxHashSet; +use rustc_metadata::dynamic_lib::DynamicLibrary; use rustc_middle::bug; use rustc_session::config::PrintRequest; use rustc_session::Session; use rustc_span::symbol::Symbol; use rustc_target::spec::{MergeFunctions, PanicStrategy}; use std::ffi::{CStr, CString}; +use tracing::debug; +use std::path::Path; use std::ptr; use std::slice; use std::str; @@ -130,7 +133,7 @@ unsafe fn configure_llvm(sess: &Session) { llvm::LLVMInitializePasses(); for plugin in &sess.opts.debugging_opts.llvm_plugins { - let path = path::Path::new(plugin); + let path = Path::new(plugin); let res = DynamicLibrary::open(path); match res { Ok(_) => debug!("configure_llvm: {}", plugin), From abdd24a0404e2ce7c879297da4aa66cf4e64ab79 Mon Sep 17 00:00:00 2001 From: Manuel Drehwald Date: Sat, 26 Jun 2021 19:30:09 +0200 Subject: [PATCH 4/4] Remove dropping of loaded plugins and better debug info --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 52d88df5b3fd0..cb9c6269b66bb 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -12,6 +12,7 @@ use rustc_target::spec::{MergeFunctions, PanicStrategy}; use std::ffi::{CStr, CString}; use tracing::debug; +use std::mem; use std::path::Path; use std::ptr; use std::slice; @@ -136,9 +137,10 @@ unsafe fn configure_llvm(sess: &Session) { let path = Path::new(plugin); let res = DynamicLibrary::open(path); match res { - Ok(_) => debug!("configure_llvm: {}", plugin), + Ok(_) => debug!("LLVM plugin loaded succesfully {} ({})", path.display(), plugin), Err(e) => bug!("couldn't load plugin: {}", e), } + mem::forget(res); } rustc_llvm::initialize_available_targets();