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

[SelectionDAG] Fold (avg x, 0) -> x >> 1 #85581

Closed
wants to merge 1 commit into from

Conversation

AtariDreams
Copy link
Contributor

No description provided.

@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Mar 17, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 17, 2024

@llvm/pr-subscribers-backend-aarch64

@llvm/pr-subscribers-llvm-selectiondag

Author: AtariDreams (AtariDreams)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+6-1)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 5eb53d57c9c2bf..f0cd23a4e65b9d 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -5083,7 +5083,12 @@ SDValue DAGCombiner::visitAVG(SDNode *N) {
   if (N0 == N1 && Level >= AfterLegalizeTypes)
     return N0;
 
-  // TODO If we use avg for scalars anywhere, we can add (avgfl x, 0) -> x >> 1
+  // Fold (avg x, 0) -> x >> 1
+  if (isNullOrNullSplat(N0))
+    return DAG.getNode(ISD::SRL, DL, VT, N1, DAG.getConstant(1, DL, VT));
+
+  if (isNullOrNullSplat(N1))
+    return DAG.getNode(ISD::SRL, DL, VT, N0, DAG.getConstant(1, DL, VT));
 
   return SDValue();
 }

@@ -5083,7 +5083,18 @@ SDValue DAGCombiner::visitAVG(SDNode *N) {
if (N0 == N1 && Level >= AfterLegalizeTypes)
return N0;

// TODO If we use avg for scalars anywhere, we can add (avgfl x, 0) -> x >> 1
// Fold (avg x, 0) -> x >> 1
if (isNullOrNullSplat(N0))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't we canonicalize constants to RHS earlier?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can't you just remove the isVector check around the existing code on line 5067?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not the same behavior. isNullOrNullSplat returns false if there are any undefs in the vector.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then replace that code with your preferred version.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also if you are intentionally improving the handling of vectors with undefs then you should add tests for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isConstantSplatVectorAllZeros is for vectors. isNullOrNullSplat is for constants and vectors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not at all changing how vectors with undefs are handled. I am only changing how zero constants/vectors are handled.

Copy link
Collaborator

@topperc topperc Mar 19, 2024

Choose a reason for hiding this comment

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

If you pass AllowUndefs=true to isNullOrNullSplat, can we remove the ISD::isConstantSplatVectorAllZeros version of the code?

Copy link
Collaborator

@davemgreen davemgreen left a comment

Choose a reason for hiding this comment

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

I think for vectors this might already be handled, and I don't know if there are any architectures that use these nodes for scalars.

@@ -464,8 +464,7 @@ define <8 x i16> @rhaddu_i_const_lhs(<8 x i16> %src1) {
define <8 x i16> @rhaddu_i_const_zero(<8 x i16> %src1) {
; CHECK-LABEL: rhaddu_i_const_zero:
; CHECK: // %bb.0:
; CHECK-NEXT: movi v1.2d, #0000000000000000
; CHECK-NEXT: urhadd v0.8h, v0.8h, v1.8h
; CHECK-NEXT: ushr v0.8h, v0.8h, #1
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think these would be incorrect for avgceil.

@jayfoad
Copy link
Contributor

jayfoad commented Mar 19, 2024

I still don't understand why we need two implementations of "fold (avgfloor x, 0) -> x >> 1" in the same function, so it would be a nack from me. Maybe one of the other reviewers can understand it.

@RKSimon RKSimon self-requested a review March 19, 2024 11:10
RKSimon added a commit that referenced this pull request Mar 19, 2024
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
Copy link

✅ With the latest revision this PR passed the Python code formatter.

Copy link

✅ With the latest revision this PR passed the C/C++ code formatter.

return DAG.getNode(ISD::SRA, DL, VT, N0, DAG.getConstant(1, DL, VT));
if (Opcode == ISD::AVGFLOORU)
return DAG.getNode(ISD::SRL, DL, VT, N0, DAG.getConstant(1, DL, VT));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove the vector folds above? (github won't let me put this comment there)

@RKSimon
Copy link
Collaborator

RKSimon commented Jun 5, 2024

Close this? This is now redundant after 9f5c8de which was added as pre-commit for #92096

@AtariDreams AtariDreams closed this Jun 5, 2024
@AtariDreams AtariDreams deleted the avg branch June 5, 2024 15:49
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
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants