From: Yap Zhi Heng <[email protected]>
Includes volatile_copy_nonoverlapping_memory as well since it's just a variant
of
volatile_copy_memory. Implementation is adapted from rustc-codegen-gcc since it
is easier
to implement as compared to generating loops to perform volatile assignment
byte-by-byte.
snipplets from 272t.optimized compile from volatile_copy_set_memory.rs test
case:
struct () volatile_set_memory<u8> (u8 * const {ref-all} dst, const u8 val,
const usize count)
{
u64 RUSTTMP.12;
i32 _1;
u64 RUSTTMP.12_3;
<bb 2> :
RUSTTMP.12_3 = count_2(D);
_1 = (i32) val_4(D);
__builtin_memset (dst_6(D), _1, RUSTTMP.12_3);
__asm__ __volatile__("" : : "r" dst_6(D) : "memory");
return;
}
struct () volatile_copy_nonoverlapping_memory<u32> (u32 * const {ref-all} dst,
const u32 * const {ref-all} src, const usize count)
{
u64 RUSTTMP.16;
u64 RUSTTMP.16_2;
<bb 2> :
RUSTTMP.16_2 = count_1(D) * 4;
__builtin_memcpy (dst_4(D), src_5(D), RUSTTMP.16_2);
__asm__ __volatile__("" : : "r" src_5(D), "r" dst_4(D) : "memory");
return;
}
struct () volatile_copy_memory<u32> (u32 * const {ref-all} dst, const u32 *
const {ref-all} src, const usize count)
{
u64 RUSTTMP.21;
u64 RUSTTMP.21_2;
<bb 2> :
RUSTTMP.21_2 = count_1(D) * 4;
__builtin_memmove (dst_4(D), src_5(D), RUSTTMP.21_2);
__asm__ __volatile__("" : : "r" src_5(D), "r" dst_4(D) : "memory");
return;
}
gcc/rust/ChangeLog:
* backend/rust-intrinsic-handlers.h (handlers::volatile_copy): New
function to handle
volatile_copy_memory and volatile_copy_nonoverlapping_memory.
(handlers::volatile_set_memory): New function to handle
volatile_set_memory.
* backend/rust-intrinsic-handlers.cc (handlers::volatile_copy):
Implement.
(handlers::volatile_set_memory): Implement.
(handlers::inner::volatile_memory_barrier): Helper function to insert
asm volatile barrier
(handlers::inner::volatile_copy): Implement codegen for
volatile_copy_memory &
volatile_copy_nonoverlapping_memory.
(hanndlers::inner::volatile_set_memory): Implement codegen for
volatile_set_memory.
* backend/rust-compile-intrinsic.cc (intrinsics): Add new entries for
new volatile
functions.
Signed-off-by: Yap Zhi Heng <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/0d49300b4c78653250aa49fd00cee47e5ac285d3
The commit has NOT been mentioned in any issue.
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/5067
gcc/rust/backend/rust-compile-intrinsic.cc | 3 +
gcc/rust/backend/rust-intrinsic-handlers.cc | 124 ++++++++++++++++++
gcc/rust/backend/rust-intrinsic-handlers.h | 2 +
.../torture/volatile_copy_set_memory.rs | 67 ++++++++++
4 files changed, 196 insertions(+)
create mode 100644
gcc/testsuite/rust/execute/torture/volatile_copy_set_memory.rs
diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc
b/gcc/rust/backend/rust-compile-intrinsic.cc
index 85600b7a2..bc43604ab 100644
--- a/gcc/rust/backend/rust-compile-intrinsic.cc
+++ b/gcc/rust/backend/rust-compile-intrinsic.cc
@@ -263,6 +263,9 @@ static const std::map<std::string, Intrinsic::CompileFn>
intrinsics = {
{IValue::VOLATILE_STORE, handlers::volatile_store (false)},
{IValue::UNALIGNED_VOLATILE_LOAD, handlers::volatile_load (true)},
{IValue::UNALIGNED_VOLATILE_STORE, handlers::volatile_store (true)},
+ {IValue::VOLATILE_COPY_MEMORY, handlers::volatile_copy (false)},
+ {IValue::VOLATILE_COPY_NONOVERLAPPING_MEMORY, handlers::volatile_copy
(true)},
+ {IValue::VOLATILE_SET_MEMORY, handlers::volatile_set_memory ()},
};
Intrinsics::Intrinsics (Context *ctx) : ctx (ctx) {}
diff --git a/gcc/rust/backend/rust-intrinsic-handlers.cc
b/gcc/rust/backend/rust-intrinsic-handlers.cc
index f103579c0..28766fa24 100644
--- a/gcc/rust/backend/rust-intrinsic-handlers.cc
+++ b/gcc/rust/backend/rust-intrinsic-handlers.cc
@@ -1264,6 +1264,116 @@ volatile_store (IntrinsicCtx &ctx, bool is_unaligned)
ctx.add_statement (assign_statement);
}
+// takes in a vec of pointers and define them as volatile with `asm volatile`
+// this is the same as `volatile_barrier` from rustc_codegen_gcc
+static tree
+volatile_memory_barrier (IntrinsicCtx &ctx, const std::vector<tree> &ptrs)
+{
+ tree inputs = NULL_TREE;
+ for (auto it : ptrs)
+ {
+ tree constraint = build_tree_list (NULL_TREE, build_string (2, "r"));
+ inputs = tree_cons (constraint, it, inputs);
+ }
+
+ tree barrier = make_node (ASM_EXPR);
+ TREE_TYPE (barrier) = void_type_node;
+ SET_EXPR_LOCATION (barrier, ctx.loc);
+
+ ASM_STRING (barrier) = build_string (1, "");
+ ASM_OUTPUTS (barrier) = NULL_TREE;
+ ASM_INPUTS (barrier) = inputs;
+ ASM_CLOBBERS (barrier)
+ = build_tree_list (NULL_TREE, build_string (7, "memory"));
+ ASM_LABELS (barrier) = NULL_TREE;
+
+ ASM_VOLATILE_P (barrier) = 1;
+ TREE_SIDE_EFFECTS (barrier) = 1;
+
+ return barrier;
+}
+
+static void
+volatile_copy (IntrinsicCtx &ctx, bool is_nonoverlapping)
+{
+ // similar to atomics, volatile functions are not pure
+ TREE_READONLY (ctx.fn) = 0;
+ TREE_SIDE_EFFECTS (ctx.fn) = 1;
+
+ tree dst = Backend::var_expression (ctx.param_vars[0], ctx.loc);
+ tree src = Backend::var_expression (ctx.param_vars[1], ctx.loc);
+ tree count = Backend::var_expression (ctx.param_vars[2], ctx.loc);
+ tree element_type = TREE_TYPE (ctx.param_types[0]);
+
+ // calculate bytes to be copied (count * sizeof(T))
+ tree size
+ = fold_build2_loc (ctx.loc, MULT_EXPR, size_type_node,
+ fold_convert_loc (ctx.loc, size_type_node, count),
+ fold_convert_loc (ctx.loc, size_type_node,
+ TYPE_SIZE_UNIT (element_type)));
+ tree size_decl = NULL_TREE;
+ auto size_var
+ = Backend::temporary_variable (ctx.fn, ctx.ctx.peek_enclosing_scope (),
+ size_type_node, size, false, ctx.loc,
+ &size_decl);
+ ctx.add_statement (size_decl);
+ tree nbytes = Backend::var_expression (size_var, ctx.loc);
+
+ tree dst_ptr = fold_convert_loc (ctx.loc, ptr_type_node, dst);
+ tree src_ptr = fold_convert_loc (ctx.loc, const_ptr_type_node, src);
+
+ auto copy_builtin = BuiltinsContext::get ().lookup_simple_builtin (
+ is_nonoverlapping ? "__builtin_memcpy" : "__builtin_memmove");
+
+ tree copy_fn = build_fold_addr_expr_loc (ctx.loc, *copy_builtin);
+ tree copy_call
+ = Backend::call_expression (copy_fn, {dst_ptr, src_ptr, nbytes}, nullptr,
+ ctx.loc);
+
+ ctx.add_statement (copy_call);
+ ctx.add_statement (volatile_memory_barrier (ctx, {dst_ptr, src_ptr}));
+}
+
+static void
+volatile_set_memory (IntrinsicCtx &ctx)
+{
+ // similar to atomics, volatile functions are not pure
+ TREE_READONLY (ctx.fn) = 0;
+ TREE_SIDE_EFFECTS (ctx.fn) = 1;
+
+ tree dst = Backend::var_expression (ctx.param_vars[0], ctx.loc);
+ tree val = Backend::var_expression (ctx.param_vars[1], ctx.loc);
+ tree count = Backend::var_expression (ctx.param_vars[2], ctx.loc);
+ tree element_type = TREE_TYPE (ctx.param_types[0]);
+
+ // calculate bytes to be copied (count * sizeof(T))
+ tree size
+ = fold_build2_loc (ctx.loc, MULT_EXPR, size_type_node,
+ fold_convert_loc (ctx.loc, size_type_node, count),
+ fold_convert_loc (ctx.loc, size_type_node,
+ TYPE_SIZE_UNIT (element_type)));
+ tree size_decl = NULL_TREE;
+ auto size_var
+ = Backend::temporary_variable (ctx.fn, ctx.ctx.peek_enclosing_scope (),
+ size_type_node, size, false, ctx.loc,
+ &size_decl);
+ ctx.add_statement (size_decl);
+ tree nbytes = Backend::var_expression (size_var, ctx.loc);
+
+ tree dst_ptr = fold_convert_loc (ctx.loc, ptr_type_node, dst);
+
+ auto memset_builtin
+ = BuiltinsContext::get ().lookup_simple_builtin ("__builtin_memset");
+ tree memset_fn = build_fold_addr_expr_loc (ctx.loc, *memset_builtin);
+
+ tree fill = fold_convert_loc (ctx.loc, integer_type_node, val);
+ tree memset_call
+ = Backend::call_expression (memset_fn, {dst_ptr, fill, nbytes}, nullptr,
+ ctx.loc);
+ ctx.add_statement (memset_call);
+ ctx.add_statement (volatile_memory_barrier (ctx, {dst_ptr}));
+}
+
static void
prefetch (IntrinsicCtx &ctx, Mode mode, Cache cache)
{
@@ -1357,6 +1467,20 @@ volatile_store (bool is_unaligned)
};
}
+Intrinsic::CompileFn
+volatile_copy (bool is_nonoverlapping)
+{
+ return [is_nonoverlapping] (IntrinsicCtx &ctx) {
+ return inner::volatile_copy (ctx, is_nonoverlapping);
+ };
+}
+
+Intrinsic::CompileFn
+volatile_set_memory ()
+{
+ return [] (IntrinsicCtx &ctx) { return inner::volatile_set_memory (ctx); };
+}
+
void
rotate_left (IntrinsicCtx &ctx)
{
diff --git a/gcc/rust/backend/rust-intrinsic-handlers.h
b/gcc/rust/backend/rust-intrinsic-handlers.h
index b9e88d874..369526c94 100644
--- a/gcc/rust/backend/rust-intrinsic-handlers.h
+++ b/gcc/rust/backend/rust-intrinsic-handlers.h
@@ -221,6 +221,8 @@ Intrinsic::CompileFn type_assert (TyAssert assert);
Intrinsic::CompileFn volatile_load (bool is_unaligned);
Intrinsic::CompileFn volatile_store (bool is_unaligned);
+Intrinsic::CompileFn volatile_copy (bool is_nonoverlapping);
+Intrinsic::CompileFn volatile_set_memory ();
void breakpoint (IntrinsicCtx &ctx);
diff --git a/gcc/testsuite/rust/execute/torture/volatile_copy_set_memory.rs
b/gcc/testsuite/rust/execute/torture/volatile_copy_set_memory.rs
new file mode 100644
index 000000000..cf1b7c695
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/volatile_copy_set_memory.rs
@@ -0,0 +1,67 @@
+#![feature(no_core, lang_items, staged_api, intrinsics)]
+#![no_core]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+// below's helper code copied from issue-1232.rs
+extern "rust-intrinsic" {
+ #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
+ fn offset<T>(dst: *const T, offset: isize) -> *const T;
+}
+
+#[lang = "const_ptr"]
+impl<T> *const T {
+ pub const unsafe fn offset(self, count: isize) -> *const T {
+ unsafe { offset(self, count) }
+ }
+
+ pub const unsafe fn add(self, count: usize) -> Self {
+ unsafe { self.offset(count as isize) }
+ }
+
+ pub const fn as_ptr(self) -> *const T {
+ self as *const T
+ }
+}
+
+extern "rust-intrinsic" {
+ pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
count: usize);
+ pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
+ pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
+}
+
+fn main() -> i32 {
+ unsafe {
+ let mut value: [u8; 8] = [0x67u8; 8];
+ let mut ptr = &mut value as *mut [u8] as *mut u8;
+
+ volatile_set_memory(ptr, 0x02, 5);
+ if value[0] != 0x02 || value[5] != 0x67 {
+ return 1;
+ }
+ }
+
+ unsafe {
+ let mut value: [u32; 8] = [0x67; 8];
+ let mut ptr = &mut value as *mut [u32] as *mut u32;
+ let mut value2: [u32; 8] = [0x10, 0x21, 0x32, 0x43, 0x54, 0xBB, 0x76,
0xAA];
+ let ptr2 = &mut value2 as *mut [u32] as *const u32;
+ volatile_copy_nonoverlapping_memory(ptr, ptr2, 5);
+ if value[0] != 0x10 || value[1] != 0x21 || value[2] != 0x32 {
+ return 1;
+ }
+ if value[3] != 0x43 || value[4] != 0x54 || value[5] != 0x67 {
+ return 1;
+ }
+
+ let ptr_offset = (ptr as *const u32).add(2);
+ volatile_copy_memory(ptr, ptr_offset, 4);
+ if value[0] != 0x32 || value[1] != 0x43 || value[2] != 0x54 ||
value[3] != 0x67 || value[4] != 0x54
+ {
+ return 1;
+ }
+ }
+
+ 0
+}
\ No newline at end of file
--
2.56.1