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

**URL:** <https://discourse.julialang.org/t/irtools-need-help-purge-unused-arguments-from-argument-list-of-a-function/86431>\
**Category:** General Usage\
**Tags:** question, package, ir\
**Created:** [August 27, 2022, 1:53pm UTC](https://discourse.julialang.org/t/irtools-need-help-purge-unused-arguments-from-argument-list-of-a-function/86431 "2022-08-27T13:53:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lian\_Yunlong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lian_yunlong/32/14437_2.png) [@Lian\_Yunlong](https://discourse.julialang.org/u/Lian_Yunlong)\
**Post date:** [August 27, 2022, 1:53pm UTC](https://discourse.julialang.org/t/irtools-need-help-purge-unused-arguments-from-argument-list-of-a-function/86431/1 "2022-08-27T13:53:44Z")

</div>

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.

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Lian\_Yunlong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lian_yunlong/32/14437_2.png) [@Lian\_Yunlong](https://discourse.julialang.org/u/Lian_Yunlong)\
**Post date:** [August 27, 2022, 2:02pm UTC](https://discourse.julialang.org/t/irtools-need-help-purge-unused-arguments-from-argument-list-of-a-function/86431/2 "2022-08-27T14:02:20Z")

</div>

> [@Transform CodeInfo of a function to get a new function by purging unused args from its arg list](https://discourse.julialang.org/t/transform-codeinfo-of-a-function-to-get-a-new-function-by-purging-unused-args-from-its-arg-list/87459/2):
>
> This code should work. function assign\_purge\_argument!(@nospecialize(p::Pair{\<:Function,\<:Function}), rule::Pair{Int,\<:Any}) (src, dst) = p (nth, value) = rule @assert length(methods(src)) == 1 "assign\_purge\_argument!(src=\>dst, n=\>val) requires src to have only one method." mod = getmodule(dst) function change\_slotnumber\_after\_nth(s) if s isa Core.SlotNumber return (s.id==nth+1) ? value : (s.id\>nth+1 ? Core.SlotNumber(s.id-1) : s) else …

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