-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
POC/WIP: leverage LLVM function attributes for better CSE #29566
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
Conversation
|
I think this is reasonable, though I think it would be even better to track this kind of thing during inference. |
vtjnash
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think doing this as an IPO pass is valid (or, at least, won't be in the future, when we allow caching native code and compiling functions non-recursively).
|
So julia> f() = sin(3)
f (generic function with 2 methods)
julia> @code_typed f()
CodeInfo(
1 1 ─ return 0.14112 │
) => Float64What is required to make it figure out that julia> f(x) = sin(x) + sin(x)
f (generic function with 2 methods)
julia> @code_typed f(3)
CodeInfo(
1 1 ─ %1 = (Base.sitofp)(Float64, x)::Float64 │╻╷╷╷ sin
│ %2 = invoke Base.Math.sin(%1::Float64)::Float64 ││
│ %3 = (Base.sitofp)(Float64, x)::Float64 ││╻╷╷ float
│ %4 = invoke Base.Math.sin(%3::Float64)::Float64 ││
│ %5 = (Base.add_float)(%2, %4)::Float64 │╻ +
└── return %5 │
) => Float64can be simplified? Something like : first notice that 1 1 ─ %1 = (Base.sitofp)(Float64, x)::Float64 │╻╷╷╷ sin
│ %2 = invoke Base.Math.sin(%1::Float64)::Float64 ││
│ %4 = invoke Base.Math.sin(%1::Float64)::Float64 ││
│ %5 = (Base.add_float)(%2, %4)::Float64 │╻ +
└── return %5 │
) => Float64Now use the knowledge that 1 1 ─ %1 = (Base.sitofp)(Float64, x)::Float64 │╻╷╷╷ sin
│ %2 = invoke Base.Math.sin(%1::Float64)::Float64 ││
│ %5 = (Base.add_float)(%2, %2)::Float64 │╻ +
└── return %5 │
) => Float64? Is there some complication or just someone has to implement it? What's the advantage of doing it in inference instead of letting LLVM handle it` |
|
Yes, we could have a GVN pass in the optimizer. However, GVN is a non-trivial pass to implement, so since LLVM already has it, it'd be nice to at least pass that information down. |
|
As a side-note I was looking today at function attributes since I would love to do LICM on Julia functions. For that we would want to infer functions attributes much earlier in the pipeline (this PR does it at the end), but if we do it before |
This branch is a proof-of-concept showing how we can get LLVM to do better CSE by leveraging/inferring function attributes. This is entirely @maleadt's work, I just want to call attention to it before the branch goes stale w.r.t. master 😛
I think Tim might be a bit busy right now, but I would be willing to devote some cycles to polishing this in November, if folks agree it's a good idea.
With this PR: