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/private_to_local_pass.cpp
Line
Count
Source
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
20.3k
Pass::Status PrivateToLocalPass::Process() {
32
20.3k
  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
20.3k
  if (context()->get_feature_mgr()->HasCapability(spv::Capability::Addresses))
37
456
    return Status::SuccessWithoutChange;
38
39
19.9k
  std::vector<std::pair<Instruction*, Function*>> variables_to_move;
40
19.9k
  std::unordered_set<uint32_t> localized_variables;
41
473k
  for (auto& inst : context()->types_values()) {
42
473k
    if (inst.opcode() != spv::Op::OpVariable) {
43
434k
      continue;
44
434k
    }
45
46
39.2k
    if (spv::StorageClass(inst.GetSingleWordInOperand(
47
39.2k
            kVariableStorageClassInIdx)) != spv::StorageClass::Private) {
48
30.5k
      continue;
49
30.5k
    }
50
51
8.73k
    Function* target_function = FindLocalFunction(inst);
52
8.73k
    if (target_function != nullptr) {
53
3.81k
      variables_to_move.push_back({&inst, target_function});
54
3.81k
    }
55
8.73k
  }
56
57
19.9k
  modified = !variables_to_move.empty();
58
19.9k
  for (auto p : variables_to_move) {
59
3.81k
    if (!MoveVariable(p.first, p.second)) {
60
0
      return Status::Failure;
61
0
    }
62
3.81k
    localized_variables.insert(p.first->result_id());
63
3.81k
  }
64
65
19.9k
  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
19.9k
  return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
86
19.9k
}
87
88
8.73k
Function* PrivateToLocalPass::FindLocalFunction(const Instruction& inst) const {
89
8.73k
  bool found_first_use = false;
90
8.73k
  Function* target_function = nullptr;
91
8.73k
  context()->get_def_use_mgr()->ForEachUser(
92
8.73k
      inst.result_id(),
93
148k
      [&target_function, &found_first_use, inst, this](Instruction* use) {
94
148k
        BasicBlock* current_block = context()->get_instr_block(use);
95
148k
        if (current_block == nullptr) {
96
2.70k
          return;
97
2.70k
        }
98
99
146k
        if (!IsValidUse(use, inst.result_id())) {
100
4.85k
          found_first_use = true;
101
4.85k
          target_function = nullptr;
102
4.85k
          return;
103
4.85k
        }
104
141k
        Function* current_function = current_block->GetParent();
105
141k
        if (!found_first_use) {
106
4.43k
          found_first_use = true;
107
4.43k
          target_function = current_function;
108
136k
        } else if (target_function != current_function) {
109
15.6k
          target_function = nullptr;
110
15.6k
        }
111
141k
      });
112
8.73k
  return target_function;
113
8.73k
}  // namespace opt
114
115
bool PrivateToLocalPass::MoveVariable(Instruction* variable,
116
3.81k
                                      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
3.81k
  variable->RemoveFromList();
120
3.81k
  std::unique_ptr<Instruction> var(variable);  // Take ownership.
121
3.81k
  context()->ForgetUses(variable);
122
123
  // Update the storage class of the variable.
124
3.81k
  variable->SetInOperand(kVariableStorageClassInIdx,
125
3.81k
                         {uint32_t(spv::StorageClass::Function)});
126
127
  // Update the type as well.
128
3.81k
  uint32_t new_type_id = GetNewType(variable->type_id());
129
3.81k
  if (new_type_id == 0) {
130
0
    return false;
131
0
  }
132
3.81k
  variable->SetResultType(new_type_id);
133
134
  // Place the variable at the start of the first basic block.
135
3.81k
  context()->AnalyzeUses(variable);
136
3.81k
  context()->set_instr_block(variable, &*function->begin());
137
3.81k
  function->begin()->begin()->InsertBefore(std::move(var));
138
139
  // Update uses where the type may have changed.
140
3.81k
  return UpdateUses(variable);
141
3.81k
}
142
143
57.2k
uint32_t PrivateToLocalPass::GetNewType(uint32_t old_type_id) {
144
57.2k
  auto type_mgr = context()->get_type_mgr();
145
57.2k
  Instruction* old_type_inst = get_def_use_mgr()->GetDef(old_type_id);
146
57.2k
  uint32_t pointee_type_id =
147
57.2k
      old_type_inst->GetSingleWordInOperand(kSpvTypePointerTypeIdInIdx);
148
57.2k
  uint32_t new_type_id =
149
57.2k
      type_mgr->FindPointerToType(pointee_type_id, spv::StorageClass::Function);
150
57.2k
  if (new_type_id != 0) {
151
57.2k
    context()->UpdateDefUse(context()->get_def_use_mgr()->GetDef(new_type_id));
152
57.2k
  }
153
57.2k
  return new_type_id;
154
57.2k
}
155
156
bool PrivateToLocalPass::IsValidUse(const Instruction* inst,
157
219k
                                    uint32_t private_variable_id) const {
158
  // The cases in this switch have to match the cases in |UpdateUse|.
159
  // If we don't know how to update it, it is not valid.
160
219k
  if (inst->GetCommonDebugOpcode() == CommonDebugInfoDebugGlobalVariable) {
161
0
    return true;
162
0
  }
163
219k
  switch (inst->opcode()) {
164
85.2k
    case spv::Op::OpLoad:
165
85.2k
    case spv::Op::OpImageTexelPointer:  // Treat like a load
166
85.2k
      return true;
167
55.1k
    case spv::Op::OpStore:
168
55.1k
      return inst->GetOperand(1).AsId() != private_variable_id;
169
74.1k
    case spv::Op::OpAccessChain:
170
74.1k
      return context()->get_def_use_mgr()->WhileEachUser(
171
74.1k
          inst, [this, inst](const Instruction* user) {
172
73.3k
            if (!IsValidUse(user, inst->result_id())) return false;
173
73.3k
            return true;
174
73.3k
          });
175
100
    case spv::Op::OpName:
176
100
      return true;
177
4.98k
    default:
178
4.98k
      return spvOpcodeIsDecoration(inst->opcode());
179
219k
  }
180
219k
}
181
182
174k
bool PrivateToLocalPass::UpdateUse(Instruction* inst, Instruction* user) {
183
  // The cases in this switch have to match the cases in |IsValidUse|.  If we
184
  // don't think it is valid, the optimization will not view the variable as a
185
  // candidate, and therefore the use will not be updated.
186
174k
  if (inst->GetCommonDebugOpcode() == CommonDebugInfoDebugGlobalVariable) {
187
0
    context()->get_debug_info_mgr()->ConvertDebugGlobalToLocalVariable(inst,
188
0
                                                                       user);
189
0
    return true;
190
0
  }
191
174k
  switch (inst->opcode()) {
192
70.6k
    case spv::Op::OpLoad:
193
118k
    case spv::Op::OpStore:
194
118k
    case spv::Op::OpImageTexelPointer:  // Treat like a load
195
      // The type is fine because it is the type pointed to, and that does not
196
      // change.
197
118k
      break;
198
53.4k
    case spv::Op::OpAccessChain: {
199
53.4k
      context()->ForgetUses(inst);
200
53.4k
      uint32_t new_type_id = GetNewType(inst->type_id());
201
53.4k
      if (new_type_id == 0) {
202
0
        return false;
203
0
      }
204
53.4k
      inst->SetResultType(new_type_id);
205
53.4k
      context()->AnalyzeUses(inst);
206
207
      // Update uses where the type may have changed.
208
53.4k
      if (!UpdateUses(inst)) {
209
0
        return false;
210
0
      }
211
53.4k
    } break;
212
53.4k
    case spv::Op::OpName:
213
2.30k
    case spv::Op::OpEntryPoint:  // entry points will be updated separately.
214
2.30k
      break;
215
250
    default:
216
250
      assert(spvOpcodeIsDecoration(inst->opcode()) &&
217
250
             "Do not know how to update the type for this instruction.");
218
250
      break;
219
174k
  }
220
174k
  return true;
221
174k
}
222
223
57.2k
bool PrivateToLocalPass::UpdateUses(Instruction* inst) {
224
57.2k
  uint32_t id = inst->result_id();
225
57.2k
  std::vector<Instruction*> uses;
226
57.2k
  context()->get_def_use_mgr()->ForEachUser(
227
174k
      id, [&uses](Instruction* use) { uses.push_back(use); });
228
229
174k
  for (Instruction* use : uses) {
230
174k
    if (!UpdateUse(use, inst)) {
231
0
      return false;
232
0
    }
233
174k
  }
234
57.2k
  return true;
235
57.2k
}
236
237
}  // namespace opt
238
}  // namespace spvtools