From eca847549619229b31b4cf47898fb975df6d3546 Mon Sep 17 00:00:00 2001 From: William Moses Date: Thu, 14 Sep 2023 09:46:39 -0500 Subject: [PATCH] Fix error when returning undef from function (#1054) --- src/compiler.jl | 2 ++ src/compiler/optimize.jl | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/compiler.jl b/src/compiler.jl index a777010921..2a926b965b 100644 --- a/src/compiler.jl +++ b/src/compiler.jl @@ -149,6 +149,7 @@ const nofreefns = Set{String}(( "jl_box_uint64", "jl_box_uint32", "ijl_box_uint64", "ijl_box_uint32", "ijl_box_char", "jl_box_char", + "ijl_subtype", "jl_subtype", "julia.get_pgcstack", "jl_in_threaded_region", "jl_object_id_", "jl_object_id", "ijl_object_id_", "ijl_object_id", "jl_breakpoint", @@ -214,6 +215,7 @@ const inactivefns = Set{String}(( "jl_box_uint64", "jl_box_uint32", "ijl_box_uint64", "ijl_box_uint32", "ijl_box_char", "jl_box_char", + "ijl_subtype", "jl_subtype", "julia.get_pgcstack", "jl_in_threaded_region", "jl_object_id_", "jl_object_id", "ijl_object_id_", "ijl_object_id", "jl_breakpoint", diff --git a/src/compiler/optimize.jl b/src/compiler/optimize.jl index 01c1c4b952..4f496c019d 100644 --- a/src/compiler/optimize.jl +++ b/src/compiler/optimize.jl @@ -400,8 +400,8 @@ function fix_decayaddr!(mod::LLVM.Module) end end if !sret - @safe_show f - @safe_show inst, st, fop + println(string(f)) + @show inst, st, fop flush(stdout) end @@ -661,10 +661,8 @@ function propagate_returned!(mod::LLVM.Module) push!(toremove, i-1) end end - if argn === nothing && length(toremove) == 0 - continue - end illegalUse = !(linkage(fn) == LLVM.API.LLVMInternalLinkage || linkage(fn) == LLVM.API.LLVMPrivateLinkage) + hasAnyUse = false for u in LLVM.uses(fn) un = LLVM.user(u) if !isa(un, LLVM.CallInst) @@ -694,8 +692,20 @@ function propagate_returned!(mod::LLVM.Module) push!(next, LLVM.name(LLVM.parent(LLVM.parent(un)))) LLVM.replace_uses!(un, ops[argn]) end + else + for u in LLVM.uses(un) + hasAnyUse = true + break + end end end + #if the function return has no users whatsoever, remove it + if argn === nothing && !hasAnyUse && LLVM.return_type(LLVM.function_type(fn)) != LLVM.VoidType() + argn = -1 + end + if argn === nothing && length(toremove) == 0 + continue + end if !illegalUse push!(tofinalize, (fn, argn === nothing, toremove)) end