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

Simplify lowering of typed comprehension #32709

Closed
wants to merge 1 commit into from

Conversation

c42f
Copy link
Member

@c42f c42f commented Jul 27, 2019

For implementing special syntax it's useful for typed comprehension to
lower to collect(T, gen). This changes typed comprehensions to use the
same lowering pattern as normal comprehensions.

@andyferris

@c42f c42f added the compiler:lowering Syntax lowering (compiler front end, 2nd stage) label Jul 27, 2019
@c42f c42f requested a review from JeffBezanson July 27, 2019 15:08
For implementing special syntax it's useful for typed comprehension to
lower to `collect(T, gen)`. This changes typed comprehensions to use the
same lowering pattern as normal comprehensions.

Needed to tweak the precompile workaround from #28808

Co-authored-by: Andy Ferris <[email protected]>
@c42f c42f force-pushed the cjf/typed-comprehension-lowering branch from 64f9bb8 to 3439c9d Compare July 29, 2019 12:38
(and (every (lambda (x) (and (pair? x) (eq? (car x) '=)))
ranges)
;; TODO: this is a hack to lower simple comprehensions to loops very
;; early, to greatly reduce the # of functions and load on the compiler
Copy link
Member

Choose a reason for hiding this comment

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

See this comment.

Copy link
Member

Choose a reason for hiding this comment

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

yes. and to expand on that a little, we use this to make it possible to use comprehensions in reflection and inference. we wouldn't be able to use comprehensions there if it wasn't for this guarantee.

Copy link
Member

@andyferris andyferris Jul 29, 2019

Choose a reason for hiding this comment

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

OK, I guess that makes sense; I just noticed I can even return a typed comprehension from a @generated function (for obvious reasons). There seems to be a bunch of Any[... for ...] comprehensions in the compiler.

On the other hand, it seems these days that collect is getting some overloads and therefore untyped comprehensions aren't so tied to Array anymore. For example, is it bad that [f(x) for x in a::StaticArray] isa StaticArray? This is juxtaposed with !(Float64[f(x) for x in a::StaticArray) isa StaticArray). I'm wondering if we can get a bit more consistency? (As in - maybe the solution we want is the opposite of this PR, and enforce the untyped version to always create an Array?)

I note that I don't see an aweful lot of typed comprehensions in user code (maybe I'm wrong and that's just me?) so do you know if outside of Julia internals does this lowering "optimization" has much of an impact on latency in the wild? If not, could the desired behavior be viewed as more of a bootstrap vs not bootstrap issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I definitely read this comment! At face value it says "this is a performance hack" and I wondered whether it was still necessary. Especially because comprehensions seem more common than typed comprehensions for user code.

Thanks @vtjnash for expanding. Here's what I think you're saying:

  • Normal comprehensions are prohibited in base/compiler due to the call to return_type here:
    Core.Compiler.return_type(first, Tuple{typeof($I)})
  • Simple typed comprehensions are allowed because they lower to a simple form which doesn't re-enter inference.
  • The comment greatly reduce the # of functions and load on the compiler refers to the performance of comprehensions in the compiler itself rather than the performance of user code (??)

But I don't understand why lowering to collect(T, gen) is not ok - this doesn't call return_type; it goes via the _collect methods here:

_collect(::Type{T}, itr, isz::HasLength) where {T} = copyto!(Vector{T}(undef, Int(length(itr)::Integer)), itr)

If this is about compiler performance, what should I be measuring? Is the time to compile Base relevant?

@c42f
Copy link
Member Author

c42f commented Sep 11, 2019

Ok, AFAICT a good reason not to do this yet is that it would break the use of typed comprehensions within the AST produced by @generated functions. So we should wait until that limitation is lifted if possible (cf. #21094 (comment)).

Other than that, the typed version of collect lowered to here doesn't re-enter inference so I can't see a fundamental problem with doing this. Some concern about performance has been raised but it's not yet clear what kind of benchmark should be run for that.

With the first point in mind, I think I'll just close this for now.

@c42f c42f closed this Sep 11, 2019
@DilumAluthge DilumAluthge deleted the cjf/typed-comprehension-lowering branch March 25, 2021 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants