Skip to content

[RISCV][VLOPT] Fix operand check in isVectorOpUsedAsScalarOp #112253

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

lukel97
Copy link
Contributor

@lukel97 lukel97 commented Oct 14, 2024

A reduction instruction always has a passthru operand, so the scalar operand should always be vs1 which is at index 3.

Even though the destination operand is also scalar, I think the passthru will need to preserve all elements so I haven't included it.

A reduction instruction always has a passthru operand, so the scalar operand should always be vs1 which is at index 3.

Even though the destination operand is also scalar, I think the passthru will need to preserve all elements so I haven't included it.
@llvmbot
Copy link
Member

llvmbot commented Oct 14, 2024

@llvm/pr-subscribers-backend-risc-v

Author: Luke Lau (lukel97)

Changes

A reduction instruction always has a passthru operand, so the scalar operand should always be vs1 which is at index 3.

Even though the destination operand is also scalar, I think the passthru will need to preserve all elements so I haven't included it.


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

2 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp (+2-4)
  • (modified) llvm/test/CodeGen/RISCV/rvv/vl-opt.mir (+17)
diff --git a/llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp b/llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp
index 1a9084f8b6cb2b..8a4dd70d961f8b 100644
--- a/llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp
+++ b/llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp
@@ -611,10 +611,8 @@ static bool isVectorOpUsedAsScalarOp(MachineOperand &MO) {
   case RISCV::VFREDOSUM_VS:
   case RISCV::VFREDUSUM_VS:
   case RISCV::VFWREDOSUM_VS:
-  case RISCV::VFWREDUSUM_VS: {
-    bool HasPassthru = RISCVII::isFirstDefTiedToFirstUse(MI->getDesc());
-    return HasPassthru ? MO.getOperandNo() == 2 : MO.getOperandNo() == 3;
-  }
+  case RISCV::VFWREDUSUM_VS:
+    return MO.getOperandNo() == 3;
   default:
     return false;
   }
diff --git a/llvm/test/CodeGen/RISCV/rvv/vl-opt.mir b/llvm/test/CodeGen/RISCV/rvv/vl-opt.mir
index 59a472c73a4624..010e3ca642269b 100644
--- a/llvm/test/CodeGen/RISCV/rvv/vl-opt.mir
+++ b/llvm/test/CodeGen/RISCV/rvv/vl-opt.mir
@@ -16,3 +16,20 @@ body: |
     %x:vr = PseudoVADD_VV_MF4 $noreg, $noreg, $noreg, -1, 4 /* e16 */, 0 /* tu, mu */
     %y:vr = PseudoVNSRL_WV_MF4 $noreg, %x, $noreg, %vl, 4 /* e16 */, 0 /* tu, mu */
 ...
+---
+name: vredsum_vv_user
+body: |
+  bb.0:
+    liveins: $x1
+    ; CHECK-LABEL: name: vredsum_vv_user
+    ; CHECK: liveins: $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: %vl:gprnox0 = COPY $x1
+    ; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1, 5 /* e32 */, 0 /* tu, mu */
+    ; CHECK-NEXT: %y:vr = PseudoVREDSUM_VS_M1_E64 $noreg, %x, $noreg, -1, 6 /* e64 */, 0 /* tu, mu */
+    ; CHECK-NEXT: %z:vr = PseudoVADD_VV_M1 $noreg, %x, $noreg, %vl, 5 /* e32 */, 0 /* tu, mu */
+    %vl:gprnox0 = COPY $x1
+    %x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1, 5 /* e32 */, 0 /* tu, mu */
+    %y:vr = PseudoVREDSUM_VS_M1_E64 $noreg, %x, $noreg, -1, 6 /* e64 */, 0 /* tu, mu */
+    %z:vr = PseudoVADD_VV_M1 $noreg, %x, $noreg, %vl, 5 /* e32 */, 0 /* tu, mu */
+...

Copy link
Contributor

@michaelmaitland michaelmaitland left a comment

Choose a reason for hiding this comment

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

LGTM

@michaelmaitland
Copy link
Contributor

I think the passthru will need to preserve all elements so I haven't included it.

I am seeing this from the spec:

The other elements in the destination vector register ( 0 < index < VLEN/SEW) are considered the tail and are managed with the current tail agnostic/undisturbed policy.

IIUC, I think this is saying we only need to preserve elements of the passthru depending on tail policy. I think we could maybe treat the destination as a scalar operand if the policy is TA?

@lukel97
Copy link
Contributor Author

lukel97 commented Oct 15, 2024

IIUC, I think this is saying we only need to preserve elements of the passthru depending on tail policy. I think we could maybe treat the destination as a scalar operand if the policy is TA?

If I'm reading this right it shouldn't matter if we mark the destination operand as scalar or not because isVectorOpUsedAsScalarOp is only called on users.

The passthru operand might be checked, but if the policy is TA then I think we still need to preserve it. Because tail agnostic still somewhat defined (it has to be either the same as the passthru or all ones), which is separate from "tail undefined" when we have an undef passthru.

@michaelmaitland
Copy link
Contributor

IIUC, I think this is saying we only need to preserve elements of the passthru depending on tail policy. I think we could maybe treat the destination as a scalar operand if the policy is TA?

If I'm reading this right it shouldn't matter if we mark the destination operand as scalar or not because isVectorOpUsedAsScalarOp is only called on users.

The passthru operand might be checked, but if the policy is TA then I think we still need to preserve it. Because tail agnostic still somewhat defined (it has to be either the same as the passthru or all ones), which is separate from "tail undefined" when we have an undef passthru.

Okay, LGTM.

@lukel97 lukel97 merged commit 043f066 into llvm:main Oct 15, 2024
10 checks passed
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
…2253)

A reduction instruction always has a passthru operand, so the scalar
operand should always be vs1 which is at index 3.

Even though the destination operand is also scalar, I think the passthru
will need to preserve all elements so I haven't included it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants