-
Notifications
You must be signed in to change notification settings - Fork 17.3k
[SPIRV] Change how to detect OpenCL/Vulkan Env and update tests accordingly. #129689
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
Merged
asudarsa
merged 25 commits into
llvm:main
from
maarquitos14:maronas/use-spirv-friendly-call
Jun 3, 2025
Merged
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
bbf7b42
Dissociate Logical/Physical from OpenCL/Vulkan env.
f6bf74b
Extra change to complete dissociation.
4c7bbc2
Use entry points to help determine the env.
ecb4f79
Explicitly add vulkan to triple of failing tests.
1a4e754
Fix clang-format issue.
54adad6
Merge remote-tracking branch 'origin/main' into maronas/use-spirv-fri…
314f4e0
Fix failing test.
6d0cf9e
Fix debug build.
0f95726
Fix failing tests.
a42ecac
Fix test failure.
3221f7e
Merge remote-tracking branch 'origin/main' into maronas/use-spirv-fri…
0cb18d0
Address code review feedback.
4d84546
Merge remote-tracking branch 'origin/main' into maronas/use-spirv-fri…
7a0be57
Fix build issues.
70835b9
Fix typo.
afa72c1
Fix test failure.
b7879b5
Merge remote-tracking branch 'origin/main' into maronas/use-spirv-fri…
85d600f
Fix test failure.
5513335
Merge remote-tracking branch 'origin/main' into maronas/use-spirv-fri…
61f2abd
Address code review feedback.
a36faa4
Address more code review feedback.
fcc219a
Turn assertion into fatal error.
42df23d
Replace is[Kernel|Shader]Env with is[Kernel|Shader].
dd530b9
Merge remote-tracking branch 'origin/main' into maronas/use-spirv-fri…
248c5e6
Fix test failure.
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
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 |
|---|---|---|
|
|
@@ -267,14 +267,37 @@ static SPIRVType *getArgSPIRVType(const Function &F, unsigned ArgIdx, | |
|
|
||
| static SPIRV::ExecutionModel::ExecutionModel | ||
| getExecutionModel(const SPIRVSubtarget &STI, const Function &F) { | ||
| if (STI.isOpenCLEnv()) | ||
| if (STI.isKernelEnv()) | ||
| return SPIRV::ExecutionModel::Kernel; | ||
|
|
||
| if (STI.isShaderEnv()) { | ||
| auto attribute = F.getFnAttribute("hlsl.shader"); | ||
| if (!attribute.isValid()) { | ||
| report_fatal_error( | ||
| "This entry point lacks mandatory hlsl.shader attribute."); | ||
| } | ||
|
|
||
| const auto value = attribute.getValueAsString(); | ||
| if (value == "compute") | ||
| return SPIRV::ExecutionModel::GLCompute; | ||
|
|
||
| report_fatal_error( | ||
| "This HLSL entry point is not supported by this backend."); | ||
| } | ||
|
|
||
| assert(STI.getEnv() == SPIRVSubtarget::Unknown); | ||
| // "hlsl.shader" attribute is mandatory for Vulkan, so we can set Env to | ||
| // Shader whenever we find it, and to Kernel otherwise. | ||
|
|
||
| // We will now change the Env based on the attribute, so we need to strip | ||
| // `const` out of the ref to STI. | ||
| SPIRVSubtarget *NonConstSTI = const_cast<SPIRVSubtarget *>(&STI); | ||
| auto attribute = F.getFnAttribute("hlsl.shader"); | ||
| if (!attribute.isValid()) { | ||
| report_fatal_error( | ||
| "This entry point lacks mandatory hlsl.shader attribute."); | ||
| NonConstSTI->setEnv(SPIRVSubtarget::Kernel); | ||
| return SPIRV::ExecutionModel::Kernel; | ||
| } | ||
| NonConstSTI->setEnv(SPIRVSubtarget::Shader); | ||
|
Comment on lines
+297
to
+300
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. I just notice this causes the result of isShader to change part way through the compilation. This can cause inconsistent results. I've opened issue #171898 to follow up on this. |
||
|
|
||
| const auto value = attribute.getValueAsString(); | ||
| if (value == "compute") | ||
|
|
@@ -319,7 +342,7 @@ bool SPIRVCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder, | |
| buildOpDecorate(VRegs[i][0], MIRBuilder, | ||
| SPIRV::Decoration::MaxByteOffset, {DerefBytes}); | ||
| } | ||
| if (Arg.hasAttribute(Attribute::Alignment) && !ST->isVulkanEnv()) { | ||
| if (Arg.hasAttribute(Attribute::Alignment) && !ST->isShaderEnv()) { | ||
|
Keenuts marked this conversation as resolved.
Outdated
|
||
| auto Alignment = static_cast<unsigned>( | ||
| Arg.getAttribute(Attribute::Alignment).getValueAsInt()); | ||
| buildOpDecorate(VRegs[i][0], MIRBuilder, SPIRV::Decoration::Alignment, | ||
|
|
@@ -439,6 +462,11 @@ bool SPIRVCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder, | |
|
|
||
| // Handle entry points and function linkage. | ||
| if (isEntryPoint(F)) { | ||
| // EntryPoints can help us to determine the environment we're working on. | ||
| // Therefore, we need a non-const pointer to SPIRVSubtarget to update the | ||
| // environment if we need to. | ||
| const SPIRVSubtarget *ST = | ||
| static_cast<const SPIRVSubtarget *>(&MIRBuilder.getMF().getSubtarget()); | ||
| auto MIB = MIRBuilder.buildInstr(SPIRV::OpEntryPoint) | ||
| .addImm(static_cast<uint32_t>(getExecutionModel(*ST, F))) | ||
| .addUse(FuncVReg); | ||
|
|
||
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
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.
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.
Let me clarify the terminology.
SPIR-V specification defines OpenCL/Vulkan as environments and Kernel/Shader as capabilities.
What exactly are we detecting here: environments or capabilities?
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.
To be honest, I don't see in the SPIR-V spec anything about environments being OpenCL/Vulkan. Anyway, I think what we are detecting here is capabilities because most uses of
isKernelEnv()/isShaderEnv()are used to check if a particular feature is allowed --e.g.Constantdecoration is only allowed ifKernelcapability is enabled. Fundamentally, we want to know if we're dealing with graphical shaders or compute kernels.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.
Please, make the method name clearer. E.g.
or
requiresKernelCapability().Right, SPIR-V spec only says that there are separate specifications:
OpenCL provides The OpenCL™ SPIR-V Environment Specification and Vulkan provides Vulkan Environment for SPIR-V. There are other execution environments supporting SPIR-V formats (e.g. Level Zero)
My understanding is that compiler performs additional checks for restrictions in the environment specification, which is specified as OS component of the target triple.
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.
Would
isCompute()/isShader()work for you?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 prefer using terminology from the spec to avoid ambiguity.
For instance, Vulkan uses "compute shader" term for shaders doing GPGPU computations (i.e. what we call just "Shader" in clang project) - https://vulkan-tutorial.com/Compute_Shader.
Is it okay to use
isKernel/isShader?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.
It works for me. Let's see if any of the people that already approved disagrees: @Keenuts @VyacheslavLevytskyy @MrSidims. If no comments in the next 24 hours, I'll assume they agree and I'll go ahead and change them to
isKernel/isShader.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'm fine with
isKernel/isShader(I'd be againstisComputefor OpenCL as this could be confusing)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.
Updated in 42df23d.