-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
JIT: Index use can't be completely optimized away #11870
Comments
Also worth looking at the locally created clearly non-negative case:
Here the jit is currently (13ae47e) is unable to remove a redundant branch as there are complementary conditions (note IR is from from a similar case, may not match exactly if you compiled the above) G_M15796_IG07:
85C9 test ecx, ecx // Index ctor: value < 0? [no]
7C40 jl SHORT G_M15796_IG14
G_M15796_IG08:
448BC1 mov r8d, ecx
4585C0 test r8d, r8d // Index.IsFromEnd ? [no]
7D0B jge SHORT G_M15796_IG09
41F7D0 not r8d
448BCF mov r9d, edi
452BC8 sub r9d, r8d
EB03 jmp SHORT G_M15796_IG10
G_M15796_IG09:
458BC8 mov r9d, r8d
G_M15796_IG10:
443BCF cmp r9d, edi // array index in bounds [yes]
732B jae SHORT G_M15796_IG15
4D63C1 movsxd r8, r9d
420FB73440 movzx rsi, word ptr [rax+2*r8]
FFC1 inc ecx
3BCF cmp ecx, edi
7CD3 jl SHORT G_M15796_IG07 we could tweak Once we do that we'd have something like: ;;; with initial redundant branch optimized away
G_M15796_IG07:
85C9 test ecx, ecx
7C2D jl SHORT G_M15796_IG12
G_M15796_IG08:
448BC1 mov r8d, ecx
443BC7 cmp r8d, edi
732B jae SHORT G_M15796_IG13
4D63C0 movsxd r8, r8d
420FB73440 movzx rsi, word ptr [rax+2*r8]
FFC1 inc ecx
3BCF cmp ecx, edi
7CE6 jl SHORT G_M15796_IG07 Haven't investigated but would hope VN could now realize we're referring to the loop index var and so assertion prop can clean up both the negative check and the bounds check. |
You can get assertion prop to look for reversed relop assertions, but it's clunky. Does not seem to hit all that often, though it helps clean up second the example above.
Would be less clunky if there was a VN operation to find the VN of the reverse relop (rather than actually forming the reverse tree and value numbering that), and if assertion lookup was driven just by VN and not by tree. Former seems doable, haven't looked at the latter. Constant prop seems prone to leave off VNs on propagated constants so one has to bail out sometimes, perhaps if this was fixed it would find a few more cases? Haven't yet looked beyond this to see why we can't subsequently show the index is always in bounds. Would guess though that when we remove the branch we don't go back and update VNs so we don't benefit as much from branch pruning as we could... such updates are probably intractable now anyways since SSA defs and uses aren't linked. I suppose if assertion prop manages to remove a non-bounds check branch we could consider just rerunning things. |
Hmm, that sounds like the issue that prompted me to attempt SCCP in VN. I need to retry that with this example. In fact, I need to try that on its own because when I tried it last time it was mixed up with a bunch of other experiments. |
Random idea of the idea: what if we somehow teach VN to recognize induction variables and assign them some special VNFunc, something like |
Don't think we can promise any improvements here for 3.0, so will move to future. |
In the following example we'd like the jit to produce the same (or very similar code) for both
Fint
andFindex
:However currently the
Index
ctor won't inline (see #11848) and the IL is not jit-friendly. If we revise it and then force it to inline, we still see some codegen deficiencies:Root cause is that
Index
uses an inverted (~negative) value to encode "from end" and the jit can't propagate this from construction down to use.Range prop might be the right place to address this but it currently does not model
not
. But doing that, opts would only kick in whenIndex
is used as an array index.Tentatively putting this in 3.0,but may defer depending on urgency of removing overhead from use of
Index
.category:cq
theme:optimization
skill-level:expert
cost:medium
The text was updated successfully, but these errors were encountered: