# \#constant-propagation

**URL:** https://discourse.julialang.org/tag/constant-propagation/367.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Why does \`@inbounds\` disable constant propagation?](https://discourse.julialang.org/t/why-does-inbounds-disable-constant-propagation/105789)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 25\
**Last updated:** [January 20, 2025, 12:00am UTC](https://discourse.julialang.org/t/why-does-inbounds-disable-constant-propagation/105789 "2025-01-20T00:00:58Z")

</div>

The Julia manual says: An explicit @inbounds annotation inside the function will also disable constant folding and not be overridden by :foldable. Some other references of this fact: Issue #48245 says: turning on -…

---

## [Unit test for constant folding success](https://discourse.julialang.org/t/unit-test-for-constant-folding-success/124625)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 6\
**Last updated:** [January 10, 2025, 11:02am UTC](https://discourse.julialang.org/t/unit-test-for-constant-folding-success/124625 "2025-01-10T11:02:52Z")

</div>

Here’s a proposal for how to test for successful constant folding: using Test function test\_constant\_folding(f, r) vec = code\_typed(f, Tuple{}) p = only(vec) code\_info = first(p) code = try code…

---

## [Can \`parse\` be computed at compile time?](https://discourse.julialang.org/t/can-parse-be-computed-at-compile-time/119507)

<div class="topic-metadata">

**Author:** [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Replies:** 4\
**Last updated:** [September 17, 2024, 9:21pm UTC](https://discourse.julialang.org/t/can-parse-be-computed-at-compile-time/119507 "2024-09-17T21:21:13Z")

</div>

The function f() = parse(Int, "10") produces native code, according to @code\_native, which involves runtime parsing rather than directly returning the integer 10. Why can’t Julia optimize this away at compile time? Is…

---

## [Difference between \`Base.@assume\_effects :foldable\` and \`Base.@constprop :aggressive\`](https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 7\
**Last updated:** [August 31, 2024, 5:21pm UTC](https://discourse.julialang.org/t/difference-between-base-assume-effects-foldable-and-base-constprop-aggressive/118846 "2024-08-31T17:21:17Z")

</div>

What’s the difference between Base.@assume\_effects :foldable and Base.@constprop :aggressive?

---

## [How does constant propagation impact latency with a Val-based dispatch?](https://discourse.julialang.org/t/how-does-constant-propagation-impact-latency-with-a-val-based-dispatch/114545)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 0\
**Last updated:** [May 22, 2024, 5:49am UTC](https://discourse.julialang.org/t/how-does-constant-propagation-impact-latency-with-a-val-based-dispatch/114545 "2024-05-22T05:49:57Z")

</div>

The example here is schematic, and isn’t really an MWE. Suppose I have a set of functions like f() = f('A') Base.@constprop :aggressive f(a, args...) = g(Val(a == 'A'), args...) Base.@constprop :aggressive g(::Val{true…

---

## [Compiler matmul optimization prevents constant folding?](https://discourse.julialang.org/t/compiler-matmul-optimization-prevents-constant-folding/107121)

<div class="topic-metadata">

**Author:** [@bremez](https://discourse.julialang.org/u/bremez)\
**Replies:** 18\
**Last updated:** [December 4, 2023, 5:50pm UTC](https://discourse.julialang.org/t/compiler-matmul-optimization-prevents-constant-folding/107121 "2023-12-04T17:50:29Z")

</div>

I have a use case where I iterate over a container of small vectors. Say vs is a Vector{SVector{2, Float64}}, M is a global constant SMatrix{2,2,Float64,4}, and an illustrative use case is function f(vs) total = zero(el…

---

## [Detect if constant](https://discourse.julialang.org/t/detect-if-constant/100246)

<div class="topic-metadata">

**Author:** [@user664303](https://discourse.julialang.org/u/user664303)\
**Replies:** 49\
**Last updated:** [June 16, 2023, 8:12pm UTC](https://discourse.julialang.org/t/detect-if-constant/100246 "2023-06-16T20:12:17Z")

</div>

Is there a way to test or detect whether a value (let’s say an Int) is constant (i.e. known to the compiler) at runtime? This has been asked before, but not often and not recently. I’m just wondering if this is now possi…

---

## [Why don't operations on strings constant-fold?](https://discourse.julialang.org/t/why-dont-operations-on-strings-constant-fold/94597)

<div class="topic-metadata">

**Author:** [@uniment](https://discourse.julialang.org/u/uniment)\
**Replies:** 9\
**Last updated:** [February 14, 2023, 11:22pm UTC](https://discourse.julialang.org/t/why-dont-operations-on-strings-constant-fold/94597 "2023-02-14T23:22:23Z")

</div>

Why doesn’t bar constant-fold, although foo does? julia\> using BenchmarkTools julia\> foo(::Type{T}) where T = parse(T, '3') bar(::Type{T}) where T = parse(T, "3"); julia\> @btime foo(Int) @btime bar(Int);…

---

## [Constant propagation and inference of constructed types](https://discourse.julialang.org/t/constant-propagation-and-inference-of-constructed-types/36279)

<div class="topic-metadata">

**Author:** [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Replies:** 14\
**Last updated:** [February 7, 2023, 10:49pm UTC](https://discourse.julialang.org/t/constant-propagation-and-inference-of-constructed-types/36279 "2023-02-07T22:49:01Z")

</div>

Context: I had a parameterized struct type with convenience constructors that input some of the parameters as arguments. Based on previous experience, I expected that if such constructors are provided with constant valu…

---

## [Why aren't expressions such as \`isinteger\` simplified in case that the result is inferrable from the arguments?](https://discourse.julialang.org/t/why-arent-expressions-such-as-isinteger-simplified-in-case-that-the-result-is-inferrable-from-the-arguments/91297)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 8\
**Last updated:** [December 11, 2022, 7:59pm UTC](https://discourse.julialang.org/t/why-arent-expressions-such-as-isinteger-simplified-in-case-that-the-result-is-inferrable-from-the-arguments/91297 "2022-12-11T19:59:48Z")

</div>

julia\> @code\_typed (n -\> isinteger(n+0.5))(2) CodeInfo( 1 ─ %1 = Base.sitofp(Float64, n)::Float64 │ %2 = Base.add\_float(%1, 0.5)::Float64 │ %3 = Base.trunc\_llvm(%2)::Float64 │ %4 = Base.sub\_float(%2, %3)::Float64 │…

---

## [Is \`Base.@constprop\` strictly for internal usage?](https://discourse.julialang.org/t/is-base-constprop-strictly-for-internal-usage/89273)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 5\
**Last updated:** [October 26, 2022, 1:43pm UTC](https://discourse.julialang.org/t/is-base-constprop-strictly-for-internal-usage/89273 "2022-10-26T13:43:34Z")

</div>

Is it likely to become a part of the public API in the future? Or is this strictly for internal use, and package developers are discouraged from using it? This often helps with type inference, so it would be great to ha…

---

## [How does the compiler know not to constant-fold methods like rand?](https://discourse.julialang.org/t/how-does-the-compiler-know-not-to-constant-fold-methods-like-rand/84092)

<div class="topic-metadata">

**Author:** [@Benny](https://discourse.julialang.org/u/Benny)\
**Replies:** 2\
**Last updated:** [July 12, 2022, 10:15am UTC](https://discourse.julialang.org/t/how-does-the-compiler-know-not-to-constant-fold-methods-like-rand/84092 "2022-07-12T10:15:53Z")

</div>

It seems like for the most part, the compiler tries very hard to compute constant expressions at compile-time, though I’ve seen it give up on loops. But there are situations where it sensibly doesn’t even try, like rand(…

---

## [Why is the kwarg \`dims\` not being constant-propagated?](https://discourse.julialang.org/t/why-is-the-kwarg-dims-not-being-constant-propagated/83610)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 5\
**Last updated:** [July 5, 2022, 3:03am UTC](https://discourse.julialang.org/t/why-is-the-kwarg-dims-not-being-constant-propagated/83610 "2022-07-05T03:03:13Z")

</div>

This doesn’t seem to be inferred julia\> a = rand(1,1); julia\> @code\_warntype (a -\> eachslice(a, dims=1))(a) MethodInstance for (::var"#13#14")(::Matrix{Float64}) from (::var"#13#14")(a) @ Main REPL\[23\]:1 Argume…

---

## [Constant propagation of kwargs possible?](https://discourse.julialang.org/t/constant-propagation-of-kwargs-possible/27810)

<div class="topic-metadata">

**Author:** [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Replies:** 3\
**Last updated:** [August 22, 2019, 2:41am UTC](https://discourse.julialang.org/t/constant-propagation-of-kwargs-possible/27810 "2019-08-22T02:41:46Z")

</div>

I suspect the answer is ‘no’, but is current Julia able to constant-propagate kwargs? In other words, is it possible to make the following infer in some way while preserving the kwarg? Julia 1.2 julia\> h(; n) = ntuple(…
