Skip to content

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1.

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.

BB, const_cast<SPIRVInstruction *>(BB->getTerminateInstr()));
SPIRVInstruction *TermInst = const_cast<SPIRVInstruction *>(BB->getTerminateInstr());
// OpLoopMerge must be the second-to-last instruction in the block,
// immediately preceding the branch instruction (OpBranch or OpBranchConditional)
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
// immediately preceding the branch instruction (OpBranch or OpBranchConditional)
// immediately preceding the branch instruction (OpBranch or OpBranchConditional).

LoopControlParameters, BB),
BB, TermInst);
} else {
// If there's no proper terminator, add at the end
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
// If there's no proper terminator, add at the end
// If there's no proper terminator, add at the end.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, is this correct? You mentioned that the spec requires this instruction to immediately precede either an OpBranch or an OpBranchConditional. My understanding is that, if none of these exist, this instruction could not be placed. Maybe we should assert here instead?

const_cast<SPIRVInstruction *>(BB->getTerminateInstr()));
SPIRVInstruction *TermInst = const_cast<SPIRVInstruction *>(BB->getTerminateInstr());
// OpLoopControlINTEL must be the second-to-last instruction in the block,
// immediately preceding the branch instruction (OpBranch or OpBranchConditional)
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
// immediately preceding the branch instruction (OpBranch or OpBranchConditional)
// immediately preceding the branch instruction (OpBranch or OpBranchConditional).

new SPIRVLoopControlINTEL(LoopControl, LoopControlParameters, BB), BB,
TermInst);
} else {
// If there's no proper terminator, add at the end
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
// If there's no proper terminator, add at the end
// If there's no proper terminator, add at the end.

Copy link
Contributor

Choose a reason for hiding this comment

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

Same than earlier. Is this compliant with the spec? Should we assert/report error instead?

; 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.

+1.


; RUN: llvm-spirv --to-text %t.spv -o - | FileCheck %s --check-prefix=CHECK-SPIRV-TEXT
; CHECK-SPIRV-TEXT: LoopMerge
; CHECK-SPIRV-TEXT-NEXT: BranchConditional No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, add newline at end of file.

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.

4 participants