# Question about constant propagation and inference

**URL:** <https://discourse.julialang.org/t/question-about-constant-propagation-and-inference/24889>\
**Category:** General Usage\
**Created:** [June 3, 2019, 1:00pm UTC](https://discourse.julialang.org/t/question-about-constant-propagation-and-inference/24889 "2019-06-03T13:00:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [June 3, 2019, 1:00pm UTC](https://discourse.julialang.org/t/question-about-constant-propagation-and-inference/24889/1 "2019-06-03T13:00:22Z")

</div>

I am puzzled by this

```julia
julia> test(flag) = flag ? 1.0 : 1
test (generic function with 2 methods)

julia> @code_warntype test(true)
Variables
  #self#::Core.Compiler.Const(test, false)
  flag::Bool

Body::Union{Float64, Int64}
1 ─ goto #4 if not flag
2 ─ return 1.0
3 ─ Core.Compiler.Const(:(goto %5), false)
4 ┄ false
└── return 1

```

Is `flag` being constant-propagated here? Why is the return type of this _particular call_ `test(true)` not inferred? Or is constant propagation orthogonal to inference?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 3, 2019, 1:02pm UTC](https://discourse.julialang.org/t/question-about-constant-propagation-and-inference/24889/2 "2019-06-03T13:02:08Z")

</div>

Wrap in a function:

```julia
julia> test(flag) = flag ? 1.0 : 1
test (generic function with 1 method)

julia> f() = test(true)
f (generic function with 1 method)

julia> @code_warntype f()
Variables
  #self#::Core.Compiler.Const(f, false)

Body::Float64
1 ─ %1 = Main.test(true)::Core.Compiler.Const(1.0, false)
└── return %1

```

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [June 3, 2019, 1:03pm UTC](https://discourse.julialang.org/t/question-about-constant-propagation-and-inference/24889/3 "2019-06-03T13:03:07Z")

</div>

Oh! Why is this? Isn’t `true` a compile-time constant here? (Still slightly puzzled…)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 3, 2019, 1:10pm UTC](https://discourse.julialang.org/t/question-about-constant-propagation-and-inference/24889/4 "2019-06-03T13:10:42Z")

</div>

See eg

> [@Inlining behavior: constant prop. fails, I need help in understanding "why"](https://discourse.julialang.org/t/inlining-behavior-constant-prop-fails-i-need-help-in-understanding-why/17134/4):
>
> This is how I usually benchmark constant propagation: julia\> function f(x) D = 1 reconstruct(x, D) end f (generic function with 1 method) julia\> @btime f($x) 871.847 ns (2 allocations: 112 bytes) 50.48725909006644

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 3, 2019, 1:14pm UTC](https://discourse.julialang.org/t/question-about-constant-propagation-and-inference/24889/5 "2019-06-03T13:14:58Z")

</div>

> [@pablosanjose](#):
>
> Oh! Why is this? Isn’t `true` a compile-time constant here? (Still slightly puzzled…)

```julia
@code_warntype test(true)

```

gets macro expanded to

```julia
code_warntype(test, Tuple{Bool})

```

where clearly, all information that the bool was a constant `true` has been lost.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [June 3, 2019, 1:18pm UTC](https://discourse.julialang.org/t/question-about-constant-propagation-and-inference/24889/6 "2019-06-03T13:18:03Z")

</div>

Aaaaah! Got it now… Many thanks to both!

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [June 3, 2019, 1:23pm UTC](https://discourse.julialang.org/t/question-about-constant-propagation-and-inference/24889/7 "2019-06-03T13:23:46Z")

</div>

If I may, just a quick followup: why doesn’t this work with kwargs?

```julia
julia> test(; flag = true) = flag ? 1.0 : 1
test (generic function with 1 method)

julia> f() = test(flag = true)
f (generic function with 1 method)

julia> @code_warntype f()
Variables
  #self#::Core.Compiler.Const(f, false)

Body::Union{Float64, Int64}
1 ─ %1 = (:flag,)::Core.Compiler.Const((:flag,), false)
│ %2 = Core.apply_type(Core.NamedTuple, %1)::Core.Compiler.Const(NamedTuple{(:flag,),T} where T<:Tuple, false)
│ %3 = Core.tuple(true)::Core.Compiler.Const((true,), false)
│ %4 = (%2)(%3)::NamedTuple{(:flag,),Tuple{Bool}}
│ %5 = Core.kwfunc(Main.test)::Core.Compiler.Const(getfield(Main, Symbol("#kw##test"))(), false)
│ %6 = (%5)(%4, Main.test)::Union{Float64, Int64}
└── return %6

```

Curiously, however

```julia
julia> f() = test()
f (generic function with 1 method)

julia> @code_warntype f()
Variables
  #self#::Core.Compiler.Const(f, false)

Body::Float64
1 ─ %1 = Main.test()::Core.Compiler.Const(1.0, false)
└── return %1

```
