From: Pierre-Emmanuel Patry <[email protected]>
gcc/rust/ChangeLog:
* backend/rust-compile-intrinsic.cc: Use predefined values.
* checks/errors/rust-unsafe-checker.cc (is_safe_intrinsic): Likewise.
* util/rust-intrinsic-values.h: New file.
Signed-off-by: Pierre-Emmanuel Patry <[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/9894195b0405d74040ee3f2573077db5bdbfe511
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4459
gcc/rust/backend/rust-compile-intrinsic.cc | 82 +++++++-------
gcc/rust/checks/errors/rust-unsafe-checker.cc | 73 +++++++------
gcc/rust/util/rust-intrinsic-values.h | 100 ++++++++++++++++++
3 files changed, 181 insertions(+), 74 deletions(-)
create mode 100644 gcc/rust/util/rust-intrinsic-values.h
diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc
b/gcc/rust/backend/rust-compile-intrinsic.cc
index c733d926f..f46341966 100644
--- a/gcc/rust/backend/rust-compile-intrinsic.cc
+++ b/gcc/rust/backend/rust-compile-intrinsic.cc
@@ -20,50 +20,54 @@
#include "rust-diagnostics.h"
#include "tree-core.h"
#include "rust-intrinsic-handlers.h"
+#include "rust-intrinsic-values.h"
namespace Rust {
namespace Compile {
+using IValue = Values::Intrinsics;
+
static const std::map<std::string, handlers::HandlerBuilder> generic_intrinsics
- = {{"offset", handlers::offset},
- {"size_of", handlers::sizeof_handler},
- {"transmute", handlers::transmute},
- {"rotate_left", handlers::rotate_left},
- {"rotate_right", handlers::rotate_right},
- {"wrapping_add", handlers::wrapping_op (PLUS_EXPR)},
- {"wrapping_sub", handlers::wrapping_op (MINUS_EXPR)},
- {"wrapping_mul", handlers::wrapping_op (MULT_EXPR)},
- {"add_with_overflow", handlers::op_with_overflow (PLUS_EXPR)},
- {"sub_with_overflow", handlers::op_with_overflow (MINUS_EXPR)},
- {"mul_with_overflow", handlers::op_with_overflow (MULT_EXPR)},
- {"copy", handlers::copy (true)},
- {"copy_nonoverlapping", handlers::copy (false)},
- {"prefetch_read_data", handlers::prefetch_read_data},
- {"prefetch_write_data", handlers::prefetch_write_data},
- {"atomic_store_seqcst", handlers::atomic_store (__ATOMIC_SEQ_CST)},
- {"atomic_store_release", handlers::atomic_store (__ATOMIC_RELEASE)},
- {"atomic_store_relaxed", handlers::atomic_store (__ATOMIC_RELAXED)},
- {"atomic_store_unordered", handlers::atomic_store (__ATOMIC_RELAXED)},
- {"atomic_load_seqcst", handlers::atomic_load (__ATOMIC_SEQ_CST)},
- {"atomic_load_acquire", handlers::atomic_load (__ATOMIC_ACQUIRE)},
- {"atomic_load_relaxed", handlers::atomic_load (__ATOMIC_RELAXED)},
- {"atomic_load_unordered", handlers::atomic_load (__ATOMIC_RELAXED)},
- {"unchecked_add", handlers::unchecked_op (PLUS_EXPR)},
- {"unchecked_sub", handlers::unchecked_op (MINUS_EXPR)},
- {"unchecked_mul", handlers::unchecked_op (MULT_EXPR)},
- {"unchecked_div", handlers::unchecked_op (TRUNC_DIV_EXPR)},
- {"unchecked_rem", handlers::unchecked_op (TRUNC_MOD_EXPR)},
- {"unchecked_shl", handlers::unchecked_op (LSHIFT_EXPR)},
- {"unchecked_shr", handlers::unchecked_op (RSHIFT_EXPR)},
- {"uninit", handlers::uninit},
- {"move_val_init", handlers::move_val_init},
- {"likely", handlers::expect (true)},
- {"unlikely", handlers::expect (false)},
- {"assume", handlers::assume},
- {"try", handlers::try_handler (false)},
- {"catch_unwind", handlers::try_handler (true)},
- {"discriminant_value", handlers::discriminant_value},
- {"variant_count", handlers::variant_count}};
+ = {{IValue::OFFSET, handlers::offset},
+ {IValue::SIZE_OF, handlers::sizeof_handler},
+ {IValue::TRANSMUTE, handlers::transmute},
+ {IValue::ROTATE_LEFT, handlers::rotate_left},
+ {IValue::ROTATE_RIGHT, handlers::rotate_right},
+ {IValue::WRAPPING_ADD, handlers::wrapping_op (PLUS_EXPR)},
+ {IValue::WRAPPING_SUB, handlers::wrapping_op (MINUS_EXPR)},
+ {IValue::WRAPPING_MUL, handlers::wrapping_op (MULT_EXPR)},
+ {IValue::ADD_WITH_OVERFLOW, handlers::op_with_overflow (PLUS_EXPR)},
+ {IValue::SUB_WITH_OVERFLOW, handlers::op_with_overflow (MINUS_EXPR)},
+ {IValue::MUL_WITH_OVERFLOW, handlers::op_with_overflow (MULT_EXPR)},
+ {IValue::COPY, handlers::copy (true)},
+ {IValue::COPY_NONOVERLAPPING, handlers::copy (false)},
+ {IValue::PREFETCH_READ_DATA, handlers::prefetch_read_data},
+ {IValue::PREFETCH_WRITE_DATA, handlers::prefetch_write_data},
+ {IValue::ATOMIC_STORE_SEQCST, handlers::atomic_store (__ATOMIC_SEQ_CST)},
+ {IValue::ATOMIC_STORE_RELEASE, handlers::atomic_store (__ATOMIC_RELEASE)},
+ {IValue::ATOMIC_STORE_RELAXED, handlers::atomic_store (__ATOMIC_RELAXED)},
+ {IValue::ATOMIC_STORE_UNORDERED,
+ handlers::atomic_store (__ATOMIC_RELAXED)},
+ {IValue::ATOMIC_LOAD_SEQCST, handlers::atomic_load (__ATOMIC_SEQ_CST)},
+ {IValue::ATOMIC_LOAD_ACQUIRE, handlers::atomic_load (__ATOMIC_ACQUIRE)},
+ {IValue::ATOMIC_LOAD_RELAXED, handlers::atomic_load (__ATOMIC_RELAXED)},
+ {IValue::ATOMIC_LOAD_UNORDERED, handlers::atomic_load (__ATOMIC_RELAXED)},
+ {IValue::UNCHECKED_ADD, handlers::unchecked_op (PLUS_EXPR)},
+ {IValue::UNCHECKED_SUB, handlers::unchecked_op (MINUS_EXPR)},
+ {IValue::UNCHECKED_MUL, handlers::unchecked_op (MULT_EXPR)},
+ {IValue::UNCHECKED_DIV, handlers::unchecked_op (TRUNC_DIV_EXPR)},
+ {IValue::UNCHECKED_REM, handlers::unchecked_op (TRUNC_MOD_EXPR)},
+ {IValue::UNCHECKED_SHL, handlers::unchecked_op (LSHIFT_EXPR)},
+ {IValue::UNCHECKED_SHR, handlers::unchecked_op (RSHIFT_EXPR)},
+ {IValue::UNINIT, handlers::uninit},
+ {IValue::MOVE_VAL_INIT, handlers::move_val_init},
+ {IValue::LIKELY, handlers::expect (true)},
+ {IValue::UNLIKELY, handlers::expect (false)},
+ {IValue::ASSUME, handlers::assume},
+ {IValue::TRY, handlers::try_handler (false)},
+ {IValue::CATCH_UNWIND, handlers::try_handler (true)},
+ {IValue::DISCRIMINANT_VALUE, handlers::discriminant_value},
+ {IValue::VARIANT_COUNT, handlers::variant_count}};
Intrinsics::Intrinsics (Context *ctx) : ctx (ctx) {}
diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc
b/gcc/rust/checks/errors/rust-unsafe-checker.cc
index 0a0261ffe..bf311384e 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.cc
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc
@@ -24,6 +24,7 @@
#include "rust-attribute-values.h"
#include "rust-system.h"
#include "rust-immutable-name-resolution-context.h"
+#include "rust-intrinsic-values.h"
// for flag_name_resolution_2_0
#include "options.h"
@@ -95,42 +96,44 @@ check_unsafe_call (HIR::Function *fn, location_t locus,
const std::string &kind)
static bool
is_safe_intrinsic (const std::string &fn_name)
{
+ using Intrinsics = Values::Intrinsics;
+
static const std::unordered_set<std::string> safe_intrinsics = {
- "abort",
- "size_of",
- "min_align_of",
- "needs_drop",
- "caller_location",
- "add_with_overflow",
- "sub_with_overflow",
- "mul_with_overflow",
- "wrapping_add",
- "wrapping_sub",
- "wrapping_mul",
- "saturating_add",
- "saturating_sub",
- "rotate_left",
- "rotate_right",
- "ctpop",
- "ctlz",
- "cttz",
- "bswap",
- "bitreverse",
- "discriminant_value",
- "type_id",
- "likely",
- "unlikely",
- "ptr_guaranteed_eq",
- "ptr_guaranteed_ne",
- "minnumf32",
- "minnumf64",
- "maxnumf32",
- "rustc_peek",
- "maxnumf64",
- "type_name",
- "forget",
- "black_box",
- "variant_count",
+ Intrinsics::ABORT,
+ Intrinsics::SIZE_OF,
+ Intrinsics::MIN_ALIGN_OF,
+ Intrinsics::NEEDS_DROP,
+ Intrinsics::CALLER_LOCATION,
+ Intrinsics::ADD_WITH_OVERFLOW,
+ Intrinsics::SUB_WITH_OVERFLOW,
+ Intrinsics::MUL_WITH_OVERFLOW,
+ Intrinsics::WRAPPING_ADD,
+ Intrinsics::WRAPPING_SUB,
+ Intrinsics::WRAPPING_MUL,
+ Intrinsics::SATURATING_ADD,
+ Intrinsics::SATURATING_SUB,
+ Intrinsics::ROTATE_LEFT,
+ Intrinsics::ROTATE_RIGHT,
+ Intrinsics::CTPOP,
+ Intrinsics::CTLZ,
+ Intrinsics::CTTZ,
+ Intrinsics::BSWAP,
+ Intrinsics::BITREVERSE,
+ Intrinsics::DISCRIMINANT_VALUE,
+ Intrinsics::TYPE_ID,
+ Intrinsics::LIKELY,
+ Intrinsics::UNLIKELY,
+ Intrinsics::PTR_GUARANTEED_EQ,
+ Intrinsics::PTR_GUARANTEED_NE,
+ Intrinsics::MINNUMF32,
+ Intrinsics::MINNUMF64,
+ Intrinsics::MAXNUMF32,
+ Intrinsics::MAXNUMF64,
+ Intrinsics::RUSTC_PEEK,
+ Intrinsics::TYPE_NAME,
+ Intrinsics::FORGET,
+ Intrinsics::BLACK_BOX,
+ Intrinsics::VARIANT_COUNT,
};
return safe_intrinsics.find (fn_name) != safe_intrinsics.end ();
diff --git a/gcc/rust/util/rust-intrinsic-values.h
b/gcc/rust/util/rust-intrinsic-values.h
new file mode 100644
index 000000000..6754392a0
--- /dev/null
+++ b/gcc/rust/util/rust-intrinsic-values.h
@@ -0,0 +1,100 @@
+// Copyright (C) 2026 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+namespace Rust {
+namespace Values {
+class Intrinsics
+{
+public:
+ static constexpr auto &ABORT = "abort";
+ static constexpr auto &OFFSET = "offset";
+ static constexpr auto &SIZE_OF = "size_of";
+ static constexpr auto &TRANSMUTE = "transmute";
+
+ static constexpr auto &ADD_WITH_OVERFLOW = "add_with_overflow";
+ static constexpr auto &SUB_WITH_OVERFLOW = "sub_with_overflow";
+ static constexpr auto &MUL_WITH_OVERFLOW = "mul_with_overflow";
+
+ static constexpr auto © = "copy";
+ static constexpr auto ©_NONOVERLAPPING = "copy_nonoverlapping";
+
+ static constexpr auto &PREFETCH_READ_DATA = "prefetch_read_data";
+ static constexpr auto &PREFETCH_WRITE_DATA = "prefetch_write_data";
+
+ static constexpr auto &ATOMIC_STORE_SEQCST = "atomic_store_seqcst";
+ static constexpr auto &ATOMIC_STORE_RELEASE = "atomic_store_release";
+ static constexpr auto &ATOMIC_STORE_RELAXED = "atomic_store_relaxed";
+ static constexpr auto &ATOMIC_STORE_UNORDERED = "atomic_store_unordered";
+
+ static constexpr auto &ATOMIC_LOAD_SEQCST = "atomic_load_seqcst";
+ static constexpr auto &ATOMIC_LOAD_ACQUIRE = "atomic_load_acquire";
+ static constexpr auto &ATOMIC_LOAD_RELAXED = "atomic_load_relaxed";
+ static constexpr auto &ATOMIC_LOAD_UNORDERED = "atomic_load_unordered";
+
+ static constexpr auto &UNCHECKED_ADD = "unchecked_add";
+ static constexpr auto &UNCHECKED_SUB = "unchecked_sub";
+ static constexpr auto &UNCHECKED_MUL = "unchecked_mul";
+ static constexpr auto &UNCHECKED_DIV = "unchecked_div";
+ static constexpr auto &UNCHECKED_REM = "unchecked_rem";
+ static constexpr auto &UNCHECKED_SHL = "unchecked_shl";
+ static constexpr auto &UNCHECKED_SHR = "unchecked_shr";
+
+ static constexpr auto &UNINIT = "uninit";
+ static constexpr auto &MOVE_VAL_INIT = "move_val_init";
+
+ static constexpr auto &ROTATE_LEFT = "rotate_left";
+ static constexpr auto &ROTATE_RIGHT = "rotate_right";
+
+ static constexpr auto &WRAPPING_ADD = "wrapping_add";
+ static constexpr auto &WRAPPING_SUB = "wrapping_sub";
+ static constexpr auto &WRAPPING_MUL = "wrapping_mul";
+
+ static constexpr auto &SATURATING_ADD = "saturating_add";
+ static constexpr auto &SATURATING_SUB = "saturating_sub";
+
+ static constexpr auto &LIKELY = "likely";
+ static constexpr auto &UNLIKELY = "unlikely";
+
+ static constexpr auto &DISCRIMINANT_VALUE = "discriminant_value";
+ static constexpr auto &VARIANT_COUNT = "variant_count";
+ static constexpr auto &CATCH_UNWIND = "catch_unwind";
+ static constexpr auto &TRY = "try";
+ static constexpr auto &ASSUME = "assume";
+
+ static constexpr auto &MIN_ALIGN_OF = "min_align_of";
+ static constexpr auto &NEEDS_DROP = "needs_drop";
+ static constexpr auto &CALLER_LOCATION = "caller_location";
+ static constexpr auto &CTPOP = "ctpop";
+ static constexpr auto &CTLZ = "ctlz";
+ static constexpr auto &CTTZ = "cttz";
+ static constexpr auto &BSWAP = "bswap";
+ static constexpr auto &BITREVERSE = "bitreverse";
+ static constexpr auto &TYPE_ID = "type_id";
+ static constexpr auto &PTR_GUARANTEED_EQ = "ptr_guaranteed_eq";
+ static constexpr auto &PTR_GUARANTEED_NE = "ptr_guaranteed_ne";
+ static constexpr auto &MINNUMF32 = "minnumf32";
+ static constexpr auto &MINNUMF64 = "minnumf64";
+ static constexpr auto &MAXNUMF32 = "maxnumf32";
+ static constexpr auto &MAXNUMF64 = "maxnumf64";
+ static constexpr auto &RUSTC_PEEK = "rustc_peek";
+ static constexpr auto &TYPE_NAME = "type_name";
+ static constexpr auto &FORGET = "forget";
+ static constexpr auto &BLACK_BOX = "black_box";
+};
+} // namespace Values
+} // namespace Rust
--
2.53.0