Coverage Report

Created: 2025-06-13 06:49

/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
14.1k
Pass::Status PrivateToLocalPass::Process() {
32
14.1k
  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
14.1k
  if (context()->get_feature_mgr()->HasCapability(spv::Capability::Addresses))
37
453
    return Status::SuccessWithoutChange;
38
39
13.6k
  std::vector<std::pair<Instruction*, Function*>> variables_to_move;
40
13.6k
  std::unordered_set<uint32_t> localized_variables;
41
318k
  for (auto& inst : context()->types_values()) {
42
318k
    if (inst.opcode() != spv::Op::OpVariable) {
43
292k
      continue;
44
292k
    }
45
46
25.9k
    if (spv::StorageClass(inst.GetSingleWordInOperand(
47
25.9k
            kVariableStorageClassInIdx)) != spv::StorageClass::Private) {
48
20.1k
      continue;
49
20.1k
    }
50
51
5.86k
    Function* target_function = FindLocalFunction(inst);
52
5.86k
    if (target_function != nullptr) {
53
2.33k
      variables_to_move.push_back({&inst, target_function});
54
2.33k
    }
55
5.86k
  }
56
57
13.6k
  modified = !variables_to_move.empty();
58
13.6k
  for (auto p : variables_to_move) {
59
2.33k
    if (!MoveVariable(p.first, p.second)) {
60
0
      return Status::Failure;
61
0
    }
62
2.33k
    localized_variables.insert(p.first->result_id());
63
2.33k
  }
64
65
13.6k
  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
13.6k
  return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
86
13.6k
}
87
88
5.86k
Function* PrivateToLocalPass::FindLocalFunction(const Instruction& inst) const {
89
5.86k
  bool found_first_use = false;
90
5.86k
  Function* target_function = nullptr;
91
5.86k
  context()->get_def_use_mgr()->ForEachUser(
92
5.86k
      inst.result_id(),
93
87.1k
      [&target_function, &found_first_use, inst, this](Instruction* use) {
94
87.1k
        BasicBlock* current_block = context()->get_instr_block(use);
95
87.1k
        if (current_block == nullptr) {
96
1.25k
          return;
97
1.25k
        }
98
99
85.8k
        if (!IsValidUse(use, inst.result_id())) {
100
3.58k
          found_first_use = true;
101
3.58k
          target_function = nullptr;
102
3.58k
          return;
103
3.58k
        }
104
82.2k
        Function* current_function = current_block->GetParent();
105
82.2k
        if (!found_first_use) {
106
2.75k
          found_first_use = true;
107
2.75k
          target_function = current_function;
108
79.5k
        } else if (target_function != current_function) {
109
11.1k
          target_function = nullptr;
110
11.1k
        }
111
82.2k
      });
112
5.86k
  return target_function;
113
5.86k
}  // namespace opt
114
115
bool PrivateToLocalPass::MoveVariable(Instruction* variable,
116
2.33k
                                      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
2.33k
  variable->RemoveFromList();
120
2.33k
  std::unique_ptr<Instruction> var(variable);  // Take ownership.
121
2.33k
  context()->ForgetUses(variable);
122
123
  // Update the storage class of the variable.
124
2.33k
  variable->SetInOperand(kVariableStorageClassInIdx,
125
2.33k
                         {uint32_t(spv::StorageClass::Function)});
126
127
  // Update the type as well.
128
2.33k
  uint32_t new_type_id = GetNewType(variable->type_id());
129
2.33k
  if (new_type_id == 0) {
130
0
    return false;
131
0
  }
132
2.33k
  variable->SetResultType(new_type_id);
133
134
  // Place the variable at the start of the first basic block.
135
2.33k
  context()->AnalyzeUses(variable);
136
2.33k
  context()->set_instr_block(variable, &*function->begin());
137
2.33k
  function->begin()->begin()->InsertBefore(std::move(var));
138
139
  // Update uses where the type may have changed.
140
2.33k
  return UpdateUses(variable);
141
2.33k
}
142
143
48.2k
uint32_t PrivateToLocalPass::GetNewType(uint32_t old_type_id) {
144
48.2k
  auto type_mgr = context()->get_type_mgr();
145
48.2k
  Instruction* old_type_inst = get_def_use_mgr()->GetDef(old_type_id);
146
48.2k
  uint32_t pointee_type_id =
147
48.2k
      old_type_inst->GetSingleWordInOperand(kSpvTypePointerTypeIdInIdx);
148
48.2k
  uint32_t new_type_id =
149
48.2k
      type_mgr->FindPointerToType(pointee_type_id, spv::StorageClass::Function);
150
48.2k
  if (new_type_id != 0) {
151
48.2k
    context()->UpdateDefUse(context()->get_def_use_mgr()->GetDef(new_type_id));
152
48.2k
  }
153
48.2k
  return new_type_id;
154
48.2k
}
155
156
bool PrivateToLocalPass::IsValidUse(const Instruction* inst,
157
146k
                                    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
146k
  if (inst->GetCommonDebugOpcode() == CommonDebugInfoDebugGlobalVariable) {
161
0
    return true;
162
0
  }
163
146k
  switch (inst->opcode()) {
164
50.8k
    case spv::Op::OpLoad:
165
50.8k
    case spv::Op::OpImageTexelPointer:  // Treat like a load
166
50.8k
      return true;
167
29.6k
    case spv::Op::OpStore:
168
29.6k
      return inst->GetOperand(1).AsId() != private_variable_id;
169
62.0k
    case spv::Op::OpAccessChain:
170
62.0k
      return context()->get_def_use_mgr()->WhileEachUser(
171
62.0k
          inst, [this, inst](const Instruction* user) {
172
60.4k
            if (!IsValidUse(user, inst->result_id())) return false;
173
60.4k
            return true;
174
60.4k
          });
175
59
    case spv::Op::OpName:
176
59
      return true;
177
3.67k
    default:
178
3.67k
      return spvOpcodeIsDecoration(inst->opcode());
179
146k
  }
180
146k
}
181
182
111k
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
111k
  if (inst->GetCommonDebugOpcode() == CommonDebugInfoDebugGlobalVariable) {
187
0
    context()->get_debug_info_mgr()->ConvertDebugGlobalToLocalVariable(inst,
188
0
                                                                       user);
189
0
    return true;
190
0
  }
191
111k
  switch (inst->opcode()) {
192
40.0k
    case spv::Op::OpLoad:
193
64.6k
    case spv::Op::OpStore:
194
64.6k
    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
64.6k
      break;
198
45.9k
    case spv::Op::OpAccessChain: {
199
45.9k
      context()->ForgetUses(inst);
200
45.9k
      uint32_t new_type_id = GetNewType(inst->type_id());
201
45.9k
      if (new_type_id == 0) {
202
0
        return false;
203
0
      }
204
45.9k
      inst->SetResultType(new_type_id);
205
45.9k
      context()->AnalyzeUses(inst);
206
207
      // Update uses where the type may have changed.
208
45.9k
      if (!UpdateUses(inst)) {
209
0
        return false;
210
0
      }
211
45.9k
    } break;
212
45.9k
    case spv::Op::OpName:
213
1.08k
    case spv::Op::OpEntryPoint:  // entry points will be updated separately.
214
1.08k
      break;
215
149
    default:
216
149
      assert(spvOpcodeIsDecoration(inst->opcode()) &&
217
149
             "Do not know how to update the type for this instruction.");
218
149
      break;
219
111k
  }
220
111k
  return true;
221
111k
}
222
223
48.2k
bool PrivateToLocalPass::UpdateUses(Instruction* inst) {
224
48.2k
  uint32_t id = inst->result_id();
225
48.2k
  std::vector<Instruction*> uses;
226
48.2k
  context()->get_def_use_mgr()->ForEachUser(
227
111k
      id, [&uses](Instruction* use) { uses.push_back(use); });
228
229
111k
  for (Instruction* use : uses) {
230
111k
    if (!UpdateUse(use, inst)) {
231
0
      return false;
232
0
    }
233
111k
  }
234
48.2k
  return true;
235
48.2k
}
236
237
}  // namespace opt
238
}  // namespace spvtools