Skip to content

Commit e1ee07d

Browse files
authored
[AMDGPU][NewPM] Port SIPeepholeSDWA pass to NPM (#107049)
1 parent 7e0008d commit e1ee07d

File tree

8 files changed

+77
-22
lines changed

8 files changed

+77
-22
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void initializeAMDGPURegBankSelectPass(PassRegistry &);
3636
FunctionPass *createGCNDPPCombinePass();
3737
FunctionPass *createSIAnnotateControlFlowLegacyPass();
3838
FunctionPass *createSIFoldOperandsLegacyPass();
39-
FunctionPass *createSIPeepholeSDWAPass();
39+
FunctionPass *createSIPeepholeSDWALegacyPass();
4040
FunctionPass *createSILowerI1CopiesLegacyPass();
4141
FunctionPass *createAMDGPUGlobalISelDivergenceLoweringPass();
4242
FunctionPass *createSIShrinkInstructionsLegacyPass();
@@ -163,8 +163,8 @@ extern char &GCNDPPCombineLegacyID;
163163
void initializeSIFoldOperandsLegacyPass(PassRegistry &);
164164
extern char &SIFoldOperandsLegacyID;
165165

166-
void initializeSIPeepholeSDWAPass(PassRegistry &);
167-
extern char &SIPeepholeSDWAID;
166+
void initializeSIPeepholeSDWALegacyPass(PassRegistry &);
167+
extern char &SIPeepholeSDWALegacyID;
168168

169169
void initializeSIShrinkInstructionsLegacyPass(PassRegistry &);
170170
extern char &SIShrinkInstructionsLegacyID;

llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,6 @@ MACHINE_FUNCTION_PASS("si-i1-copies", SILowerI1CopiesPass())
100100
MACHINE_FUNCTION_PASS("si-fold-operands", SIFoldOperandsPass());
101101
MACHINE_FUNCTION_PASS("gcn-dpp-combine", GCNDPPCombinePass())
102102
MACHINE_FUNCTION_PASS("si-load-store-opt", SILoadStoreOptimizerPass())
103+
MACHINE_FUNCTION_PASS("si-peephole-sdwa", SIPeepholeSDWAPass())
103104
MACHINE_FUNCTION_PASS("si-shrink-instructions", SIShrinkInstructionsPass())
104105
#undef MACHINE_FUNCTION_PASS

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "SILoadStoreOptimizer.h"
4040
#include "SIMachineFunctionInfo.h"
4141
#include "SIMachineScheduler.h"
42+
#include "SIPeepholeSDWA.h"
4243
#include "SIShrinkInstructions.h"
4344
#include "TargetInfo/AMDGPUTargetInfo.h"
4445
#include "Utils/AMDGPUBaseInfo.h"
@@ -415,7 +416,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
415416
initializeSIFixSGPRCopiesLegacyPass(*PR);
416417
initializeSIFixVGPRCopiesPass(*PR);
417418
initializeSIFoldOperandsLegacyPass(*PR);
418-
initializeSIPeepholeSDWAPass(*PR);
419+
initializeSIPeepholeSDWALegacyPass(*PR);
419420
initializeSIShrinkInstructionsLegacyPass(*PR);
420421
initializeSIOptimizeExecMaskingPreRAPass(*PR);
421422
initializeSIOptimizeVGPRLiveRangePass(*PR);
@@ -1275,7 +1276,7 @@ void GCNPassConfig::addMachineSSAOptimization() {
12751276
addPass(&GCNDPPCombineLegacyID);
12761277
addPass(&SILoadStoreOptimizerLegacyID);
12771278
if (isPassEnabled(EnableSDWAPeephole)) {
1278-
addPass(&SIPeepholeSDWAID);
1279+
addPass(&SIPeepholeSDWALegacyID);
12791280
addPass(&EarlyMachineLICMID);
12801281
addPass(&MachineCSELegacyID);
12811282
addPass(&SIFoldOperandsLegacyID);

llvm/lib/Target/AMDGPU/SIPeepholeSDWA.cpp

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
///
2020
//===----------------------------------------------------------------------===//
2121

22+
#include "SIPeepholeSDWA.h"
2223
#include "AMDGPU.h"
2324
#include "GCNSubtarget.h"
2425
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
@@ -45,7 +46,7 @@ class SDWADstOperand;
4546
using SDWAOperandsVector = SmallVector<SDWAOperand *, 4>;
4647
using SDWAOperandsMap = MapVector<MachineInstr *, SDWAOperandsVector>;
4748

48-
class SIPeepholeSDWA : public MachineFunctionPass {
49+
class SIPeepholeSDWA {
4950
private:
5051
MachineRegisterInfo *MRI;
5152
const SIRegisterInfo *TRI;
@@ -57,23 +58,27 @@ class SIPeepholeSDWA : public MachineFunctionPass {
5758

5859
std::optional<int64_t> foldToImm(const MachineOperand &Op) const;
5960

60-
public:
61-
static char ID;
62-
63-
SIPeepholeSDWA() : MachineFunctionPass(ID) {
64-
initializeSIPeepholeSDWAPass(*PassRegistry::getPassRegistry());
65-
}
66-
67-
bool runOnMachineFunction(MachineFunction &MF) override;
6861
void matchSDWAOperands(MachineBasicBlock &MBB);
6962
std::unique_ptr<SDWAOperand> matchSDWAOperand(MachineInstr &MI);
7063
void pseudoOpConvertToVOP2(MachineInstr &MI,
7164
const GCNSubtarget &ST) const;
7265
bool convertToSDWA(MachineInstr &MI, const SDWAOperandsVector &SDWAOperands);
7366
void legalizeScalarOperands(MachineInstr &MI, const GCNSubtarget &ST) const;
7467

68+
public:
69+
bool run(MachineFunction &MF);
70+
};
71+
72+
class SIPeepholeSDWALegacy : public MachineFunctionPass {
73+
public:
74+
static char ID;
75+
76+
SIPeepholeSDWALegacy() : MachineFunctionPass(ID) {}
77+
7578
StringRef getPassName() const override { return "SI Peephole SDWA"; }
7679

80+
bool runOnMachineFunction(MachineFunction &MF) override;
81+
7782
void getAnalysisUsage(AnalysisUsage &AU) const override {
7883
AU.setPreservesCFG();
7984
MachineFunctionPass::getAnalysisUsage(AU);
@@ -192,17 +197,17 @@ class SDWADstPreserveOperand : public SDWADstOperand {
192197

193198
} // end anonymous namespace
194199

195-
INITIALIZE_PASS(SIPeepholeSDWA, DEBUG_TYPE, "SI Peephole SDWA", false, false)
200+
INITIALIZE_PASS(SIPeepholeSDWALegacy, DEBUG_TYPE, "SI Peephole SDWA", false,
201+
false)
196202

197-
char SIPeepholeSDWA::ID = 0;
203+
char SIPeepholeSDWALegacy::ID = 0;
198204

199-
char &llvm::SIPeepholeSDWAID = SIPeepholeSDWA::ID;
205+
char &llvm::SIPeepholeSDWALegacyID = SIPeepholeSDWALegacy::ID;
200206

201-
FunctionPass *llvm::createSIPeepholeSDWAPass() {
202-
return new SIPeepholeSDWA();
207+
FunctionPass *llvm::createSIPeepholeSDWALegacyPass() {
208+
return new SIPeepholeSDWALegacy();
203209
}
204210

205-
206211
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
207212
static raw_ostream& operator<<(raw_ostream &OS, SdwaSel Sel) {
208213
switch(Sel) {
@@ -1235,10 +1240,17 @@ void SIPeepholeSDWA::legalizeScalarOperands(MachineInstr &MI,
12351240
}
12361241
}
12371242

1238-
bool SIPeepholeSDWA::runOnMachineFunction(MachineFunction &MF) {
1243+
bool SIPeepholeSDWALegacy::runOnMachineFunction(MachineFunction &MF) {
1244+
if (skipFunction(MF.getFunction()))
1245+
return false;
1246+
1247+
return SIPeepholeSDWA().run(MF);
1248+
}
1249+
1250+
bool SIPeepholeSDWA::run(MachineFunction &MF) {
12391251
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
12401252

1241-
if (!ST.hasSDWA() || skipFunction(MF.getFunction()))
1253+
if (!ST.hasSDWA())
12421254
return false;
12431255

12441256
MRI = &MF.getRegInfo();
@@ -1295,3 +1307,13 @@ bool SIPeepholeSDWA::runOnMachineFunction(MachineFunction &MF) {
12951307

12961308
return Ret;
12971309
}
1310+
1311+
PreservedAnalyses SIPeepholeSDWAPass::run(MachineFunction &MF,
1312+
MachineFunctionAnalysisManager &) {
1313+
if (MF.getFunction().hasOptNone() || !SIPeepholeSDWA().run(MF))
1314+
return PreservedAnalyses::all();
1315+
1316+
PreservedAnalyses PA = getMachineFunctionPassPreservedAnalyses();
1317+
PA.preserveSet<CFGAnalyses>();
1318+
return PA;
1319+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===--------- SIPeepholeSDWA.h -------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIB_TARGET_AMDGPU_SIPEEPHOLESDWA_H
10+
#define LLVM_LIB_TARGET_AMDGPU_SIPEEPHOLESDWA_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
16+
class SIPeepholeSDWAPass : public PassInfoMixin<SIPeepholeSDWAPass> {
17+
public:
18+
PreservedAnalyses run(MachineFunction &MF,
19+
MachineFunctionAnalysisManager &MFAM);
20+
};
21+
22+
} // namespace llvm
23+
24+
#endif // LLVM_LIB_TARGET_AMDGPU_SIPEEPHOLESDWA_H

llvm/test/CodeGen/AMDGPU/sdwa-gfx9.mir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# RUN: llc -mtriple=amdgcn -mcpu=kaveri -run-pass=si-peephole-sdwa -o - %s | FileCheck -check-prefix=CI -check-prefix=GCN %s
22
# RUN: llc -mtriple=amdgcn -mcpu=fiji -run-pass=si-peephole-sdwa -o - %s | FileCheck -check-prefix=VI -check-prefix=GCN %s
33
# RUN: llc -mtriple=amdgcn -mcpu=gfx900 -run-pass=si-peephole-sdwa -o - %s | FileCheck -check-prefix=GFX9 -check-prefix=GCN %s
4+
# RUN: llc -mtriple=amdgcn -mcpu=kaveri -passes=si-peephole-sdwa -o - %s | FileCheck -check-prefix=CI -check-prefix=GCN %s
5+
# RUN: llc -mtriple=amdgcn -mcpu=fiji -passes=si-peephole-sdwa -o - %s | FileCheck -check-prefix=VI -check-prefix=GCN %s
6+
# RUN: llc -mtriple=amdgcn -mcpu=gfx900 -passes=si-peephole-sdwa -o - %s | FileCheck -check-prefix=GFX9 -check-prefix=GCN %s
47

58
# GCN-LABEL: {{^}}name: add_shr_i32
69
# GCN: [[SMOV:%[0-9]+]]:sreg_32_xm0 = S_MOV_B32 123

llvm/test/CodeGen/AMDGPU/sdwa-ops.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# RUN: llc -mtriple=amdgcn -mcpu=gfx900 -verify-machineinstrs -run-pass=si-peephole-sdwa -o - %s | FileCheck -check-prefix=GFX9 %s
22
# RUN: llc -mtriple=amdgcn -mcpu=fiji -verify-machineinstrs -run-pass=si-peephole-sdwa -o - %s | FileCheck -check-prefix=GFX9 %s
3+
# RUN: llc -mtriple=amdgcn -mcpu=gfx900 -passes=si-peephole-sdwa -o - %s | FileCheck -check-prefix=GFX9 %s
4+
# RUN: llc -mtriple=amdgcn -mcpu=fiji -passes=si-peephole-sdwa -o - %s | FileCheck -check-prefix=GFX9 %s
35

46
# test for 3 consecutive _sdwa's
57
# GFX9-LABEL: name: test1_add_co_sdwa

llvm/test/CodeGen/AMDGPU/sdwa-preserve.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
22
# RUN: llc -mtriple=amdgcn -mcpu=fiji -run-pass=si-peephole-sdwa -verify-machineinstrs -o - %s | FileCheck -check-prefix=SDWA %s
33
# RUN: llc -mtriple=amdgcn -mcpu=gfx900 -run-pass=si-peephole-sdwa -verify-machineinstrs -o - %s | FileCheck -check-prefix=SDWA %s
4+
# RUN: llc -mtriple=amdgcn -mcpu=fiji -passes=si-peephole-sdwa -o - %s | FileCheck -check-prefix=SDWA %s
5+
# RUN: llc -mtriple=amdgcn -mcpu=gfx900 -passes=si-peephole-sdwa -o - %s | FileCheck -check-prefix=SDWA %s
46
---
57
name: add_f16_u32_preserve
68
tracksRegLiveness: true

0 commit comments

Comments
 (0)