# How to overdub static parameters in overdubbing

**URL:** <https://discourse.julialang.org/t/how-to-overdub-static-parameters-in-overdubbing/71993>\
**Category:** Internals & Design\
**Created:** [November 24, 2021, 5:10am UTC](https://discourse.julialang.org/t/how-to-overdub-static-parameters-in-overdubbing/71993 "2021-11-24T05:10:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [November 24, 2021, 5:10am UTC](https://discourse.julialang.org/t/how-to-overdub-static-parameters-in-overdubbing/71993/1 "2021-11-24T05:10:20Z")

</div>

Hello,

This question has stemmed from the work described in [Does overdubbing in generated function inserts inlined code](https://discourse.julialang.org/t/does-overdubbing-in-generated-function-inserts-inlined-code/71868) , where I would like to understand the code rewriting of CodeInstances. I decided to try to do everything manually without IRTools / Cassete to know, on which internals they are built on.

Currently, I am stucked on the following problem. Consider following function

```julia
function test(x::T) where T<:Union{Float64, Float32}
    x < T(pi)
end

```

which test, if some variable is smaller then `pi` (I have encountered this when overdubbing `Main.sin`). When I retrieve the code using

> **retrieve\_code\_info**
>
> ```julia
> function retrieve_code_info(sigtypes, world = Base.get_world_counter())
> S = Tuple{map(s -> Core.Compiler.has_free_typevars(s) ? typeof(s.parameters[1]) : s, sigtypes)...}
> _methods = Base._methods_by_ftype(S, -1, world)
> if isempty(_methods) 
> @info("method $(sigtypes) does not exist")
> return(nothing)
> end
> type_signature, raw_static_params, method = _methods[1]
> method_instance = Core.Compiler.specialize_method(method, type_signature, raw_static_params, false)
> code_info = Core.Compiler.retrieve_code_info(method_instance)
> end
> 
> ```

the code looks like this

```julia
CodeInfo(
    @ REPL[33]:2 within `test'
1 ─ %1 = ($(Expr(:static_parameter, 1)))(Main.pi)
│ %2 = x < %1
└── return %2
)

```

from which I conclude that `T(pi)` is translated to ` ($(Expr(:static_parameter, 1)))(Main.pi)` and the `Expr(:static_parameter, 1)` is an accessor to the first Type parameter `T`.  
Now comes the problem.  
When I am overdubbing, I do substitute function signature `test(x)` with a generated function `overdub(f args...)` which takes code info for `test(x)` and manipulates it (and it does the same for any other function) does not have any type parameters. Hence, I do not have access to `T` in a generated function `overdub(typeof(test), args...)`. My question is, what is the general strategy to translate such line, for example in `Zygote` to make it compatible with a signature of `overdub(f, args...)? The only strategy I can imagine at the moment is to remove the `$(Expr(:static\_parameter, 1))`from the type definition, yet I am afraid that doing so will make the code type-unstable. I have search IRTools, Zygote and Cassette for`static\_parameters` but without luck.  
Thanks for help and clarifications.

Tomas

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [November 24, 2021, 5:35am UTC](https://discourse.julialang.org/t/how-to-overdub-static-parameters-in-overdubbing/71993/2 "2021-11-24T05:35:36Z")

</div>

Zygote and IRTools call [`Meta.partially_inline!`](https://github.com/JuliaLang/julia/blob/35f0c911f443ae0d3933e1bce4321afd0f345aed/base/meta.jl#L305) to inline the static parameters from the method match directly into the IR.

I’ll highly recommend just using Cassette directly though, since all these tricky details can be quite annoying to handle correctly.

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [November 24, 2021, 6:15am UTC](https://discourse.julialang.org/t/how-to-overdub-static-parameters-in-overdubbing/71993/3 "2021-11-24T06:15:22Z")

</div>

I see. Thanks.  
I know that I should just use directly one of these libraries but as I said, I would like to get at least a partial understanding what is going on. It is for educational purposes for a course on Julia on Czech Technical University ([https://github.com/JuliaTeachingCTU/Scientific-Programming-in-Julia](https://github.com/JuliaTeachingCTU/Scientific-Programming-in-Julia)).  
I am now playing with IRTools and of course, it makes life much simpler, as expected…

I appreciate your help.  
Best wishes,  
Tomas
