From 9aebb4cdd3eee2f13c79ae0daa7bc78b2567c82f Mon Sep 17 00:00:00 2001 From: Sukera Date: Wed, 6 Dec 2023 18:39:05 +0100 Subject: [PATCH 1/5] Allow type hashing to be concretely evaluated --- base/hashing.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/base/hashing.jl b/base/hashing.jl index 5dbae09123bd6..fbc224be899b0 100644 --- a/base/hashing.jl +++ b/base/hashing.jl @@ -29,7 +29,10 @@ See also: [`objectid`](@ref), [`Dict`](@ref), [`Set`](@ref). """ hash(x::Any) = hash(x, zero(UInt)) hash(w::WeakRef, h::UInt) = hash(w.value, h) -hash(T::Type, h::UInt) = hash_uint(3h - ccall(:jl_type_hash, UInt, (Any,), T)) + +# Types can't be deleted, so marking as total allows the compiler to look up the hash +@assume_effects :total _typehash(T::Type) = ccall(:jl_type_hash, UInt, (Any,), T) +hash(T::Type, h::UInt) = hash_uint(3h - _typehash(T)) ## hashing general objects ## From 0d4b0261d0a331828229d14e0a0c3d19b035e2d9 Mon Sep 17 00:00:00 2001 From: Sukera Date: Wed, 6 Dec 2023 18:44:14 +0100 Subject: [PATCH 2/5] Add effects test for type hashing --- test/hashing.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/hashing.jl b/test/hashing.jl index 7c3024640aa32..0e6833c6dbf4b 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -302,3 +302,5 @@ struct AUnionParam{T<:Union{Nothing,Float32,Float64}} end # test hashing of rational with odd denominator @test hash(5//3) == hash(big(5)//3) end + +@test Base.infer_effects(hash, Tuple{Type{Int}, UInt}) == Core.Compiler.EFFECTS_TOTAL From b3c731b1f33fd8e34693d2efbe8e32386da58b41 Mon Sep 17 00:00:00 2001 From: Sukera Date: Thu, 7 Dec 2023 09:10:48 +0100 Subject: [PATCH 3/5] Fix effects testing per code review --- test/hashing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/hashing.jl b/test/hashing.jl index 0e6833c6dbf4b..b0baba71c930f 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -303,4 +303,4 @@ struct AUnionParam{T<:Union{Nothing,Float32,Float64}} end @test hash(5//3) == hash(big(5)//3) end -@test Base.infer_effects(hash, Tuple{Type{Int}, UInt}) == Core.Compiler.EFFECTS_TOTAL +@test Core.Compiler.is_foldable_nothrow(hash, Tuple{Type{Int}, UInt}) From 5be5ed29f901c9db3bc51100ddc001c90050772e Mon Sep 17 00:00:00 2001 From: Sukera <11753998+Seelengrab@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:19:42 +0100 Subject: [PATCH 4/5] Update test/hashing.jl Co-authored-by: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> --- test/hashing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/hashing.jl b/test/hashing.jl index b0baba71c930f..173a31d10a6a9 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -303,4 +303,4 @@ struct AUnionParam{T<:Union{Nothing,Float32,Float64}} end @test hash(5//3) == hash(big(5)//3) end -@test Core.Compiler.is_foldable_nothrow(hash, Tuple{Type{Int}, UInt}) +@test Core.Compiler.is_foldable_nothrow(Base.infer_effects(hash, Tuple{Type{Int}, UInt})) From 77bfb466b8b198f214540c6851bd66c5d12406fd Mon Sep 17 00:00:00 2001 From: Sukera <11753998+Seelengrab@users.noreply.github.com> Date: Thu, 7 Dec 2023 10:23:21 +0100 Subject: [PATCH 5/5] Use callsite annotation instead of outlined function Co-authored-by: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> --- base/hashing.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/base/hashing.jl b/base/hashing.jl index fbc224be899b0..7de9f47de3182 100644 --- a/base/hashing.jl +++ b/base/hashing.jl @@ -31,8 +31,7 @@ hash(x::Any) = hash(x, zero(UInt)) hash(w::WeakRef, h::UInt) = hash(w.value, h) # Types can't be deleted, so marking as total allows the compiler to look up the hash -@assume_effects :total _typehash(T::Type) = ccall(:jl_type_hash, UInt, (Any,), T) -hash(T::Type, h::UInt) = hash_uint(3h - _typehash(T)) +hash(T::Type, h::UInt) = hash_uint(3h - @assume_effects :total ccall(:jl_type_hash, UInt, (Any,), T)) ## hashing general objects ##