[llvm] [LLVM][SVE] Add MachineInst pass to coalesce PTRUE instructions. (PR #204820)

Paul Walker via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 10 04:00:49 PDT 2026


================
@@ -0,0 +1,270 @@
+//===- AArch64PTrueCoalescing.cpp - Coalesce SVE PTRUEs ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass coalesces compatible all-active SVE PTRUE instructions.
+//
+// Consider two all-active PTRUE instructions X and Y with element sizes XSize
+// and YSize. If X dominates Y and XSize <= YSize, then every predicate bit that
+// Y sets is also set by X. In that case, uses of Y can be redirected to X as
+// long as each user of Y only reads predicate bits at YSize granularity or
+// larger.
+//
+// If the dominating PTRUE has a larger element size, we can coalesce the pair
+// by changing the dominating PTRUE to the smaller element size, provided that
+// all of its existing users are also safe with that granularity.
+//
+//===----------------------------------------------------------------------===//
+
+#include "AArch64.h"
+#include "AArch64InstrInfo.h"
+#include "AArch64Subtarget.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/CodeGen/MachineDominators.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
+#include "llvm/InitializePasses.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Debug.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "aarch64-ptrue-coalesce"
+
+static cl::opt<bool> EnablePTrueCoalescing(
+    "aarch64-enable-ptrue-coalescing", cl::init(false), cl::Hidden,
+    cl::desc("Enable coalescing of compatible AArch64 SVE PTRUE instructions"));
+
+namespace {
+
+class AArch64PTrueCoalescingImpl {
+  const AArch64InstrInfo *TII = nullptr;
+  MachineRegisterInfo *MRI = nullptr;
+  MachineDominatorTree *MDT = nullptr;
+
+public:
+  explicit AArch64PTrueCoalescingImpl(MachineDominatorTree &MDT) : MDT(&MDT) {}
+
+  bool run(MachineFunction &MF);
+
+private:
+  struct PredicateInfo {
+    // Instruction that created the predicate.
+    MachineInstr *MI = nullptr;
+    // Element size of the MI.
+    unsigned ElementSize = AArch64::ElementSizeNone;
+    // Smallest element size of all instructions that use the predicate.
+    unsigned SmallestUsedElementSize = AArch64::ElementSizeNone;
+
+    bool isValid() const {
+      return MI && ElementSize != AArch64::ElementSizeNone &&
+             SmallestUsedElementSize != AArch64::ElementSizeNone;
+    }
+
+    void invalidate() {
+      assert(isValid());
+      MI = nullptr;
+    }
+  };
+
+  std::optional<PredicateInfo> createPredicateInfo(MachineInstr &MI) const {
+    // TODO: Extend support beyond "PTRUE all"?
+    if (!isPTrueOpcode(MI.getOpcode()) || MI.getOperand(1).getImm() != 31)
+      return std::nullopt;
+
+    Register Pred = MI.getOperand(0).getReg();
+    unsigned SmallestUsedElementSize = getSmallestElementSizeInUse(Pred);
+    unsigned ElementSize = TII->getElementSizeForOpcode(MI.getOpcode());
+
+    if (ElementSize == AArch64::ElementSizeNone ||
+        SmallestUsedElementSize == AArch64::ElementSizeNone)
+      return std::nullopt;
+
+    return PredicateInfo{&MI, ElementSize, SmallestUsedElementSize};
+  }
+
+  // Return the smallest element size of all instructions that use Reg, or
+  // AArch64::ElementSizeNone when unknown.
+  unsigned getSmallestElementSizeInUse(Register Reg) const;
+
+  // Try to replace uses of CanPred with DomPred. In some cases that means
+  // modifying DomPred to support smaller element types.
+  bool tryCoalesce(PredicateInfo &DomPred, PredicateInfo &CanPred) const;
+};
+
+class AArch64PTrueCoalescingLegacy : public MachineFunctionPass {
+public:
+  static char ID;
+
+  AArch64PTrueCoalescingLegacy() : MachineFunctionPass(ID) {}
+
+  bool runOnMachineFunction(MachineFunction &MF) override;
+
+  StringRef getPassName() const override { return "AArch64 PTRUE Coalescing"; }
+
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.setPreservesCFG();
+    AU.addRequired<MachineDominatorTreeWrapperPass>();
+    AU.addPreserved<MachineDominatorTreeWrapperPass>();
+    MachineFunctionPass::getAnalysisUsage(AU);
+  }
+};
+
+char AArch64PTrueCoalescingLegacy::ID = 0;
+
+} // end anonymous namespace
+
+INITIALIZE_PASS_BEGIN(AArch64PTrueCoalescingLegacy, DEBUG_TYPE,
+                      "AArch64 PTRUE Coalescing", false, false)
+INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
+INITIALIZE_PASS_END(AArch64PTrueCoalescingLegacy, DEBUG_TYPE,
+                    "AArch64 PTRUE Coalescing", false, false)
+
+unsigned
+AArch64PTrueCoalescingImpl::getSmallestElementSizeInUse(Register Reg) const {
+  // SSA form only applies to virtual registers.
+  if (!Reg.isVirtual())
+    return AArch64::ElementSizeNone;
+
+  unsigned SmallestElementSize = AArch64::ElementSizeNone;
+
+  for (MachineOperand &UseMO : MRI->use_nodbg_operands(Reg)) {
+    if (UseMO.getSubReg())
+      return AArch64::ElementSizeNone;
+
+    MachineInstr *UseMI = UseMO.getParent();
+
+    unsigned ElementSize = TII->getElementSizeForOpcode(UseMI->getOpcode());
+    if (ElementSize == AArch64::ElementSizeNone)
+      return AArch64::ElementSizeNone;
+
+    if (SmallestElementSize == AArch64::ElementSizeNone ||
+        SmallestElementSize > ElementSize)
+      SmallestElementSize = ElementSize;
+  }
+
+  return SmallestElementSize;
+}
+
+bool AArch64PTrueCoalescingImpl::tryCoalesce(PredicateInfo &DomPI,
+                                             PredicateInfo &CanPI) const {
+  assert(DomPI.isValid() && CanPI.isValid());
+  MachineInstr *DomMI = DomPI.MI;
+  MachineInstr *CanMI = CanPI.MI;
+
+  if (DomMI == CanMI || !MDT->dominates(DomMI, CanMI))
+    return false;
+
+  // A predicate's observable shape is the larger of the element size of the
+  // instruction writing the predicate and the one reading it. First check if
+  // DomPI can replace CanPI as-is for CanPI's users. If not, try changing DomPI
+  // to CanPI's element size, but only if DomPI's existing users would observe
+  // the same shape after that change.
+
+  bool MutateDomPTrue = false;
+  if (std::max(CanPI.ElementSize, CanPI.SmallestUsedElementSize) !=
+      std::max(DomPI.ElementSize, CanPI.SmallestUsedElementSize)) {
+    if (std::max(CanPI.ElementSize, DomPI.SmallestUsedElementSize) !=
+        std::max(DomPI.ElementSize, DomPI.SmallestUsedElementSize))
+      return false;
+
+    MutateDomPTrue = true;
+  }
+
+  Register DomReg = DomMI->getOperand(0).getReg();
+  Register CanReg = CanMI->getOperand(0).getReg();
+  if (!MRI->constrainRegClass(DomReg, MRI->getRegClass(CanReg)))
+    return false;
----------------
paulwalker-arm wrote:

Thanks for the tip, test added.

https://github.com/llvm/llvm-project/pull/204820


More information about the llvm-commits mailing list