Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wrap function in @inbounds if checkbounds = false #1390

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/build_function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/stencils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using SymbolicUtils.Code: toexpr, LiteralExpr

_repr(x) = repr(toexpr(LiteralExpr(x)) |> Base.remove_linenums!)
function test_funcs(name, f, args...; broken=false)
outplace, inplace = build_function(f, args...)
outplace, inplace = build_function(f, args...; checkbounds = true)
if broken
@test_broken open(x->read(x, String), "build_function_tests/$name-outplace.jl") == _repr(outplace)
@test_broken open(x->read(x, String), "build_function_tests/$name-inplace.jl") == _repr(outplace)
Expand Down
Loading