IRTools need help: purge unused arguments from argument list of a function

Hello,
I want to implement a method called “purge_function_args”, which remove the n’th argument (which is known to be dummy) from the function arg list. I need a kickstarter for the application of IRTools.jl package. Can anyone help to fill up a few lines of the functions in the template below, or provide me some hints? I am new to the theory of compilers and cannot understand the source codes of IRTools by staring at the lines…

Here is a code template with key functions not implemented yet.

using IRTools

"""
returns a IR transformer that purges n'th argument from 
the argument list of a function. For example, 

    R = purge_argument(2)
    f(a,b) = 2*a
    g = R(f)

produces a function `g(x) = 2*x`. 
"""
function purge_argument(n::Int)::Function
    x -> purge_argument_f(n, x)
end

function purge_argument_f(n::Int, ir0::IRTools.Inner.IR)::IRTools.Inner.IR
    #TODO recursive ?
    IRTools.Inner.IR(
        ir0.defs .|> purge_argument(n) |> final_step(n), #::Vector{Tuple{Int64, Int64}}
        ir0.blocks .|> purge_argument(n) |> final_step(n), #::Vector{IRTools.Inner.BasicBlock}
        ir0.lines .|> purge_argument(n) |> final_step(n), #::Vector{Core.LineInfoNode}
        copy(ir0.meta) #::Any
    )
end

function final_step(n::Int)::Function
    x -> final_step_f(n, x)
end


#

function purge_argument_f(n::Int, def0::Tuple{Int64,Int64})::Tuple{Int64,Int64}

end

function purge_argument_f(n::Int, blk0::IRTools.Inner.BasicBlock)::IRTools.Inner.BasicBlock
    # IRTools.Inner.BasicBlock <: Any
    #     stmts::Vector{IRTools.Inner.Statement}
    #     args::Vector{Any}
    #     argtypes::Vector{Any}
    #     branches::Vector{IRTools.Inner.Branch}
end

function purge_argument_f(n::Int, ln0::Core.LineInfoNode)::Core.LineInfoNode
    # code should not contain the n'th variable 
end

#

function final_step_f(n::Int, Vdef0::Vector{Tuple{Int64,Int64}})::Vector{Tuple{Int64,Int64}}
end

function final_step_f(n::Int, Vblk0::Vector{IRTools.Inner.BasicBlock})::Vector{IRTools.Inner.BasicBlock}
end

function final_step_f(n::Int, Vln0::Vector{Core.LineInfoNode})::Vector{Core.LineInfoNode}
end

1 Like

Achieved the goal without using IRTools.jl but I am still hoping to use it correctly…