diff --git a/src/compiler/transform/rewrite.jl b/src/compiler/transform/rewrite.jl index 43ad5303..d6aefc0e 100644 --- a/src/compiler/transform/rewrite.jl +++ b/src/compiler/transform/rewrite.jl @@ -198,10 +198,24 @@ mutable struct RewriteDriver dispatch::Dict{Any, Vector{RewriteRule}} worklist::Worklist constants::Union{Nothing, ConstantInfo} - modified::Set{SSAValue} # instructions whose operands were modified by forwarding + # Operand-modified cascade state. `modified[v] = g` means v's operands + # changed due to forwarding generation `g` (one generation per RBind + # rewrite). `cascaded[v]` is the generation that was current when v last + # propagated its mark to its users. A mark with generation <= cascaded[v] + # is stale: every IR change it stands for happened before that cascade, + # so cascading again cannot enable anything new. Without this check, a + # node reachable along k paths cascades k times, which is exponential in + # the depth of diamond-shaped use chains (e.g. add-with-carry chains). + modified::Dict{SSAValue, Int} + cascaded::Dict{SSAValue, Int} + generation::Int max_rewrites::Int end +function mark_modified!(driver::RewriteDriver, val::SSAValue, gen::Int) + driver.modified[val] = max(get(driver.modified, val, 0), gen) +end + #============================================================================= Rewriter listener: driver-side bookkeeping reacts to IR mutations =============================================================================# @@ -247,6 +261,7 @@ function notify_erased!(d::RewriteDriver, ::Block, val::SSAValue, delete!(d.defs, val) remove!(d.worklist, val) delete!(d.modified, val) + delete!(d.cascaded, val) end #============================================================================= @@ -408,8 +423,9 @@ function apply_rewrite!(driver::RewriteDriver, block, val::SSAValue, rule, match # driver propagates to THEIR users (see modified check in main loop). # This gives MLIR-style notifyOperationModified cascading. new_val = match.bindings[rule.rhs.name] + driver.generation += 1 for u in users_of(driver, val) - push!(driver.modified, u) + mark_modified!(driver, u, driver.generation) push!(driver.worklist, u) end replace_uses!(driver.rewriter, val, new_val) @@ -489,7 +505,8 @@ function rewrite_patterns!(sci::StructuredIRCode, rules::Vector{RewriteRule}; end rewriter = Rewriter(sci) - driver = RewriteDriver(rewriter, defs, dispatch, wl, constants, Set{SSAValue}(), + driver = RewriteDriver(rewriter, defs, dispatch, wl, constants, + Dict{SSAValue, Int}(), Dict{SSAValue, Int}(), 0, max_rewrites) rewriter.listener = driver @@ -538,12 +555,16 @@ function rewrite_patterns!(sci::StructuredIRCode, rules::Vector{RewriteRule}; # if this instruction's operands were changed by a forwarding rewrite but # no rule fired here, propagate to users — the operand change may enable # new matches further up the use chain. Mark users as modified too so the - # cascade continues through the fixpoint. - if !matched && val in driver.modified - delete!(driver.modified, val) - for u in users_of(driver, val) - push!(driver.modified, u) - haskey(driver.defs, u) && push!(driver.worklist, u) + # cascade continues through the fixpoint. Each node cascades at most + # once per forwarding generation (see `RewriteDriver.cascaded`). + if !matched && haskey(driver.modified, val) + gen = pop!(driver.modified, val) + if gen > get(driver.cascaded, val, 0) + driver.cascaded[val] = driver.generation + for u in users_of(driver, val) + mark_modified!(driver, u, gen) + haskey(driver.defs, u) && push!(driver.worklist, u) + end end end end diff --git a/test/codegen/rewrite.jl b/test/codegen/rewrite.jl new file mode 100644 index 00000000..348c352d --- /dev/null +++ b/test/codegen/rewrite.jl @@ -0,0 +1,24 @@ +# Codegen tests for the pattern-rewrite driver (`rewrite_patterns!`). + +@testset "rewrite — modified cascade is not exponential in diamond depth" begin + # An add-with-carry chain: every carry `t` is used both by the next step + # and by the low byte `b`, which also feeds the next step. Each step is a + # diamond in the use graph, so a node `D` steps down is reachable along + # 2^D paths. The operand-modified cascade used to re-propagate once per + # path, making normalization exponential in `D` (D=18 took ~35 s). + spec = ct.ArraySpec{1}(16, true) + D = 24 + steps = [:(t = UInt16.(a) .+ UInt16.(b) .+ (t .>> 0x0008); b = UInt8.(t)) for _ in 1:D] + carry_chain = @eval function (x, y, o) + a = ct.load(x, 1, (16,)) + b = ct.load(y, 1, (16,)) + t = UInt16.(a) + $(steps...) + ct.store(o, 1, b) + return + end + argtypes = Tuple{ct.TileArray{UInt8,1,Int32,spec}, ct.TileArray{UInt8,1,Int32,spec}, + ct.TileArray{UInt8,1,Int32,spec}} + ct.code_typed(carry_chain, argtypes) # keep inference out of the timing + @test @elapsed(ct.code_structured(carry_chain, argtypes)) < 30 +end