Skip to content

Commit

Permalink
Minor optimization of loop unrolling (#22)
Browse files Browse the repository at this point in the history
* increment position before break

* remove the upper limit of loop unrolling
  • Loading branch information
bicycle1885 authored Jul 22, 2017
1 parent a57c2fe commit 8ee444c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Arguments
- `vars`: variable names used in generated code
- `generator`: code generator (`:table`, `:inline` or `:goto`)
- `checkbounds`: flag of bounds check
- `loopunroll`: loop unroll factor (0..8)
- `loopunroll`: loop unroll factor (≥ 0)
- `getbyte`: function of byte access (i.e. `getbyte(data, p)`)
- `clean`: flag of code cleansing
"""
Expand All @@ -66,8 +66,8 @@ function CodeGenContext(;
loopunroll::Integer=0,
getbyte::Function=Base.getindex,
clean::Bool=false)
if !(0 loopunroll 8)
throw(ArgumentError("unroll factor must be within 0..8"))
if loopunroll < 0
throw(ArgumentError("loop unroll factor must be a non-negative integer"))
elseif loopunroll > 0 && generator != :goto
throw(ArgumentError("loop unrolling is not supported for $(generator)"))
end
Expand Down Expand Up @@ -358,7 +358,10 @@ function generate_unrolled_loop(ctx::CodeGenContext, edge::Edge, t::Node)
body.args,
quote
$(generate_geybyte_code(ctx, l, k))
!$(generate_simple_condition_code(edge, l)) && break
$(generate_simple_condition_code(edge, l)) || begin
$(ctx.vars.p) += $(k-1)
break
end
end)
end
push!(body.args, :($(ctx.vars.p) += $(ctx.loopunroll)))
Expand Down
2 changes: 1 addition & 1 deletion test/test16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Base.Test
@testset "Test16" begin
@test_throws ArgumentError Automa.CodeGenContext(generator=:table, loopunroll=1)
@test_throws ArgumentError Automa.CodeGenContext(generator=:inline, loopunroll=1)
@test_throws ArgumentError Automa.CodeGenContext(generator=:goto, loopunroll=9)
@test_throws ArgumentError Automa.CodeGenContext(generator=:goto, loopunroll=-1)

re = re"A+(B+C)*(D|E)+"
machine = Automa.compile(re)
Expand Down

0 comments on commit 8ee444c

Please sign in to comment.