[llvm-branch-commits] [llvm] [MachinePipeliner] Don't count implicit-def values as register pressure (PR #242474)
Vedant Paranjape via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Nov 17 18:07:29 PST 2026
https://github.com/VedantParanjape updated https://github.com/llvm/llvm-project/pull/242474
>From 9305f3af621370457a9d79cd864e2d54fdc7d3b5 Mon Sep 17 00:00:00 2001
From: Vedant Paranjape <veparanjape at microsoft.com>
Date: Wed, 18 Nov 2026 00:45:26 +0000
Subject: [PATCH 1/2] [ProcessImplicitDefs] Split the analysis from the
transformation (NFC)
Add ImplicitDefInfo, which computes which uses become undef, which
instructions become IMPLICIT_DEFs, and which IMPLICIT_DEFs are erased,
without modifying the function. ProcessImplicitDefs now applies its
results, then erases or moves the IMPLICIT_DEFs bottom-up in each
block. Physical registers are handled as before.
This lets passes that run earlier, such as the MachinePipeliner's
register pressure check, see what ProcessImplicitDefs will do without
duplicating its rules.
---
.../llvm/CodeGen/ProcessImplicitDefs.h | 32 +++++
llvm/lib/CodeGen/ProcessImplicitDefs.cpp | 111 ++++++++++--------
2 files changed, 93 insertions(+), 50 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/ProcessImplicitDefs.h b/llvm/include/llvm/CodeGen/ProcessImplicitDefs.h
index d581ae116b3827..b11f9f8ee25272 100644
--- a/llvm/include/llvm/CodeGen/ProcessImplicitDefs.h
+++ b/llvm/include/llvm/CodeGen/ProcessImplicitDefs.h
@@ -9,10 +9,42 @@
#ifndef LLVM_CODEGEN_PROCESSIMPLICITDEFS_H
#define LLVM_CODEGEN_PROCESSIMPLICITDEFS_H
+#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/CodeGen/MachinePassManager.h"
namespace llvm {
+/// Computes what ProcessImplicitDefs does to virtual registers defined by
+/// IMPLICIT_DEF, without modifying the function:
+/// - Each use of such a register becomes undef, unless its instruction reads
+/// the register through both a tied operand and another operand.
+/// - A copy, INSERT_SUBREG, REG_SEQUENCE or PHI whose uses all become undef
+/// becomes an IMPLICIT_DEF.
+/// - An IMPLICIT_DEF whose uses all become undef is erased.
+class ImplicitDefInfo {
+ SmallPtrSet<const MachineOperand *, 16> UndefUses;
+ SmallPtrSet<const MachineInstr *, 16> NewImplicitDefs;
+ SmallPtrSet<const MachineInstr *, 16> ErasedImplicitDefs;
+
+public:
+ LLVM_ABI explicit ImplicitDefInfo(const MachineFunction &MF);
+
+ /// Returns true if \p MO is a use that becomes undef.
+ bool isUndef(const MachineOperand &MO) const {
+ return UndefUses.contains(&MO);
+ }
+
+ /// Returns true if \p MI becomes an IMPLICIT_DEF.
+ bool becomesImplicitDef(const MachineInstr &MI) const {
+ return NewImplicitDefs.contains(&MI);
+ }
+
+ /// Returns true if \p MI is, or becomes, an IMPLICIT_DEF that is erased.
+ bool isErased(const MachineInstr &MI) const {
+ return ErasedImplicitDefs.contains(&MI);
+ }
+};
+
class ProcessImplicitDefsPass
: public RequiredPassInfoMixin<ProcessImplicitDefsPass> {
public:
diff --git a/llvm/lib/CodeGen/ProcessImplicitDefs.cpp b/llvm/lib/CodeGen/ProcessImplicitDefs.cpp
index 60e8e852cf162f..e4d33799f97f4b 100644
--- a/llvm/lib/CodeGen/ProcessImplicitDefs.cpp
+++ b/llvm/lib/CodeGen/ProcessImplicitDefs.cpp
@@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/ProcessImplicitDefs.h"
-#include "llvm/ADT/SetVector.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
@@ -46,10 +45,7 @@ class ProcessImplicitDefs {
const TargetRegisterInfo *TRI = nullptr;
MachineRegisterInfo *MRI = nullptr;
- SmallSetVector<MachineInstr *, 16> WorkList;
-
- void processImplicitDef(MachineInstr *MI);
- bool canTurnIntoImplicitDef(MachineInstr *MI);
+ void processImplicitDef(MachineInstr *MI, const ImplicitDefInfo &Info);
public:
bool run(MachineFunction &MF);
@@ -68,19 +64,46 @@ void ProcessImplicitDefsLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
MachineFunctionPass::getAnalysisUsage(AU);
}
-bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
- if (!MI->isCopyLike() &&
- !MI->isInsertSubreg() &&
- !MI->isRegSequence() &&
- !MI->isPHI())
- return false;
- for (const MachineOperand &MO : MI->all_uses())
- if (MO.readsReg())
- return false;
- return true;
+static bool canBecomeImplicitDef(const MachineInstr &MI) {
+ return MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
+ MI.isPHI();
}
-void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
+ImplicitDefInfo::ImplicitDefInfo(const MachineFunction &MF) {
+ const MachineRegisterInfo &MRI = MF.getRegInfo();
+ SmallVector<const MachineInstr *> WorkList;
+ for (const MachineBasicBlock &MBB : MF)
+ for (const MachineInstr &MI : MBB)
+ if (MI.isImplicitDef() && MI.getOperand(0).getReg().isVirtual())
+ WorkList.push_back(&MI);
+
+ while (!WorkList.empty()) {
+ const MachineInstr *MI = WorkList.pop_back_val();
+ Register Reg = MI->getOperand(0).getReg();
+ bool AllUsesUndef = true;
+ for (const MachineOperand &MO : MRI.use_nodbg_operands(Reg)) {
+ const MachineInstr &UseMI = *MO.getParent();
+ if (UseMI.hasTiedAndOtherReadOf(Reg, MO.getSubReg())) {
+ AllUsesUndef = false;
+ continue;
+ }
+ UndefUses.insert(&MO);
+ if (canBecomeImplicitDef(UseMI) && !NewImplicitDefs.contains(&UseMI) &&
+ all_of(UseMI.all_uses(), [&](const MachineOperand &Use) {
+ return !Use.readsReg() || UndefUses.contains(&Use);
+ })) {
+ NewImplicitDefs.insert(&UseMI);
+ if (UseMI.getOperand(0).getReg().isVirtual())
+ WorkList.push_back(&UseMI);
+ }
+ }
+ if (AllUsesUndef)
+ ErasedImplicitDefs.insert(MI);
+ }
+}
+
+void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI,
+ const ImplicitDefInfo &Info) {
LLVM_DEBUG(dbgs() << "Processing " << *MI);
Register Reg = MI->getOperand(0).getReg();
// Trim any extra operands.
@@ -90,23 +113,7 @@ void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
MachineBasicBlock *MBB = MI->getParent();
if (Reg.isVirtual()) {
- // For virtual registers, mark all uses as <undef>, and convert users to
- // implicit-def when possible.
- bool AllUsesUndef = true;
- for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
- MachineInstr *UserMI = MO.getParent();
- if (UserMI->hasTiedAndOtherReadOf(Reg, MO.getSubReg())) {
- AllUsesUndef = false;
- continue;
- }
- MO.setIsUndef();
- if (!canTurnIntoImplicitDef(UserMI))
- continue;
- LLVM_DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
- UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
- WorkList.insert(UserMI);
- }
- if (AllUsesUndef) {
+ if (Info.isErased(*MI)) {
MI->eraseFromParent();
return;
}
@@ -201,29 +208,33 @@ bool ProcessImplicitDefs::run(MachineFunction &MF) {
LLVM_DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
<< "********** Function: " << MF.getName() << '\n');
- bool Changed = false;
-
TII = MF.getSubtarget().getInstrInfo();
TRI = MF.getSubtarget().getRegisterInfo();
MRI = &MF.getRegInfo();
- assert(WorkList.empty() && "Inconsistent worklist state");
+ ImplicitDefInfo Info(MF);
for (MachineBasicBlock &MBB : MF) {
- // Scan the basic block for implicit defs.
- for (MachineInstr &MI : MBB)
- if (MI.isImplicitDef())
- WorkList.insert(&MI);
-
- if (WorkList.empty())
- continue;
-
- LLVM_DEBUG(dbgs() << printMBBReference(MBB) << " has " << WorkList.size()
- << " implicit defs.\n");
- Changed = true;
+ for (MachineInstr &MI : MBB) {
+ for (MachineOperand &MO : MI.all_uses())
+ if (Info.isUndef(MO))
+ MO.setIsUndef();
+ if (Info.becomesImplicitDef(MI)) {
+ LLVM_DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << MI);
+ MI.setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
+ }
+ }
+ }
- // Drain the WorkList to recursively process any new implicit defs.
- do processImplicitDef(WorkList.pop_back_val());
- while (!WorkList.empty());
+ // Bottom-up, so that erased IMPLICIT_DEFs don't count towards the search
+ // limit for physical registers.
+ bool Changed = false;
+ for (MachineBasicBlock &MBB : MF) {
+ for (MachineInstr &MI : make_early_inc_range(reverse(MBB))) {
+ if (MI.isImplicitDef()) {
+ processImplicitDef(&MI, Info);
+ Changed = true;
+ }
+ }
}
return Changed;
}
>From 50be8c848cc1a9182d54ce2cd81e7920b663fdec Mon Sep 17 00:00:00 2001
From: Vedant Paranjape <veparanjape at microsoft.com>
Date: Wed, 18 Nov 2026 01:33:22 +0000
Subject: [PATCH 2/2] [MachinePipeliner] Don't count implicit-def values as
register pressure
The register pressure detector runs before ProcessImplicitDefs, so it
counts values that only come from IMPLICIT_DEF as registers, although
they never get one. Later in the pipeline, ProcessImplicitDefs marks
their reads undef and deletes their definitions. MachineLICM hoists
these IMPLICIT_DEFs and the copies of them out of loops, so the
detector counts each one as a register live in the whole loop and
overestimates the pressure. On AMDGPU, which enables the detector by
default, it can then reject every II and leave the loop unpipelined.
Use ImplicitDefInfo to leave out the reads that ProcessImplicitDefs
marks undef and the definitions it deletes. On targets where InitUndef
runs first, keep counting the untied undef reads of instructions with
an early-clobber def, since InitUndef may give them a register.
Fixes #238634
Assisted-By: Opus 5.5
---
llvm/lib/CodeGen/MachinePipeliner.cpp | 42 +++++
...dgpu-pipeline-regpressure-implicit-def.mir | 144 ++++++++++++++++++
...ine-pipeliner-regpressure-implicit-def.mir | 122 +++++++++++++++
3 files changed, 308 insertions(+)
create mode 100644 llvm/test/CodeGen/AMDGPU/swp-amdgpu-pipeline-regpressure-implicit-def.mir
create mode 100644 llvm/test/CodeGen/RISCV/rvv/machine-pipeliner-regpressure-implicit-def.mir
diff --git a/llvm/lib/CodeGen/MachinePipeliner.cpp b/llvm/lib/CodeGen/MachinePipeliner.cpp
index 50c350e06be5ae..0dadedadac2278 100644
--- a/llvm/lib/CodeGen/MachinePipeliner.cpp
+++ b/llvm/lib/CodeGen/MachinePipeliner.cpp
@@ -59,6 +59,7 @@
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/ModuloSchedule.h"
+#include "llvm/CodeGen/ProcessImplicitDefs.h"
#include "llvm/CodeGen/Register.h"
#include "llvm/CodeGen/RegisterClassInfo.h"
#include "llvm/CodeGen/RegisterPressure.h"
@@ -1788,6 +1789,28 @@ class HighRegisterPressureDetector {
MRI.getDefBlock(VRegOrUnit.asVirtualReg()) == OrigMBB;
}
+ // Returns true if \p MO is a use that ProcessImplicitDefs makes undef, or a
+ // def that it erases.
+ bool isUndefOrErased(const MachineOperand &MO,
+ const ImplicitDefInfo &ImpDefs) const {
+ // On targets that require disjoint early-clobber and undef operands,
+ // InitUndef runs first and may give the untied undef uses of an
+ // instruction with an early-clobber def a register, so keep counting them.
+ bool RunsInitUndef = OrigMBB->getParent()
+ ->getSubtarget()
+ .requiresDisjointEarlyClobberAndUndef();
+ auto GetsInitUndef = [&](const MachineOperand &Use) {
+ return RunsInitUndef && !Use.isTied() &&
+ any_of(Use.getParent()->all_defs(), [](const MachineOperand &Def) {
+ return Def.isEarlyClobber();
+ });
+ };
+ if (MO.isUse())
+ return ImpDefs.isUndef(MO) && !GetsInitUndef(MO);
+ return ImpDefs.isErased(*MO.getParent()) &&
+ none_of(MRI.use_nodbg_operands(MO.getReg()), GetsInitUndef);
+ }
+
// Search for live-in variables. They are factored into the register pressure
// from the begining. Live-in variables used by every iteration should be
// considered as alive throughout the loop. For example, the variable `c` in
@@ -2000,6 +2023,25 @@ class HighRegisterPressureDetector {
ROMap[&MI].collect(MI, *TRI, MRI, false, true);
}
+ ImplicitDefInfo ImpDefs(*OrigMBB->getParent());
+ for (MachineInstr &MI : *OrigMBB) {
+ if (MI.isDebugInstr())
+ continue;
+ // Leave out the reads that ProcessImplicitDefs marks undef and the
+ // definitions it deletes. Otherwise, for example, an IMPLICIT_DEF hoisted
+ // out of the loop would count as a register live in the whole loop.
+ auto IsUndef = [&](const VRegMaskOrUnit &RegOrUnit) {
+ return RegOrUnit.VRegOrUnit.isVirtualReg() &&
+ all_of(MI.operands(), [&](const MachineOperand &MO) {
+ return !MO.isReg() ||
+ MO.getReg() != RegOrUnit.VRegOrUnit.asVirtualReg() ||
+ isUndefOrErased(MO, ImpDefs);
+ });
+ };
+ erase_if(ROMap[&MI].Uses, IsUndef);
+ erase_if(ROMap[&MI].Defs, IsUndef);
+ }
+
computeLiveIn();
computePressureSetLimit(RCI);
}
diff --git a/llvm/test/CodeGen/AMDGPU/swp-amdgpu-pipeline-regpressure-implicit-def.mir b/llvm/test/CodeGen/AMDGPU/swp-amdgpu-pipeline-regpressure-implicit-def.mir
new file mode 100644
index 00000000000000..7ec5d3046964d3
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/swp-amdgpu-pipeline-regpressure-implicit-def.mir
@@ -0,0 +1,144 @@
+# RUN: llc -mtriple=amdgpu9.50-amd-amdhsa -run-pass=pipeliner -pass-remarks-analysis=pipeliner %s -o /dev/null 2>&1 | FileCheck %s
+
+# ProcessImplicitDefs, which runs after the pipeliner, marks the reads of values
+# that only come from IMPLICIT_DEF undef and deletes their definitions. Check
+# that the register pressure detector leaves such reads out, and still counts
+# the reads that ProcessImplicitDefs keeps.
+#
+# "amdgpu-num-vgpr"="4" limits both functions to 8 VGPRs.
+
+# In implicit_def_reads, the old operand of each DPP move only comes from an
+# IMPLICIT_DEF: through a COPY hoisted to the preheader, as MachineLICM does,
+# through a COPY in the loop, or directly. Counted as registers, they would
+# need more than 8 VGPRs, and the detector would reject every schedule.
+# CHECK-NOT: Unable to find schedule
+# CHECK: Schedule found with Initiation Interval
+
+# In tied_and_other_read, each DPP add reads its IMPLICIT_DEF value through the
+# tied old operand and through src1. ProcessImplicitDefs keeps these reads, so
+# they need more than 8 VGPRs, and the detector rejects every schedule.
+# CHECK-NOT: Schedule found
+# CHECK: Unable to find schedule
+
+--- |
+ define amdgpu_kernel void @implicit_def_reads() #0 {
+ ret void
+ }
+
+ define amdgpu_kernel void @tied_and_other_read() #0 {
+ ret void
+ }
+
+ attributes #0 = { "amdgpu-num-vgpr"="4" }
+...
+---
+name: implicit_def_reads
+tracksRegLiveness: true
+isSSA: true
+machineFunctionInfo:
+ isEntryFunction: true
+body: |
+ bb.0:
+ successors: %bb.1
+ liveins: $vgpr0, $sgpr0
+
+ %n:sreg_32 = COPY $sgpr0
+ %tid:vgpr_32 = COPY $vgpr0
+ %zero:sreg_32 = S_MOV_B32 0
+ %vzero:vgpr_32 = V_MOV_B32_e32 0, implicit $exec
+ %imp0:sreg_32 = IMPLICIT_DEF
+ %old0:vgpr_32 = COPY %imp0
+ %imp1:sreg_32 = IMPLICIT_DEF
+ %old1:vgpr_32 = COPY %imp1
+ %imp2:sreg_32 = IMPLICIT_DEF
+ %old2:vgpr_32 = COPY %imp2
+ %imp3:sreg_32 = IMPLICIT_DEF
+ %old3:vgpr_32 = COPY %imp3
+ %imp4:sreg_32 = IMPLICIT_DEF
+ %old4:vgpr_32 = COPY %imp4
+ %imp5:sreg_32 = IMPLICIT_DEF
+ %old5:vgpr_32 = COPY %imp5
+ %imp6:sreg_32 = IMPLICIT_DEF
+ %old7:vgpr_32 = IMPLICIT_DEF
+
+ bb.1:
+ successors: %bb.1, %bb.2
+
+ %i:sreg_32 = PHI %zero, %bb.0, %i_next, %bb.1
+ %acc:vgpr_32 = PHI %vzero, %bb.0, %acc_next, %bb.1
+ %addr:vgpr_32 = V_ADD_U32_e32 %i, %tid, implicit $exec
+ %x:vgpr_32 = DS_READ_B32_gfx9 %addr, 0, 0, implicit $exec
+ %d0:vgpr_32 = V_MOV_B32_dpp %old0, %x, 273, 15, 15, 0, implicit $exec
+ %d1:vgpr_32 = V_MOV_B32_dpp %old1, %d0, 273, 15, 15, 0, implicit $exec
+ %d2:vgpr_32 = V_MOV_B32_dpp %old2, %d1, 273, 15, 15, 0, implicit $exec
+ %d3:vgpr_32 = V_MOV_B32_dpp %old3, %d2, 273, 15, 15, 0, implicit $exec
+ %d4:vgpr_32 = V_MOV_B32_dpp %old4, %d3, 273, 15, 15, 0, implicit $exec
+ %d5:vgpr_32 = V_MOV_B32_dpp %old5, %d4, 273, 15, 15, 0, implicit $exec
+ %old6:vgpr_32 = COPY %imp6
+ %d6:vgpr_32 = V_MOV_B32_dpp %old6, %d5, 273, 15, 15, 0, implicit $exec
+ %d7:vgpr_32 = V_MOV_B32_dpp %old7, %d6, 273, 15, 15, 0, implicit $exec
+ %acc_next:vgpr_32 = V_ADD_U32_e32 %acc, %d7, implicit $exec
+ %i_next:sreg_32 = S_ADD_I32 %i, 1, implicit-def dead $scc
+ S_CMP_LT_I32 %i_next, %n, implicit-def $scc
+ S_CBRANCH_SCC1 %bb.1, implicit $scc
+ S_BRANCH %bb.2
+
+ bb.2:
+ S_ENDPGM 0
+...
+---
+name: tied_and_other_read
+tracksRegLiveness: true
+isSSA: true
+machineFunctionInfo:
+ isEntryFunction: true
+body: |
+ bb.0:
+ successors: %bb.1
+ liveins: $vgpr0, $sgpr0
+
+ %n:sreg_32 = COPY $sgpr0
+ %tid:vgpr_32 = COPY $vgpr0
+ %zero:sreg_32 = S_MOV_B32 0
+ %vzero:vgpr_32 = V_MOV_B32_e32 0, implicit $exec
+ %imp0:sreg_32 = IMPLICIT_DEF
+ %old0:vgpr_32 = COPY %imp0
+ %imp1:sreg_32 = IMPLICIT_DEF
+ %old1:vgpr_32 = COPY %imp1
+ %imp2:sreg_32 = IMPLICIT_DEF
+ %old2:vgpr_32 = COPY %imp2
+ %imp3:sreg_32 = IMPLICIT_DEF
+ %old3:vgpr_32 = COPY %imp3
+ %imp4:sreg_32 = IMPLICIT_DEF
+ %old4:vgpr_32 = COPY %imp4
+ %imp5:sreg_32 = IMPLICIT_DEF
+ %old5:vgpr_32 = COPY %imp5
+ %imp6:sreg_32 = IMPLICIT_DEF
+ %old6:vgpr_32 = COPY %imp6
+ %imp7:sreg_32 = IMPLICIT_DEF
+ %old7:vgpr_32 = COPY %imp7
+
+ bb.1:
+ successors: %bb.1, %bb.2
+
+ %i:sreg_32 = PHI %zero, %bb.0, %i_next, %bb.1
+ %acc:vgpr_32 = PHI %vzero, %bb.0, %acc_next, %bb.1
+ %addr:vgpr_32 = V_ADD_U32_e32 %i, %tid, implicit $exec
+ %x:vgpr_32 = DS_READ_B32_gfx9 %addr, 0, 0, implicit $exec
+ %d0:vgpr_32 = V_ADD_U32_dpp %old0, %x, %old0, 273, 15, 15, 0, implicit $exec
+ %d1:vgpr_32 = V_ADD_U32_dpp %old1, %d0, %old1, 273, 15, 15, 0, implicit $exec
+ %d2:vgpr_32 = V_ADD_U32_dpp %old2, %d1, %old2, 273, 15, 15, 0, implicit $exec
+ %d3:vgpr_32 = V_ADD_U32_dpp %old3, %d2, %old3, 273, 15, 15, 0, implicit $exec
+ %d4:vgpr_32 = V_ADD_U32_dpp %old4, %d3, %old4, 273, 15, 15, 0, implicit $exec
+ %d5:vgpr_32 = V_ADD_U32_dpp %old5, %d4, %old5, 273, 15, 15, 0, implicit $exec
+ %d6:vgpr_32 = V_ADD_U32_dpp %old6, %d5, %old6, 273, 15, 15, 0, implicit $exec
+ %d7:vgpr_32 = V_ADD_U32_dpp %old7, %d6, %old7, 273, 15, 15, 0, implicit $exec
+ %acc_next:vgpr_32 = V_ADD_U32_e32 %acc, %d7, implicit $exec
+ %i_next:sreg_32 = S_ADD_I32 %i, 1, implicit-def dead $scc
+ S_CMP_LT_I32 %i_next, %n, implicit-def $scc
+ S_CBRANCH_SCC1 %bb.1, implicit $scc
+ S_BRANCH %bb.2
+
+ bb.2:
+ S_ENDPGM 0
+...
diff --git a/llvm/test/CodeGen/RISCV/rvv/machine-pipeliner-regpressure-implicit-def.mir b/llvm/test/CodeGen/RISCV/rvv/machine-pipeliner-regpressure-implicit-def.mir
new file mode 100644
index 00000000000000..bcca7728aed51b
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/rvv/machine-pipeliner-regpressure-implicit-def.mir
@@ -0,0 +1,122 @@
+# RUN: llc -mtriple=riscv64 -mcpu=sifive-p670 -run-pass=pipeliner \
+# RUN: -pipeliner-register-pressure -pipeliner-register-pressure-margin=75 \
+# RUN: -pass-remarks-analysis=pipeliner %s -o /dev/null 2>&1 | FileCheck %s
+
+# The register pressure detector leaves out the reads of IMPLICIT_DEF values
+# that ProcessImplicitDefs, which runs after the pipeliner, marks undef. On
+# RISC-V, InitUndef runs before ProcessImplicitDefs. In an instruction with an
+# early-clobber def, it replaces each untied read of an IMPLICIT_DEF with a
+# read of a new INIT_UNDEF register. Check that the detector still counts these
+# reads.
+#
+# -pipeliner-register-pressure-margin=75 leaves the detector a quarter of each
+# register pressure limit, which is 8 vector registers.
+
+# In vadd_reads, each vadd.vv reads an IMPLICIT_DEF hoisted to the preheader.
+# ProcessImplicitDefs marks these reads undef. Counted as registers, they would
+# need more than 8 vector registers, and the detector would reject every
+# schedule.
+# CHECK-NOT: Unable to find schedule
+# CHECK: Schedule found with Initiation Interval
+
+# In vrgather_reads, each vrgather.vv has an early-clobber def and reads an
+# IMPLICIT_DEF in the same way. InitUndef gives these reads registers, so the
+# detector counts them and rejects every schedule.
+# CHECK-NOT: Schedule found
+# CHECK: Unable to find schedule
+
+--- |
+ define void @vadd_reads() {
+ ret void
+ }
+
+ define void @vrgather_reads() {
+ ret void
+ }
+...
+---
+name: vadd_reads
+tracksRegLiveness: true
+body: |
+ bb.0:
+ successors: %bb.1
+ liveins: $x10, $x11, $v8
+
+ %base:gpr = COPY $x10
+ %end:gpr = COPY $x11
+ %init:vr = COPY $v8
+ %u0:vr = IMPLICIT_DEF
+ %u1:vr = IMPLICIT_DEF
+ %u2:vr = IMPLICIT_DEF
+ %u3:vr = IMPLICIT_DEF
+ %u4:vr = IMPLICIT_DEF
+ %u5:vr = IMPLICIT_DEF
+ %u6:vr = IMPLICIT_DEF
+ %u7:vr = IMPLICIT_DEF
+
+ bb.1:
+ successors: %bb.1, %bb.2
+
+ %p:gpr = PHI %base, %bb.0, %p_next, %bb.1
+ %acc:vr = PHI %init, %bb.0, %acc_next, %bb.1
+ %x:vr = PseudoVLE32_V_M1 $noreg, %p, 4, 5 /* e32 */, 3 /* ta, ma */ :: (load (s128))
+ %d0:vr = PseudoVADD_VV_M1 $noreg, %u0, %x, 4, 5 /* e32 */, 3 /* ta, ma */
+ %d1:vr = PseudoVADD_VV_M1 $noreg, %u1, %d0, 4, 5 /* e32 */, 3 /* ta, ma */
+ %d2:vr = PseudoVADD_VV_M1 $noreg, %u2, %d1, 4, 5 /* e32 */, 3 /* ta, ma */
+ %d3:vr = PseudoVADD_VV_M1 $noreg, %u3, %d2, 4, 5 /* e32 */, 3 /* ta, ma */
+ %d4:vr = PseudoVADD_VV_M1 $noreg, %u4, %d3, 4, 5 /* e32 */, 3 /* ta, ma */
+ %d5:vr = PseudoVADD_VV_M1 $noreg, %u5, %d4, 4, 5 /* e32 */, 3 /* ta, ma */
+ %d6:vr = PseudoVADD_VV_M1 $noreg, %u6, %d5, 4, 5 /* e32 */, 3 /* ta, ma */
+ %d7:vr = PseudoVADD_VV_M1 $noreg, %u7, %d6, 4, 5 /* e32 */, 3 /* ta, ma */
+ %acc_next:vr = PseudoVADD_VV_M1 $noreg, %acc, %d7, 4, 5 /* e32 */, 3 /* ta, ma */
+ %p_next:gpr = ADDI %p, 16
+ BNE %p_next, %end, %bb.1
+ PseudoBR %bb.2
+
+ bb.2:
+ $v8 = COPY %acc_next
+ PseudoRET implicit $v8
+...
+---
+name: vrgather_reads
+tracksRegLiveness: true
+body: |
+ bb.0:
+ successors: %bb.1
+ liveins: $x10, $x11, $v8
+
+ %base:gpr = COPY $x10
+ %end:gpr = COPY $x11
+ %init:vr = COPY $v8
+ %u0:vr = IMPLICIT_DEF
+ %u1:vr = IMPLICIT_DEF
+ %u2:vr = IMPLICIT_DEF
+ %u3:vr = IMPLICIT_DEF
+ %u4:vr = IMPLICIT_DEF
+ %u5:vr = IMPLICIT_DEF
+ %u6:vr = IMPLICIT_DEF
+ %u7:vr = IMPLICIT_DEF
+
+ bb.1:
+ successors: %bb.1, %bb.2
+
+ %p:gpr = PHI %base, %bb.0, %p_next, %bb.1
+ %acc:vr = PHI %init, %bb.0, %acc_next, %bb.1
+ %x:vr = PseudoVLE32_V_M1 $noreg, %p, 4, 5 /* e32 */, 3 /* ta, ma */ :: (load (s128))
+ early-clobber %d0:vr = PseudoVRGATHER_VV_M1_E32 $noreg, %u0, %x, 4, 5 /* e32 */, 3 /* ta, ma */
+ early-clobber %d1:vr = PseudoVRGATHER_VV_M1_E32 $noreg, %u1, %d0, 4, 5 /* e32 */, 3 /* ta, ma */
+ early-clobber %d2:vr = PseudoVRGATHER_VV_M1_E32 $noreg, %u2, %d1, 4, 5 /* e32 */, 3 /* ta, ma */
+ early-clobber %d3:vr = PseudoVRGATHER_VV_M1_E32 $noreg, %u3, %d2, 4, 5 /* e32 */, 3 /* ta, ma */
+ early-clobber %d4:vr = PseudoVRGATHER_VV_M1_E32 $noreg, %u4, %d3, 4, 5 /* e32 */, 3 /* ta, ma */
+ early-clobber %d5:vr = PseudoVRGATHER_VV_M1_E32 $noreg, %u5, %d4, 4, 5 /* e32 */, 3 /* ta, ma */
+ early-clobber %d6:vr = PseudoVRGATHER_VV_M1_E32 $noreg, %u6, %d5, 4, 5 /* e32 */, 3 /* ta, ma */
+ early-clobber %d7:vr = PseudoVRGATHER_VV_M1_E32 $noreg, %u7, %d6, 4, 5 /* e32 */, 3 /* ta, ma */
+ %acc_next:vr = PseudoVADD_VV_M1 $noreg, %acc, %d7, 4, 5 /* e32 */, 3 /* ta, ma */
+ %p_next:gpr = ADDI %p, 16
+ BNE %p_next, %end, %bb.1
+ PseudoBR %bb.2
+
+ bb.2:
+ $v8 = COPY %acc_next
+ PseudoRET implicit $v8
+...
More information about the llvm-branch-commits
mailing list