Skip to content

Fix LoopMerge Instruction Placement#3277

Merged
MrSidims merged 2 commits intoKhronosGroup:mainfrom
CHIP-SPV:LoopMerge
Jan 6, 2026
Merged

Fix LoopMerge Instruction Placement#3277
MrSidims merged 2 commits intoKhronosGroup:mainfrom
CHIP-SPV:LoopMerge

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

@pvelesko pvelesko force-pushed the LoopMerge branch 2 times, most recently from da5d2bf to 27b6c29 Compare December 15, 2025 05:01
@pvelesko
Copy link
Contributor Author

pvelesko commented Jan 6, 2026

@MrSidims

Any ETA on fixing the CI?
Any more comments/TODOs for me in regards to this PR?

@MrSidims
Copy link
Contributor

MrSidims commented Jan 6, 2026

Restarting out-of-tree build with new HEAD

@MrSidims MrSidims closed this Jan 6, 2026
@MrSidims MrSidims reopened this Jan 6, 2026
@MrSidims
Copy link
Contributor

MrSidims commented Jan 6, 2026

/home/runner/work/SPIRV-LLVM-Translator/SPIRV-LLVM-Translator/SPIRV-LLVM-Translator/lib/SPIRV/PassPlugin.cpp:52:10: fatal error: llvm/Plugins/PassPlugin.h: No such file or directory
52 | #include "llvm/Plugins/PassPlugin.h"

Is expected, it requires yet another package update in apt.llvm.org. Unrelated to this project and not gating this PR

@MrSidims MrSidims merged commit 691d303 into KhronosGroup:main Jan 6, 2026
12 of 18 checks passed
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