Repository navigation
Add unstable -Zllvm-tpde flag to rustc_codegen_llvm
#164108
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
Open
TechnoPorg
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
TechnoPorg:unstable-tpde-flag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+407
−17
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| use std::path::PathBuf; | ||
| use std::sync::OnceLock; | ||
|
|
||
| use rustc_session::config::{self, Sysroot, host_tuple}; | ||
| use rustc_session::filesearch; | ||
|
|
||
| unsafe extern "C" { | ||
| type LLVMPassPluginLibraryInfo; | ||
| } | ||
|
|
||
| type LLVMGetPassPluginInfoFn = unsafe extern "C" fn() -> LLVMPassPluginLibraryInfo; | ||
|
|
||
| /// LLVMRustTpdeMode | ||
| #[repr(C)] | ||
| #[derive(Clone)] | ||
| pub(crate) enum LLVMRustTpdeMode { | ||
| None, | ||
| Only, | ||
| Try, | ||
| } | ||
|
|
||
| /// LLVMRustTpdeOptions | ||
| #[repr(C)] | ||
| #[derive(Clone)] | ||
| pub(crate) struct LLVMRustTpdeOptions { | ||
| mode: LLVMRustTpdeMode, | ||
| // `None` if mode is `LLVMRustTpdeMode::None` | ||
| plugin: Option<LLVMGetPassPluginInfoFn>, | ||
| } | ||
|
|
||
| pub(crate) struct TpdeWrapper { | ||
| options: LLVMRustTpdeOptions, | ||
| // Keep the dynamic library loaded while the function pointers are used. | ||
| _lib: Option<libloading::Library>, | ||
| } | ||
|
|
||
| static TPDE_INSTANCE: OnceLock<TpdeWrapper> = OnceLock::new(); | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) enum TpdeLibraryError { | ||
| NotFound { err: String }, | ||
| LoadFailed { err: String }, | ||
| } | ||
|
|
||
| impl From<libloading::Error> for TpdeLibraryError { | ||
| fn from(err: libloading::Error) -> Self { | ||
| Self::LoadFailed { err: format!("{err:?}") } | ||
| } | ||
| } | ||
|
|
||
| impl TpdeWrapper { | ||
| /// Initialize TpdeWrapper with the given sysroot and mode if not already initialized. | ||
| /// Safe to call multiple times - subsequent calls are no-ops due to OnceLock. | ||
| pub(crate) fn get_or_init( | ||
| sysroot: &rustc_session::config::Sysroot, | ||
| mode: &Option<config::LlvmTpde>, | ||
| ) -> Result<&'static Self, TpdeLibraryError> { | ||
| let wrapper = TPDE_INSTANCE.get_or_try_init(|| { | ||
| let w = match mode { | ||
| Some(mode) => Self::call_dynamic(sysroot, mode)?, | ||
| None => Self { | ||
| options: LLVMRustTpdeOptions { mode: LLVMRustTpdeMode::None, plugin: None }, | ||
| _lib: None, | ||
| }, | ||
| }; | ||
| Ok::<_, TpdeLibraryError>(w) | ||
| })?; | ||
|
|
||
| Ok(wrapper) | ||
| } | ||
|
|
||
| /// Get the TpdeWrapper instance. Panics if not initialized. | ||
| pub(crate) fn get_instance() -> &'static Self { | ||
| TPDE_INSTANCE | ||
| .get() | ||
| .expect("TpdeWrapper not initialized. Call get_or_init with sysroot first.") | ||
| } | ||
|
|
||
| pub(crate) fn options(&self) -> &LLVMRustTpdeOptions { | ||
| &self.options | ||
| } | ||
|
|
||
| fn call_dynamic( | ||
| sysroot: &rustc_session::config::Sysroot, | ||
| mode: &config::LlvmTpde, | ||
| ) -> Result<Self, TpdeLibraryError> { | ||
| let mode = match mode { | ||
| config::LlvmTpde::Only => LLVMRustTpdeMode::Only, | ||
| config::LlvmTpde::Try => LLVMRustTpdeMode::Try, | ||
| }; | ||
| let tpde_path = Self::get_tpde_path(sysroot)?; | ||
| let lib = unsafe { libloading::Library::new(tpde_path)? }; | ||
| let llvm_get_pass_plugin_info = | ||
| *unsafe { lib.get::<LLVMGetPassPluginInfoFn>(b"llvmGetPassPluginInfo\0")? }; | ||
| let options = LLVMRustTpdeOptions { mode, plugin: Some(llvm_get_pass_plugin_info) }; | ||
| Ok(Self { options, _lib: Some(lib) }) | ||
| } | ||
|
|
||
| fn get_tpde_path(sysroot: &Sysroot) -> Result<PathBuf, TpdeLibraryError> { | ||
| let path_buf = sysroot | ||
| .all_paths() | ||
| .map(|sysroot_path| { | ||
| filesearch::make_target_lib_path(sysroot_path, host_tuple()) | ||
| .join("lib") | ||
| .with_file_name("tpde-plugin") | ||
| .with_extension(std::env::consts::DLL_EXTENSION) | ||
| }) | ||
| .find(|f| f.exists()) | ||
| .ok_or_else(|| { | ||
| let candidates = sysroot | ||
| .all_paths() | ||
| .map(|p| p.join("lib").display().to_string()) | ||
| .collect::<Vec<String>>() | ||
| .join("\n* "); | ||
| TpdeLibraryError::NotFound { | ||
| err: format!( | ||
| "failed to find the `tpde-plugin` shared library \ | ||
| in the sysroot candidates:\n* {candidates}" | ||
| ), | ||
| } | ||
| })?; | ||
|
|
||
| Ok(path_buf) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.