Coverage Report

Created: 2026-08-14 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spirv-tools/source/opt/loop_fusion_pass.cpp
Line
Count
Source
1
// Copyright (c) 2018 Google LLC.
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/opt/loop_fusion_pass.h"
16
17
#include "source/opt/loop_descriptor.h"
18
#include "source/opt/loop_fusion.h"
19
#include "source/opt/register_pressure.h"
20
21
namespace spvtools {
22
namespace opt {
23
24
0
Pass::Status LoopFusionPass::Process() {
25
0
  Status status = Status::SuccessWithoutChange;
26
0
  Module* module = context()->module();
27
28
  // Process each function in the module
29
0
  for (Function& f : *module) {
30
0
    status = CombineStatus(status, ProcessFunction(&f));
31
0
    if (status == Status::Failure) return Status::Failure;
32
0
  }
33
34
0
  return status;
35
0
}
36
37
0
Pass::Status LoopFusionPass::ProcessFunction(Function* function) {
38
0
  LoopDescriptor& ld = *context()->GetLoopDescriptor(function);
39
40
  // If a loop doesn't have a preheader needs then it needs to be created. Make
41
  // sure to return Status::SuccessWithChange in that case.
42
0
  bool modified = false;
43
0
  auto status = ld.CreatePreHeaderBlocksIfMissing();
44
0
  if (status == LoopDescriptor::Status::Failure) return Status::Failure;
45
0
  modified = status == LoopDescriptor::Status::SuccessWithChange;
46
47
  // TODO(tremmelg): Could the only loop that |loop| could possibly be fused be
48
  // picked out so don't have to check every loop
49
0
  for (auto& loop_0 : ld) {
50
0
    for (auto& loop_1 : ld) {
51
0
      LoopFusion fusion(context(), &loop_0, &loop_1);
52
53
0
      if (fusion.AreCompatible() && fusion.IsLegal()) {
54
0
        RegisterLiveness liveness(context(), function);
55
0
        RegisterLiveness::RegionRegisterLiveness reg_pressure{};
56
0
        liveness.SimulateFusion(loop_0, loop_1, &reg_pressure);
57
58
0
        if (reg_pressure.used_registers_ <= max_registers_per_loop_) {
59
0
          fusion.Fuse();
60
          // Recurse, as the current iterators will have been invalidated.
61
0
          ProcessFunction(function);
62
0
          return Status::SuccessWithChange;
63
0
        }
64
0
      }
65
0
    }
66
0
  }
67
68
0
  return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
69
0
}
70
71
}  // namespace opt
72
}  // namespace spvtools