public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] intrinsics: Add unchecked operation intrinsics
@ 2022-10-27 18:55 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-10-27 18:55 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:fc2aa3cd381613a69129cd183a352f3e3c23643b

commit fc2aa3cd381613a69129cd183a352f3e3c23643b
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Wed Oct 26 15:57:54 2022 +0200

    intrinsics: Add unchecked operation intrinsics

Diff:
---
 gcc/rust/backend/rust-compile-intrinsic.cc         | 80 ++++++++++++++++++++++
 gcc/testsuite/rust/compile/torture/intrinsics-6.rs | 21 ++++++
 gcc/testsuite/rust/compile/torture/intrinsics-7.rs | 10 +++
 3 files changed, 111 insertions(+)

diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc
index 7c592dabb38..46ea5b3f795 100644
--- a/gcc/rust/backend/rust-compile-intrinsic.cc
+++ b/gcc/rust/backend/rust-compile-intrinsic.cc
@@ -34,6 +34,22 @@
 namespace Rust {
 namespace Compile {
 
+static bool
+is_basic_integer_type (TyTy::BaseType *type)
+{
+  switch (type->get_kind ())
+    {
+    case TyTy::INT:
+    case TyTy::UINT:
+    case TyTy::USIZE:
+    case TyTy::ISIZE:
+      return true;
+    default:
+      return false;
+      break;
+    }
+}
+
 static tree
 offset_handler (Context *ctx, TyTy::FnType *fntype);
 static tree
@@ -104,6 +120,17 @@ atomic_store_handler (int ordering)
   };
 }
 
+static inline tree
+unchecked_op_inner (Context *ctx, TyTy::FnType *fntype, tree_code op);
+
+const static std::function<tree (Context *, TyTy::FnType *)>
+unchecked_op_handler (tree_code op)
+{
+  return [op] (Context *ctx, TyTy::FnType *fntype) {
+    return unchecked_op_inner (ctx, fntype, op);
+  };
+}
+
 static inline tree
 sorry_handler (Context *ctx, TyTy::FnType *fntype)
 {
@@ -132,6 +159,13 @@ static const std::map<std::string,
     {"atomic_store_release", atomic_store_handler (__ATOMIC_RELEASE)},
     {"atomic_store_relaxed", atomic_store_handler (__ATOMIC_RELAXED)},
     {"atomic_store_unordered", atomic_store_handler (__ATOMIC_RELAXED)},
+    {"unchecked_add", unchecked_op_handler (PLUS_EXPR)},
+    {"unchecked_sub", unchecked_op_handler (MINUS_EXPR)},
+    {"unchecked_mul", unchecked_op_handler (MULT_EXPR)},
+    {"unchecked_div", unchecked_op_handler (TRUNC_DIV_EXPR)},
+    {"unchecked_rem", unchecked_op_handler (TRUNC_MOD_EXPR)},
+    {"unchecked_shl", unchecked_op_handler (LSHIFT_EXPR)},
+    {"unchecked_shr", unchecked_op_handler (RSHIFT_EXPR)},
 };
 
 Intrinsics::Intrinsics (Context *ctx) : ctx (ctx) {}
@@ -721,6 +755,52 @@ atomic_store_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering)
   TREE_SIDE_EFFECTS (store_call) = 1;
 
   ctx->add_statement (store_call);
+  finalize_intrinsic_block (ctx, fndecl);
+
+  return fndecl;
+}
+
+static inline tree
+unchecked_op_inner (Context *ctx, TyTy::FnType *fntype, tree_code op)
+{
+  rust_assert (fntype->get_params ().size () == 2);
+  rust_assert (fntype->get_num_substitutions () == 1);
+
+  tree lookup = NULL_TREE;
+  if (check_for_cached_intrinsic (ctx, fntype, &lookup))
+    return lookup;
+
+  auto fndecl = compile_intrinsic_function (ctx, fntype);
+
+  // setup the params
+  std::vector<Bvariable *> param_vars;
+  compile_fn_params (ctx, fntype, fndecl, &param_vars);
+
+  if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
+    return error_mark_node;
+
+  enter_intrinsic_block (ctx, fndecl);
+
+  // BUILTIN unchecked_<op> BODY BEGIN
+
+  auto x = ctx->get_backend ()->var_expression (param_vars[0], Location ());
+  auto y = ctx->get_backend ()->var_expression (param_vars[1], Location ());
+
+  auto *monomorphized_type
+    = fntype->get_substs ().at (0).get_param_ty ()->resolve ();
+  if (!is_basic_integer_type (monomorphized_type))
+    rust_error_at (fntype->get_locus (),
+		   "unchecked operation intrinsics can only be used with "
+		   "basic integer types (got %qs)",
+		   monomorphized_type->get_name ().c_str ());
+
+  auto expr = build2 (op, TREE_TYPE (x), x, y);
+  auto return_statement
+    = ctx->get_backend ()->return_statement (fndecl, {expr}, Location ());
+
+  ctx->add_statement (return_statement);
+
+  // BUILTIN unchecked_<op> BODY END
 
   finalize_intrinsic_block (ctx, fndecl);
 
diff --git a/gcc/testsuite/rust/compile/torture/intrinsics-6.rs b/gcc/testsuite/rust/compile/torture/intrinsics-6.rs
new file mode 100644
index 00000000000..143b62a2b27
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/intrinsics-6.rs
@@ -0,0 +1,21 @@
+extern "rust-intrinsic" {
+    pub fn unchecked_add<T>(x: T, y: T) -> T;
+    pub fn unchecked_sub<T>(x: T, y: T) -> T;
+    pub fn unchecked_mul<T>(x: T, y: T) -> T;
+    pub fn unchecked_div<T>(x: T, y: T) -> T;
+    pub fn unchecked_rem<T>(x: T, y: T) -> T;
+    pub fn unchecked_shl<T>(x: T, y: T) -> T;
+    pub fn unchecked_shr<T>(x: T, y: T) -> T;
+}
+
+fn main() -> i32 {
+    let zero0 = unsafe { (1 + 5) - unchecked_add(1, 5) };
+    let zero1 = unsafe { (1 - 5) - unchecked_sub(1, 5) };
+    let zero2 = unsafe { (1 * 5) - unchecked_mul(1, 5) };
+    let zero3 = unsafe { (1 / 5) - unchecked_div(1, 5) };
+    let zero4 = unsafe { (1 % 5) - unchecked_rem(1, 5) };
+    let zero5 = unsafe { (1 << 5) - unchecked_shl(1, 5) };
+    let zero6 = unsafe { (1 >> 5) - unchecked_shr(1, 5) };
+
+    zero0 + zero1 + zero2 + zero3 + zero4 + zero5 + zero6
+}
diff --git a/gcc/testsuite/rust/compile/torture/intrinsics-7.rs b/gcc/testsuite/rust/compile/torture/intrinsics-7.rs
new file mode 100644
index 00000000000..8e8c5fe0cdf
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/intrinsics-7.rs
@@ -0,0 +1,10 @@
+extern "rust-intrinsic" {
+    pub fn unchecked_add<T>(x: T, y: T) -> T;
+    // { dg-error "unchecked operation intrinsics can only be used with basic integer types .got .NotAdd.." "" { target *-*-* } .-1 }
+}
+
+fn main() {
+    struct NotAdd;
+
+    unsafe { unchecked_add(NotAdd, NotAdd) };
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-10-27 18:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-27 18:55 [gcc/devel/rust/master] intrinsics: Add unchecked operation intrinsics Thomas Schwinge

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).