This is an archive of the discontinued LLVM Phabricator instance.

[SeparateConstOffsetFromGEP] Fix: `b - a` matched `a - b` during reuniteExts
ClosedPublic

Authored by Peakulorain on Feb 7 2023, 5:21 PM.

Details

Summary

During the SeparateConstOffsetFromGEP pass, a - b and b - a will be
considered equivalent in some instances.

An example- the IR contains:

BB1:
    %add = add %a, 511
    br label %BB2
BB2:
    %sub2 = sub %b,  %a
    br label %BB3
BB3:
    %sub1 = sub %add, %b
    %gep = getelementptr float, ptr %p, %sub1

Step 1 in the SeparateConstOffsetFromGEP pass, after split constant index:

BB1:
    %add = add %a, 511
    br label %BB2
BB2:
    %sub2 = sub %b,  %a
    br label %BB3
BB3:
    %sub.t = sub %a, %b
    %gep.base = getelementptr float, ptr %p, %sub.t
    %gep = getelementptr float, ptr %gep.base, 511

Step 2, after reuniteExts:

BB1:
    br label %BB2
BB2:
    %sub2 = sub %b,  %a
    br label %BB3
BB3:
    %gep.base = getelementptr float, ptr %p, %sub2
    %gep = getelementptr float, ptr %gep.base, 511

Obviously, reuniteExts treated a - b and b - a as equivalent.
This patch fixes that.

Diff Detail

Event Timeline

Peakulorain created this revision.Feb 7 2023, 5:21 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 7 2023, 5:21 PM
Peakulorain requested review of this revision.Feb 7 2023, 5:21 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 7 2023, 5:21 PM

We triggered this bug on the aarch64 target, since aarch64 has enabled LowerGEP.
Pls use command line in the case split-gep-sub.ll of my patch to trigger this problem.

Peakulorain retitled this revision from Fix: `b - a` matched `a - b` during reuniteExts to [SeparateConstOffsetFromGEP] Fix: `b - a` matched `a - b` during reuniteExts.Feb 12 2023, 11:04 PM

Please upload with more context - use something like "git diff -U9999".

llvm/test/Transforms/SeparateConstOffsetFromGEP/AArch64/split-gep-sub.ll
1 ↗(On Diff #495691)

It would be better to use a RUN line with opt like existing tests in this and the parent directory.
IIUC, we have a miscompile, so you can create a baseline test that shows the bug, commit that to main, and then regenerate the CHECK lines with this patch, so we show the difference in IR. Please use "utils/update_test_checks.py" to auto-generate the CHECK lines instead of manually writing them.

Please upload with more context - use something like "git diff -U9999".

Your reply is appreciated. First step to show this miscompile, we should add a flag to enable LowerGEP manually.
So, see the first patch pls: https://reviews.llvm.org/D143980
I will update after the above patch is merged.

Peakulorain edited the summary of this revision. (Show Details)

Please upload with more context - use something like "git diff -U9999".

Thanks, uploaded again.

nikic accepted this revision.Feb 13 2023, 11:48 PM

LGTM. Using SCEV expressions as a deduplication key seems kind of odd in the first place, but I guess for adds it does provide some value, because you get complexity canonicalization for free. Of course, for subs we don't want that, and this effectively prevents it.

llvm/test/Transforms/SeparateConstOffsetFromGEP/split-gep-sub.ll
3–4

Please include a comment on what this is testing, it's not particularly obvious.

This revision is now accepted and ready to land.Feb 13 2023, 11:48 PM
spatel accepted this revision.Feb 14 2023, 6:21 AM

More knowledgeable reviewers have approved, so just making it clear that there's no need to wait for more comments from me. :)

More knowledgeable reviewers have approved, so just making it clear that there's no need to wait for more comments from me. :)

Also thanks a lot for your review. :)

This revision was landed with ongoing or failed builds.Feb 14 2023, 6:33 PM
This revision was automatically updated to reflect the committed changes.