Coverage Report

Created: 2025-07-11 07:07

/src/shaderc/third_party/spirv-tools/source/val/construct.cpp
Line
Count
Source (jump to first uncovered line)
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
3.84k
    : type_(construct_type),
29
3.84k
      corresponding_constructs_(constructs),
30
3.84k
      entry_block_(entry),
31
3.84k
      exit_block_(exit) {}
32
33
45.9k
ConstructType Construct::type() const { return type_; }
34
35
2.00k
const std::vector<Construct*>& Construct::corresponding_constructs() const {
36
2.00k
  return corresponding_constructs_;
37
2.00k
}
38
2.90k
std::vector<Construct*>& Construct::corresponding_constructs() {
39
2.90k
  return corresponding_constructs_;
40
2.90k
}
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
1.99k
    std::vector<Construct*> constructs) {
60
1.99k
  assert(ValidateConstructSize(type_, constructs.size()));
61
1.99k
  corresponding_constructs_ = constructs;
62
1.99k
}
63
64
13.4k
const BasicBlock* Construct::entry_block() const { return entry_block_; }
65
6.93k
BasicBlock* Construct::entry_block() { return entry_block_; }
66
67
12.8k
const BasicBlock* Construct::exit_block() const { return exit_block_; }
68
0
BasicBlock* Construct::exit_block() { return exit_block_; }
69
70
964
void Construct::set_exit(BasicBlock* block) { exit_block_ = block; }
71
72
3.78k
Construct::ConstructBlockSet Construct::blocks(Function* /*function*/) const {
73
3.78k
  const auto header = entry_block();
74
3.78k
  const auto exit = exit_block();
75
3.78k
  const bool is_continue = type() == ConstructType::kContinue;
76
3.78k
  const bool is_loop = type() == ConstructType::kLoop;
77
3.78k
  const BasicBlock* continue_header = nullptr;
78
3.78k
  if (is_loop) {
79
    // The only corresponding construct for a loop is the continue.
80
964
    continue_header = (*corresponding_constructs().begin())->entry_block();
81
964
  }
82
3.78k
  std::vector<BasicBlock*> stack;
83
3.78k
  stack.push_back(const_cast<BasicBlock*>(header));
84
3.78k
  ConstructBlockSet construct_blocks;
85
29.6k
  while (!stack.empty()) {
86
25.8k
    auto* block = stack.back();
87
25.8k
    stack.pop_back();
88
89
25.8k
    if (header->structurally_dominates(*block)) {
90
24.7k
      bool include = false;
91
24.7k
      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
964
        include = true;
95
23.8k
      } 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
15.9k
        include = true;
99
15.9k
        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
1.92k
          include = false;
105
1.92k
        }
106
15.9k
      }
107
24.7k
      if (include) {
108
14.9k
        if (!construct_blocks.insert(block).second) continue;
109
110
22.0k
        for (auto succ : *block->structural_successors()) {
111
22.0k
          stack.push_back(succ);
112
22.0k
        }
113
11.6k
      }
114
24.7k
    }
115
25.8k
  }
116
117
3.78k
  return construct_blocks;
118
3.78k
}
119
120
7.09k
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
7.09k
  assert(type() != ConstructType::kCase);
135
7.09k
  if (type() == ConstructType::kLoop) {
136
1.85k
    auto header = entry_block();
137
1.85k
    auto terminator = header->terminator();
138
1.85k
    auto index = terminator - &_.ordered_instructions()[0];
139
1.85k
    auto merge_inst = &_.ordered_instructions()[index - 1];
140
1.85k
    auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
141
1.85k
    auto continue_block_id = merge_inst->GetOperandAs<uint32_t>(1u);
142
1.85k
    if (dest->id() == merge_block_id || dest->id() == continue_block_id) {
143
1.85k
      return true;
144
1.85k
    }
145
5.24k
  } else if (type() == ConstructType::kContinue) {
146
1.04k
    auto loop_construct = corresponding_constructs()[0];
147
1.04k
    auto header = loop_construct->entry_block();
148
1.04k
    auto terminator = header->terminator();
149
1.04k
    auto index = terminator - &_.ordered_instructions()[0];
150
1.04k
    auto merge_inst = &_.ordered_instructions()[index - 1];
151
1.04k
    auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
152
1.04k
    if (dest == header || dest->id() == merge_block_id) {
153
1.04k
      return true;
154
1.04k
    }
155
4.20k
  } else {
156
4.20k
    assert(type() == ConstructType::kSelection);
157
4.20k
    if (dest == exit_block()) {
158
4.20k
      return true;
159
4.20k
    }
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
7.09k
}
220
221
}  // namespace val
222
}  // namespace spvtools