-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[AMDGPU] Support function attribute to override postRA scheduling direction #147708
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
Conversation
|
@llvm/pr-subscribers-backend-amdgpu Author: Harrison Hao (harrisonGPU) ChangesFull diff: https://github.com/llvm/llvm-project/pull/147708.diff 3 Files Affected:
diff --git a/llvm/include/llvm/CodeGen/MachineScheduler.h b/llvm/include/llvm/CodeGen/MachineScheduler.h
index e7a7091acee64..1635eae758590 100644
--- a/llvm/include/llvm/CodeGen/MachineScheduler.h
+++ b/llvm/include/llvm/CodeGen/MachineScheduler.h
@@ -116,6 +116,7 @@ enum Direction {
} // namespace MISched
LLVM_ABI extern cl::opt<MISched::Direction> PreRADirection;
+LLVM_ABI extern cl::opt<MISched::Direction> PostRADirection;
LLVM_ABI extern cl::opt<bool> VerifyScheduling;
#ifndef NDEBUG
extern cl::opt<bool> ViewMISchedDAGs;
diff --git a/llvm/lib/CodeGen/MachineScheduler.cpp b/llvm/lib/CodeGen/MachineScheduler.cpp
index 76cba2949af60..72e1fce07f33e 100644
--- a/llvm/lib/CodeGen/MachineScheduler.cpp
+++ b/llvm/lib/CodeGen/MachineScheduler.cpp
@@ -190,7 +190,7 @@ cl::opt<MISched::Direction> PreRADirection(
clEnumValN(MISched::Bidirectional, "bidirectional",
"Force bidirectional pre reg-alloc list scheduling")));
-static cl::opt<MISched::Direction> PostRADirection(
+cl::opt<MISched::Direction> PostRADirection(
"misched-postra-direction", cl::Hidden,
cl::desc("Post reg-alloc list scheduling direction"),
cl::init(MISched::Unspecified),
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
index c3536113e9bef..4c46e703b4604 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
@@ -1144,6 +1144,23 @@ GCNTargetMachine::createMachineScheduler(MachineSchedContext *C) const {
ScheduleDAGInstrs *
GCNTargetMachine::createPostMachineScheduler(MachineSchedContext *C) const {
+ Attribute PostRADirectionAttr =
+ C->MF->getFunction().getFnAttribute("misched-postra-direction");
+
+ if (PostRADirectionAttr.isValid()) {
+ StringRef PostRADirectionStr = PostRADirectionAttr.getValueAsString();
+ if (PostRADirectionStr == "topdown")
+ PostRADirection = MISched::TopDown;
+ else if (PostRADirectionStr == "bottomup")
+ PostRADirection = MISched::BottomUp;
+ else if (PostRADirectionStr == "bidirectional")
+ PostRADirection = MISched::Bidirectional;
+ else
+ report_fatal_error(
+ Twine("invalid value for 'misched-postra-direction' attribute: ") +
+ PostRADirectionStr);
+ }
+
ScheduleDAGMI *DAG =
new GCNPostScheduleDAGMILive(C, std::make_unique<PostGenericScheduler>(C),
/*RemoveKillFlags=*/true);
|
arsenm
left a comment
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 tests and I thought there already was an attribute for this
I wonder what the reason of having a function attribute for this |
We use function attributes here because in graphics shader compiler, performance tuning often needs to be done per shader stage—not across the whole pipeline. |
No, we don’t have this attribute. I’ve run into this issue, the existing option is a static one, so it applies to the entire pipeline, not to individual shader stages. Also, I’m not sure how to write a proper lit test for this , what exactly should the test check? |
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.
Function attributes should not override anything actually set on the command line, i.e. if command line sets an option it should override function attributes.
This mechanism as implemented is too global.
It will change direction of all PostRA schedulers.
Can you revise this change to add some way of setting a direction for specific schedulers directly?
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.
Thanks Carl, I understand your point. So I created a new option called amdgpu-post-ra-direction. The function attribute will no longer override the command-line option.
It's similar to how amdgpu-sched-strategy works:
llvm-project/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
Lines 1122 to 1144 in 43a9ec2
| Attribute SchedStrategyAttr = | |
| C->MF->getFunction().getFnAttribute("amdgpu-sched-strategy"); | |
| StringRef SchedStrategy = SchedStrategyAttr.isValid() | |
| ? SchedStrategyAttr.getValueAsString() | |
| : AMDGPUSchedStrategy; | |
| if (SchedStrategy == "max-ilp") | |
| return createGCNMaxILPMachineScheduler(C); | |
| if (SchedStrategy == "max-memory-clause") | |
| return createGCNMaxMemoryClauseMachineScheduler(C); | |
| if (SchedStrategy == "iterative-ilp") | |
| return createIterativeILPMachineScheduler(C); | |
| if (SchedStrategy == "iterative-minreg") | |
| return createMinRegScheduler(C); | |
| if (SchedStrategy == "iterative-maxocc") | |
| return createIterativeGCNMaxOccupancyMachineScheduler(C); | |
| return createGCNMaxOccupancyMachineScheduler(C); | |
| } |
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.
Code changes do not seem to solve either of the two issues I mentioned.
New option AMDGPUPostRADirection is not connected, and would not solve the issue of function attribute overriding the direction of all machine schedulers.
To fix this issue, the direction needs to be passed at scheduler construction or otherwise stored/updated in the scheduler object.
i.e. there needs to be a per-scheduler way of setting direction that doesn't currently exist.
To avoid a function attribute overriding command line options, you should check for presence of command line option before parsing function attribute.
Then ignore the function attribute if a command line option is already present/set.
Again you don't need to do this part if the command line override applies to a per-scheduler direction as implemented above -- this detail can be encapsulated within the MachineScheduler code, then AMDGPU code only need set the desired direction.
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.
Thanks! I noticed that LLVM provides the overridePostRASchedPolicy hook, so I’ve switched to using a target feature to control the post-RA direction. This avoids overriding the direction for all machine schedulers and no longer conflicts with command-line overrides. What do you think about it?
arsenm
left a comment
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.
Needs tests
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.
Better to emit an LLVMContext warning and ignore the value, plus this needs testing
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.
Thanks Matt. I've already added a DiagnosticInfoOptimizationFailure to emit a warning. I also thought about how to add a lit test for this patch, so I added a dbgs() print for the PostRA direction and checked it using FileCheck. What do you think?
4bcd9a6 to
220345b
Compare
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.
Don't need the other attributes
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.
Do you mean we don't need the amdgpu-post-ra-direction attribute?
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.
Other attributes = alwaysinline nounwind memory(readwrite).
You only need the attribute under test.
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.
Thanks, I have updated it. :-)
99be971 to
db514b4
Compare
arsenm
left a comment
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.
This should absolutely not go into subtarget features. This made more sense as an attribute
|
If you modify |
db514b4 to
bb7a6eb
Compare
|
I have already used function attribute approach, what do think about? |
|
Ping. |
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.
leftover?
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.
Thanks, I have removed it.
shiltian
left a comment
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.
LGTM
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.
| Twine("invalid value for postRa direction attribute: '") + | |
| Twine("invalid value for postRA direction attribute: '") + |
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.
Done.
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.
This check misses the actual value of the attribute
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.
I have removed actual value of the attribute. :-)
bfb5deb to
195ce35
Compare
This patch adds support for controlling the post-RA machine scheduler direction
(topdown, bottomup, bidirectional) on a per-function basis using the
"amdgpu-post-ra-direction" function attribute.