From 248ffeeb97be87b53ec00a59551a3b95d458d9e4 Mon Sep 17 00:00:00 2001
From: Aayush Sabharwal <aayush.sabharwal@gmail.com>
Date: Wed, 18 Dec 2024 13:08:04 +0530
Subject: [PATCH] feat: wrap function in `@inbounds` if `checkbounds = false`

---
 src/build_function.jl | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/build_function.jl b/src/build_function.jl
index ffb21142b..93e6af290 100644
--- a/src/build_function.jl
+++ b/src/build_function.jl
@@ -119,7 +119,10 @@ function _build_function(target::JuliaTarget, op, args...;
         (wrap_code !== nothing) && (fun = wrap_code(fun))
         conv(fun, states)
     end
-
+    if !checkbounds
+        @assert Meta.isexpr(expr, :function)
+        expr.args[2] = :(@inbounds begin; $(expr.args[2]); end)
+    end
     if expression == Val{true}
         expr
     else
@@ -165,6 +168,12 @@ function _build_function(target::JuliaTarget, op::Union{Arr, ArrayOp}, args...;
           end) |> LiteralExpr
     end
     ip_expr = conv(Func(dargs, [], op_body), states)
+    if !checkbounds
+        @assert Meta.isexpr(oop_expr, :function)
+        oop_expr.args[2] = :(@inbounds begin; $(oop_expr.args[2]); end)
+        @assert Meta.isexpr(ip_expr, :function)
+        ip_expr.args[2] = :(@inbounds begin; $(ip_expr.args[2]); end)
+    end
     if expression == Val{true}
         oop_expr, ip_expr
     else
@@ -325,11 +334,18 @@ function _build_function(target::JuliaTarget, rhss::AbstractArray, args...;
         ip_expr = wrap_code[2](ip_expr)
     end
 
+    oop_expr, ip_expr = conv(oop_expr, states), conv(ip_expr, states)
+    if !checkbounds
+        @assert Meta.isexpr(oop_expr, :function)
+        oop_expr.args[2] = :(@inbounds begin; $(oop_expr.args[2]); end)
+        @assert Meta.isexpr(ip_expr, :function)
+        ip_expr.args[2] = :(@inbounds begin; $(ip_expr.args[2]); end)
+    end
     if expression == Val{true}
-        return conv(oop_expr, states), conv(ip_expr, states)
+        return oop_expr, ip_expr
     else
-        return _build_and_inject_function(expression_module, conv(oop_expr, states)),
-        _build_and_inject_function(expression_module, conv(ip_expr, states))
+        return _build_and_inject_function(expression_module, oop_expr),
+        _build_and_inject_function(expression_module, ip_expr)
     end
 end