diff --git a/source/opt/loop_descriptor.cpp b/source/opt/loop_descriptor.cpp index b0064b4f2c..01726746dd 100644 --- a/source/opt/loop_descriptor.cpp +++ b/source/opt/loop_descriptor.cpp @@ -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()); diff --git a/source/opt/loop_fusion.cpp b/source/opt/loop_fusion.cpp index dc63553066..6ff5b3da70 100644 --- a/source/opt/loop_fusion.cpp +++ b/source/opt/loop_fusion.cpp @@ -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; @@ -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; } diff --git a/source/opt/loop_utils.cpp b/source/opt/loop_utils.cpp index dffb47fc6f..7603f107fb 100644 --- a/source/opt/loop_utils.cpp +++ b/source/opt/loop_utils.cpp @@ -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) { diff --git a/test/opt/loop_optimizations/fusion_pass.cpp b/test/opt/loop_optimizations/fusion_pass.cpp index 9493923750..2273855136 100644 --- a/test/opt/loop_optimizations/fusion_pass.cpp +++ b/test/opt/loop_optimizations/fusion_pass.cpp @@ -128,6 +128,67 @@ TEST_F(FusionPassTest, SimpleFusion) { SinglePassRunAndMatch(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(text, true, 5); +} + /* Generated from the following GLSL + --eliminate-local-multi-store diff --git a/test/opt/loop_optimizations/peeling_pass.cpp b/test/opt/loop_optimizations/peeling_pass.cpp index 16877623b3..07748d2a22 100644 --- a/test/opt/loop_optimizations/peeling_pass.cpp +++ b/test/opt/loop_optimizations/peeling_pass.cpp @@ -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(text, true); +} + } // namespace } // namespace opt } // namespace spvtools