Skip to content

Commit 1012d66

Browse files
authored
Rollup merge of rust-lang#76669 - lzutao:core_asm, r=Amanieu
Prefer asm! over llvm_asm! in core Replace llvm_asm! with asm! in core. x86 asm compare (in somecases I replaced generic type with String). * https://rust.godbolt.org/z/59eEMv * https://rust.godbolt.org/z/v78s6q * https://rust.godbolt.org/z/7qYY41
2 parents b0e36db + 1c7204f commit 1012d66

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

library/core/src/hint.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub fn spin_loop() {
111111
#[inline]
112112
#[unstable(feature = "test", issue = "50297")]
113113
#[allow(unreachable_code)] // this makes #[cfg] a bit easier below.
114-
pub fn black_box<T>(dummy: T) -> T {
114+
pub fn black_box<T>(mut dummy: T) -> T {
115115
// We need to "use" the argument in some way LLVM can't introspect, and on
116116
// targets that support it we can typically leverage inline assembly to do
117117
// this. LLVM's interpretation of inline assembly is that it's, well, a black
@@ -121,7 +121,31 @@ pub fn black_box<T>(dummy: T) -> T {
121121
#[cfg(not(miri))] // This is just a hint, so it is fine to skip in Miri.
122122
// SAFETY: the inline assembly is a no-op.
123123
unsafe {
124-
llvm_asm!("" : : "r"(&dummy));
124+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
125+
{
126+
asm!(
127+
"/* {0} */",
128+
in(reg) &mut dummy,
129+
// FIXME: We are using ATT syntax to support LLVM 8 and LLVM 9.
130+
options(att_syntax, nostack, preserves_flags),
131+
);
132+
}
133+
#[cfg(target_arch = "nvptx64")]
134+
{
135+
asm!(
136+
"/* {0} */",
137+
in(reg64) &mut dummy,
138+
options(nostack, preserves_flags),
139+
);
140+
}
141+
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "nvptx64")))]
142+
{
143+
asm!(
144+
"/* {0} */",
145+
in(reg) &mut dummy,
146+
options(nostack, preserves_flags),
147+
);
148+
}
125149
}
126150

127151
dummy

library/core/src/num/dec2flt/algorithm.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,19 @@ mod fpu_precision {
6060
fn set_cw(cw: u16) {
6161
// SAFETY: the `fldcw` instruction has been audited to be able to work correctly with
6262
// any `u16`
63-
unsafe { llvm_asm!("fldcw $0" :: "m" (cw) :: "volatile") }
63+
unsafe {
64+
asm!(
65+
"fldcw ({})",
66+
in(reg) &cw,
67+
// FIXME: We are using ATT syntax to support LLVM 8 and LLVM 9.
68+
options(att_syntax, nostack),
69+
)
70+
}
6471
}
6572

6673
/// Sets the precision field of the FPU to `T` and returns a `FPUControlWord`.
6774
pub fn set_precision<T>() -> FPUControlWord {
68-
let cw = 0u16;
75+
let mut cw = 0_u16;
6976

7077
// Compute the value for the Precision Control field that is appropriate for `T`.
7178
let cw_precision = match size_of::<T>() {
@@ -78,7 +85,14 @@ mod fpu_precision {
7885
// `FPUControlWord` structure is dropped
7986
// SAFETY: the `fnstcw` instruction has been audited to be able to work correctly with
8087
// any `u16`
81-
unsafe { llvm_asm!("fnstcw $0" : "=*m" (&cw) ::: "volatile") }
88+
unsafe {
89+
asm!(
90+
"fnstcw ({})",
91+
in(reg) &mut cw,
92+
// FIXME: We are using ATT syntax to support LLVM 8 and LLVM 9.
93+
options(att_syntax, nostack),
94+
)
95+
}
8296

8397
// Set the control word to the desired precision. This is achieved by masking away the old
8498
// precision (bits 8 and 9, 0x300) and replacing it with the precision flag computed above.

0 commit comments

Comments
 (0)