Skip to content

Commit ec3de57

Browse files
committed
[AMDGPU] Add post-RA scheduling direction control via target features
1 parent d3b7844 commit ec3de57

File tree

9 files changed

+90
-87
lines changed

9 files changed

+90
-87
lines changed

llvm/include/llvm/CodeGen/MachineScheduler.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ enum Direction {
116116
} // namespace MISched
117117

118118
LLVM_ABI extern cl::opt<MISched::Direction> PreRADirection;
119-
LLVM_ABI extern cl::opt<MISched::Direction> PostRADirection;
120119
LLVM_ABI extern cl::opt<bool> VerifyScheduling;
121120
#ifndef NDEBUG
122121
extern cl::opt<bool> ViewMISchedDAGs;

llvm/lib/CodeGen/MachineScheduler.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ cl::opt<MISched::Direction> PreRADirection(
190190
clEnumValN(MISched::Bidirectional, "bidirectional",
191191
"Force bidirectional pre reg-alloc list scheduling")));
192192

193-
cl::opt<MISched::Direction> PostRADirection(
193+
static cl::opt<MISched::Direction> PostRADirection(
194194
"misched-postra-direction", cl::Hidden,
195195
cl::desc("Post reg-alloc list scheduling direction"),
196196
cl::init(MISched::Unspecified),
@@ -4336,25 +4336,6 @@ void PostGenericScheduler::initPolicy(MachineBasicBlock::iterator Begin,
43364336
RegionPolicy.OnlyTopDown = false;
43374337
}
43384338

4339-
LLVM_DEBUG({
4340-
const char *DirStr = "default";
4341-
switch (PostRADirection) {
4342-
case MISched::TopDown:
4343-
DirStr = "topdown";
4344-
break;
4345-
case MISched::BottomUp:
4346-
DirStr = "bottomup";
4347-
break;
4348-
case MISched::Bidirectional:
4349-
DirStr = "bidirectional";
4350-
break;
4351-
default:;
4352-
}
4353-
4354-
dbgs() << "Post-MI-sched direction (" << MF.getName() << "): " << DirStr
4355-
<< '\n';
4356-
});
4357-
43584339
BotIdx = NumRegionInstrs - 1;
43594340
this->NumRegionInstrs = NumRegionInstrs;
43604341
}

llvm/lib/Target/AMDGPU/AMDGPUFeatures.td

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
defvar TopDown = [{ MISched::TopDown }];
10+
defvar BottomUp = [{ MISched::BottomUp }];
11+
defvar Bidirectional = [{ MISched::Bidirectional }];
12+
913
def FeatureFP64 : SubtargetFeature<"fp64",
1014
"FP64",
1115
"true",
@@ -54,3 +58,20 @@ def FeaturePromoteAlloca : SubtargetFeature <"promote-alloca",
5458
"Enable promote alloca pass"
5559
>;
5660

61+
def FeaturePostRATopDown : SubtargetFeature <"postra-top-down",
62+
"PostRASchedDirection",
63+
TopDown,
64+
"Force Post-RA scheduler to run top-down"
65+
>;
66+
67+
def FeaturePostRABottomUp : SubtargetFeature <"postra-bottom-up",
68+
"PostRASchedDirection",
69+
BottomUp,
70+
"Force Post-RA scheduler to run bottom-up"
71+
>;
72+
73+
def FeaturePostRABidirectional : SubtargetFeature <"postra-bidirectional",
74+
"PostRASchedDirection",
75+
Bidirectional,
76+
"Force Post-RA scheduler to run bidirectionally"
77+
>;

llvm/lib/Target/AMDGPU/AMDGPUSubtarget.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
1616

1717
#include "llvm/ADT/SmallVector.h"
18+
#include "llvm/CodeGen/MachineScheduler.h"
1819
#include "llvm/IR/CallingConv.h"
1920
#include "llvm/Support/Alignment.h"
2021
#include "llvm/TargetParser/Triple.h"
@@ -80,6 +81,7 @@ class AMDGPUSubtarget {
8081
unsigned LocalMemorySize = 0;
8182
unsigned AddressableLocalMemorySize = 0;
8283
char WavefrontSizeLog2 = 0;
84+
MISched::Direction PostRASchedDirection = MISched::TopDown;
8385

8486
public:
8587
AMDGPUSubtarget(Triple TT);
@@ -382,6 +384,10 @@ class AMDGPUSubtarget {
382384
AMDGPUDwarfFlavour getAMDGPUDwarfFlavour() const;
383385

384386
virtual ~AMDGPUSubtarget() = default;
387+
388+
MISched::Direction getPostRASchedDirection() const {
389+
return PostRASchedDirection;
390+
}
385391
};
386392

387393
} // end namespace llvm

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,6 @@ static cl::opt<std::string>
460460
cl::desc("Select custom AMDGPU scheduling strategy."),
461461
cl::Hidden, cl::init(""));
462462

463-
static cl::opt<std::string>
464-
AMDGPUPostRADirection("amdgpu-post-ra-direction",
465-
cl::desc("Select custom AMDGPU postRA direction."),
466-
cl::Hidden, cl::init(""));
467-
468463
static cl::opt<bool> EnableRewritePartialRegUses(
469464
"amdgpu-enable-rewrite-partial-reg-uses",
470465
cl::desc("Enable rewrite partial reg uses pass"), cl::init(true),
@@ -1158,29 +1153,6 @@ GCNTargetMachine::createMachineScheduler(MachineSchedContext *C) const {
11581153

11591154
ScheduleDAGInstrs *
11601155
GCNTargetMachine::createPostMachineScheduler(MachineSchedContext *C) const {
1161-
if (PostRADirection.getNumOccurrences() == 0) {
1162-
Attribute PostRADirectionAttr =
1163-
C->MF->getFunction().getFnAttribute("amdgpu-post-ra-direction");
1164-
1165-
if (PostRADirectionAttr.isValid()) {
1166-
StringRef PostRADirectionStr = PostRADirectionAttr.getValueAsString();
1167-
if (PostRADirectionStr == "topdown")
1168-
PostRADirection = MISched::TopDown;
1169-
else if (PostRADirectionStr == "bottomup")
1170-
PostRADirection = MISched::BottomUp;
1171-
else if (PostRADirectionStr == "bidirectional")
1172-
PostRADirection = MISched::Bidirectional;
1173-
else {
1174-
PostRADirection = MISched::Unspecified;
1175-
DiagnosticInfoOptimizationFailure Diag(
1176-
C->MF->getFunction(), C->MF->getFunction().getSubprogram(),
1177-
Twine("invalid value for postRa direction attribute: '") +
1178-
PostRADirectionStr);
1179-
C->MF->getFunction().getContext().diagnose(Diag);
1180-
}
1181-
}
1182-
}
1183-
11841156
ScheduleDAGMI *DAG =
11851157
new GCNPostScheduleDAGMILive(C, std::make_unique<PostGenericScheduler>(C),
11861158
/*RemoveKillFlags=*/true);

llvm/lib/Target/AMDGPU/GCNSubtarget.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,40 @@ void GCNSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
340340
Policy.ShouldTrackLaneMasks = true;
341341
}
342342

343+
void GCNSubtarget::overridePostRASchedPolicy(MachineSchedPolicy &Policy,
344+
unsigned NumRegionInstrs) const {
345+
switch (getPostRASchedDirection()) {
346+
case MISched::TopDown:
347+
Policy.OnlyTopDown = true;
348+
Policy.OnlyBottomUp = false;
349+
break;
350+
case MISched::BottomUp:
351+
Policy.OnlyTopDown = false;
352+
Policy.OnlyBottomUp = true;
353+
break;
354+
case MISched::Bidirectional:
355+
default:
356+
Policy.OnlyTopDown = false;
357+
Policy.OnlyBottomUp = false;
358+
break;
359+
}
360+
361+
LLVM_DEBUG({
362+
const char *DirStr = "topdown";
363+
switch (getPostRASchedDirection()) {
364+
case MISched::BottomUp:
365+
DirStr = "bottomup";
366+
break;
367+
case MISched::Bidirectional:
368+
DirStr = "bidirectional";
369+
break;
370+
default:
371+
break;
372+
}
373+
dbgs() << "Post-MI-sched direction: " << DirStr << '\n';
374+
});
375+
}
376+
343377
void GCNSubtarget::mirFileLoaded(MachineFunction &MF) const {
344378
if (isWave32()) {
345379
// Fix implicit $vcc operands after MIParser has verified that they match

llvm/lib/Target/AMDGPU/GCNSubtarget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,9 @@ class GCNSubtarget final : public AMDGPUGenSubtargetInfo,
10411041
void overrideSchedPolicy(MachineSchedPolicy &Policy,
10421042
const SchedRegion &Region) const override;
10431043

1044+
void overridePostRASchedPolicy(MachineSchedPolicy &Policy,
1045+
unsigned NumRegionInstrs) const override;
1046+
10441047
void mirFileLoaded(MachineFunction &MF) const override;
10451048

10461049
unsigned getMaxNumUserSGPRs() const {

llvm/test/CodeGen/AMDGPU/postra-sched-attribute.ll

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; REQUIRES: asserts
2+
3+
; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1100 -debug-only=gcn-subtarget < %s 2>&1 | FileCheck %s
4+
5+
; CHECK: Post-MI-sched direction: topdown
6+
define float @postra-sched-topdown(float %input) nounwind #0 {
7+
%x = fadd float %input, 1.000000e+00
8+
ret float %x
9+
}
10+
11+
; CHECK: Post-MI-sched direction: bottomup
12+
define float @postra-sched-bottomup(float %input) nounwind #1 {
13+
%x = fsub float %input, 1.000000e+00
14+
ret float %x
15+
}
16+
17+
; CHECK: Post-MI-sched direction: bidirectional
18+
define float @postra-sched-bidirectional(float %input) nounwind #2 {
19+
%x = fadd float %input, 1.000000e+00
20+
ret float %x
21+
}
22+
23+
attributes #0 = { "target-features"="+postra-top-down" }
24+
attributes #1 = { "target-features"="+postra-bottom-up" }
25+
attributes #2 = { "target-features"="+postra-bidirectional" }

0 commit comments

Comments
 (0)