julia
julia copied to clipboard
`CC.populate_def_use_map!` doesn't work as expected.
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:
- the argument to
userefsis anInstruction, meaning that the relevant flag of the returnedUseRefIteratorwill always be set tofalse.- The relevant flag being
falsemeans that no iterations of the linked for loop will ever be performed.- Therefore, calling
populate_def_use_mapdoes not appear to ever modifytpdum.
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
Thanks for sorting this @vtjnash !