From 1c739e81ab123a171e90bb901255bd25f158e268 Mon Sep 17 00:00:00 2001 From: Simon Renblad Date: Tue, 19 Nov 2024 15:17:06 +0800 Subject: [PATCH] cursor: prevent llvm-opt inserting memcpy in r/w --- src/libio/cursor.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libio/cursor.rs b/src/libio/cursor.rs index b07ed4d..7ed2271 100644 --- a/src/libio/cursor.rs +++ b/src/libio/cursor.rs @@ -1,6 +1,8 @@ #[cfg(feature = "alloc")] use alloc::vec::Vec; +use core::arch::asm; + use crate::{Read, Write}; #[derive(Debug, Clone)] @@ -49,6 +51,9 @@ impl> Read for Cursor { let len = buf.len().min(data.len()); // ``copy_from_slice`` generates AXI bursts, use a regular loop instead for i in 0..len { + // HACK: On opt-level=2 or above, LLVM 18 may insert memcpy calls as an optimization pass, + // causing AXI bursts. + unsafe { asm!("", options(preserves_flags, nostack, readonly)); } buf[i] = data[i]; } self.pos += len; @@ -64,6 +69,8 @@ impl Write for Cursor<&mut [u8]> { let data = &mut self.inner[self.pos..]; let len = buf.len().min(data.len()); for i in 0..len { + // HACK: On opt-level=2 or above, this is too fast and sends corrupted packets. + unsafe { asm!("", options(preserves_flags, nostack, readonly)); } data[i] = buf[i]; } self.pos += len;