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

RFC / WIP: no longer warn in code_warntype for variables not used in the body #23280

Merged
merged 1 commit into from
Aug 21, 2017

Conversation

KristofferC
Copy link
Sponsor Member

@KristofferC KristofferC commented Aug 16, 2017

The fact that @code_warntype shows unused variables in red is something that keeps tripping up users (both new and experienced), see e.g. https://discourse.julialang.org/t/question-about-type-stability-of-arrays-in-julia-0-6/5389.

This PR traverses the codeinfo to check if the variable is actually used somewhere in the body. If not, we no longer print the variable in red even if it is inferred as a non concrete type.

Before:

image

After:

image

Maybe the (Unused in body) is too verbose?

Will add test / docs later if people think this is a good idea. Also, I'm new to the codeinfo data structure so someone should probably look over that code. CI skipped as to not waste resources so don't merge.

@KristofferC KristofferC force-pushed the kc/no_red_unused branch 3 times, most recently from 0190354 to 59d0059 Compare August 16, 2017 09:44
return false
end

is_variable_used(arg::SlotNumber, slotnames, v) = slotnames[arg.id] == v
Copy link
Contributor

Choose a reason for hiding this comment

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

Compare the id directly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also should be ::Slotinstead.

@@ -347,14 +347,35 @@ problematic for performance, so the results need to be used judiciously.
See [`@code_warntype`](@ref man-code-warntype) for more information.
"""
function code_warntype(io::IO, f, @nospecialize(t))
function is_variable_used(bodies::Vector, slotnames, v)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is also wrong. This can incorrectly catch constant arrays spliced into the code.

@oscardssmith
Copy link
Member

Would it make sense to make things yellow if unused in body? I feel like that would be a useful piece of info (although code warntype might not be a good place to show it)

@yuyichao
Copy link
Contributor

What's this info (unused vars) useful for? It's just a signature of the optimization.

It might also be better to just do a single scan of slots uses instead of multiple ones and there's no need to use dispatch for that.

@KristofferC
Copy link
Sponsor Member Author

I changed to only doing a single scan. I am not sure how I would avoid using dispatch and if the Vector dispatch function is still problematic.

@yuyichao
Copy link
Contributor

yuyichao commented Aug 16, 2017

    function scan_exprs(exprs, used)
        for ex in exprs
            if isa(ex, Slot)
                used[ex.id] = true
            elseif isa(ex, Expr)
                scan_exprs(ex.args, used)
            end
        end
    end
    function slot_usaged(ci, slotnames)
        used = falses(length(slotnames))
        scan_exprs(ci.code, used)
        return used
    end

@@ -346,15 +346,33 @@ This serves as a warning of potential type instability. Not all non-leaf types a
problematic for performance, so the results need to be used judiciously.
See [`@code_warntype`](@ref man-code-warntype) for more information.
"""
function code_warntype(io::IO, f, @nospecialize(t))
function code_warntype(io::IO, f, t::ANY) #@nospecialize(t))
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I imagine this is for testing only but this should be changed back....

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Yeah

@yuyichao
Copy link
Contributor

Note that the code above rely on the AST to be valid. You can add ex.id <= length(used) && (used[ex.id] = true) to guard it if you want.

@KristofferC
Copy link
Sponsor Member Author

KristofferC commented Aug 17, 2017

Thanks! Updated with that code. Also added a test.

Now for the details. I currently avoid printing the inferred type and just print (unused) when the variable is not used. Do people like that or are there other suggestions for other ways of printing that give the same information?

@KristofferC
Copy link
Sponsor Member Author

With some inspiration from GDB I went with:

image

Any opinions on that?

@KristofferC
Copy link
Sponsor Member Author

Not sure if eligible for backport but tentatively putting the label there.

@KristofferC
Copy link
Sponsor Member Author

Will merge tomorrow unless [the usual stuff].

@KristofferC KristofferC merged commit 33fbfad into master Aug 21, 2017
@KristofferC KristofferC deleted the kc/no_red_unused branch August 21, 2017 19:30
@fredrikekre
Copy link
Member

Could we do something like this for the red Union{} on throw lines as well?
screenshot from 2017-08-22 16-25-47

@KristofferC
Copy link
Sponsor Member Author

Yeah, should be fairly easy.

ararslan pushed a commit that referenced this pull request Sep 11, 2017
ararslan pushed a commit that referenced this pull request Sep 13, 2017
vtjnash pushed a commit that referenced this pull request Sep 14, 2017
ararslan pushed a commit that referenced this pull request Sep 15, 2017
ararslan pushed a commit that referenced this pull request Sep 16, 2017
ararslan pushed a commit that referenced this pull request Sep 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants