File: remove_block_reduction_opportunity_finder.cpp

package info (click to toggle)
spirv-tools 2023.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,608 kB
  • sloc: cpp: 408,882; javascript: 5,890; python: 2,847; ansic: 403; sh: 385; ruby: 88; makefile: 17; lisp: 9
file content (95 lines) | stat: -rw-r--r-- 3,238 bytes parent folder | download | duplicates (21)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "source/reduce/remove_block_reduction_opportunity_finder.h"

#include "source/reduce/remove_block_reduction_opportunity.h"

namespace spvtools {
namespace reduce {

std::string RemoveBlockReductionOpportunityFinder::GetName() const {
  return "RemoveBlockReductionOpportunityFinder";
}

std::vector<std::unique_ptr<ReductionOpportunity>>
RemoveBlockReductionOpportunityFinder::GetAvailableOpportunities(
    opt::IRContext* context, uint32_t target_function) const {
  std::vector<std::unique_ptr<ReductionOpportunity>> result;

  // Consider every block in every relevant function.
  for (auto* function : GetTargetFunctions(context, target_function)) {
    for (auto bi = function->begin(); bi != function->end(); ++bi) {
      if (IsBlockValidOpportunity(context, function, &bi)) {
        result.push_back(MakeUnique<RemoveBlockReductionOpportunity>(
            context, function, &*bi));
      }
    }
  }
  return result;
}

bool RemoveBlockReductionOpportunityFinder::IsBlockValidOpportunity(
    opt::IRContext* context, opt::Function* function,
    opt::Function::iterator* bi) {
  assert(*bi != function->end() && "Block iterator was out of bounds");

  // Don't remove first block; we don't want to end up with no blocks.
  if (*bi == function->begin()) {
    return false;
  }

  // Don't remove blocks with references.
  if (context->get_def_use_mgr()->NumUsers((*bi)->id()) > 0) {
    return false;
  }

  // Don't remove blocks whose instructions have outside references.
  if (!BlockInstructionsHaveNoOutsideReferences(context, *bi)) {
    return false;
  }

  return true;
}

bool RemoveBlockReductionOpportunityFinder::
    BlockInstructionsHaveNoOutsideReferences(
        opt::IRContext* context, const opt::Function::iterator& bi) {
  // Get all instructions in block.
  std::unordered_set<uint32_t> instructions_in_block;
  for (const opt::Instruction& instruction : *bi) {
    instructions_in_block.insert(instruction.unique_id());
  }

  // For each instruction...
  for (const opt::Instruction& instruction : *bi) {
    // For each use of the instruction...
    bool no_uses_outside_block = context->get_def_use_mgr()->WhileEachUser(
        &instruction, [&instructions_in_block](opt::Instruction* user) -> bool {
          // If the use is in this block, continue (return true). Otherwise, we
          // found an outside use; return false (and stop).
          return instructions_in_block.find(user->unique_id()) !=
                 instructions_in_block.end();
        });

    if (!no_uses_outside_block) {
      return false;
    }
  }

  return true;
}

}  // namespace reduce
}  // namespace spvtools