Issue 1:
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!).

I was trying to optimize a toolbox I was coding and I had a very strange type warnings using @code_warntype. I posted the problem on discourse to verify if I was doing something wrong [1] with global declaration of constants.

It turns out that @tkoolen and @StefanKarpinski identified a bigger problem (that I cannot really describe in technical detail) and asked to open an issue.

Consider the following code:

VERSION >= v"0.4.0-dev+6521" && __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

If I analyze the type using @code_warntype test(1,1), then I get:

Variables:
#self# <optimized out>
n <optimized out>
e <optimized out>
K1 <optimized out>
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}

However, consider the following code in which a check of the variable n is added:

VERSION >= v"0.4.0-dev+6521" && __precompile__()

module TestConst

export test

function test(n::Number, e::Number)
if (n <= 0)
throw(ArgumentError("The angular velocity must be greater than 0."))
end

# Auxiliary constant to compute the functions.
K1 = 3.0

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

end

In this case, I see problems about the type:

Variables:
#self# <optimized out>
n::Int64
e <optimized out>
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

I performed a simple benchmark on both version using:

@time for k=1:1000000000
test(1,1)
end

The result was 0.93s for the first algorithm and 18s for the second one.

References

[1] https://discourse.julialang.org/t/correct-way-to-declare-global-constant-in-modules/10234/6

Issue 2:
Hi,

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 using CUDA

julia> 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<:(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<: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