Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suboptimal return type inference for Base.setindex #54462

Closed
nsajko opened this issue May 14, 2024 · 3 comments · Fixed by #54528
Closed

suboptimal return type inference for Base.setindex #54462

nsajko opened this issue May 14, 2024 · 3 comments · Fixed by #54528
Labels
compiler:inference Type inference

Comments

@nsajko
Copy link
Contributor

nsajko commented May 14, 2024

julia> Core.Compiler.return_type(Base.setindex, Tuple{Tuple,Nothing,Int}) <: Tuple
false

julia> versioninfo()
Julia Version 1.12.0-DEV.495
Commit 3c966a5107a (2024-05-09 11:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-17.0.6 (ORCJIT, znver2)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

FWIW the type inference for Base.setindex was better on v1.7.3.

A quick fix, for Base.setindex at least, is adding a ::Tuple type assertion for the Base._setindex return value.

However, I'm not sure why does the inference fail, actually? We do have:

Core.Compiler.return_type(ntuple, Tuple{Any,Val}) <: Tuple

Cthulhu.descend shows that, in Base._setindex, the return value gets inferred as Any even though the Val{N}() gets inferred as Val. Weird.

@nsajko nsajko added the compiler:inference Type inference label May 14, 2024
nsajko added a commit to nsajko/julia that referenced this issue May 14, 2024
nsajko added a commit to nsajko/julia that referenced this issue May 15, 2024
FTR, this PR is part of a series of changes which (re)implement many of
the operations on tuples using a new recursive technique. The ultimate
goal is to hopefully increase the value of the loop-vs-recurse cutoff
(`Any32`, sometimes hardcoded `32`) for tuple operations.

Updates JuliaLang#54462

Closes JuliaLang#54463

Example type inference improvement:

```julia-repl
julia> Core.Compiler.return_type(Base.setindex, Tuple{Tuple{Vararg{Int}},Int,Int})
Tuple{Vararg{Int64}}
```
@martinholters
Copy link
Member

julia> methods(ntuple, Tuple{Any,Val})
# 5 methods for generic function "ntuple" from Base:
 [1] ntuple(f, ::Val{3})
     @ ntuple.jl:50
 [2] ntuple(f, ::Val{2})
     @ ntuple.jl:49
 [3] ntuple(f, ::Val{1})
     @ ntuple.jl:48
 [4] ntuple(f, ::Val{0})
     @ ntuple.jl:47
 [5] ntuple(f::F, ::Val{N}) where {F, N}
     @ ntuple.jl:69

Five methods is too much for inference which therefore bails out. Which is quite unfortunate. Maybe it's worth renaming those to _ntuple and adding ntuple(f::F, ::Val{N}) where {F,N} = _ntuple(f, Val{N}())::Tuple{Vararg{Any,N}}?

@KristofferC
Copy link
Sponsor Member

#54528 is an alternative to that

@martinholters
Copy link
Member

👍 Yes, that looks like good idea to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:inference Type inference
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants