<p dir="auto">First of all, sorry about the title! I can't really summarize the problem in a good way (if anyone can change it to a better one, then, please, be my guest!).</p>
<p dir="auto">I was trying to optimize a toolbox I was coding and I had a very strange type warnings using <code class="notranslate">@code_warntype</code>. I posted the problem on discourse to verify if I was doing something wrong [1] with global declaration of constants.</p>
<p dir="auto">It turns out that <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/tkoolen/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/tkoolen">@tkoolen</a> and <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/StefanKarpinski/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/StefanKarpinski">@StefanKarpinski</a> identified a bigger problem (that I cannot really describe in technical detail) and asked to open an issue.</p>
<p dir="auto">Consider the following code:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="VERSION &gt;= v&quot;0.4.0-dev+6521&quot; &amp;&amp; __precompile__()

module TestConst

export test

function test(n::Number, e::Number)
    # Auxiliary constant to compute the functions.
    K1 = 3.0

    # Declare the functions that must solved for 0.
    f1(a, i) = K1
end

end"><pre class="notranslate"><span class="pl-c1">VERSION</span> <span class="pl-k">&gt;=</span> <span class="pl-s"><span class="pl-pds"><span class="pl-c1">v</span>"</span>0.4.0-dev+6521<span class="pl-pds">"</span></span> <span class="pl-k">&amp;&amp;</span> <span class="pl-c1">__precompile__</span>()

<span class="pl-k">module</span> TestConst

<span class="pl-k">export</span> test

<span class="pl-k">function</span> <span class="pl-en">test</span>(n<span class="pl-k">::</span><span class="pl-c1">Number</span>, e<span class="pl-k">::</span><span class="pl-c1">Number</span>)
    <span class="pl-c"><span class="pl-c">#</span> Auxiliary constant to compute the functions.</span>
    K1 <span class="pl-k">=</span> <span class="pl-c1">3.0</span>

    <span class="pl-c"><span class="pl-c">#</span> Declare the functions that must solved for 0.</span>
    <span class="pl-en">f1</span>(a, i) <span class="pl-k">=</span> K1
<span class="pl-k">end</span>

<span class="pl-k">end</span></pre></div>
<p dir="auto">If I analyze the type using <code class="notranslate">@code_warntype test(1,1)</code>, then I get:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Variables:
  #self# &lt;optimized out&gt;
  n &lt;optimized out&gt;
  e &lt;optimized out&gt;
  K1 &lt;optimized out&gt;
  f1::TestConst.#f1#1{Float64}

Body:
  begin  # line 12:
      f1::TestConst.#f1#1{Float64} = $(Expr(:new, TestConst.#f1#1{Float64}, 3.0))
      return f1::TestConst.#f1#1{Float64}
  end::TestConst.#f1#1{Float64}"><pre class="notranslate"><code class="notranslate">Variables:
  #self# &lt;optimized out&gt;
  n &lt;optimized out&gt;
  e &lt;optimized out&gt;
  K1 &lt;optimized out&gt;
  f1::TestConst.#f1#1{Float64}

Body:
  begin  # line 12:
      f1::TestConst.#f1#1{Float64} = $(Expr(:new, TestConst.#f1#1{Float64}, 3.0))
      return f1::TestConst.#f1#1{Float64}
  end::TestConst.#f1#1{Float64}
</code></pre></div>
<p dir="auto">However, consider the following code in which a check of the variable <code class="notranslate">n</code> is added:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="VERSION &gt;= v&quot;0.4.0-dev+6521&quot; &amp;&amp; __precompile__()

module TestConst

export test

function test(n::Number, e::Number)
    if (n &lt;= 0)
        throw(ArgumentError(&quot;The angular velocity must be greater than 0.&quot;))
    end

    # Auxiliary constant to compute the functions.
    K1 = 3.0

    # Declare the functions that must solved for 0.
    f1(a, i) = K1
end

end"><pre class="notranslate"><span class="pl-c1">VERSION</span> <span class="pl-k">&gt;=</span> <span class="pl-s"><span class="pl-pds"><span class="pl-c1">v</span>"</span>0.4.0-dev+6521<span class="pl-pds">"</span></span> <span class="pl-k">&amp;&amp;</span> <span class="pl-c1">__precompile__</span>()

<span class="pl-k">module</span> TestConst

<span class="pl-k">export</span> test

<span class="pl-k">function</span> <span class="pl-en">test</span>(n<span class="pl-k">::</span><span class="pl-c1">Number</span>, e<span class="pl-k">::</span><span class="pl-c1">Number</span>)
    <span class="pl-k">if</span> (n <span class="pl-k">&lt;=</span> <span class="pl-c1">0</span>)
        <span class="pl-c1">throw</span>(<span class="pl-c1">ArgumentError</span>(<span class="pl-s"><span class="pl-pds">"</span>The angular velocity must be greater than 0.<span class="pl-pds">"</span></span>))
    <span class="pl-k">end</span>

    <span class="pl-c"><span class="pl-c">#</span> Auxiliary constant to compute the functions.</span>
    K1 <span class="pl-k">=</span> <span class="pl-c1">3.0</span>

    <span class="pl-c"><span class="pl-c">#</span> Declare the functions that must solved for 0.</span>
    <span class="pl-en">f1</span>(a, i) <span class="pl-k">=</span> K1
<span class="pl-k">end</span>

<span class="pl-k">end</span></pre></div>
<p dir="auto">In this case, I see problems about the type:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Variables:
  #self# &lt;optimized out&gt;
  n::Int64
  e &lt;optimized out&gt;
  K1::Core.Box
  f1::TestConst.#f1#1

Body:
  begin 
      K1::Core.Box = $(Expr(:new, :(Core.Box)))
      NewvarNode(:(f1::TestConst.#f1#1))
      unless (Base.sle_int)(n::Int64, 0)::Bool goto 6 # line 9:
      (TestConst.throw)($(Expr(:new, :(Base.ArgumentError), &quot;The angular velocity must be greater than 0.&quot;)))::Union{}
      6:  # line 13:
      (Core.setfield!)(K1::Core.Box, :contents, 3.0)::Float64 # line 16:
      f1::TestConst.#f1#1 = $(Expr(:new, :(TestConst.#f1#1), :(K1)))
      return f1::TestConst.#f1#1
  end::TestConst.#f1#1"><pre class="notranslate"><code class="notranslate">Variables:
  #self# &lt;optimized out&gt;
  n::Int64
  e &lt;optimized out&gt;
  K1::Core.Box
  f1::TestConst.#f1#1

Body:
  begin 
      K1::Core.Box = $(Expr(:new, :(Core.Box)))
      NewvarNode(:(f1::TestConst.#f1#1))
      unless (Base.sle_int)(n::Int64, 0)::Bool goto 6 # line 9:
      (TestConst.throw)($(Expr(:new, :(Base.ArgumentError), "The angular velocity must be greater than 0.")))::Union{}
      6:  # line 13:
      (Core.setfield!)(K1::Core.Box, :contents, 3.0)::Float64 # line 16:
      f1::TestConst.#f1#1 = $(Expr(:new, :(TestConst.#f1#1), :(K1)))
      return f1::TestConst.#f1#1
  end::TestConst.#f1#1
</code></pre></div>
<p dir="auto">I performed a simple benchmark on both version using:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@time for k=1:1000000000
    test(1,1)
end"><pre class="notranslate"><span class="pl-c1">@time</span> <span class="pl-k">for</span> k<span class="pl-k">=</span><span class="pl-c1">1</span><span class="pl-k">:</span><span class="pl-c1">1000000000</span>
    <span class="pl-c1">test</span>(<span class="pl-c1">1</span>,<span class="pl-c1">1</span>)
<span class="pl-k">end</span></pre></div>
<p dir="auto">The result was <code class="notranslate">0.93s</code> for the first algorithm and <code class="notranslate">18s</code> for the second one.</p>
<hr>
<h1 dir="auto">References</h1>
<p dir="auto">[1] <a href="https://discourse.julialang.org/t/correct-way-to-declare-global-constant-in-modules/10234/6" rel="nofollow">https://discourse.julialang.org/t/correct-way-to-declare-global-constant-in-modules/10234/6</a></p>
<p dir="auto">Hi,<br>
I'm getting the error below from a fresh installation of julia-1.7.0beta2 on powerpc. ChainRulesCore loads fine, the error only occurs with <code class="notranslate">using CUDA</code></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; using CUDA
[ Info: Precompiling CUDA [052768ef-5323-5732-b1bb-66c8b64840ba]
WARNING: Method definition (::ChainRulesCore.Thunk{F})() in module ChainRulesCore at /home/rkube/.julia/packages/ChainRulesCore/EgLlm/src/deprecated.jl:52 overwritten on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::ChainRulesCore.InplaceableThunk{T&lt;:(ChainRulesCore.Thunk{F} where F), F})() in module ChainRulesCore at /home/rkube/.julia/packages/ChainRulesCore/EgLlm/src/deprecated.jl:52 overwritten on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

ERROR: LoadError: TypeError: in Atomic, in T, expected T&lt;:Union{Bool, Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8}, got Type{Float64}
Stacktrace:
  [1] top-level scope
    @ ~/.julia/packages/CUDA/Ozu5O/src/pool.jl:450
  [2] include(mod::Module, _path::String)
    @ Base ./Base.jl:417
  [3] include(x::String)
    @ CUDA ~/.julia/packages/CUDA/Ozu5O/src/CUDA.jl:1
  [4] top-level scope
    @ ~/.julia/packages/CUDA/Ozu5O/src/CUDA.jl:67
  [5] include
    @ ./Base.jl:417 [inlined]
  [6] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt64}}, source::Nothing)
    @ Base ./loading.jl:1318
  [7] top-level scope
    @ none:1
  [8] eval
    @ ./boot.jl:373 [inlined]
  [9] eval(x::Expr)
    @ Base.MainInclude ./client.jl:453
 [10] top-level scope
    @ none:1
in expression starting at /home/rkube/.julia/packages/CUDA/Ozu5O/src/pool.jl:450
in expression starting at /home/rkube/.julia/packages/CUDA/Ozu5O/src/CUDA.jl:1
ERROR: Failed to precompile CUDA [052768ef-5323-5732-b1bb-66c8b64840ba] to /home/rkube/.julia/compiled/v1.7/CUDA/jl_TuxLx5.
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] compilecache(pkg::Base.PkgId, path::String, internal_stderr::IO, internal_stdout::IO, ignore_loaded_modules::Bool)
   @ Base ./loading.jl:1466
 [3] compilecache(pkg::Base.PkgId, path::String)
   @ Base ./loading.jl:1410
 [4] _require(pkg::Base.PkgId)
   @ Base ./loading.jl:1120
 [5] require(uuidkey::Base.PkgId)
   @ Base ./loading.jl:1013
 [6] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:997
"><pre class="notranslate"><code class="notranslate">julia&gt; using CUDA
[ Info: Precompiling CUDA [052768ef-5323-5732-b1bb-66c8b64840ba]
WARNING: Method definition (::ChainRulesCore.Thunk{F})() in module ChainRulesCore at /home/rkube/.julia/packages/ChainRulesCore/EgLlm/src/deprecated.jl:52 overwritten on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

WARNING: Method definition (::ChainRulesCore.InplaceableThunk{T&lt;:(ChainRulesCore.Thunk{F} where F), F})() in module ChainRulesCore at /home/rkube/.julia/packages/ChainRulesCore/EgLlm/src/deprecated.jl:52 overwritten on the same line (check for duplicate calls to `include`).
  ** incremental compilation may be fatally broken for this module **

ERROR: LoadError: TypeError: in Atomic, in T, expected T&lt;:Union{Bool, Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8}, got Type{Float64}
Stacktrace:
  [1] top-level scope
    @ ~/.julia/packages/CUDA/Ozu5O/src/pool.jl:450
  [2] include(mod::Module, _path::String)
    @ Base ./Base.jl:417
  [3] include(x::String)
    @ CUDA ~/.julia/packages/CUDA/Ozu5O/src/CUDA.jl:1
  [4] top-level scope
    @ ~/.julia/packages/CUDA/Ozu5O/src/CUDA.jl:67
  [5] include
    @ ./Base.jl:417 [inlined]
  [6] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt64}}, source::Nothing)
    @ Base ./loading.jl:1318
  [7] top-level scope
    @ none:1
  [8] eval
    @ ./boot.jl:373 [inlined]
  [9] eval(x::Expr)
    @ Base.MainInclude ./client.jl:453
 [10] top-level scope
    @ none:1
in expression starting at /home/rkube/.julia/packages/CUDA/Ozu5O/src/pool.jl:450
in expression starting at /home/rkube/.julia/packages/CUDA/Ozu5O/src/CUDA.jl:1
ERROR: Failed to precompile CUDA [052768ef-5323-5732-b1bb-66c8b64840ba] to /home/rkube/.julia/compiled/v1.7/CUDA/jl_TuxLx5.
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] compilecache(pkg::Base.PkgId, path::String, internal_stderr::IO, internal_stdout::IO, ignore_loaded_modules::Bool)
   @ Base ./loading.jl:1466
 [3] compilecache(pkg::Base.PkgId, path::String)
   @ Base ./loading.jl:1410
 [4] _require(pkg::Base.PkgId)
   @ Base ./loading.jl:1120
 [5] require(uuidkey::Base.PkgId)
   @ Base ./loading.jl:1013
 [6] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:997

</code></pre></div>