public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Arthur Cohen <cohenarthur@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-7952] gccrs: Fix overflow intrinsic use before init
Date: Tue, 16 Jan 2024 18:10:10 +0000 (GMT)	[thread overview]
Message-ID: <20240116181010.AAF863857728@sourceware.org> (raw)

https://gcc.gnu.org/g:215b6acecb609d2a424e36116f1be8cf30d53438

commit r14-7952-g215b6acecb609d2a424e36116f1be8cf30d53438
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Thu Aug 31 11:32:18 2023 +0100

    gccrs: Fix overflow intrinsic use before init
    
    The overflow intrinsic returns a tuple of (value, boolean) where it value
    is the operator result and boolean if it overflowed or not. The intrinsic
    here did not initilize the resulting tuple and therefore was creating a use
    before init error resulting in garbage results
    
    Addresses #1895
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile-intrinsic.cc (op_with_overflow_inner): fix use before init
    
    Signed-off-by: Philip Herron <herron.philip@googlemail.com>

Diff:
---
 gcc/rust/backend/rust-compile-intrinsic.cc | 46 +++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 16 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc
index 5c1cde3b202..cbf6c0bb2b8 100644
--- a/gcc/rust/backend/rust-compile-intrinsic.cc
+++ b/gcc/rust/backend/rust-compile-intrinsic.cc
@@ -598,7 +598,25 @@ op_with_overflow_inner (Context *ctx, TyTy::FnType *fntype, tree_code op)
   if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
     return error_mark_node;
 
-  enter_intrinsic_block (ctx, fndecl);
+  rust_assert (fntype->get_num_substitutions () == 1);
+  auto &param_mapping = fntype->get_substs ().at (0);
+  const TyTy::ParamType *param_tyty = param_mapping.get_param_ty ();
+  TyTy::BaseType *resolved_tyty = param_tyty->resolve ();
+  tree template_parameter_type
+    = TyTyResolveCompile::compile (ctx, resolved_tyty);
+
+  // this should match y as well or we can take it from the TyTy structure
+  tree tmp_stmt = error_mark_node;
+  Bvariable *result_variable = ctx->get_backend ()->temporary_variable (
+    fndecl, NULL_TREE, template_parameter_type, NULL_TREE,
+    true /*address_is_taken*/, UNDEF_LOCATION, &tmp_stmt);
+  Bvariable *bool_variable
+    = ctx->get_backend ()->temporary_variable (fndecl, NULL_TREE,
+					       boolean_type_node, NULL_TREE,
+					       true /*address_is_taken*/,
+					       UNDEF_LOCATION, &tmp_stmt);
+
+  enter_intrinsic_block (ctx, fndecl, {result_variable, bool_variable});
 
   // BUILTIN op_with_overflow FN BODY BEGIN
   auto x = ctx->get_backend ()->var_expression (x_param, UNDEF_LOCATION);
@@ -628,24 +646,20 @@ op_with_overflow_inner (Context *ctx, TyTy::FnType *fntype, tree_code op)
     }
   rust_assert (overflow_builtin != error_mark_node);
 
-  // this should match y as well or we can take it from the TyTy structure
-  tree overflow_op_type = TREE_TYPE (x);
-  tree tmp_stmt = error_mark_node;
-  Bvariable *bvar
-    = ctx->get_backend ()->temporary_variable (fndecl, NULL_TREE,
-					       overflow_op_type, NULL_TREE,
-					       true /*address_is_taken*/,
-					       UNDEF_LOCATION, &tmp_stmt);
-  ctx->add_statement (tmp_stmt);
-
-  tree result_decl = bvar->get_tree (UNDEF_LOCATION);
+  tree bool_decl = bool_variable->get_tree (BUILTINS_LOCATION);
+  tree result_decl = result_variable->get_tree (BUILTINS_LOCATION);
   tree result_ref = build_fold_addr_expr_loc (BUILTINS_LOCATION, result_decl);
 
-  tree did_overflow_node
-    = build_call_expr_loc (BUILTINS_LOCATION, overflow_builtin, 3, x, y,
-			   result_ref);
+  tree builtin_call = build_call_expr_loc (BUILTINS_LOCATION, overflow_builtin,
+					   3, x, y, result_ref);
+
+  tree overflow_assignment
+    = ctx->get_backend ()->assignment_statement (bool_decl, builtin_call,
+						 BUILTINS_LOCATION);
+
+  ctx->add_statement (overflow_assignment);
 
-  std::vector<tree> vals = {result_decl, did_overflow_node};
+  std::vector<tree> vals = {result_decl, bool_decl};
   tree tuple_type = TREE_TYPE (DECL_RESULT (fndecl));
   tree result_expr
     = ctx->get_backend ()->constructor_expression (tuple_type, false, vals, -1,

                 reply	other threads:[~2024-01-16 18:10 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=20240116181010.AAF863857728@sourceware.org \
    --to=cohenarthur@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).