-
Notifications
You must be signed in to change notification settings - Fork 134
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
cpufeatures: Add support for LoongArch64 #955
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,106 @@ | ||
//! LoongArch64 CPU feature detection support. | ||
//! | ||
//! This implementation relies on OS-specific APIs for feature detection. | ||
|
||
// Evaluate the given `$body` expression any of the supplied target features | ||
// are not enabled. Otherwise returns true. | ||
#[macro_export] | ||
#[doc(hidden)] | ||
macro_rules! __unless_target_features { | ||
($($tf:tt),+ => $body:expr ) => { | ||
{ | ||
#[cfg(not(all($(target_feature=$tf,)*)))] | ||
$body | ||
|
||
#[cfg(all($(target_feature=$tf,)*))] | ||
true | ||
} | ||
}; | ||
} | ||
|
||
// Linux runtime detection of target CPU features using `getauxval`. | ||
#[cfg(target_os = "linux")] | ||
#[macro_export] | ||
#[doc(hidden)] | ||
macro_rules! __detect_target_features { | ||
($($tf:tt),+) => {{ | ||
let hwcaps = $crate::loongarch64::getauxval_hwcap(); | ||
$($crate::check!(hwcaps, $tf) & )+ true | ||
}}; | ||
} | ||
|
||
/// Linux helper function for calling `getauxval` to get `AT_HWCAP`. | ||
#[cfg(target_os = "linux")] | ||
pub fn getauxval_hwcap() -> u64 { | ||
unsafe { libc::getauxval(libc::AT_HWCAP) } | ||
} | ||
|
||
// Linux `expand_check_macro` | ||
#[cfg(target_os = "linux")] | ||
macro_rules! __expand_check_macro { | ||
($(($name:tt, $hwcap:ident)),* $(,)?) => { | ||
#[macro_export] | ||
#[doc(hidden)] | ||
macro_rules! check { | ||
$( | ||
($hwcaps:expr, $name) => { | ||
(($hwcaps & $crate::loongarch64::hwcaps::$hwcap) != 0) | ||
}; | ||
)* | ||
} | ||
}; | ||
} | ||
|
||
// Linux `expand_check_macro` | ||
#[cfg(target_os = "linux")] | ||
__expand_check_macro! { | ||
("cpucfg", CPUCFG), // Enable CPUCFG support. | ||
("lam", LAM), // Enable LAM support. | ||
("ual", UAL), // Enable UAL support. | ||
("fpu", FPU), // Enable FPU support. | ||
("lsx", LSX), // Enable LSX support. | ||
("lasx", LASX), // Enable LASX support. | ||
("crc32", CRC32), // Enable CRC32 support. | ||
("complex", COMPLEX), // Enable COMPLEX support. | ||
("crypto", CRYPTO), // Enable CRYPTO support. | ||
("lvz", LVZ), // Enable LVZ support. | ||
("lbt.x86", LBT_X86), // Enable LBT_X86 support. | ||
("lbt.arm", LBT_ARM), // Enable LBT_ARM support. | ||
("lbt.mips", LBT_MIPS), // Enable LBT_MIPS support. | ||
("ptw", PTW), // Enable PTW support. | ||
} | ||
|
||
/// Linux hardware capabilities mapped to target features. | ||
/// | ||
/// Note that LLVM target features are coarser grained than what Linux supports | ||
/// and imply more capabilities under each feature. This module attempts to | ||
/// provide that mapping accordingly. | ||
#[cfg(target_os = "linux")] | ||
pub mod hwcaps { | ||
use libc::c_ulong; | ||
|
||
pub const CPUCFG: c_ulong = libc::HWCAP_CPUCFG; | ||
pub const LAM: c_ulong = libc::HWCAP_LAM; | ||
pub const UAL: c_ulong = libc::HWCAP_UAL; | ||
pub const FPU: c_ulong = libc::HWCAP_FPU; | ||
pub const LSX: c_ulong = libc::HWCAP_LSX; | ||
pub const LASX: c_ulong = libc::HWCAP_LASX; | ||
pub const CRC32: c_ulong = libc::HWCAP_CRC32; | ||
pub const COMPLEX: c_ulong = libc::HWCAP_COMPLEX; | ||
pub const CRYPTO: c_ulong = libc::HWCAP_CRYPTO; | ||
pub const LVZ: c_ulong = libc::HWCAP_LVZ; | ||
pub const LBT_X86: c_ulong = libc::HWCAP_LBT_X86; | ||
pub const LBT_ARM: c_ulong = libc::HWCAP_LBT_ARM; | ||
pub const LBT_MIPS: c_ulong = libc::HWCAP_LBT_MIPS; | ||
pub const PTW: c_ulong = libc::HWCAP_PTW; | ||
} | ||
|
||
// On other targets, runtime CPU feature detection is unavailable | ||
#[cfg(not(target_os = "linux"))] | ||
#[macro_export] | ||
#[doc(hidden)] | ||
macro_rules! __detect_target_features { | ||
($($tf:tt),+) => { | ||
false | ||
}; | ||
} |
This file contains 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,20 @@ | ||
//! LoongArch64 tests | ||
|
||
#![cfg(target_arch = "loongarch64")] | ||
|
||
cpufeatures::new!( | ||
lacaps, "cpucfg", "lam", "ual", "fpu", "lsx", "lasx", "crc32", "complex", "crypto", "lvz", | ||
"lbt.x86", "lbt.arm", "lbt.mips", "ptw" | ||
); | ||
|
||
#[test] | ||
fn init() { | ||
let token: lacaps::InitToken = lacaps::init(); | ||
assert_eq!(token.get(), lacaps::get()); | ||
} | ||
|
||
#[test] | ||
fn init_get() { | ||
let (token, val) = lacaps::init_get(); | ||
assert_eq!(val, token.get()); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for missing this.