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

inference: add error/throwness checks for setfield! #43603

Merged
merged 2 commits into from
Jan 8, 2022
Merged

Conversation

aviatesk
Copy link
Member

@aviatesk aviatesk commented Dec 30, 2021

In a similar spirit to #43587, this commit introduces error check for
setfield!. We can bail out from inference if we can prove either of:

  • the object is not mutable type
  • the object is Module object
  • the value being assigned is incompatible with the declared type of
    object field

This commit also adds throwness check implementation for setfield! (i.e. setfield!_nothrow).
This throwness check won't be used in the current native compilation
pipeline since setfield! call can't be eliminated anyway even if we can prove
that it never throws. But this throwness check would be used by
EscapeAnalysis.jl integration and so I'd like to include it in Base.

@ianatol ianatol self-requested a review December 30, 2021 17:18
@ianatol
Copy link
Member

ianatol commented Jan 3, 2022

Seems sensible to me, but I am not intimately familiar with this part of inference

if s <: Tuple && fld >= nf && isvarargtype(ftypes[nf])
return rewrap_unionall(unwrapva(ftypes[nf]), s00)
end
if fld < 1 || fld > nf
return Bottom
elseif setfield && isconst(s, fld)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added a support for new const declaration for mutable struct field.

@aviatesk aviatesk added the compiler:inference Type inference label Jan 4, 2022
setfield!_tfunc(o, f, v) = (@nospecialize; v)
function setfield!_tfunc(o, f, v, order)
@nospecialize
if !isvarargtype(order)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we only ever call tfuncs with real types

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately argtypes[end] can sometimes be varargtype:

push!(argtypes, unconstrain_vararg_length(vatype))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we want to try to make sure that most tfuncs deal with real types?

base/compiler/tfuncs.jl Outdated Show resolved Hide resolved
aviatesk and others added 2 commits January 8, 2022 13:51
In a similar spirit to #43587, this commit introduces error check for
`setfield!`. We can bail out from inference if we can prove either of:
- the object is not mutable type
- the object is `Module` object
- the value being assigned is incompatible with the declared type of
  object field

This commit also adds the throwness check for `setfield!` (i.e. `setfield!_nothrow`).
This throwness check won't be used in the current native compilation
pipeline since `setfield!` call can't be eliminated even if we can prove
that it never throws. But this throwness check would be used by
EscapeAnalysis.jl integration and so I'd like to include it in Base.
Co-authored-by: Jameson Nash <vtjnash@gmail.com>
@aviatesk aviatesk merged commit 95cfbcc into master Jan 8, 2022
@aviatesk aviatesk deleted the avi/setfield! branch January 8, 2022 07:25
aviatesk added a commit to aviatesk/EscapeAnalysis.jl that referenced this pull request Jan 8, 2022
aviatesk added a commit that referenced this pull request Jan 8, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
In a similar spirit to JuliaLang#43587, this commit introduces error check for
`setfield!`. We can bail out from inference if we can prove either of:
- the object is not mutable type
- the object is `Module` object
- the value being assigned is incompatible with the declared type of
  object field

This commit also adds the throwness check for `setfield!` (i.e. `setfield!_nothrow`).
This throwness check won't be used in the current native compilation
pipeline since `setfield!` call can't be eliminated even if we can prove
that it never throws. But this throwness check would be used by
EscapeAnalysis.jl integration and so I'd like to include it in Base.
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
In a similar spirit to JuliaLang#43587, this commit introduces error check for
`setfield!`. We can bail out from inference if we can prove either of:
- the object is not mutable type
- the object is `Module` object
- the value being assigned is incompatible with the declared type of
  object field

This commit also adds the throwness check for `setfield!` (i.e. `setfield!_nothrow`).
This throwness check won't be used in the current native compilation
pipeline since `setfield!` call can't be eliminated even if we can prove
that it never throws. But this throwness check would be used by
EscapeAnalysis.jl integration and so I'd like to include it in Base.
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
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 this pull request may close these issues.

3 participants