Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion source/opt/loop_descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ bool Loop::IsLCSSA() const {
&insn,
[&exit_blocks, ir_context, this](Instruction* use) -> bool {
BasicBlock* parent = ir_context->get_instr_block(use);
assert(parent && "Invalid analysis");
// Annotations and debug names are not in any block.
if (!parent) return true;
if (IsInsideLoop(parent)) return true;
if (use->opcode() != spv::Op::OpPhi) return false;
return exit_blocks.count(parent->id());
Expand Down
12 changes: 6 additions & 6 deletions source/opt/loop_fusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ bool LoopFusion::UsedInContinueOrConditionBlock(Instruction* phi_instruction,
auto not_used = context_->get_def_use_mgr()->WhileEachUser(
phi_instruction,
[this, condition_block, continue_block](Instruction* instruction) {
auto block_id = context_->get_instr_block(instruction)->id();
return block_id != condition_block && block_id != continue_block;
// Annotations and debug names are not in any block.
BasicBlock* block = context_->get_instr_block(instruction);
if (!block) return true;
return block->id() != condition_block && block->id() != continue_block;
});

return !not_used;
Expand Down Expand Up @@ -381,10 +383,8 @@ LoopFusion::GetLoadsAndStoresInLoop(Loop* loop) {

bool LoopFusion::IsUsedInLoop(Instruction* instruction, Loop* loop) {
auto not_used = context_->get_def_use_mgr()->WhileEachUser(
instruction, [this, loop](Instruction* user) {
auto block_id = context_->get_instr_block(user)->id();
return !loop->IsInsideLoop(block_id);
});
instruction,
[loop](Instruction* user) { return !loop->IsInsideLoop(user); });

return !not_used;
}
Expand Down
3 changes: 2 additions & 1 deletion source/opt/loop_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ inline bool MakeSetClosedSSA(IRContext* context, Function* function,
&inst, [&blocks, &rewriter, &exit_bb, context](
Instruction* use, uint32_t operand_index) {
BasicBlock* use_parent = context->get_instr_block(use);
assert(use_parent);
// Annotations and debug names are not in any block.
if (!use_parent) return true;
if (blocks.count(use_parent->id())) return true;

if (use->opcode() == spv::Op::OpPhi) {
Expand Down
61 changes: 61 additions & 0 deletions test/opt/loop_optimizations/fusion_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,67 @@ TEST_F(FusionPassTest, SimpleFusion) {
SinglePassRunAndMatch<LoopFusionPass>(text, true, 20);
}

// The decorations of the loop phis are users that belong to no basic block.
TEST_F(FusionPassTest, DecoratedLoopValues) {
const std::string text = R"(
; CHECK: OpLoopMerge
; CHECK-NOT: OpLoopMerge
; CHECK: OpFunctionEnd

OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %out
OpExecutionMode %main OriginUpperLeft
OpDecorate %out Location 0
OpDecorate %i RelaxedPrecision
OpDecorate %j RelaxedPrecision
%void = OpTypeVoid
%fn = OpTypeFunction %void
%bool = OpTypeBool
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%int_1 = OpConstant %int 1
%int_10 = OpConstant %int 10
%ptr_out = OpTypePointer Output %int
%out = OpVariable %ptr_out Output
%main = OpFunction %void None %fn
%entry = OpLabel
OpBranch %h0
%h0 = OpLabel
%i = OpPhi %int %int_0 %entry %i1 %c0
OpLoopMerge %m0 %c0 None
OpBranch %t0
%t0 = OpLabel
%cmp0 = OpSLessThan %bool %i %int_10
OpBranchConditional %cmp0 %b0 %m0
%b0 = OpLabel
OpBranch %c0
%c0 = OpLabel
%i1 = OpIAdd %int %i %int_1
OpBranch %h0
%m0 = OpLabel
OpBranch %h1
%h1 = OpLabel
%j = OpPhi %int %int_0 %m0 %j1 %c1
OpLoopMerge %m1 %c1 None
OpBranch %t1
%t1 = OpLabel
%cmp1 = OpSLessThan %bool %j %int_10
OpBranchConditional %cmp1 %b1 %m1
%b1 = OpLabel
OpBranch %c1
%c1 = OpLabel
%j1 = OpIAdd %int %j %int_1
OpBranch %h1
%m1 = OpLabel
OpStore %out %j
OpReturn
OpFunctionEnd
)";

SinglePassRunAndMatch<LoopFusionPass>(text, true, 5);
}

/*
Generated from the following GLSL + --eliminate-local-multi-store

Expand Down
50 changes: 50 additions & 0 deletions test/opt/loop_optimizations/peeling_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,56 @@ TEST_F(PeelingPassTest, PeelingNoChanges) {
}
}

// The decorations of the loop phi are users that belong to no basic block.
TEST_F(PeelingPassTest, DecoratedLoopValueUsedAfterLoop) {
const std::string text = R"(
; CHECK: OpLoopMerge [[merge:%\w+]] %{{\w+}} None
; CHECK: [[merge]] = OpLabel
; CHECK-NEXT: [[phi:%\w+]] = OpPhi %int
; CHECK-NEXT: OpConvertSToF %float [[phi]]

OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %out
OpExecutionMode %main OriginUpperLeft
OpDecorate %out Location 0
OpDecorate %i RelaxedPrecision
OpDecorate %inc RelaxedPrecision
%void = OpTypeVoid
%fn = OpTypeFunction %void
%bool = OpTypeBool
%int = OpTypeInt 32 1
%float = OpTypeFloat 32
%int_0 = OpConstant %int 0
%int_1 = OpConstant %int 1
%int_10 = OpConstant %int 10
%ptr_out = OpTypePointer Output %float
%out = OpVariable %ptr_out Output
%main = OpFunction %void None %fn
%entry = OpLabel
OpBranch %header
%header = OpLabel
%i = OpPhi %int %int_0 %entry %inc %continue
OpLoopMerge %merge %continue None
OpBranch %cond
%cond = OpLabel
%cmp = OpSLessThan %bool %i %int_10
OpBranchConditional %cmp %body %merge
%body = OpLabel
OpBranch %continue
%continue = OpLabel
%inc = OpIAdd %int %i %int_1
OpBranch %header
%merge = OpLabel
%f = OpConvertSToF %float %i
OpStore %out %f
OpReturn
OpFunctionEnd
)";

SinglePassRunAndMatch<LoopPeelingPass>(text, true);
}

} // namespace
} // namespace opt
} // namespace spvtools
Loading