JuliaLang/julia

`@noinline` confuses `@code_typed` reporting wrong return type when return value is unused.

Open

#52,772 创建于 2024年1月5日

在 GitHub 查看
 (5 评论) (0 反应) (0 负责人)Julia (5,773 fork)batch import
good first issueobservability

仓库指标

Star
 (48,709 star)
PR 合并指标
 (平均合并 20天 6小时) (30 天内合并 157 个 PR)

描述

Just a small bug in the @code_typed information for a function that calls a no-inlined callee (with side effects), whose return value is unused:

julia> @noinline f() = (println(); true)
f (generic function with 1 method)

julia> f1(x) = (f(); return x+1)
f1 (generic function with 1 method)

julia> @code_typed f1(10)
CodeInfo(
1 ─      invoke Main.f()::Any
│   %2 = Base.add_int(x, 1)::Int64
└──      return %2
) => Int64

The incorrect line is: invoke Main.f()::Any. This should instead be: invoke Main.f()::Bool.

Of course, since the return value is unused, this isn't very consequential, but it confused me as a red herring for a bit in a more complicated scenario earlier today.

Finally, it's worth noting that the compiler does know the true return type, since @code_llvm shows it return an i8:

julia> @code_llvm f1(10)
; Function Signature: f1(Int64)
;  @ REPL[16]:1 within `f1`
define i64 @julia_f1_2311(i64 signext %"x::Int64") #0 {
top:
  %0 = call i8 @j_f_2314()
; ┌ @ int.jl:87 within `+`
   %1 = add i64 %"x::Int64", 1
; └
  ret i64 %1
}

贡献者指南