-
Notifications
You must be signed in to change notification settings - Fork 295
Implementation for Aarch64 TME intrinsics #855
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
//! ARM's Transactional Memory Extensions (TME). | ||
//! | ||
//! This CPU feature is available on Aarch64 - A architecture profile. | ||
//! This feature is in the non-neon feature set. TME specific vendor documentation can | ||
//! be found [TME Intrinsics Introduction][tme_intrinsics_intro]. | ||
//! | ||
//! The reference is [ACLE Q4 2019][acle_q4_2019_ref]. | ||
//! | ||
//! ACLE has a section for TME extensions and state masks for aborts and failure codes. | ||
//! [ARM A64 Architecture Register Datasheet][a_profile_future] also describes possible failure code scenarios. | ||
//! | ||
//! [acle_q4_2019_ref]: https://static.docs.arm.com/101028/0010/ACLE_2019Q4_release-0010.pdf | ||
//! [tme_intrinsics_intro]: https://developer.arm.com/docs/101028/0010/transactional-memory-extension-tme-intrinsics | ||
//! [llvm_aarch64_int]: https://github.com/llvm/llvm-project/commit/a36d31478c182903523e04eb271bbf102bfab2cc#diff-ff24e1c35f4d54f1110ce5d90c709319R626-R646 | ||
//! [a_profile_future]: https://static.docs.arm.com/ddi0601/a/SysReg_xml_futureA-2019-04.pdf?_ga=2.116560387.441514988.1590524918-1110153136.1588469296 | ||
|
||
#[cfg(test)] | ||
use stdarch_test::assert_instr; | ||
|
||
extern "C" { | ||
#[link_name = "llvm.aarch64.tstart"] | ||
fn aarch64_tstart() -> i32; | ||
#[link_name = "llvm.aarch64.tcommit"] | ||
fn aarch64_tcommit() -> (); | ||
#[link_name = "llvm.aarch64.tcancel"] | ||
fn aarch64_tcancel(imm0: i64) -> (); | ||
#[link_name = "llvm.aarch64.ttest"] | ||
fn aarch64_ttest() -> i32; | ||
} | ||
|
||
/// Transaction successfully started. | ||
pub const _TMSTART_SUCCESS: u32 = 0x00_u32; | ||
|
||
/// Extraction mask for failure reason | ||
pub const _TMFAILURE_REASON: u32 = 0x00007FFF_u32; | ||
|
||
/// Transaction retry is possible. | ||
pub const _TMFAILURE_RTRY: u32 = 1 << 15; | ||
|
||
/// Transaction executed a TCANCEL instruction | ||
pub const _TMFAILURE_CNCL: u32 = 1 << 16; | ||
|
||
/// Transaction aborted because a conflict occurred | ||
pub const _TMFAILURE_MEM: u32 = 1 << 17; | ||
|
||
/// Fallback error type for any other reason | ||
pub const _TMFAILURE_IMP: u32 = 1 << 18; | ||
|
||
/// Transaction aborted because a non-permissible operation was attempted | ||
pub const _TMFAILURE_ERR: u32 = 1 << 19; | ||
|
||
/// Transaction aborted due to read or write set limit was exceeded | ||
pub const _TMFAILURE_SIZE: u32 = 1 << 20; | ||
|
||
/// Transaction aborted due to transactional nesting level was exceeded | ||
pub const _TMFAILURE_NEST: u32 = 1 << 21; | ||
|
||
/// Transaction aborted due to a debug trap. | ||
pub const _TMFAILURE_DBG: u32 = 1 << 22; | ||
|
||
/// Transaction failed from interrupt | ||
pub const _TMFAILURE_INT: u32 = 1 << 23; | ||
|
||
/// Indicates a TRIVIAL version of TM is available | ||
pub const _TMFAILURE_TRIVIAL: u32 = 1 << 24; | ||
|
||
/// Starts a new transaction. When the transaction starts successfully the return value is 0. | ||
/// If the transaction fails, all state modifications are discarded and a cause of the failure | ||
/// is encoded in the return value. | ||
/// | ||
/// [ARM TME Intrinsics](https://developer.arm.com/docs/101028/0010/transactional-memory-extension-tme-intrinsics). | ||
#[inline] | ||
#[target_feature(enable = "tme")] | ||
#[cfg_attr(test, assert_instr(tstart))] | ||
pub unsafe fn __tstart() -> u32 { | ||
aarch64_tstart() as _ | ||
} | ||
|
||
/// Commits the current transaction. For a nested transaction, the only effect is that the | ||
/// transactional nesting depth is decreased. For an outer transaction, the state modifications | ||
/// performed transactionally are committed to the architectural state. | ||
/// | ||
/// [ARM TME Intrinsics](https://developer.arm.com/docs/101028/0010/transactional-memory-extension-tme-intrinsics). | ||
#[inline] | ||
#[target_feature(enable = "tme")] | ||
#[cfg_attr(test, assert_instr(tcommit))] | ||
pub unsafe fn __tcommit() { | ||
aarch64_tcommit() | ||
} | ||
|
||
/// Cancels the current transaction and discards all state modifications that were performed transactionally. | ||
/// | ||
/// [ARM TME Intrinsics](https://developer.arm.com/docs/101028/0010/transactional-memory-extension-tme-intrinsics). | ||
#[inline] | ||
#[target_feature(enable = "tme")] | ||
#[cfg_attr(test, assert_instr(tcancel, imm0 = 0x0))] | ||
#[rustc_args_required_const(0)] | ||
pub unsafe fn __tcancel(imm0: u32) { | ||
macro_rules! call { | ||
($imm0:expr) => { | ||
aarch64_tcancel($imm0) | ||
}; | ||
} | ||
constify_imm8!(imm0, call) | ||
} | ||
|
||
/// Tests if executing inside a transaction. If no transaction is currently executing, | ||
/// the return value is 0. Otherwise, this intrinsic returns the depth of the transaction. | ||
/// | ||
/// [ARM TME Intrinsics](https://developer.arm.com/docs/101028/0010/transactional-memory-extension-tme-intrinsics). | ||
#[inline] | ||
#[target_feature(enable = "tme")] | ||
#[cfg_attr(test, assert_instr(ttest))] | ||
pub unsafe fn __ttest() -> u32 { | ||
aarch64_ttest() as _ | ||
Amanieu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use stdarch_test::simd_test; | ||
|
||
use crate::core_arch::aarch64::*; | ||
|
||
#[simd_test(enable = "tme")] | ||
unsafe fn test_tstart() { | ||
let mut x = 0; | ||
for i in 0..10 { | ||
let code = tme::__tstart(); | ||
if code == _TMSTART_SUCCESS { | ||
x += 1; | ||
assert_eq!(x, i+1); | ||
break; | ||
} | ||
assert_eq!(x, 0); | ||
} | ||
} | ||
|
||
#[simd_test(enable = "tme")] | ||
unsafe fn test_tcommit() { | ||
let mut x = 0; | ||
for i in 0..10 { | ||
let code = tme::__tstart(); | ||
if code == _TMSTART_SUCCESS { | ||
x += 1; | ||
assert_eq!(x, i+1); | ||
tme::__tcommit(); | ||
} | ||
assert_eq!(x, i+1); | ||
} | ||
} | ||
|
||
#[simd_test(enable = "tme")] | ||
unsafe fn test_tcancel() { | ||
let reason = 0x123; | ||
let cancel_code = (0 | (reason & _TMFAILURE_REASON) as i32) as u32; | ||
let mut x = 0; | ||
|
||
for i in 0..10 { | ||
let code = tme::__tstart(); | ||
if code == _TMSTART_SUCCESS { | ||
x += 1; | ||
assert_eq!(x, i+1); | ||
tme::__tcancel(cancel_code); | ||
break; | ||
} | ||
} | ||
|
||
assert_eq!(x, 0); | ||
} | ||
|
||
#[simd_test(enable = "tme")] | ||
unsafe fn test_ttest() { | ||
let reason = 0x123; | ||
let cancel_code = (0 | (reason & _TMFAILURE_REASON) as i32) as u32; | ||
for _ in 0..10 { | ||
let code = tme::__tstart(); | ||
if code == _TMSTART_SUCCESS { | ||
if tme::__ttest() == 2 { | ||
tme::__tcancel(cancel_code); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ fn aarch64_linux() { | |
println!("rdm: {}", is_aarch64_feature_detected!("rdm")); | ||
println!("rcpc: {}", is_aarch64_feature_detected!("rcpc")); | ||
println!("dotprod: {}", is_aarch64_feature_detected!("dotprod")); | ||
println!("tme: {}", is_aarch64_feature_detected!("tme")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to implement detection for this feature. Do a search for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated with feature detection, didn't find the feature tbh. Even instructions and signatures are the same. llvm/llvm-project@a36d314#diff-3674dac5c4241890431fece8594c3143R25-R34 What am I doing wrong? (I still suspect from qemu, checked source code, it doesn't exist there yet. Might be wrong.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current feature detection is fine. Linux doesn't have the TME flag in /proc/cpuinfo or AT_HWCAPS yet, so there's nothing we can do about that for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, when it enters to kernel I will add here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed your comments. Thanks for the help! @Amanieu |
||
} | ||
|
||
#[test] | ||
|
Uh oh!
There was an error while loading. Please reload this page.