-
Notifications
You must be signed in to change notification settings - Fork 247
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1855,20 +1855,42 @@ SPIRVInstruction *SPIRVModuleImpl::addSelectionMergeInst( | |||||
| SPIRVInstruction *SPIRVModuleImpl::addLoopMergeInst( | ||||||
| SPIRVId MergeBlock, SPIRVId ContinueTarget, SPIRVWord LoopControl, | ||||||
| std::vector<SPIRVWord> LoopControlParameters, SPIRVBasicBlock *BB) { | ||||||
| return addInstruction( | ||||||
| new SPIRVLoopMerge(MergeBlock, ContinueTarget, LoopControl, | ||||||
| LoopControlParameters, BB), | ||||||
| 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) | ||||||
| if (TermInst && (TermInst->getOpCode() == OpBranch || | ||||||
| TermInst->getOpCode() == OpBranchConditional)) { | ||||||
| return addInstruction( | ||||||
| new SPIRVLoopMerge(MergeBlock, ContinueTarget, LoopControl, | ||||||
| LoopControlParameters, BB), | ||||||
| BB, TermInst); | ||||||
| } else { | ||||||
| // If there's no proper terminator, add at the end | ||||||
|
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.
Suggested change
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. 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? |
||||||
| return addInstruction( | ||||||
| new SPIRVLoopMerge(MergeBlock, ContinueTarget, LoopControl, | ||||||
| LoopControlParameters, BB), | ||||||
| BB); | ||||||
| } | ||||||
|
Comment on lines
+1867
to
+1873
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. Probably clang-tidy will recommend to remove else as previous if terminates with return. |
||||||
| } | ||||||
|
|
||||||
| SPIRVInstruction *SPIRVModuleImpl::addLoopControlINTELInst( | ||||||
| SPIRVWord LoopControl, std::vector<SPIRVWord> LoopControlParameters, | ||||||
| SPIRVBasicBlock *BB) { | ||||||
| addCapability(CapabilityUnstructuredLoopControlsINTEL); | ||||||
| addExtension(ExtensionID::SPV_INTEL_unstructured_loop_controls); | ||||||
| return addInstruction( | ||||||
| new SPIRVLoopControlINTEL(LoopControl, LoopControlParameters, BB), BB, | ||||||
| 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) | ||||||
|
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.
Suggested change
|
||||||
| if (TermInst && (TermInst->getOpCode() == OpBranch || | ||||||
| TermInst->getOpCode() == OpBranchConditional)) { | ||||||
| return addInstruction( | ||||||
| new SPIRVLoopControlINTEL(LoopControl, LoopControlParameters, BB), BB, | ||||||
| TermInst); | ||||||
| } else { | ||||||
| // If there's no proper terminator, add at the end | ||||||
|
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.
Suggested change
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. Same than earlier. Is this compliant with the spec? Should we assert/report error instead? |
||||||
| return addInstruction( | ||||||
| new SPIRVLoopControlINTEL(LoopControl, LoopControlParameters, BB), BB); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| SPIRVInstruction *SPIRVModuleImpl::addFixedPointIntelInst( | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| ; Test case for OpLoopMerge instruction placement validation. | ||
| ; 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 | ||
|
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. Please move .bc file content to the text and call llvm-as in the test before the translation. 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. +1. |
||
| ; RUN: spirv-val %t.spv | ||
|
|
||
| ; RUN: llvm-spirv --to-text %t.spv -o - | FileCheck %s --check-prefix=CHECK-SPIRV-TEXT | ||
| ; CHECK-SPIRV-TEXT: LoopMerge | ||
| ; CHECK-SPIRV-TEXT-NEXT: BranchConditional | ||
|
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. Please, add newline at end of file. |
||
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.