-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[AMDGPU] Add scaffolding for ML focused scheduling strategy #169616
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
Open
kerbowa
wants to merge
1
commit into
main
Choose a base branch
from
users/kerbowa/ml-sched-strategy-scaffold
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| //===-- AMDGPUMLSchedStrategy.cpp - ML-focused Scheduler Strategy ---------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| /// \file | ||
| /// ML-focused scheduling strategy for AMDGPU. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "AMDGPUMLSchedStrategy.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| AMDGPUMLSchedStrategy::AMDGPUMLSchedStrategy(const MachineSchedContext *C) | ||
| : GCNSchedStrategy(C) { | ||
| SchedStages.push_back(GCNSchedStageID::ILPInitialSchedule); | ||
| SchedStages.push_back(GCNSchedStageID::PreRARematerialize); | ||
| // Use more accurate GCN pressure trackers. | ||
| UseGCNTrackers = true; | ||
| } | ||
|
|
||
| void AMDGPUMLSchedStrategy::initialize(ScheduleDAGMI *DAG) { | ||
| // ML scheduling strategy is only done top-down to support new resource | ||
| // balancing heuristics. | ||
| RegionPolicy.OnlyTopDown = true; | ||
| RegionPolicy.OnlyBottomUp = false; | ||
|
|
||
| GCNSchedStrategy::initialize(DAG); | ||
| } | ||
|
|
||
| bool AMDGPUMLSchedStrategy::tryCandidate(SchedCandidate &Cand, | ||
| SchedCandidate &TryCand, | ||
| SchedBoundary *Zone) const { | ||
| // Initialize the candidate if needed. | ||
| if (!Cand.isValid()) { | ||
| TryCand.Reason = FirstValid; | ||
| return true; | ||
| } | ||
|
|
||
| // Bias PhysReg Defs and copies to their uses and defined respectively. | ||
| if (tryGreater(biasPhysReg(TryCand.SU, TryCand.AtTop), | ||
| biasPhysReg(Cand.SU, Cand.AtTop), TryCand, Cand, PhysReg)) | ||
| return TryCand.Reason != NoCand; | ||
|
|
||
| // Avoid exceeding the target's limit. | ||
| if (DAG->isTrackingPressure() && | ||
| tryPressure(TryCand.RPDelta.Excess, Cand.RPDelta.Excess, TryCand, Cand, | ||
| RegExcess, TRI, DAG->MF)) | ||
| return TryCand.Reason != NoCand; | ||
|
|
||
| // We only compare a subset of features when comparing nodes between | ||
| // Top and Bottom boundary. Some properties are simply incomparable, in many | ||
| // other instances we should only override the other boundary if something | ||
| // is a clear good pick on one boundary. Skip heuristics that are more | ||
| // "tie-breaking" in nature. | ||
| bool SameBoundary = Zone != nullptr; | ||
| if (SameBoundary) { | ||
| // For loops that are acyclic path limited, aggressively schedule for | ||
| // latency. Within an single cycle, whenever CurrMOps > 0, allow normal | ||
| // heuristics to take precedence. | ||
| if (Rem.IsAcyclicLatencyLimited && !Zone->getCurrMOps() && | ||
| tryLatency(TryCand, Cand, *Zone)) | ||
| return TryCand.Reason != NoCand; | ||
|
|
||
| // Prioritize instructions that read unbuffered resources by stall cycles. | ||
| if (tryLess(Zone->getLatencyStallCycles(TryCand.SU), | ||
| Zone->getLatencyStallCycles(Cand.SU), TryCand, Cand, Stall)) | ||
| return TryCand.Reason != NoCand; | ||
| } | ||
|
|
||
| // Keep clustered nodes together to encourage downstream peephole | ||
| // optimizations which may reduce resource requirements. | ||
| // | ||
| // This is a best effort to set things up for a post-RA pass. Optimizations | ||
| // like generating loads of multiple registers should ideally be done within | ||
| // the scheduler pass by combining the loads during DAG postprocessing. | ||
| unsigned CandZoneCluster = Cand.AtTop ? TopClusterID : BotClusterID; | ||
| unsigned TryCandZoneCluster = TryCand.AtTop ? TopClusterID : BotClusterID; | ||
| bool CandIsClusterSucc = | ||
| isTheSameCluster(CandZoneCluster, Cand.SU->ParentClusterIdx); | ||
| bool TryCandIsClusterSucc = | ||
| isTheSameCluster(TryCandZoneCluster, TryCand.SU->ParentClusterIdx); | ||
|
|
||
| if (tryGreater(TryCandIsClusterSucc, CandIsClusterSucc, TryCand, Cand, | ||
| Cluster)) | ||
| return TryCand.Reason != NoCand; | ||
|
|
||
| if (SameBoundary) { | ||
| // Weak edges are for clustering and other constraints. | ||
| if (tryLess(getWeakLeft(TryCand.SU, TryCand.AtTop), | ||
| getWeakLeft(Cand.SU, Cand.AtTop), TryCand, Cand, Weak)) | ||
| return TryCand.Reason != NoCand; | ||
| } | ||
|
|
||
| // Avoid increasing the max pressure of the entire region. | ||
| if (DAG->isTrackingPressure() && | ||
| tryPressure(TryCand.RPDelta.CurrentMax, Cand.RPDelta.CurrentMax, TryCand, | ||
| Cand, RegMax, TRI, DAG->MF)) | ||
| return TryCand.Reason != NoCand; | ||
|
|
||
| if (SameBoundary) { | ||
| // Avoid critical resource consumption and balance the schedule. | ||
| TryCand.initResourceDelta(DAG, SchedModel); | ||
| if (tryLess(TryCand.ResDelta.CritResources, Cand.ResDelta.CritResources, | ||
| TryCand, Cand, ResourceReduce)) | ||
| return TryCand.Reason != NoCand; | ||
| if (tryGreater(TryCand.ResDelta.DemandedResources, | ||
| Cand.ResDelta.DemandedResources, TryCand, Cand, | ||
| ResourceDemand)) | ||
| return TryCand.Reason != NoCand; | ||
|
|
||
| // Avoid serializing long latency dependence chains. | ||
| // For acyclic path limited loops, latency was already checked above. | ||
| if (!RegionPolicy.DisableLatencyHeuristic && TryCand.Policy.ReduceLatency && | ||
| !Rem.IsAcyclicLatencyLimited && tryLatency(TryCand, Cand, *Zone)) | ||
| return TryCand.Reason != NoCand; | ||
|
|
||
| // Fall through to original instruction order. | ||
| if ((Zone->isTop() && TryCand.SU->NodeNum < Cand.SU->NodeNum) || | ||
| (!Zone->isTop() && TryCand.SU->NodeNum > Cand.SU->NodeNum)) { | ||
| TryCand.Reason = NodeOrder; | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| AMDGPUMLPostSchedStrategy::AMDGPUMLPostSchedStrategy( | ||
| const MachineSchedContext *C) | ||
| : PostGenericScheduler(C) {} | ||
|
|
||
| bool AMDGPUMLPostSchedStrategy::tryCandidate(SchedCandidate &Cand, | ||
| SchedCandidate &TryCand) { | ||
| // Initialize the candidate if needed. | ||
| if (!Cand.isValid()) { | ||
| TryCand.Reason = FirstValid; | ||
| return true; | ||
| } | ||
|
|
||
| // Fall through to original instruction order. | ||
| // This effectively only enables hazard checking for post-RA scheduling. | ||
| if (TryCand.SU->NodeNum < Cand.SU->NodeNum) { | ||
| TryCand.Reason = NodeOrder; | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| //===-- AMDGPUMLSchedStrategy.h - ML-focused Scheduler Strategy -*- 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| /// \file | ||
| /// ML-focused scheduling strategy for AMDGPU. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUMLSCHEDSTRATEGY_H | ||
| #define LLVM_LIB_TARGET_AMDGPU_AMDGPUMLSCHEDSTRATEGY_H | ||
|
|
||
| #include "GCNSchedStrategy.h" | ||
| #include "llvm/CodeGen/MachineScheduler.h" | ||
|
|
||
| namespace llvm { | ||
|
|
||
| class AMDGPUMLSchedStrategy final : public GCNSchedStrategy { | ||
| protected: | ||
| bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand, | ||
| SchedBoundary *Zone) const override; | ||
|
|
||
| public: | ||
| AMDGPUMLSchedStrategy(const MachineSchedContext *C); | ||
|
|
||
| void initialize(ScheduleDAGMI *DAG) override; | ||
| }; | ||
|
|
||
| class AMDGPUMLPostSchedStrategy : public PostGenericScheduler { | ||
| protected: | ||
| bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand) override; | ||
|
|
||
| public: | ||
| AMDGPUMLPostSchedStrategy(const MachineSchedContext *C); | ||
| }; | ||
|
|
||
| } // End namespace llvm | ||
|
|
||
| #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUMLSCHEDSTRATEGY_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| #include "AMDGPUIGroupLP.h" | ||
| #include "AMDGPUISelDAGToDAG.h" | ||
| #include "AMDGPULowerVGPREncoding.h" | ||
| #include "AMDGPUMLSchedStrategy.h" | ||
| #include "AMDGPUMacroFusion.h" | ||
| #include "AMDGPUPerfHintAnalysis.h" | ||
| #include "AMDGPUPreloadKernArgProlog.h" | ||
|
|
@@ -636,6 +637,11 @@ static ScheduleDAGInstrs *createSIMachineScheduler(MachineSchedContext *C) { | |
| return new SIScheduleDAGMI(C); | ||
| } | ||
|
|
||
| static bool isMLWorkload(const Function &F) { | ||
| Attribute WorkloadAttr = F.getFnAttribute("amdgpu-workload-type"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just use the existing scheduler override attribute? |
||
| return WorkloadAttr.isValid() && WorkloadAttr.getValueAsString() == "ml"; | ||
| } | ||
|
|
||
| static ScheduleDAGInstrs * | ||
| createGCNMaxOccupancyMachineScheduler(MachineSchedContext *C) { | ||
| const GCNSubtarget &ST = C->MF->getSubtarget<GCNSubtarget>(); | ||
|
|
@@ -659,6 +665,11 @@ createGCNMaxILPMachineScheduler(MachineSchedContext *C) { | |
| return DAG; | ||
| } | ||
|
|
||
| static ScheduleDAGInstrs *createGCNMLMachineScheduler(MachineSchedContext *C) { | ||
| return new GCNScheduleDAGMILive(C, | ||
| std::make_unique<AMDGPUMLSchedStrategy>(C)); | ||
| } | ||
|
|
||
| static ScheduleDAGInstrs * | ||
| createGCNMaxMemoryClauseMachineScheduler(MachineSchedContext *C) { | ||
| const GCNSubtarget &ST = C->MF->getSubtarget<GCNSubtarget>(); | ||
|
|
@@ -1170,6 +1181,9 @@ GCNTargetMachine::createMachineScheduler(MachineSchedContext *C) const { | |
| if (ST.enableSIScheduler()) | ||
| return createSIMachineScheduler(C); | ||
|
|
||
| if (isMLWorkload(C->MF->getFunction())) | ||
| return createGCNMLMachineScheduler(C); | ||
|
|
||
| Attribute SchedStrategyAttr = | ||
| C->MF->getFunction().getFnAttribute("amdgpu-sched-strategy"); | ||
| StringRef SchedStrategy = SchedStrategyAttr.isValid() | ||
|
|
@@ -1191,11 +1205,19 @@ GCNTargetMachine::createMachineScheduler(MachineSchedContext *C) const { | |
| if (SchedStrategy == "iterative-maxocc") | ||
| return createIterativeGCNMaxOccupancyMachineScheduler(C); | ||
|
|
||
| if (SchedStrategy == "ml") | ||
| return createGCNMLMachineScheduler(C); | ||
|
|
||
| return createGCNMaxOccupancyMachineScheduler(C); | ||
| } | ||
|
|
||
| ScheduleDAGInstrs * | ||
| GCNTargetMachine::createPostMachineScheduler(MachineSchedContext *C) const { | ||
| if (isMLWorkload(C->MF->getFunction())) | ||
| return new GCNPostScheduleDAGMILive( | ||
| C, std::make_unique<AMDGPUMLPostSchedStrategy>(C), | ||
| /*RemoveKillFlags=*/true); | ||
|
|
||
| ScheduleDAGMI *DAG = | ||
| new GCNPostScheduleDAGMILive(C, std::make_unique<PostGenericScheduler>(C), | ||
| /*RemoveKillFlags=*/true); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing empty line at EoF