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

release/18.x: [DAGCombiner] In mergeTruncStore, make sure we aren't storing shifted in bits. (#90939) #91038

Open
wants to merge 1 commit into
base: release/18.x
Choose a base branch
from

Conversation

AtariDreams
Copy link
Contributor

When looking through a right shift, we need to make sure that all of the bits we are using from the shift come from the shift input and not the sign or zero bits that are shifted in.

Fixes #90936.

(cherry picked from commit 3563af6)

@nikic nikic added this to the LLVM 18.X Release milestone May 6, 2024
@nikic nikic requested review from RKSimon and topperc May 6, 2024 04:41
@topperc
Copy link
Collaborator

topperc commented May 6, 2024

@AtariDreams This bug has existed since at least LLVM 10. What makes it a candidate for backporting?

@llvmbot
Copy link
Collaborator

llvmbot commented May 9, 2024

@llvm/pr-subscribers-llvm-selectiondag

@llvm/pr-subscribers-backend-aarch64

Author: AtariDreams (AtariDreams)

Changes

When looking through a right shift, we need to make sure that all of the bits we are using from the shift come from the shift input and not the sign or zero bits that are shifted in.

Fixes #90936.

(cherry picked from commit 3563af6)


Full diff: https://github.com/llvm/llvm-project/pull/91038.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+4)
  • (added) llvm/test/CodeGen/AArch64/pr90936.ll (+20)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 5038f8a1fc156..4951e45edb9ed 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -8952,6 +8952,10 @@ SDValue DAGCombiner::mergeTruncStores(StoreSDNode *N) {
       if (ShiftAmtC % NarrowNumBits != 0)
         return SDValue();
 
+      // Make sure we aren't reading bits that are shifted in.
+      if (ShiftAmtC > WideVal.getScalarValueSizeInBits() - NarrowNumBits)
+        return SDValue();
+
       Offset = ShiftAmtC / NarrowNumBits;
       WideVal = WideVal.getOperand(0);
     }
diff --git a/llvm/test/CodeGen/AArch64/pr90936.ll b/llvm/test/CodeGen/AArch64/pr90936.ll
new file mode 100644
index 0000000000000..38cda8d388945
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/pr90936.ll
@@ -0,0 +1,20 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=aarch64 | FileCheck %s
+
+define void @f(i16 %arg, ptr %arg1) {
+; CHECK-LABEL: f:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    ubfx w8, w0, #8, #6
+; CHECK-NEXT:    strb w0, [x1]
+; CHECK-NEXT:    strb w8, [x1, #1]
+; CHECK-NEXT:    ret
+bb:
+  %i = trunc i16 %arg to i8
+  %i2 = trunc i16 %arg to i14
+  %i3 = lshr i14 %i2, 8
+  store i8 %i, ptr %arg1, align 1
+  %i4 = getelementptr i8, ptr %arg1, i64 1
+  %i5 = trunc i14 %i3 to i8
+  store i8 %i5, ptr %i4, align 1
+  ret void
+}

@AtariDreams
Copy link
Contributor Author

AtariDreams commented May 9, 2024

@AtariDreams This bug has existed since at least LLVM 10. What makes it a candidate for backporting?

At best, if the code triggers, we abort the fold, so there is no risk of anything crazy going on if this is added.

If this code were to instead try alternate folding, there is a risk that a bug may slip through, however, given that this is simply aborting the merge, there is no harm in leaving the DAG untouched.

So, the worst that could happen:

  1. We may not merge stores that could be valid for all we know, but that won't mis-compile.
  2. Stores simply are not merged, as the DAG is left untouched.

Pros:

  1. More stores that should not be merged are prevented from merging.

Again, less risk of miscompile because this just bails out of the merge just like the condition above this code does.

@AtariDreams
Copy link
Contributor Author

We do not need to know how to fold every single possible permutation that comes our way, especially if they are so rare that writing compile code optimizing it isn't even worth it. We do, however, need to strive to avoid miscompiles wherever we can, no matter how esoteric the code is.

Now, this isn't always possible, but in this case, the alternative codepath given just bails the transform, which is preferable to folding something that should not be.

@tstellar
Copy link
Collaborator

@topperc Do you have any strong objections to backporting this? This looks small to me and I think it's OK to fix long-standing bugs.

@topperc
Copy link
Collaborator

topperc commented May 15, 2024

@topperc Do you have any strong objections to backporting this? This looks small to me and I think it's OK to fix long-standing bugs.

No objection.

@tstellar
Copy link
Collaborator

@AtariDreams I've noticed you've filed a lot of backport requests. How are you choosing which fixes to backport? Is there a specific use case you care about?

@AtariDreams
Copy link
Contributor Author

@AtariDreams I've noticed you've filed a lot of backport requests. How are you choosing which fixes to backport? Is there a specific use case you care about?

There a particular LLVM miscompile bug in WebKit I'm trying to figure out. It's been there since 2019. Backports is literally just avoiding miscompilations

@nikic
Copy link
Contributor

nikic commented May 15, 2024

@tstellar AtariDreams is requesting backports for random commits that somehow mention miscompilations or crashes, without having any understanding of what the changes are about or how they relate to other changes. They have submitted a large amount of invalid or nonsensical backports for that reason, despite many requests to stop doing so. When there is any doubt, you should not merge backport PRs created by this user. That said, this specific one does look harmless to me.

@RKSimon
Copy link
Collaborator

RKSimon commented May 27, 2024

@AtariDreams I've noticed you've filed a lot of backport requests. How are you choosing which fixes to backport? Is there a specific use case you care about?

There a particular LLVM miscompile bug in WebKit I'm trying to figure out. It's been there since 2019. Backports is literally just avoiding miscompilations

@AtariDreams Has the bug disappeared in llvm trunk and you think a recent commit has fixed/hidden it? Has this bug been reported either to WebKit or LLVM that we can track please? Have you been able to confirm if its a llvm bug or UB in WebKit?

… in bits. (llvm#90939)

When looking through a right shift, we need to make sure that all of
the bits we are using from the shift come from the shift input and
not the sign or zero bits that are shifted in.

Fixes llvm#90936.

(cherry picked from commit 3563af6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AArch64 llvm:SelectionDAG SelectionDAGISel as well
Projects
Status: Needs Triage
Development

Successfully merging this pull request may close these issues.

None yet

6 participants