Skip to content

[RISCV][VLOpt] Minor worklist invariant cleanup [NFC] #123989

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

Merged
merged 1 commit into from
Jan 22, 2025

Conversation

preames
Copy link
Collaborator

@preames preames commented Jan 22, 2025

In retrospect, this probably should have been rolled into #123973. It seemed more involved when I first decided to split. :)

In retrospect, this probably should have been rolled into llvm#123973.  It seemed
more involved when I first decided to split.  :)
@llvmbot
Copy link
Member

llvmbot commented Jan 22, 2025

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

Author: Philip Reames (preames)

Changes

In retrospect, this probably should have been rolled into #123973. It seemed more involved when I first decided to split. :)


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

1 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp (+7-8)
diff --git a/llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp b/llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp
index 66d26bf5b11e2d..529bffcae1319e 100644
--- a/llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp
+++ b/llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp
@@ -1189,6 +1189,10 @@ bool RISCVVLOptimizer::isCandidate(const MachineInstr &MI) const {
     return false;
   }
 
+  assert(MI.getOperand(0).isReg() &&
+         isVectorRegClass(MI.getOperand(0).getReg(), MRI) &&
+         "All supported instructions produce a vector register result");
+
   LLVM_DEBUG(dbgs() << "Found a candidate for VL reduction: " << MI << "\n");
   return true;
 }
@@ -1295,9 +1299,6 @@ std::optional<MachineOperand> RISCVVLOptimizer::checkUsers(MachineInstr &MI) {
 bool RISCVVLOptimizer::tryReduceVL(MachineInstr &MI) {
   LLVM_DEBUG(dbgs() << "Trying to reduce VL for " << MI << "\n");
 
-  if (!isVectorRegClass(MI.getOperand(0).getReg(), MRI))
-    return false;
-
   auto CommonVL = checkUsers(MI);
   if (!CommonVL)
     return false;
@@ -1347,14 +1348,11 @@ bool RISCVVLOptimizer::runOnMachineFunction(MachineFunction &MF) {
   auto PushOperands = [this, &Worklist](MachineInstr &MI,
                                         bool IgnoreSameBlock) {
     for (auto &Op : MI.operands()) {
-      if (!Op.isReg() || !Op.isUse() || !Op.getReg().isVirtual())
-        continue;
-
-      if (!isVectorRegClass(Op.getReg(), MRI))
+      if (!Op.isReg() || !Op.isUse() || !Op.getReg().isVirtual() ||
+          !isVectorRegClass(Op.getReg(), MRI))
         continue;
 
       MachineInstr *DefMI = MRI->getVRegDef(Op.getReg());
-
       if (!isCandidate(*DefMI))
         continue;
 
@@ -1388,6 +1386,7 @@ bool RISCVVLOptimizer::runOnMachineFunction(MachineFunction &MF) {
   while (!Worklist.empty()) {
     assert(MadeChange);
     MachineInstr &MI = *Worklist.pop_back_val();
+    assert(isCandidate(MI));
     if (!tryReduceVL(MI))
       continue;
     PushOperands(MI, /*IgnoreSameBlock*/ false);

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 with minor point.

@@ -1388,6 +1386,7 @@ bool RISCVVLOptimizer::runOnMachineFunction(MachineFunction &MF) {
while (!Worklist.empty()) {
assert(MadeChange);
MachineInstr &MI = *Worklist.pop_back_val();
assert(isCandidate(MI));
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this assert redundant? isCandidate explicitly blocks insertion to the Worklist in PushOperands. That being said, isCandidate is cheap, so I don't have a strong objection.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can drop it if you want. Personally, I find it helpful to have the assertion at the point of extraction from a worklist. This is particular true when there's more than one insert point though - which doesn't apply here. Happy to defer to your preference.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can keep it. It is cheap.

@preames preames merged commit 27ccc99 into llvm:main Jan 22, 2025
10 checks passed
@preames preames deleted the pr-riscv-vlopt-worklist-cleanup branch January 22, 2025 22:42
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.

4 participants