-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[mlir][spirv] Fix lookup logic spirv.target_env
for gpu.module
#147262
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
The `gpu.module` operation can contain `spirv.target_env` attributes within an array attribute named `"targets"`. So it accounts for that case by iterating over the `"targets"` attribute, if present, and looking up `spirv.target_env`.
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir-gpu @llvm/pr-subscribers-mlir-spirv Author: Jaeho Kim (oojahooo) ChangesThe Full diff: https://github.com/llvm/llvm-project/pull/147262.diff 1 Files Affected:
diff --git a/mlir/lib/Dialect/SPIRV/IR/TargetAndABI.cpp b/mlir/lib/Dialect/SPIRV/IR/TargetAndABI.cpp
index 5ecbd5d7c59d5..dbaa10e89bd42 100644
--- a/mlir/lib/Dialect/SPIRV/IR/TargetAndABI.cpp
+++ b/mlir/lib/Dialect/SPIRV/IR/TargetAndABI.cpp
@@ -184,6 +184,15 @@ spirv::TargetEnvAttr spirv::lookupTargetEnv(Operation *op) {
if (!op)
break;
+ if (auto arrAttr = op->getAttrOfType<ArrayAttr>("targets")) {
+ for (auto attr : arrAttr) {
+ if (auto spirvTargetEnvAttr =
+ llvm::dyn_cast<spirv::TargetEnvAttr>(attr)) {
+ return spirvTargetEnvAttr;
+ }
+ }
+ }
+
if (auto attr = op->getAttrOfType<spirv::TargetEnvAttr>(
spirv::getTargetEnvAttrName()))
return attr;
|
@llvm/pr-subscribers-mlir Author: Jaeho Kim (oojahooo) ChangesThe Full diff: https://github.com/llvm/llvm-project/pull/147262.diff 1 Files Affected:
diff --git a/mlir/lib/Dialect/SPIRV/IR/TargetAndABI.cpp b/mlir/lib/Dialect/SPIRV/IR/TargetAndABI.cpp
index 5ecbd5d7c59d5..dbaa10e89bd42 100644
--- a/mlir/lib/Dialect/SPIRV/IR/TargetAndABI.cpp
+++ b/mlir/lib/Dialect/SPIRV/IR/TargetAndABI.cpp
@@ -184,6 +184,15 @@ spirv::TargetEnvAttr spirv::lookupTargetEnv(Operation *op) {
if (!op)
break;
+ if (auto arrAttr = op->getAttrOfType<ArrayAttr>("targets")) {
+ for (auto attr : arrAttr) {
+ if (auto spirvTargetEnvAttr =
+ llvm::dyn_cast<spirv::TargetEnvAttr>(attr)) {
+ return spirvTargetEnvAttr;
+ }
+ }
+ }
+
if (auto attr = op->getAttrOfType<spirv::TargetEnvAttr>(
spirv::getTargetEnvAttrName()))
return attr;
|
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.
A rather naive question, as I'm not familiar with the GPU dialect. Is it possible for the target env to be present in both "targets"
and as an op attribute at the same time? If so, does the look-up order matter? I’d hope the same target env is returned either way, but hypothetically if there is a different env in "targets"
and in the op, I wonder which one should take a precedence?
Also, it'd be nice to have a test for the change. Probably in GPU to SPIR-V conversion test directory?
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.
Could you add a test that shows where this comes up? I'm not sure if we should look up the target env in"targets"
attribute in any of possible parents
When the Also I added a lit test in GPU to SPIR-V conversion test directory. Thank you for your comment. |
Thank you for your comment. I added a test that reproduces the situation I had in mind. |
To me the sounds like the wrong fix. Could we teach the passes you mentioned to also look in |
If I understand your point correctly, rather than hardcoding the There are two possible approaches that I think:
I would greatly appreciate it if you could let me know which is aligned with your suggestion. If so, I would be happy to revise the implementation accordingly based on the preferred direction. Thank you for your support. |
Approach 1. sounds to me like the way to go |
…ule` Add lookup target env in "targets" attr logic to GPUToSPIRV pass
Thank you for replying. Happy to make any further changes if needed. Otherwise, would it be okay to merge? Thank you! |
remove unnecessary namespace qualifiers
spirv.target_env
for gpu.module
spirv.target_env
for gpu.module
1c22145
to
9344c20
Compare
It had to be checked for a lack of `"targets"` attributes in a GPU module
Hi @kuhar, |
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 % one minor request
Hi @kuhar, thank you again for the approval. |
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.
Just a small question, but LGTM otherwise.
if (spirv::TargetEnvAttr targetEnvAttr = lookupTargetEnvInTargets(moduleOp)) | ||
return targetEnvAttr; | ||
|
||
return spirv::lookupTargetEnvOrDefault(moduleOp); |
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.
Looking at the tests, it looks like they both test the if
statement above. Is there a test checking this part of the function? If not, do we need a test to check that the function behaves as intended when no attributes are attached?
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.
If I understood correctly, we should test the behavior when the gpu.module
does not have a targets
attribute with spirv target env so that the line 78 code is executed.
I believe this case might already be covered by existing tests. For instance, gpu-to-spirv.mlir tests the behavior when no target env is present at all, and load-store.mlir covers the case where the target env is not attached to thegpu.module
's targets
attribute but to the module
.
However, if a more specific test is needed, such as one where the gpu.module
lacks the targets
attribute but has a target env in its attr-dict, or if other additional tests are necessary, I would be happy to add them based on your feedback.
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.
If I understood correctly, we should test the behavior when the gpu.module does not have a targets attribute with spirv target env so that the line 78 code is executed.
Correct.
I believe this case might already be covered by existing tests. For instance, gpu-to-spirv.mlir tests the behavior when no target env is present at all, and load-store.mlir covers the case where the target env is not attached to thegpu.module's targets attribute but to the module.
Sounds good, I think we are good.
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 for addressing my question. LGTM.
@oojahooo Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
The
gpu.module
operation can containspirv.target_env
attributes within an array attribute named"targets"
. So it accounts for that case by iterating over the"targets"
attribute, if present, and looking upspirv.target_env
.