Coverage Report

Created: 2025-08-29 07:31

/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
4.40k
    : type_(construct_type),
29
4.40k
      corresponding_constructs_(constructs),
30
4.40k
      entry_block_(entry),
31
4.40k
      exit_block_(exit) {}
32
33
52.4k
ConstructType Construct::type() const { return type_; }
34
35
1.95k
const std::vector<Construct*>& Construct::corresponding_constructs() const {
36
1.95k
  return corresponding_constructs_;
37
1.95k
}
38
2.85k
std::vector<Construct*>& Construct::corresponding_constructs() {
39
2.85k
  return corresponding_constructs_;
40
2.85k
}
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.94k
    std::vector<Construct*> constructs) {
60
1.94k
  assert(ValidateConstructSize(type_, constructs.size()));
61
1.94k
  corresponding_constructs_ = constructs;
62
1.94k
}
63
64
15.2k
const BasicBlock* Construct::entry_block() const { return entry_block_; }
65
6.50k
BasicBlock* Construct::entry_block() { return entry_block_; }
66
67
15.5k
const BasicBlock* Construct::exit_block() const { return exit_block_; }
68
0
BasicBlock* Construct::exit_block() { return exit_block_; }
69
70
946
void Construct::set_exit(BasicBlock* block) { exit_block_ = block; }
71
72
4.35k
Construct::ConstructBlockSet Construct::blocks(Function* /*function*/) const {
73
4.35k
  const auto header = entry_block();
74
4.35k
  const auto exit = exit_block();
75
4.35k
  const bool is_continue = type() == ConstructType::kContinue;
76
4.35k
  const bool is_loop = type() == ConstructType::kLoop;
77
4.35k
  const BasicBlock* continue_header = nullptr;
78
4.35k
  if (is_loop) {
79
    // The only corresponding construct for a loop is the continue.
80
946
    continue_header = (*corresponding_constructs().begin())->entry_block();
81
946
  }
82
4.35k
  std::vector<BasicBlock*> stack;
83
4.35k
  stack.push_back(const_cast<BasicBlock*>(header));
84
4.35k
  ConstructBlockSet construct_blocks;
85
36.1k
  while (!stack.empty()) {
86
31.7k
    auto* block = stack.back();
87
31.7k
    stack.pop_back();
88
89
31.7k
    if (header->structurally_dominates(*block)) {
90
30.7k
      bool include = false;
91
30.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
946
        include = true;
95
29.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
19.9k
        include = true;
99
19.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.89k
          include = false;
105
1.89k
        }
106
19.9k
      }
107
30.7k
      if (include) {
108
19.0k
        if (!construct_blocks.insert(block).second) continue;
109
110
27.4k
        for (auto succ : *block->structural_successors()) {
111
27.4k
          stack.push_back(succ);
112
27.4k
        }
113
14.3k
      }
114
30.7k
    }
115
31.7k
  }
116
117
4.35k
  return construct_blocks;
118
4.35k
}
119
120
8.38k
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
8.38k
  assert(type() != ConstructType::kCase);
135
8.38k
  if (type() == ConstructType::kLoop) {
136
1.83k
    auto header = entry_block();
137
1.83k
    auto terminator = header->terminator();
138
1.83k
    auto index = terminator - &_.ordered_instructions()[0];
139
1.83k
    auto merge_inst = &_.ordered_instructions()[index - 1];
140
1.83k
    auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
141
1.83k
    auto continue_block_id = merge_inst->GetOperandAs<uint32_t>(1u);
142
1.83k
    if (dest->id() == merge_block_id || dest->id() == continue_block_id) {
143
1.83k
      return true;
144
1.83k
    }
145
6.55k
  } else if (type() == ConstructType::kContinue) {
146
1.00k
    auto loop_construct = corresponding_constructs()[0];
147
1.00k
    auto header = loop_construct->entry_block();
148
1.00k
    auto terminator = header->terminator();
149
1.00k
    auto index = terminator - &_.ordered_instructions()[0];
150
1.00k
    auto merge_inst = &_.ordered_instructions()[index - 1];
151
1.00k
    auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
152
1.00k
    if (dest == header || dest->id() == merge_block_id) {
153
1.00k
      return true;
154
1.00k
    }
155
5.54k
  } else {
156
5.54k
    assert(type() == ConstructType::kSelection);
157
5.54k
    if (dest == exit_block()) {
158
5.54k
      return true;
159
5.54k
    }
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
8.38k
}
220
221
}  // namespace val
222
}  // namespace spvtools