Skip to content

Fix LoopMerge Instruction Placement #3277

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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

pvelesko
Copy link
Contributor

The SPIR-V specification requires that OpLoopMerge instructions must be placed as the second-to-last instruction in their basic block, immediately preceding the branch instruction (OpBranch or OpBranchConditional). The original implementation was not ensuring this correct placement, which could lead to SPIR-V validation failures.

  1. Added validation logic to check if the basic block has a proper terminating instruction (OpBranch or OpBranchConditional)
  2. Conditional placement:
  • If a proper terminator exists → Place the LoopMerge instruction immediately before it
  • If no proper terminator exists → Add the instruction at the end of the block
  1. Added a test case (test/OpLoopMerge_loopMerge.ll) that:
  • Verifies SPIR-V validation passes with spirv-val
  • Checks that LoopMerge appears before BranchConditional in the text output

@MrSidims
Copy link
Contributor

Restarting CI with the new HEAD

@MrSidims MrSidims closed this Jul 28, 2025
@MrSidims MrSidims reopened this Jul 28, 2025
; This test verifies that OpLoopMerge instructions are properly placed as the second-to-last
; instruction in their basic block, immediately preceding the branch instruction.

; RUN: llvm-spirv %S/loopMerge.bc -o %t.spv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move .bc file content to the text and call llvm-as in the test before the translation.

Comment on lines +9 to +10
; CHECK-SPIRV-TEXT: LoopMerge
; CHECK-SPIRV-TEXT: BranchConditional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
; CHECK-SPIRV-TEXT: LoopMerge
; CHECK-SPIRV-TEXT: BranchConditional
; CHECK-SPIRV-TEXT: LoopMerge
; CHECK-SPIRV-TEXT-NEXT: BranchConditional

Comment on lines +1867 to +1873
} else {
// If there's no proper terminator, add at the end
return addInstruction(
new SPIRVLoopMerge(MergeBlock, ContinueTarget, LoopControl,
LoopControlParameters, BB),
BB);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably clang-tidy will recommend to remove else as previous if terminates with return.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants