julia icon indicating copy to clipboard operation
julia copied to clipboard

`CC.populate_def_use_map!` doesn't work as expected.

Open jumerckx opened this issue 1 year ago • 1 comments

MWE:

using Test

const CC = Core.Compiler

ir, ret = only(Base.code_ircode((Int, ); optimize_until="compact 1") do x
    y = 2*x
    return y
end)

tpdum = CC.TwoPhaseDefUseMap(length(ir.stmts));

CC.populate_def_use_map!(tpdum, ir)

@test !isempty(tpdum.data) # fails, tpdum is empty

The problem is the following, as described by @willtebbutt on Slack:

I'm a bit confused by the use of userefs in populate_def_use_map! , see here: https://github.com/JuliaLang/julia/blob/b19a7c1721f623ae085354889b183622537543b0/base/compiler/ssair/irinterp.jl#L290 In particular:

  1. the argument to userefs is an Instruction, meaning that the relevant flag of the returned UseRefIterator will always be set to false.
  2. The relevant flag being false means that no iterations of the linked for loop will ever be performed.
  3. Therefore, calling populate_def_use_map does not appear to ever modify tpdum.

jumerckx avatar Oct 16 '24 13:10 jumerckx

That MWE isn't quite right. Looking at TwoPhaseDefUseMap in the Julia source, I believe this is supposed to work:

const CC = Core.Compiler

ir, ret = only(Base.code_ircode((Int, ); optimize_until="compact 1") do x
    y = 2*x
    return y
end)

function def_use_map(ir)
    tpdum = CC.TwoPhaseDefUseMap(length(ir.stmts));
    scanner = CC.BBScanner(ir)
    stmt_ip = CC.BitSetBoundedMinPrioritySet(length(ir.stmts))
    CC.scan!(scanner, false) do inst::CC.Instruction, lstmt::Int, bb::Int
        idx = inst.idx
        stmt = inst[:stmt]
        for ur in CC.userefs(stmt)
            val = ur[]
            if isa(val, Core.SSAValue)
                CC.count!(tpdum, val)
            end
        end
        return true
    end
    CC.complete!(tpdum); CC.push!(scanner.bb_ip, 1)
    CC.populate_def_use_map!(tpdum, ir)
    tpdum
end

def_use_map(ir) # doesn't contain uses

jumerckx avatar Oct 16 '24 18:10 jumerckx

Thanks for sorting this @vtjnash !

willtebbutt avatar Oct 14 '25 16:10 willtebbutt