# What does \`$(Expr(:static\_parameter, 1))\` do?

**URL:** <https://discourse.julialang.org/t/what-does-expr-static-parameter-1-do/79302>\
**Category:** Internals & Design\
**Created:** [April 10, 2022, 2:56pm UTC](https://discourse.julialang.org/t/what-does-expr-static-parameter-1-do/79302 "2022-04-10T14:56:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [April 10, 2022, 2:56pm UTC](https://discourse.julialang.org/t/what-does-expr-static-parameter-1-do/79302/1 "2022-04-10T14:56:59Z")

</div>

After I ran across this [here](https://github.com/dfdx/Umlaut.jl/issues/8), @dfdx pointed me to @Oscar_Smith 's suggestion of better documentation for this [here](https://github.com/JuliaLang/julia/issues/43074). But I still have no idea what this does, or how to handle it when working with ASTs. Can someone give some more detail please?

---

<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:** [April 10, 2022, 4:20pm UTC](https://discourse.julialang.org/t/what-does-expr-static-parameter-1-do/79302/2 "2022-04-10T16:20:14Z")

</div>

It’s not valid in the surface-level AST, only in unspecialized Julia IR, so you should never encounter it in macros. It refers to the static parameters of a method definition, i.e. the names on the right side of the `where`. You can see that in the following example all references to `S` were replaced by `Expr(:static_parameter, 1)` and similarly for `T`:

```julia
julia> f(x::S, y::T) where {S, T} = S, T
f (generic function with 1 method)

julia> @code_lowered f(1, 2)
CodeInfo(
1 ─ %1 = Core.tuple($(Expr(:static_parameter, 1)), $(Expr(:static_parameter, 2)))
└── return %1
)

```

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [April 10, 2022, 8:47pm UTC](https://discourse.julialang.org/t/what-does-expr-static-parameter-1-do/79302/3 "2022-04-10T20:47:23Z")

</div>

Thanks @simeonschaub . This seems tricky, because the semantics depend on values outside the expression (the types corresponding to static parameters), and those values are never explicitly assigned in the lowered code. So in your example,

```julia
CodeInfo(
1 ─ %1 = Core.tuple($(Expr(:static_parameter, 1)), $(Expr(:static_parameter, 2)))
└── return %1
)

```

All we have is “static parameters of the method” but no information about what that method is. Is that right? So in general, lowered code isn’t enough to run things, we need information about the call stack at the AST level?

What does the compiler gain by not assigning the static parameters… statically? I mean, I would have expected something more like

```julia
CodeInfo(
1 ─ %1 = Core.Int64
│ %2 = Core.Int64
│ %3 = Core.tuple(%1, %2)
└── return %3
)

```

I guess more to the point, what are best practices for working with static parameters?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 10, 2022, 8:55pm UTC](https://discourse.julialang.org/t/what-does-expr-static-parameter-1-do/79302/4 "2022-04-10T20:55:14Z")

</div>

I’d assume you would have an problem with overspecialization here?

---

<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:** [April 10, 2022, 9:00pm UTC](https://discourse.julialang.org/t/what-does-expr-static-parameter-1-do/79302/5 "2022-04-10T21:00:31Z")

</div>

> [@cscherrer](#):
>
> All we have is “static parameters of the method” but no information about what that method is. Is that right? So in general, lowered code isn’t enough to run things, we need information about the call stack at the AST level?

Yes. Not really at the AST-level or the entire call stack, but you need to know about the method specialization.

> [@cscherrer](#):
>
> What does the compiler gain by not assigning the static parameters… statically? I mean, I would have expected something more like

That’s exactly what the optimizer will eventually do (if it’s specializing on concrete types):

```julia
julia> @code_typed f(1, 2)
CodeInfo(
1 ─ return (Int64, Int64)
) => Tuple{DataType, DataType}

```

That’s not what `code_lowered` represents though, lowering is purely based on the original AST and doesn’t reason about types.

---

<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:** [April 10, 2022, 9:05pm UTC](https://discourse.julialang.org/t/what-does-expr-static-parameter-1-do/79302/6 "2022-04-10T21:05:15Z")

</div>

> [@cscherrer](#):
>
> I guess more to the point, what are best practices for working with static parameters?

It entirely depends on what you’re doing. Tools like Cassette typically call [`Meta.partially_inline!`](https://github.com/JuliaLang/julia/blob/992b26194d93be4867a817da8170e4040c49e9a3/base/meta.jl#L288) to prepare lowered code for rewriting and emitting from a generated function, but there’s a lot more additional complexity that comes with that, so I’d recommend just using those packages if that’s what you want to do.

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [April 10, 2022, 9:56pm UTC](https://discourse.julialang.org/t/what-does-expr-static-parameter-1-do/79302/7 "2022-04-10T21:56:13Z")

</div>

> [@goerch](#):
>
> I’d assume you would have an problem with overspecialization here?

Right, as @simeonschaub points out, I was confusing _lowered_ vs _typed_ code.

> [@simeonschaub](#):
>
> Tools like Cassette typically call [`Meta.partially_inline!`](https://github.com/JuliaLang/julia/blob/992b26194d93be4867a817da8170e4040c49e9a3/base/meta.jl#L288)

That’s interesting, thanks. That function also has a really nice docstring, so that’s also helpful.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [April 10, 2022, 10:11pm UTC](https://discourse.julialang.org/t/what-does-expr-static-parameter-1-do/79302/8 "2022-04-10T22:11:48Z")

</div>

Can we safely assume that `Expr(:static_parameter, n)` always refers to the parameters of the function being currently analyzed? At least in Umlaut I actually have access to the function and its arguments, so perhaps I can get the values of the static parameters with something like this:

```julia
julia> f(::Val{N}) where N = N + 1

julia> @code_lowered f(Val(3))
CodeInfo(
1 ─ %1 = $(Expr(:static_parameter, 1)) + 1
└── return %1
)

julia> f(Val(4))
5

julia> mi = Base.method_instances(f, (4,))[1]
MethodInstance for f(::4)

julia> mi.sparam_vals[1]
4

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 10, 2022, 10:19pm UTC](https://discourse.julialang.org/t/what-does-expr-static-parameter-1-do/79302/9 "2022-04-10T22:19:27Z")

</div>

> [@dfdx](#):
>
> Can we safely assume that `Expr(:static_parameter, n)` always refers to the parameters of the function being currently analyzed?

AFAIU, yes.
