public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] transmute: Fix behavior by always performing the raw copy
Date: Thu, 11 Aug 2022 19:19:11 +0000 (GMT)	[thread overview]
Message-ID: <20220811191911.88C8D3857BAB@sourceware.org> (raw)

https://gcc.gnu.org/g:71b5bc8705c789fa61ad0d7858441da861213356

commit 71b5bc8705c789fa61ad0d7858441da861213356
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Tue Aug 9 13:50:39 2022 +0200

    transmute: Fix behavior by always performing the raw copy
    
    This desugars calls to transmute the following way:
    
    `transmute::<T1, T2>(value)`
    
    ->
    
    `*((T2 *) &value)`
    
    This always ends up being optimized into a simple copy for small types
    and a memcpy for larger types.
    
    Co-authored-by: philberty <philip.herron@embecosm.com>

Diff:
---
 gcc/rust/backend/rust-compile-intrinsic.cc       | 36 ++++++------------------
 gcc/testsuite/rust/execute/torture/transmute1.rs | 23 +++++++++++++++
 2 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc
index ebf1ed61f55..75e8c99158b 100644
--- a/gcc/rust/backend/rust-compile-intrinsic.cc
+++ b/gcc/rust/backend/rust-compile-intrinsic.cc
@@ -390,33 +390,15 @@ transmute_intrinsic_handler (Context *ctx, TyTy::BaseType *fntype_tyty)
   ctx->push_block (code_block);
 
   // BUILTIN transmute FN BODY BEGIN
-  tree result_type_tree = TREE_TYPE (DECL_RESULT (fndecl));
-  tree result_expr = error_mark_node;
-  if (AGGREGATE_TYPE_P (TREE_TYPE (convert_me_expr)))
-    {
-      // Return *(orig_type*)&decl.  */
-      // tree t = build_fold_addr_expr_loc (location.gcc_location (), this->t_);
-      // t = fold_build1_loc (location.gcc_location (), NOP_EXPR,
-      //       	       build_pointer_type (this->orig_type_), t);
-      // return build_fold_indirect_ref_loc (location.gcc_location (), t);
-
-      // result_expr = fold_build1_loc (Location ().gcc_location (),
-      // CONVERT_EXPR,
-      //   			     result_type_tree, convert_me_expr);
-
-      tree t = build_fold_addr_expr_loc (Location ().gcc_location (),
-					 convert_me_expr);
-      t = fold_build1_loc (Location ().gcc_location (), NOP_EXPR,
-			   build_pointer_type (target_type_expr), t);
-      result_expr
-	= build_fold_indirect_ref_loc (Location ().gcc_location (), t);
-    }
-  else
-    {
-      result_expr = ctx->get_backend ()->convert_expression (result_type_tree,
-							     convert_me_expr,
-							     Location ());
-    }
+
+  // Return *((orig_type*)&decl)  */
+
+  tree t
+    = build_fold_addr_expr_loc (Location ().gcc_location (), convert_me_expr);
+  t = fold_build1_loc (Location ().gcc_location (), NOP_EXPR,
+		       build_pointer_type (target_type_expr), t);
+  tree result_expr
+    = build_fold_indirect_ref_loc (Location ().gcc_location (), t);
 
   auto return_statement
     = ctx->get_backend ()->return_statement (fndecl, {result_expr},
diff --git a/gcc/testsuite/rust/execute/torture/transmute1.rs b/gcc/testsuite/rust/execute/torture/transmute1.rs
new file mode 100644
index 00000000000..b9ec38ca618
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/transmute1.rs
@@ -0,0 +1,23 @@
+// { dg-additional-options "-w" }
+
+extern "rust-intrinsic" {
+    fn transmute<T, U>(value: T) -> U;
+}
+
+struct WrapI {
+    inner: i32,
+}
+
+struct WrapF {
+    inner: f32,
+}
+
+fn main() -> i32 {
+    let f = 15.4f32;
+    let f_wrap = WrapF { inner: f };
+
+    let fst = unsafe { transmute::<f32, i32>(f) };
+    let snd = unsafe { transmute::<WrapF, WrapI>(f_wrap) };
+
+    fst - snd.inner
+}


                 reply	other threads:[~2022-08-11 19:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220811191911.88C8D3857BAB@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).