Skip to content

[AMDGPU][NewPM] Port SIOptimizeVGPRLiveRange pass to NPM. #117686

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 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions llvm/lib/Target/AMDGPU/AMDGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ FunctionPass *createSILoadStoreOptimizerLegacyPass();
FunctionPass *createSIWholeQuadModePass();
FunctionPass *createSIFixControlFlowLiveIntervalsPass();
FunctionPass *createSIOptimizeExecMaskingPreRAPass();
FunctionPass *createSIOptimizeVGPRLiveRangePass();
FunctionPass *createSIOptimizeVGPRLiveRangeLegacyPass();
FunctionPass *createSIFixSGPRCopiesLegacyPass();
FunctionPass *createLowerWWMCopiesPass();
FunctionPass *createSIMemoryLegalizerPass();
Expand Down Expand Up @@ -361,8 +361,8 @@ struct AMDGPUUnifyMetadataPass : PassInfoMixin<AMDGPUUnifyMetadataPass> {
void initializeSIOptimizeExecMaskingPreRAPass(PassRegistry&);
extern char &SIOptimizeExecMaskingPreRAID;

void initializeSIOptimizeVGPRLiveRangePass(PassRegistry &);
extern char &SIOptimizeVGPRLiveRangeID;
void initializeSIOptimizeVGPRLiveRangeLegacyPass(PassRegistry &);
extern char &SIOptimizeVGPRLiveRangeLegacyID;

void initializeAMDGPUAnnotateUniformValuesLegacyPass(PassRegistry &);
extern char &AMDGPUAnnotateUniformValuesLegacyPassID;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ MACHINE_FUNCTION_PASS("si-fold-operands", SIFoldOperandsPass());
MACHINE_FUNCTION_PASS("gcn-dpp-combine", GCNDPPCombinePass())
MACHINE_FUNCTION_PASS("si-load-store-opt", SILoadStoreOptimizerPass())
MACHINE_FUNCTION_PASS("si-lower-sgpr-spills", SILowerSGPRSpillsPass())
MACHINE_FUNCTION_PASS("si-opt-vgpr-liverange", SIOptimizeVGPRLiveRangePass())
MACHINE_FUNCTION_PASS("si-peephole-sdwa", SIPeepholeSDWAPass())
MACHINE_FUNCTION_PASS("si-pre-allocate-wwm-regs", SIPreAllocateWWMRegsPass())
MACHINE_FUNCTION_PASS("si-shrink-instructions", SIShrinkInstructionsPass())
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "SILowerSGPRSpills.h"
#include "SIMachineFunctionInfo.h"
#include "SIMachineScheduler.h"
#include "SIOptimizeVGPRLiveRange.h"
#include "SIPeepholeSDWA.h"
#include "SIPreAllocateWWMRegs.h"
#include "SIShrinkInstructions.h"
Expand Down Expand Up @@ -473,7 +474,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
initializeSIPeepholeSDWALegacyPass(*PR);
initializeSIShrinkInstructionsLegacyPass(*PR);
initializeSIOptimizeExecMaskingPreRAPass(*PR);
initializeSIOptimizeVGPRLiveRangePass(*PR);
initializeSIOptimizeVGPRLiveRangeLegacyPass(*PR);
initializeSILoadStoreOptimizerLegacyPass(*PR);
initializeAMDGPUCtorDtorLoweringLegacyPass(*PR);
initializeAMDGPUAlwaysInlinePass(*PR);
Expand Down Expand Up @@ -1423,7 +1424,7 @@ void GCNPassConfig::addOptimizedRegAlloc() {
// the register in LiveVariables, this would trigger a failure in verifier,
// we should fix it and enable the verifier.
if (OptVGPRLiveRange)
insertPass(&LiveVariablesID, &SIOptimizeVGPRLiveRangeID);
insertPass(&LiveVariablesID, &SIOptimizeVGPRLiveRangeLegacyID);

// This must be run immediately after phi elimination and before
// TwoAddressInstructions, otherwise the processing of the tied operand of
Expand Down
65 changes: 49 additions & 16 deletions llvm/lib/Target/AMDGPU/SIOptimizeVGPRLiveRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
//
//===----------------------------------------------------------------------===//

#include "SIOptimizeVGPRLiveRange.h"
#include "AMDGPU.h"
#include "GCNSubtarget.h"
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Expand All @@ -86,7 +87,7 @@ using namespace llvm;

namespace {

class SIOptimizeVGPRLiveRange : public MachineFunctionPass {
class SIOptimizeVGPRLiveRange {
private:
const SIRegisterInfo *TRI = nullptr;
const SIInstrInfo *TII = nullptr;
Expand All @@ -96,7 +97,10 @@ class SIOptimizeVGPRLiveRange : public MachineFunctionPass {
MachineRegisterInfo *MRI = nullptr;

public:
static char ID;
SIOptimizeVGPRLiveRange(LiveVariables *LV, MachineDominatorTree *MDT,
MachineLoopInfo *Loops)
: LV(LV), MDT(MDT), Loops(Loops) {}
bool run(MachineFunction &MF);

MachineBasicBlock *getElseTarget(MachineBasicBlock *MBB) const;

Expand Down Expand Up @@ -136,8 +140,13 @@ class SIOptimizeVGPRLiveRange : public MachineFunctionPass {
Register Reg, MachineBasicBlock *LoopHeader,
SmallSetVector<MachineBasicBlock *, 2> &LoopBlocks,
SmallVectorImpl<MachineInstr *> &Instructions) const;
};

SIOptimizeVGPRLiveRange() : MachineFunctionPass(ID) {}
class SIOptimizeVGPRLiveRangeLegacy : public MachineFunctionPass {
public:
static char ID;

SIOptimizeVGPRLiveRangeLegacy() : MachineFunctionPass(ID) {}

bool runOnMachineFunction(MachineFunction &MF) override;

Expand Down Expand Up @@ -611,35 +620,59 @@ void SIOptimizeVGPRLiveRange::optimizeWaterfallLiveRange(
}
}

char SIOptimizeVGPRLiveRange::ID = 0;
char SIOptimizeVGPRLiveRangeLegacy::ID = 0;

INITIALIZE_PASS_BEGIN(SIOptimizeVGPRLiveRange, DEBUG_TYPE,
INITIALIZE_PASS_BEGIN(SIOptimizeVGPRLiveRangeLegacy, DEBUG_TYPE,
"SI Optimize VGPR LiveRange", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(LiveVariablesWrapperPass)
INITIALIZE_PASS_END(SIOptimizeVGPRLiveRange, DEBUG_TYPE,
INITIALIZE_PASS_END(SIOptimizeVGPRLiveRangeLegacy, DEBUG_TYPE,
"SI Optimize VGPR LiveRange", false, false)

char &llvm::SIOptimizeVGPRLiveRangeID = SIOptimizeVGPRLiveRange::ID;
char &llvm::SIOptimizeVGPRLiveRangeLegacyID = SIOptimizeVGPRLiveRangeLegacy::ID;

FunctionPass *llvm::createSIOptimizeVGPRLiveRangeLegacyPass() {
return new SIOptimizeVGPRLiveRangeLegacy();
}

bool SIOptimizeVGPRLiveRangeLegacy::runOnMachineFunction(MachineFunction &MF) {
if (skipFunction(MF.getFunction()))
return false;

FunctionPass *llvm::createSIOptimizeVGPRLiveRangePass() {
return new SIOptimizeVGPRLiveRange();
LiveVariables *LV = &getAnalysis<LiveVariablesWrapperPass>().getLV();
MachineDominatorTree *MDT =
&getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
MachineLoopInfo *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
return SIOptimizeVGPRLiveRange(LV, MDT, Loops).run(MF);
}

bool SIOptimizeVGPRLiveRange::runOnMachineFunction(MachineFunction &MF) {
PreservedAnalyses
SIOptimizeVGPRLiveRangePass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
MFPropsModifier _(*this, MF);
LiveVariables *LV = &MFAM.getResult<LiveVariablesAnalysis>(MF);
MachineDominatorTree *MDT = &MFAM.getResult<MachineDominatorTreeAnalysis>(MF);
MachineLoopInfo *Loops = &MFAM.getResult<MachineLoopAnalysis>(MF);

bool Changed = SIOptimizeVGPRLiveRange(LV, MDT, Loops).run(MF);
if (!Changed)
return PreservedAnalyses::all();

auto PA = getMachineFunctionPassPreservedAnalyses();
PA.preserve<LiveVariablesAnalysis>();
PA.preserve<DominatorTreeAnalysis>();
PA.preserve<MachineLoopAnalysis>();
PA.preserveSet<CFGAnalyses>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should fix old pm in follow up

return PA;
}

bool SIOptimizeVGPRLiveRange::run(MachineFunction &MF) {
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
TII = ST.getInstrInfo();
TRI = &TII->getRegisterInfo();
MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
LV = &getAnalysis<LiveVariablesWrapperPass>().getLV();
MRI = &MF.getRegInfo();

if (skipFunction(MF.getFunction()))
return false;

bool MadeChange = false;

// TODO: we need to think about the order of visiting the blocks to get
Expand Down
33 changes: 33 additions & 0 deletions llvm/lib/Target/AMDGPU/SIOptimizeVGPRLiveRange.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===- SIOptimizeVGPRLiveRange.h --------------------------------*- 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_AMDGPU_SIOPTIMIZEVGPRLIVERANGE_H
#define LLVM_LIB_TARGET_AMDGPU_SIOPTIMIZEVGPRLIVERANGE_H

#include "llvm/CodeGen/MachinePassManager.h"

namespace llvm {
class SIOptimizeVGPRLiveRangePass
: public PassInfoMixin<SIOptimizeVGPRLiveRangePass> {
public:
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);

MachineFunctionProperties getRequiredProperties() const {
return MachineFunctionProperties().set(
MachineFunctionProperties::Property::IsSSA);
}

MachineFunctionProperties getClearedProperties() const {
return MachineFunctionProperties().set(
MachineFunctionProperties::Property::NoPHIs);
}
};
} // namespace llvm

#endif // LLVM_LIB_TARGET_AMDGPU_SIOPTIMIZEVGPRLIVERANGE_H
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a -verify-machineinstrs -run-pass=si-opt-vgpr-liverange -o - %s | FileCheck %s
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a -passes=si-opt-vgpr-liverange -o - %s | FileCheck %s
#
# This is a very rare case which comes from llvm-reduce. The SI_IF/SI_ELSE usage is quite different from normal.
#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 2
# RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -verify-machineinstrs -run-pass=si-opt-vgpr-liverange -o - %s | FileCheck -check-prefixes=CHECK %s
# RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -passes=si-opt-vgpr-liverange -o - %s | FileCheck -check-prefixes=CHECK %s

# Tests a case that used to assert in SIOptimizeVGPRLiveRange when trying to optimize %3 which still appears
# (though in an undef operand) in the REG_SEQUENCE of the "endif block". This undef pattern was caused by
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -run-pass=si-opt-vgpr-liverange %s -o - | FileCheck -check-prefix=GCN %s
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -passes=si-opt-vgpr-liverange %s -o - | FileCheck -check-prefix=GCN %s

# SIOptimizeVGPRLiveRange shouldn't try to modify use of %5 in DBG_VALUE_LIST

Expand Down