Coverage Report

Created: 2024-09-11 07:09

/src/spirv-tools/source/opt/private_to_local_pass.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2017 Google 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/opt/private_to_local_pass.h"
16
17
#include <memory>
18
#include <utility>
19
#include <vector>
20
21
#include "source/opt/ir_context.h"
22
#include "source/spirv_constant.h"
23
24
namespace spvtools {
25
namespace opt {
26
namespace {
27
constexpr uint32_t kVariableStorageClassInIdx = 0;
28
constexpr uint32_t kSpvTypePointerTypeIdInIdx = 1;
29
}  // namespace
30
31
10.7k
Pass::Status PrivateToLocalPass::Process() {
32
10.7k
  bool modified = false;
33
34
  // Private variables require the shader capability.  If this is not a shader,
35
  // there is no work to do.
36
10.7k
  if (context()->get_feature_mgr()->HasCapability(spv::Capability::Addresses))
37
311
    return Status::SuccessWithoutChange;
38
39
10.4k
  std::vector<std::pair<Instruction*, Function*>> variables_to_move;
40
10.4k
  std::unordered_set<uint32_t> localized_variables;
41
237k
  for (auto& inst : context()->types_values()) {
42
237k
    if (inst.opcode() != spv::Op::OpVariable) {
43
217k
      continue;
44
217k
    }
45
46
20.5k
    if (spv::StorageClass(inst.GetSingleWordInOperand(
47
20.5k
            kVariableStorageClassInIdx)) != spv::StorageClass::Private) {
48
14.5k
      continue;
49
14.5k
    }
50
51
5.97k
    Function* target_function = FindLocalFunction(inst);
52
5.97k
    if (target_function != nullptr) {
53
1.89k
      variables_to_move.push_back({&inst, target_function});
54
1.89k
    }
55
5.97k
  }
56
57
10.4k
  modified = !variables_to_move.empty();
58
10.4k
  for (auto p : variables_to_move) {
59
1.89k
    if (!MoveVariable(p.first, p.second)) {
60
0
      return Status::Failure;
61
0
    }
62
1.89k
    localized_variables.insert(p.first->result_id());
63
1.89k
  }
64
65
10.4k
  if (get_module()->version() >= SPV_SPIRV_VERSION_WORD(1, 4)) {
66
    // In SPIR-V 1.4 and later entry points must list private storage class
67
    // variables that are statically used by the entry point. Go through the
68
    // entry points and remove any references to variables that were localized.
69
0
    for (auto& entry : get_module()->entry_points()) {
70
0
      std::vector<Operand> new_operands;
71
0
      for (uint32_t i = 0; i < entry.NumInOperands(); ++i) {
72
        // Execution model, function id and name are always kept.
73
0
        if (i < 3 ||
74
0
            !localized_variables.count(entry.GetSingleWordInOperand(i))) {
75
0
          new_operands.push_back(entry.GetInOperand(i));
76
0
        }
77
0
      }
78
0
      if (new_operands.size() != entry.NumInOperands()) {
79
0
        entry.SetInOperands(std::move(new_operands));
80
0
        context()->AnalyzeUses(&entry);
81
0
      }
82
0
    }
83
0
  }
84
85
10.4k
  return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
86
10.4k
}
87
88
5.97k
Function* PrivateToLocalPass::FindLocalFunction(const Instruction& inst) const {
89
5.97k
  bool found_first_use = false;
90
5.97k
  Function* target_function = nullptr;
91
5.97k
  context()->get_def_use_mgr()->ForEachUser(
92
5.97k
      inst.result_id(),
93
43.2k
      [&target_function, &found_first_use, this](Instruction* use) {
94
43.2k
        BasicBlock* current_block = context()->get_instr_block(use);
95
43.2k
        if (current_block == nullptr) {
96
694
          return;
97
694
        }
98
99
42.5k
        if (!IsValidUse(use)) {
100
3.00k
          found_first_use = true;
101
3.00k
          target_function = nullptr;
102
3.00k
          return;
103
3.00k
        }
104
39.5k
        Function* current_function = current_block->GetParent();
105
39.5k
        if (!found_first_use) {
106
2.21k
          found_first_use = true;
107
2.21k
          target_function = current_function;
108
37.3k
        } else if (target_function != current_function) {
109
6.23k
          target_function = nullptr;
110
6.23k
        }
111
39.5k
      });
112
5.97k
  return target_function;
113
5.97k
}  // namespace opt
114
115
bool PrivateToLocalPass::MoveVariable(Instruction* variable,
116
1.89k
                                      Function* function) {
117
  // The variable needs to be removed from the global section, and placed in the
118
  // header of the function.  First step remove from the global list.
119
1.89k
  variable->RemoveFromList();
120
1.89k
  std::unique_ptr<Instruction> var(variable);  // Take ownership.
121
1.89k
  context()->ForgetUses(variable);
122
123
  // Update the storage class of the variable.
124
1.89k
  variable->SetInOperand(kVariableStorageClassInIdx,
125
1.89k
                         {uint32_t(spv::StorageClass::Function)});
126
127
  // Update the type as well.
128
1.89k
  uint32_t new_type_id = GetNewType(variable->type_id());
129
1.89k
  if (new_type_id == 0) {
130
0
    return false;
131
0
  }
132
1.89k
  variable->SetResultType(new_type_id);
133
134
  // Place the variable at the start of the first basic block.
135
1.89k
  context()->AnalyzeUses(variable);
136
1.89k
  context()->set_instr_block(variable, &*function->begin());
137
1.89k
  function->begin()->begin()->InsertBefore(std::move(var));
138
139
  // Update uses where the type may have changed.
140
1.89k
  return UpdateUses(variable);
141
1.89k
}
142
143
24.4k
uint32_t PrivateToLocalPass::GetNewType(uint32_t old_type_id) {
144
24.4k
  auto type_mgr = context()->get_type_mgr();
145
24.4k
  Instruction* old_type_inst = get_def_use_mgr()->GetDef(old_type_id);
146
24.4k
  uint32_t pointee_type_id =
147
24.4k
      old_type_inst->GetSingleWordInOperand(kSpvTypePointerTypeIdInIdx);
148
24.4k
  uint32_t new_type_id =
149
24.4k
      type_mgr->FindPointerToType(pointee_type_id, spv::StorageClass::Function);
150
24.4k
  if (new_type_id != 0) {
151
24.4k
    context()->UpdateDefUse(context()->get_def_use_mgr()->GetDef(new_type_id));
152
24.4k
  }
153
24.4k
  return new_type_id;
154
24.4k
}
155
156
73.4k
bool PrivateToLocalPass::IsValidUse(const Instruction* inst) const {
157
  // The cases in this switch have to match the cases in |UpdateUse|.
158
  // If we don't know how to update it, it is not valid.
159
73.4k
  if (inst->GetCommonDebugOpcode() == CommonDebugInfoDebugGlobalVariable) {
160
0
    return true;
161
0
  }
162
73.4k
  switch (inst->opcode()) {
163
25.2k
    case spv::Op::OpLoad:
164
39.0k
    case spv::Op::OpStore:
165
39.0k
    case spv::Op::OpImageTexelPointer:  // Treat like a load
166
39.0k
      return true;
167
31.3k
    case spv::Op::OpAccessChain:
168
31.3k
      return context()->get_def_use_mgr()->WhileEachUser(
169
31.3k
          inst, [this](const Instruction* user) {
170
30.9k
            if (!IsValidUse(user)) return false;
171
30.9k
            return true;
172
30.9k
          });
173
18
    case spv::Op::OpName:
174
18
      return true;
175
3.00k
    default:
176
3.00k
      return spvOpcodeIsDecoration(inst->opcode());
177
73.4k
  }
178
73.4k
}
179
180
53.3k
bool PrivateToLocalPass::UpdateUse(Instruction* inst, Instruction* user) {
181
  // The cases in this switch have to match the cases in |IsValidUse|.  If we
182
  // don't think it is valid, the optimization will not view the variable as a
183
  // candidate, and therefore the use will not be updated.
184
53.3k
  if (inst->GetCommonDebugOpcode() == CommonDebugInfoDebugGlobalVariable) {
185
0
    context()->get_debug_info_mgr()->ConvertDebugGlobalToLocalVariable(inst,
186
0
                                                                       user);
187
0
    return true;
188
0
  }
189
53.3k
  switch (inst->opcode()) {
190
19.4k
    case spv::Op::OpLoad:
191
30.1k
    case spv::Op::OpStore:
192
30.1k
    case spv::Op::OpImageTexelPointer:  // Treat like a load
193
      // The type is fine because it is the type pointed to, and that does not
194
      // change.
195
30.1k
      break;
196
22.5k
    case spv::Op::OpAccessChain: {
197
22.5k
      context()->ForgetUses(inst);
198
22.5k
      uint32_t new_type_id = GetNewType(inst->type_id());
199
22.5k
      if (new_type_id == 0) {
200
0
        return false;
201
0
      }
202
22.5k
      inst->SetResultType(new_type_id);
203
22.5k
      context()->AnalyzeUses(inst);
204
205
      // Update uses where the type may have changed.
206
22.5k
      if (!UpdateUses(inst)) {
207
0
        return false;
208
0
      }
209
22.5k
    } break;
210
22.5k
    case spv::Op::OpName:
211
594
    case spv::Op::OpEntryPoint:  // entry points will be updated separately.
212
594
      break;
213
36
    default:
214
36
      assert(spvOpcodeIsDecoration(inst->opcode()) &&
215
36
             "Do not know how to update the type for this instruction.");
216
36
      break;
217
53.3k
  }
218
53.3k
  return true;
219
53.3k
}
220
221
24.4k
bool PrivateToLocalPass::UpdateUses(Instruction* inst) {
222
24.4k
  uint32_t id = inst->result_id();
223
24.4k
  std::vector<Instruction*> uses;
224
24.4k
  context()->get_def_use_mgr()->ForEachUser(
225
53.3k
      id, [&uses](Instruction* use) { uses.push_back(use); });
226
227
53.3k
  for (Instruction* use : uses) {
228
53.3k
    if (!UpdateUse(use, inst)) {
229
0
      return false;
230
0
    }
231
53.3k
  }
232
24.4k
  return true;
233
24.4k
}
234
235
}  // namespace opt
236
}  // namespace spvtools