Coverage Report

Created: 2026-07-24 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/third_party/spirv-tools/source/val/construct.cpp
Line
Count
Source
1
// Copyright (c) 2015-2016 The Khronos Group Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "source/val/construct.h"
16
17
#include <cassert>
18
#include <cstddef>
19
20
#include "source/val/function.h"
21
#include "source/val/validation_state.h"
22
23
namespace spvtools {
24
namespace val {
25
26
Construct::Construct(ConstructType construct_type, BasicBlock* entry,
27
                     BasicBlock* exit, std::vector<Construct*> constructs)
28
994
    : type_(construct_type),
29
994
      corresponding_constructs_(constructs),
30
994
      entry_block_(entry),
31
994
      exit_block_(exit) {}
32
33
11.4k
ConstructType Construct::type() const { return type_; }
34
35
192
const std::vector<Construct*>& Construct::corresponding_constructs() const {
36
192
  return corresponding_constructs_;
37
192
}
38
296
std::vector<Construct*>& Construct::corresponding_constructs() {
39
296
  return corresponding_constructs_;
40
296
}
41
42
0
bool ValidateConstructSize(ConstructType type, size_t size) {
43
0
  switch (type) {
44
0
    case ConstructType::kSelection:
45
0
      return size == 0;
46
0
    case ConstructType::kContinue:
47
0
      return size == 1;
48
0
    case ConstructType::kLoop:
49
0
      return size == 1;
50
0
    case ConstructType::kCase:
51
0
      return size >= 1;
52
0
    default:
53
0
      assert(1 == 0 && "Type not defined");
54
0
  }
55
0
  return false;
56
0
}
57
58
void Construct::set_corresponding_constructs(
59
224
    std::vector<Construct*> constructs) {
60
224
  assert(ValidateConstructSize(type_, constructs.size()));
61
224
  corresponding_constructs_ = constructs;
62
224
}
63
64
3.17k
const BasicBlock* Construct::entry_block() const { return entry_block_; }
65
524
BasicBlock* Construct::entry_block() { return entry_block_; }
66
67
3.86k
const BasicBlock* Construct::exit_block() const { return exit_block_; }
68
0
BasicBlock* Construct::exit_block() { return exit_block_; }
69
70
96
void Construct::set_exit(BasicBlock* block) { exit_block_ = block; }
71
72
962
Construct::ConstructBlockSet Construct::blocks(Function* /*function*/) const {
73
962
  const auto header = entry_block();
74
962
  const auto exit = exit_block();
75
962
  const bool is_continue = type() == ConstructType::kContinue;
76
962
  const bool is_loop = type() == ConstructType::kLoop;
77
962
  const BasicBlock* continue_header = nullptr;
78
962
  if (is_loop) {
79
    // The only corresponding construct for a loop is the continue.
80
96
    continue_header = (*corresponding_constructs().begin())->entry_block();
81
96
  }
82
962
  std::vector<BasicBlock*> stack;
83
962
  stack.push_back(const_cast<BasicBlock*>(header));
84
962
  ConstructBlockSet construct_blocks;
85
9.37k
  while (!stack.empty()) {
86
8.41k
    auto* block = stack.back();
87
8.41k
    stack.pop_back();
88
89
8.41k
    if (header->structurally_dominates(*block)) {
90
8.32k
      bool include = false;
91
8.32k
      if (is_continue && exit->structurally_postdominates(*block)) {
92
        // Continue construct include blocks dominated by the continue target
93
        // and post-dominated by the back-edge block.
94
96
        include = true;
95
8.22k
      } else if (!exit->structurally_dominates(*block)) {
96
        // Selection and loop constructs include blocks dominated by the header
97
        // and not dominated by the merge.
98
5.59k
        include = true;
99
5.59k
        if (is_loop && continue_header->structurally_dominates(*block)) {
100
          // Loop constructs have an additional constraint that they do not
101
          // include blocks dominated by the continue construct. Since all
102
          // blocks in the continue construct are dominated by the continue
103
          // target, we just test for dominance by continue target.
104
192
          include = false;
105
192
        }
106
5.59k
      }
107
8.32k
      if (include) {
108
5.50k
        if (!construct_blocks.insert(block).second) continue;
109
110
7.45k
        for (auto succ : *block->structural_successors()) {
111
7.45k
          stack.push_back(succ);
112
7.45k
        }
113
3.80k
      }
114
8.32k
    }
115
8.41k
  }
116
117
962
  return construct_blocks;
118
962
}
119
120
1.95k
bool Construct::IsStructuredExit(ValidationState_t& _, BasicBlock* dest) const {
121
  // Structured Exits:
122
  // - Selection:
123
  //  - branch to its merge
124
  //  - branch to nearest enclosing loop merge or continue
125
  //  - branch to nearest enclosing switch selection merge
126
  // - Loop:
127
  //  - branch to its merge
128
  //  - branch to its continue
129
  // - Continue:
130
  //  - branch to loop header
131
  //  - branch to loop merge
132
  //
133
  // Note: we will never see a case construct here.
134
1.95k
  assert(type() != ConstructType::kCase);
135
1.95k
  if (type() == ConstructType::kLoop) {
136
192
    auto header = entry_block();
137
192
    auto terminator = header->terminator();
138
192
    auto index = terminator - &_.ordered_instructions()[0];
139
192
    auto merge_inst = &_.ordered_instructions()[index - 1];
140
192
    auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
141
192
    auto continue_block_id = merge_inst->GetOperandAs<uint32_t>(1u);
142
192
    if (dest->id() == merge_block_id || dest->id() == continue_block_id) {
143
192
      return true;
144
192
    }
145
1.76k
  } else if (type() == ConstructType::kContinue) {
146
96
    auto loop_construct = corresponding_constructs()[0];
147
96
    auto header = loop_construct->entry_block();
148
96
    auto terminator = header->terminator();
149
96
    auto index = terminator - &_.ordered_instructions()[0];
150
96
    auto merge_inst = &_.ordered_instructions()[index - 1];
151
96
    auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
152
96
    if (dest == header || dest->id() == merge_block_id) {
153
96
      return true;
154
96
    }
155
1.66k
  } else {
156
1.66k
    assert(type() == ConstructType::kSelection);
157
1.66k
    if (dest == exit_block()) {
158
1.66k
      return true;
159
1.66k
    }
160
161
    // The next block in the traversal is either:
162
    //  i.  The header block that declares |block| as its merge block.
163
    //  ii. The immediate dominator of |block|.
164
0
    auto NextBlock = [](const BasicBlock* block) -> const BasicBlock* {
165
0
      for (auto& use : block->label()->uses()) {
166
0
        if ((use.first->opcode() == spv::Op::OpLoopMerge ||
167
0
             use.first->opcode() == spv::Op::OpSelectionMerge) &&
168
0
            use.second == 1 &&
169
0
            use.first->block()->structurally_dominates(*block) &&
170
            // A header likely declared itself as its merge.
171
0
            use.first->block() != block) {
172
0
          return use.first->block();
173
0
        }
174
0
      }
175
0
      return block->immediate_structural_dominator();
176
0
    };
177
178
0
    bool seen_switch = false;
179
0
    auto header = entry_block();
180
0
    auto block = NextBlock(header);
181
0
    while (block) {
182
0
      auto terminator = block->terminator();
183
0
      auto index = terminator - &_.ordered_instructions()[0];
184
0
      auto merge_inst = &_.ordered_instructions()[index - 1];
185
0
      if (merge_inst->opcode() == spv::Op::OpLoopMerge ||
186
0
          (header->terminator()->opcode() != spv::Op::OpSwitch &&
187
0
           merge_inst->opcode() == spv::Op::OpSelectionMerge &&
188
0
           terminator->opcode() == spv::Op::OpSwitch)) {
189
0
        auto merge_target = merge_inst->GetOperandAs<uint32_t>(0u);
190
0
        auto merge_block = merge_inst->function()->GetBlock(merge_target).first;
191
0
        if (merge_block->structurally_dominates(*header)) {
192
0
          block = NextBlock(block);
193
0
          continue;
194
0
        }
195
196
0
        if ((!seen_switch || merge_inst->opcode() == spv::Op::OpLoopMerge) &&
197
0
            dest->id() == merge_target) {
198
0
          return true;
199
0
        } else if (merge_inst->opcode() == spv::Op::OpLoopMerge) {
200
0
          auto continue_target = merge_inst->GetOperandAs<uint32_t>(1u);
201
0
          if (dest->id() == continue_target) {
202
0
            return true;
203
0
          }
204
0
        }
205
206
0
        if (terminator->opcode() == spv::Op::OpSwitch) {
207
0
          seen_switch = true;
208
0
        }
209
210
        // Hit an enclosing loop and didn't break or continue.
211
0
        if (merge_inst->opcode() == spv::Op::OpLoopMerge) return false;
212
0
      }
213
214
0
      block = NextBlock(block);
215
0
    }
216
0
  }
217
218
0
  return false;
219
1.95k
}
220
221
}  // namespace val
222
}  // namespace spvtools