From f2a768b0cbf53799f9788648108f9e1e4da4d3d5 Mon Sep 17 00:00:00 2001 From: apaz-cli Date: Mon, 7 Nov 2022 14:52:20 -0600 Subject: [PATCH 1/2] Fast path constants in update_julia_type only if correct type Fixes #47247 --- src/codegen.cpp | 4 +++- test/compiler/codegen.jl | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index 7190fffd000eb..f02815df37e73 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1726,8 +1726,10 @@ static inline jl_cgval_t mark_julia_type(jl_codectx_t &ctx, Value *v, bool isbox // see if it might be profitable (and cheap) to change the type of v to typ static inline jl_cgval_t update_julia_type(jl_codectx_t &ctx, const jl_cgval_t &v, jl_value_t *typ) { - if (v.typ == jl_bottom_type || v.constant || typ == (jl_value_t*)jl_any_type || jl_egal(v.typ, typ)) + if (v.typ == jl_bottom_type || typ == (jl_value_t*)jl_any_type || jl_egal(v.typ, typ)) return v; // fast-path + if (v.constant) + return jl_isa(v.constant, typ) ? v : jl_cgval_t(); if (jl_is_concrete_type(v.typ) && !jl_is_kind(v.typ)) { if (jl_is_concrete_type(typ) && !jl_is_kind(typ)) { // type mismatch: changing from one leaftype to another diff --git a/test/compiler/codegen.jl b/test/compiler/codegen.jl index 2880ee6879c64..38e39ed87d684 100644 --- a/test/compiler/codegen.jl +++ b/test/compiler/codegen.jl @@ -781,3 +781,7 @@ f_isdefined_nospecialize(@nospecialize(x)) = isdefined(x, 1) # Test codegen for isa(::Any, Type) f_isa_type(@nospecialize(x)) = isa(x, Type) @test !occursin("jl_isa", get_llvm(f_isa_type, Tuple{Any}, true, false, false)) + +# Issue #47247 +f47247(a::Ref{Int64}, b::Nothing) = setfield!(a, :x, b) +@test_throws TypeError f47247(Ref(5), nothing) From ddd4ceb7bd8fa167ff155a3c455353f62a5e55ec Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Tue, 8 Nov 2022 00:19:15 -0500 Subject: [PATCH 2/2] Update test/compiler/codegen.jl --- test/compiler/codegen.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/compiler/codegen.jl b/test/compiler/codegen.jl index 38e39ed87d684..fac1c8b70fc54 100644 --- a/test/compiler/codegen.jl +++ b/test/compiler/codegen.jl @@ -783,5 +783,5 @@ f_isa_type(@nospecialize(x)) = isa(x, Type) @test !occursin("jl_isa", get_llvm(f_isa_type, Tuple{Any}, true, false, false)) # Issue #47247 -f47247(a::Ref{Int64}, b::Nothing) = setfield!(a, :x, b) +f47247(a::Ref{Int}, b::Nothing) = setfield!(a, :x, b) @test_throws TypeError f47247(Ref(5), nothing)