Add patch and regression test for invalid SPIR-V produced from duplicate-predecessor phi nodes #1306
Draft
colleeneb wants to merge 4 commits into
Draft
Add patch and regression test for invalid SPIR-V produced from duplicate-predecessor phi nodes #1306colleeneb wants to merge 4 commits into
colleeneb wants to merge 4 commits into
Conversation
Contributor
Author
|
/run-aurora-ci |
Collaborator
|
nice. how did you find this? |
Contributor
Author
|
Ah, it showed up in one of the applications on Aurora -- the JIT was failing, so I ran the validator on it and found it. The code has -O3 + |
Collaborator
|
Need to investigate this further. |
Contributor
Author
|
I don't think HipVerify caught it, at least I didn't see an error at compile or runtime. But if you run the example code, dump the SPIRV, and then run spir-val on it, you can see that it fails validation: The code in main.cpp: Let me know if I missed something! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
TLDR: This is a patch for the SPIRV translator to avoid producing invalid SPIRV. A C++ reproducer and test is included. Feedback on the patch is needed since it was done with the help of Claude and although it seems correct I am not a SPIRV expert.
Background: The SPIRV translator produces invalid SPV from LLVM IR for the case where LLVM IR has a phi node with multiple entries that have the same predecessor.
For example:
In LLVM IR, duplicate entries are legal only if in the value in the entries which have the same predecessor are all the same (https://github.com/llvm/llvm-project/blob/ca7933e47d3a3451d81e72ac174dcb5aa28b59d1/llvm/lib/IR/Verifier.cpp#L3406 ). LLVM may generate IR with the entries with the same predecessor and the same value in code with switch/case statements (see the C++ example in
tests/compiler/TestSpirvDuplicatePhiHip.hip)However, if the SPIRV translator encounters code like this, it currently produces:
This SPIRV is invalid since SPIRV does not allow multiple entries in the
OpPhiwith the same predecessor (https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpPh "There must be exactly one Parent i for each parent block of the current block in the CFG.”), and runningspirv-valfails on it (https://github.com/KhronosGroup/SPIRV-Tools/blob/58fe144fdc8847b303be51d4f8fcc9e7da17056e/source/val/validate_cfg.cpp#L88)This is also discussed in KhronosGroup/SPIRV-LLVM-Translator#2702 . The PR that closed it (KhronosGroup/SPIRV-LLVM-Translator#2736) is only for the translation of SPIRV to LLVM IR, not the LLVM IR to SPIRV direction. (
This PR partially fixes issue #2702 in the part that is responsible for SPIR-V to LLVM IR translation.).This patch collapses the duplicate entries into one entry. In my understanding, this shouldn’t lose any information as all the duplicate entries are the same.