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

Reject @simd loops with break / continue / @goto in inner loop body #8624

Merged
merged 2 commits into from
Oct 14, 2014
Merged
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Library improvements
* New `ordschur` and `ordschur!` functions for sorting a schur factorization by the eigenvalues.

* `deepcopy` recurses through immutable types and makes copies of their mutable fields ([#8560]).

* `@simd` now rejects invalid control flow (`@goto` / break / continue) in the inner loop body at compile time ([#8624]).

Julia v0.3.0 Release Notes
==========================
Expand Down
16 changes: 16 additions & 0 deletions base/simdloop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,26 @@ function parse_iteration_space(x)
x.args # symbol, range
end

# reject invalid control flow statements in @simd loop body
function check_body!(x::Expr)
if x.head === :break || x.head == :continue
throw(SimdError("$(x.head) is not allowed inside a @simd loop body"))
elseif x.head === :macrocall && x.args[1] === symbol("@goto")
throw(SimdError("$(x.args[1]) is not allowed inside a @simd loop body"))
end
for arg in x.args
check_body!(arg)
end
return true
end
check_body!(x::QuoteNode) = check_body!(x.value)
check_body!(x) = true

# Compile Expr x in context of @simd.
function compile(x)
(isa(x, Expr) && x.head == :for) || throw(SimdError("for loop expected"))
length(x.args) == 2 || throw(SimdError("1D for loop expected"))
check_body!(x)

var,range = parse_iteration_space(x.args[1])
r = gensym("r") # Range value
Expand Down
23 changes: 23 additions & 0 deletions test/simdloop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,26 @@ let j=4
end
@test !simd_loop_local_present
end

import Base.SimdLoop.SimdError

# Test that @simd rejects inner loop body with invalid control flow statements
# issue #8613
@test_throws SimdError eval(:(begin
@simd for x = 1:10
x == 1 && break
end
end))

@test_throws SimdError eval(:(begin
@simd for x = 1:10
x < 5 && continue
end
end))

@test_throws SimdError eval(:(begin
@simd for x = 1:10
x == 1 || @goto exit_loop
end
@label exit_loop
end))