Skip to content

Commit 2d1c287

Browse files
Merge #1620
1620: Intrinsics unchecked ops r=CohenArthur a=CohenArthur Addresses #658 Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2 parents 353ed6c + fc2aa3c commit 2d1c287

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

gcc/rust/backend/rust-compile-intrinsic.cc

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@
3434
namespace Rust {
3535
namespace Compile {
3636

37+
static bool
38+
is_basic_integer_type (TyTy::BaseType *type)
39+
{
40+
switch (type->get_kind ())
41+
{
42+
case TyTy::INT:
43+
case TyTy::UINT:
44+
case TyTy::USIZE:
45+
case TyTy::ISIZE:
46+
return true;
47+
default:
48+
return false;
49+
break;
50+
}
51+
}
52+
3753
static tree
3854
offset_handler (Context *ctx, TyTy::FnType *fntype);
3955
static tree
@@ -97,6 +113,17 @@ atomic_store_handler (int ordering)
97113
};
98114
}
99115

116+
static inline tree
117+
unchecked_op_inner (Context *ctx, TyTy::FnType *fntype, tree_code op);
118+
119+
const static std::function<tree (Context *, TyTy::FnType *)>
120+
unchecked_op_handler (tree_code op)
121+
{
122+
return [op] (Context *ctx, TyTy::FnType *fntype) {
123+
return unchecked_op_inner (ctx, fntype, op);
124+
};
125+
}
126+
100127
static inline tree
101128
sorry_handler (Context *ctx, TyTy::FnType *fntype)
102129
{
@@ -125,6 +152,13 @@ static const std::map<std::string,
125152
{"atomic_store_release", atomic_store_handler (__ATOMIC_RELEASE)},
126153
{"atomic_store_relaxed", atomic_store_handler (__ATOMIC_RELAXED)},
127154
{"atomic_store_unordered", atomic_store_handler (__ATOMIC_RELAXED)},
155+
{"unchecked_add", unchecked_op_handler (PLUS_EXPR)},
156+
{"unchecked_sub", unchecked_op_handler (MINUS_EXPR)},
157+
{"unchecked_mul", unchecked_op_handler (MULT_EXPR)},
158+
{"unchecked_div", unchecked_op_handler (TRUNC_DIV_EXPR)},
159+
{"unchecked_rem", unchecked_op_handler (TRUNC_MOD_EXPR)},
160+
{"unchecked_shl", unchecked_op_handler (LSHIFT_EXPR)},
161+
{"unchecked_shr", unchecked_op_handler (RSHIFT_EXPR)},
128162
};
129163

130164
Intrinsics::Intrinsics (Context *ctx) : ctx (ctx) {}
@@ -714,6 +748,52 @@ atomic_store_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering)
714748
TREE_SIDE_EFFECTS (store_call) = 1;
715749

716750
ctx->add_statement (store_call);
751+
finalize_intrinsic_block (ctx, fndecl);
752+
753+
return fndecl;
754+
}
755+
756+
static inline tree
757+
unchecked_op_inner (Context *ctx, TyTy::FnType *fntype, tree_code op)
758+
{
759+
rust_assert (fntype->get_params ().size () == 2);
760+
rust_assert (fntype->get_num_substitutions () == 1);
761+
762+
tree lookup = NULL_TREE;
763+
if (check_for_cached_intrinsic (ctx, fntype, &lookup))
764+
return lookup;
765+
766+
auto fndecl = compile_intrinsic_function (ctx, fntype);
767+
768+
// setup the params
769+
std::vector<Bvariable *> param_vars;
770+
compile_fn_params (ctx, fntype, fndecl, &param_vars);
771+
772+
if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
773+
return error_mark_node;
774+
775+
enter_intrinsic_block (ctx, fndecl);
776+
777+
// BUILTIN unchecked_<op> BODY BEGIN
778+
779+
auto x = ctx->get_backend ()->var_expression (param_vars[0], Location ());
780+
auto y = ctx->get_backend ()->var_expression (param_vars[1], Location ());
781+
782+
auto *monomorphized_type
783+
= fntype->get_substs ().at (0).get_param_ty ()->resolve ();
784+
if (!is_basic_integer_type (monomorphized_type))
785+
rust_error_at (fntype->get_locus (),
786+
"unchecked operation intrinsics can only be used with "
787+
"basic integer types (got %qs)",
788+
monomorphized_type->get_name ().c_str ());
789+
790+
auto expr = build2 (op, TREE_TYPE (x), x, y);
791+
auto return_statement
792+
= ctx->get_backend ()->return_statement (fndecl, {expr}, Location ());
793+
794+
ctx->add_statement (return_statement);
795+
796+
// BUILTIN unchecked_<op> BODY END
717797

718798
finalize_intrinsic_block (ctx, fndecl);
719799

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
extern "rust-intrinsic" {
2+
pub fn unchecked_add<T>(x: T, y: T) -> T;
3+
pub fn unchecked_sub<T>(x: T, y: T) -> T;
4+
pub fn unchecked_mul<T>(x: T, y: T) -> T;
5+
pub fn unchecked_div<T>(x: T, y: T) -> T;
6+
pub fn unchecked_rem<T>(x: T, y: T) -> T;
7+
pub fn unchecked_shl<T>(x: T, y: T) -> T;
8+
pub fn unchecked_shr<T>(x: T, y: T) -> T;
9+
}
10+
11+
fn main() -> i32 {
12+
let zero0 = unsafe { (1 + 5) - unchecked_add(1, 5) };
13+
let zero1 = unsafe { (1 - 5) - unchecked_sub(1, 5) };
14+
let zero2 = unsafe { (1 * 5) - unchecked_mul(1, 5) };
15+
let zero3 = unsafe { (1 / 5) - unchecked_div(1, 5) };
16+
let zero4 = unsafe { (1 % 5) - unchecked_rem(1, 5) };
17+
let zero5 = unsafe { (1 << 5) - unchecked_shl(1, 5) };
18+
let zero6 = unsafe { (1 >> 5) - unchecked_shr(1, 5) };
19+
20+
zero0 + zero1 + zero2 + zero3 + zero4 + zero5 + zero6
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extern "rust-intrinsic" {
2+
pub fn unchecked_add<T>(x: T, y: T) -> T;
3+
// { dg-error "unchecked operation intrinsics can only be used with basic integer types .got .NotAdd.." "" { target *-*-* } .-1 }
4+
}
5+
6+
fn main() {
7+
struct NotAdd;
8+
9+
unsafe { unchecked_add(NotAdd, NotAdd) };
10+
}

0 commit comments

Comments
 (0)