From: Yap Zhi Heng <[email protected]>

272t.optimized dumps of the volatile_store_load.rs (note the {v} markers):

...
;; Function volatile_store<u32> (_ZN14volatile_store17h30605ec9f58c8c97E, 
funcdef_no=7, decl_uid=5269, cgraph_uid=1, symbol_order=0)

struct () volatile_store<u32> (u32 * const {ref-all} dst, const u32 val)
{
  <bb 2> :
  MEM[(u32 *)dst_2(D)] ={v} val_3(D);
  return;

}

;; Function volatile_load<u32> (_ZN13volatile_load17h56bb3098c76a08adE, 
funcdef_no=3, decl_uid=5283, cgraph_uid=2, symbol_order=1)

u32 volatile_load<u32> (const u32 * const {ref-all} src)
{
  u32 D.5433;
  const u32 RUSTTMP.4;
  const u32 RUSTTMP.4_3;
  u32 _4;

  <bb 2> :
  RUSTTMP.4_3 ={v} MEM[(const u32 *)src_2(D)];
  _4 = RUSTTMP.4_3;

  <bb 3> :
<L0>:
  return _4;

}

;; Function unaligned_volatile_store<u32> 
(_ZN24unaligned_volatile_store17h4ca7620ccd0201f1E, funcdef_no=9, 
decl_uid=5375, cgraph_uid=6, symbol_order=5)

struct () unaligned_volatile_store<u32> (u32 * const {ref-all} dst, const u32 
val)
{
  <bb 2> :
  MEM[(u32 *)dst_2(D)] ={v} val_3(D);
  return;

}

;; Function unaligned_volatile_load<u32> 
(_ZN23unaligned_volatile_load17hf1b132ce860b52beE, funcdef_no=6, decl_uid=5387, 
cgraph_uid=7, symbol_order=6)

u32 unaligned_volatile_load<u32> (const u32 * const {ref-all} src)
{
  u32 D.5435;
  const u32 RUSTTMP.12;
  const u32 RUSTTMP.12_3;
  u32 _4;

  <bb 2> :
  RUSTTMP.12_3 ={v} MEM[(const u32 *)src_2(D)];
  _4 = RUSTTMP.12_3;

  <bb 3> :
<L0>:
  return _4;

}
...

gcc/rust/ChangeLog:

        * util/rust-intrinsic-values.h (Intrinsics): Add new entries for 
volatile function
        names.
        * backend/rust-intrinsic-handlers.h (handlers::volatile_load): New 
function to handle
        volatile_load and unaligned_volatile_load.
        (handlers::volatile_store): Likewise for volatile_store & 
unaligned_volatile_store.
        * backend/rust-intrinsic-handlers.cc (handlers::volatile_load): 
Implement.
        (handlers::volatile_store): Implement.
        (handlers::inner::volatile_memory_ref): New function to convert 
pointers into volatile
        memory references.
        (handlers::inner::volatile_load): Implement codegen for volatile_load &
        unaligned_volatile_load.
        (handlers::inner::volatile_store): Likewise for volatile_store & 
unaligned_volatile-store.
        * backend/rust-compile-intrinsic.cc (intrinsics): Add new entries for 
new volatile
        functions.
        * typecheck/rust-hir-type-check-intrinsic.cc (intrinsic_rules): 
Likewise, but for type
        checking.

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/cdf8d4d62236b241d86f5a8159c2c115a39529ea

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    |  4 +
 gcc/rust/backend/rust-intrinsic-handlers.cc   | 86 +++++++++++++++++++
 gcc/rust/backend/rust-intrinsic-handlers.h    |  3 +
 .../rust-hir-type-check-intrinsic.cc          | 27 ++++++
 gcc/rust/util/rust-intrinsic-values.h         |  9 ++
 .../execute/torture/volatile_store_load.rs    | 61 +++++++++++++
 6 files changed, 190 insertions(+)
 create mode 100644 gcc/testsuite/rust/execute/torture/volatile_store_load.rs

diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc 
b/gcc/rust/backend/rust-compile-intrinsic.cc
index 9cf498312..85600b7a2 100644
--- a/gcc/rust/backend/rust-compile-intrinsic.cc
+++ b/gcc/rust/backend/rust-compile-intrinsic.cc
@@ -259,6 +259,10 @@ static const std::map<std::string, Intrinsic::CompileFn> 
intrinsics = {
   {IValue::ASSERT_INHABITED, handlers::type_assert (TyAssert::Inhabited)},
   {IValue::ASSERT_UNINIT_VALID, handlers::type_assert (TyAssert::UninitValid)},
   {IValue::PTR_OFFSET_FROM, handlers::ptr_offset_from},
+  {IValue::VOLATILE_LOAD, handlers::volatile_load (false)},
+  {IValue::VOLATILE_STORE, handlers::volatile_store (false)},
+  {IValue::UNALIGNED_VOLATILE_LOAD, handlers::volatile_load (true)},
+  {IValue::UNALIGNED_VOLATILE_STORE, handlers::volatile_store (true)},
 };
 
 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 d4ef4a9c7..f103579c0 100644
--- a/gcc/rust/backend/rust-intrinsic-handlers.cc
+++ b/gcc/rust/backend/rust-intrinsic-handlers.cc
@@ -1194,6 +1194,76 @@ type_assert (IntrinsicCtx &ctx, TyAssert assert)
     }
 }
 
+static tree
+volatile_memory_ref (tree ptr, tree value_type, bool is_unaligned,
+                    location_t locus)
+{
+  tree access_type = value_type;
+  if (is_unaligned && TYPE_ALIGN (access_type) != BITS_PER_UNIT)
+    {
+      // treat the pointer as unaligned (i.e. 1-byte alignment), setting
+      // type align to BITS_PER_UNIT should do the trick
+      access_type = build_variant_type_copy (access_type);
+      SET_TYPE_ALIGN (access_type, BITS_PER_UNIT);
+      TYPE_USER_ALIGN (access_type) = 1;
+    }
+
+  tree ptr_type = build_pointer_type (access_type);
+  tree address = fold_convert_loc (locus, ptr_type, ptr);
+
+  tree ref = build2_loc (locus, MEM_REF, access_type, address,
+                        build_int_cst (ptr_type, 0));
+
+  TREE_THIS_VOLATILE (ref) = 1;
+  TREE_SIDE_EFFECTS (ref) = 1;
+
+  return ref;
+}
+
+static void
+volatile_load (IntrinsicCtx &ctx, bool is_unaligned)
+{
+  // similar to atomics, volatile functions are not pure
+  TREE_READONLY (ctx.fn) = 0;
+  TREE_SIDE_EFFECTS (ctx.fn) = 1;
+
+  tree src = Backend::var_expression (ctx.param_vars[0], ctx.loc);
+  tree value_type = TREE_TYPE (ctx.param_types[0]);
+
+  tree src_ref = volatile_memory_ref (src, value_type, is_unaligned, ctx.loc);
+  tree decl_statement = NULL_TREE;
+  Bvariable *result
+    = Backend::temporary_variable (ctx.fn, ctx.ctx.peek_enclosing_scope (),
+                                  value_type, NULL_TREE, false, ctx.loc,
+                                  &decl_statement);
+  ctx.add_statement (decl_statement);
+
+  tree result_expr = Backend::var_expression (result, ctx.loc);
+  tree assign_statement
+    = Backend::assignment_statement (result_expr, src_ref, ctx.loc);
+  tree return_statement
+    = Backend::return_statement (ctx.fn, result_expr, ctx.loc);
+  ctx.add_statement (assign_statement);
+  ctx.add_statement (return_statement);
+}
+
+static void
+volatile_store (IntrinsicCtx &ctx, bool is_unaligned)
+{
+  // 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 value = Backend::var_expression (ctx.param_vars[1], ctx.loc);
+  tree value_type = ctx.param_types[1];
+
+  tree dst_ref = volatile_memory_ref (dst, value_type, is_unaligned, ctx.loc);
+  tree assign_statement
+    = Backend::assignment_statement (dst_ref, value, ctx.loc);
+  ctx.add_statement (assign_statement);
+}
+
 static void
 prefetch (IntrinsicCtx &ctx, Mode mode, Cache cache)
 {
@@ -1271,6 +1341,22 @@ type_assert (TyAssert assert)
     [assert] (IntrinsicCtx &ctx) { return inner::type_assert (ctx, assert); };
 }
 
+Intrinsic::CompileFn
+volatile_load (bool is_unaligned)
+{
+  return [is_unaligned] (IntrinsicCtx &ctx) {
+    return inner::volatile_load (ctx, is_unaligned);
+  };
+}
+
+Intrinsic::CompileFn
+volatile_store (bool is_unaligned)
+{
+  return [is_unaligned] (IntrinsicCtx &ctx) {
+    return inner::volatile_store (ctx, is_unaligned);
+  };
+}
+
 void
 rotate_left (IntrinsicCtx &ctx)
 {
diff --git a/gcc/rust/backend/rust-intrinsic-handlers.h 
b/gcc/rust/backend/rust-intrinsic-handlers.h
index 7e5691041..b9e88d874 100644
--- a/gcc/rust/backend/rust-intrinsic-handlers.h
+++ b/gcc/rust/backend/rust-intrinsic-handlers.h
@@ -219,6 +219,9 @@ Intrinsic::CompileFn fop_fast (ArithmeticOrLogicalOperator 
op);
  */
 Intrinsic::CompileFn type_assert (TyAssert assert);
 
+Intrinsic::CompileFn volatile_load (bool is_unaligned);
+Intrinsic::CompileFn volatile_store (bool is_unaligned);
+
 void breakpoint (IntrinsicCtx &ctx);
 
 void ptr_offset_from (IntrinsicCtx &ctx);
diff --git a/gcc/rust/typecheck/rust-hir-type-check-intrinsic.cc 
b/gcc/rust/typecheck/rust-hir-type-check-intrinsic.cc
index 2b0f3fb36..a081c0e32 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-intrinsic.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-intrinsic.cc
@@ -544,6 +544,33 @@ const std::unordered_map<std::string, IntrinsicRules>
     // fn assert_maybe_uninit<T>();
     {IValue::ASSERT_UNINIT_VALID, {1, {}, IRT::Unit}},
 
+    // pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const 
T,
+    // count: usize)
+    {IValue::VOLATILE_COPY_NONOVERLAPPING_MEMORY,
+     {1,
+      {IRT::MutPtrFirstGeneric, IRT::ConstPtrFirstGeneric, IRT::Usize},
+      IRT::Unit}},
+    // pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize)
+    {IValue::VOLATILE_COPY_MEMORY,
+     {1,
+      {IRT::MutPtrFirstGeneric, IRT::ConstPtrFirstGeneric, IRT::Usize},
+      IRT::Unit}},
+    // pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
+    {IValue::VOLATILE_SET_MEMORY,
+     {1, {IRT::MutPtrFirstGeneric, IRT::U8, IRT::Usize}, IRT::Unit}},
+    // pub fn volatile_load<T>(src: *const T) -> T;
+    {IValue::VOLATILE_LOAD,
+     {1, {IRT::ConstPtrFirstGeneric}, IRT::FirstGeneric}},
+    // pub fn volatile_store<T>(dst: *mut T, val: T);
+    {IValue::VOLATILE_STORE,
+     {1, {IRT::MutPtrFirstGeneric, IRT::FirstGeneric}, IRT::Unit}},
+    // pub fn unaligned_volatile_load<T>(src: *const T) -> T;
+    {IValue::UNALIGNED_VOLATILE_LOAD,
+     {1, {IRT::ConstPtrFirstGeneric}, IRT::FirstGeneric}},
+    // pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
+    {IValue::UNALIGNED_VOLATILE_STORE,
+     {1, {IRT::MutPtrFirstGeneric, IRT::FirstGeneric}, IRT::Unit}},
+
     // pub fn fadd_fast<T>(a: T, b: T) -> T
     // same for fsub_fast, fmul_fast, fdiv_fast and frem_fast
     {IValue::FADD_FAST,
diff --git a/gcc/rust/util/rust-intrinsic-values.h 
b/gcc/rust/util/rust-intrinsic-values.h
index f8067a62a..bbe023f9c 100644
--- a/gcc/rust/util/rust-intrinsic-values.h
+++ b/gcc/rust/util/rust-intrinsic-values.h
@@ -275,6 +275,15 @@ public:
   static constexpr auto &ASSERT_INHABITED = "assert_inhabited";
   static constexpr auto &ASSERT_UNINIT_VALID = "assert_uninit_valid";
 
+  static constexpr auto &VOLATILE_COPY_NONOVERLAPPING_MEMORY
+    = "volatile_copy_nonoverlapping_memory";
+  static constexpr auto &VOLATILE_COPY_MEMORY = "volatile_copy_memory";
+  static constexpr auto &VOLATILE_SET_MEMORY = "volatile_set_memory";
+  static constexpr auto &VOLATILE_LOAD = "volatile_load";
+  static constexpr auto &VOLATILE_STORE = "volatile_store";
+  static constexpr auto &UNALIGNED_VOLATILE_LOAD = "unaligned_volatile_load";
+  static constexpr auto &UNALIGNED_VOLATILE_STORE = "unaligned_volatile_store";
+
   static constexpr auto &FADD_FAST = "fadd_fast";
   static constexpr auto &FSUB_FAST = "fsub_fast";
   static constexpr auto &FMUL_FAST = "fmul_fast";
diff --git a/gcc/testsuite/rust/execute/torture/volatile_store_load.rs 
b/gcc/testsuite/rust/execute/torture/volatile_store_load.rs
new file mode 100644
index 000000000..5caea6a16
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/volatile_store_load.rs
@@ -0,0 +1,61 @@
+#![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" {
+    fn volatile_load<T>(src: *const T) -> T;
+    fn volatile_store<T>(dst: *mut T, val: T);
+    fn unaligned_volatile_load<T>(src: *const T) -> T;
+    fn unaligned_volatile_store<T>(dst: *mut T, val: T);
+}
+
+fn main() -> i32 {
+    let mut value: u32 = 0;
+    unsafe {
+        // aligned store/load tests
+        volatile_store (&mut value as *mut u32, 0x67FACADEu32);
+        if volatile_load(&value as *const u32) != 0x67FACADEu32 {
+            return 1;
+        }
+
+        // unaligned store/load tests
+        let value2: [u8; 8] = [0xABu8; 8];
+        let mut ptr = &value2 as *const [u8] as *const u8;
+        let unaligned_ptr = ptr.add(1);
+        let unaligned_u32_ptr = unaligned_ptr as *mut u32;
+        unaligned_volatile_store(unaligned_u32_ptr, 0xDEADBEEFu32);
+
+        if unaligned_volatile_load(unaligned_u32_ptr as *const u32) != 
0xDEADBEEFu32 {
+            return 1;
+        }
+
+        if value2[0] != 0xABu8 || value2[5] != 0xABu8 {
+            return 1;
+        }
+    }
+
+    0
+}
\ No newline at end of file

base-commit: 009865ec86703db7ef8d3a3c2fdc354938fe97d8
-- 
2.56.1

Reply via email to