# Transform CodeInfo of a function to get a new function by purging unused args from its arg list

**URL:** <https://discourse.julialang.org/t/transform-codeinfo-of-a-function-to-get-a-new-function-by-purging-unused-args-from-its-arg-list/87459>\
**Category:** General Usage\
**Tags:** question, ir\
**Created:** [September 19, 2022, 7:43am UTC](https://discourse.julialang.org/t/transform-codeinfo-of-a-function-to-get-a-new-function-by-purging-unused-args-from-its-arg-list/87459 "2022-09-19T07:43:36Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [September 19, 2022, 10:31am UTC](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 "2022-09-19T10:31:13Z")

</div>

This code should work.

```julia

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
            return s
        end
    end
    for m in methods(src).ms
        # to CodeInfo
        ci = uncompressed_ast(m)
        # transform CodeInfo
        for ex in ci.code
            if ex isa Expr
                map!(change_slotnumber_after_nth, ex.args, ex.args)
            end
        end
        # purge nth argument from slots
        ci.slotnames = ci.slotnames[[i for i = 1:length(ci.slotnames) if i!=nth+1]]
        ci.slotflags = ci.slotflags[[i for i = 1:length(ci.slotflags) if i!=nth+1]]
        # purge nth argument from m.sig
        sig1 = Tuple{[m.sig.parameters[i] for i=1:length(m.sig.parameters) if i!=nth+1]...}
        # inject CodeInfo to mod = getmodule(dst)
        jl_method_def(argdata(sig1, dst), ci, mod)
    end
end

```

After debuging with `@code_lowered` and `@code_typed` I just realized that I forgot to decrease the id of each slot number after the n’th one (which is assigned and then purged). Now the updated version of `assign_purge_argument!` works (at least for all test cases I have at hand).

---

_[View the full topic](https://discourse.julialang.org/t/transform-codeinfo-of-a-function-to-get-a-new-function-by-purging-unused-args-from-its-arg-list/87459)._
