Coverage Report

Created: 2023-03-01 07:33

/src/spirv-tools/source/opt/upgrade_memory_model.cpp
Line
Count
Source (jump to first uncovered line)
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 "upgrade_memory_model.h"
16
17
#include <utility>
18
19
#include "source/opt/ir_builder.h"
20
#include "source/opt/ir_context.h"
21
#include "source/spirv_constant.h"
22
#include "source/util/make_unique.h"
23
#include "source/util/string_utils.h"
24
25
namespace spvtools {
26
namespace opt {
27
28
0
Pass::Status UpgradeMemoryModel::Process() {
29
  // TODO: This pass needs changes to support cooperative matrices.
30
0
  if (context()->get_feature_mgr()->HasCapability(
31
0
          spv::Capability::CooperativeMatrixNV)) {
32
0
    return Pass::Status::SuccessWithoutChange;
33
0
  }
34
35
  // Only update Logical GLSL450 to Logical VulkanKHR.
36
0
  Instruction* memory_model = get_module()->GetMemoryModel();
37
0
  if (memory_model->GetSingleWordInOperand(0u) !=
38
0
          uint32_t(spv::AddressingModel::Logical) ||
39
0
      memory_model->GetSingleWordInOperand(1u) !=
40
0
          uint32_t(spv::MemoryModel::GLSL450)) {
41
0
    return Pass::Status::SuccessWithoutChange;
42
0
  }
43
44
0
  UpgradeMemoryModelInstruction();
45
0
  UpgradeInstructions();
46
0
  CleanupDecorations();
47
0
  UpgradeBarriers();
48
0
  UpgradeMemoryScope();
49
50
0
  return Pass::Status::SuccessWithChange;
51
0
}
52
53
0
void UpgradeMemoryModel::UpgradeMemoryModelInstruction() {
54
  // Overall changes necessary:
55
  // 1. Add the OpExtension.
56
  // 2. Add the OpCapability.
57
  // 3. Modify the memory model.
58
0
  Instruction* memory_model = get_module()->GetMemoryModel();
59
0
  context()->AddCapability(MakeUnique<Instruction>(
60
0
      context(), spv::Op::OpCapability, 0, 0,
61
0
      std::initializer_list<Operand>{
62
0
          {SPV_OPERAND_TYPE_CAPABILITY,
63
0
           {uint32_t(spv::Capability::VulkanMemoryModelKHR)}}}));
64
0
  const std::string extension = "SPV_KHR_vulkan_memory_model";
65
0
  std::vector<uint32_t> words = spvtools::utils::MakeVector(extension);
66
0
  context()->AddExtension(
67
0
      MakeUnique<Instruction>(context(), spv::Op::OpExtension, 0, 0,
68
0
                              std::initializer_list<Operand>{
69
0
                                  {SPV_OPERAND_TYPE_LITERAL_STRING, words}}));
70
0
  memory_model->SetInOperand(1u, {uint32_t(spv::MemoryModel::VulkanKHR)});
71
0
}
72
73
0
void UpgradeMemoryModel::UpgradeInstructions() {
74
  // Coherent and Volatile decorations are deprecated. Remove them and replace
75
  // with flags on the memory/image operations. The decorations can occur on
76
  // OpVariable, OpFunctionParameter (of pointer type) and OpStructType (member
77
  // decoration). Trace from the decoration target(s) to the final memory/image
78
  // instructions. Additionally, Workgroup storage class variables and function
79
  // parameters are implicitly coherent in GLSL450.
80
81
  // Upgrade modf and frexp first since they generate new stores.
82
  // In SPIR-V 1.4 or later, normalize OpCopyMemory* access operands.
83
0
  for (auto& func : *get_module()) {
84
0
    func.ForEachInst([this](Instruction* inst) {
85
0
      if (inst->opcode() == spv::Op::OpExtInst) {
86
0
        auto ext_inst = inst->GetSingleWordInOperand(1u);
87
0
        if (ext_inst == GLSLstd450Modf || ext_inst == GLSLstd450Frexp) {
88
0
          auto import =
89
0
              get_def_use_mgr()->GetDef(inst->GetSingleWordInOperand(0u));
90
0
          if (import->GetInOperand(0u).AsString() == "GLSL.std.450") {
91
0
            UpgradeExtInst(inst);
92
0
          }
93
0
        }
94
0
      } else if (get_module()->version() >= SPV_SPIRV_VERSION_WORD(1, 4)) {
95
0
        if (inst->opcode() == spv::Op::OpCopyMemory ||
96
0
            inst->opcode() == spv::Op::OpCopyMemorySized) {
97
0
          uint32_t start_operand =
98
0
              inst->opcode() == spv::Op::OpCopyMemory ? 2u : 3u;
99
0
          if (inst->NumInOperands() > start_operand) {
100
0
            auto num_access_words = MemoryAccessNumWords(
101
0
                inst->GetSingleWordInOperand(start_operand));
102
0
            if ((num_access_words + start_operand) == inst->NumInOperands()) {
103
              // There is a single memory access operand. Duplicate it to have a
104
              // separate operand for both source and target.
105
0
              for (uint32_t i = 0; i < num_access_words; ++i) {
106
0
                auto operand = inst->GetInOperand(start_operand + i);
107
0
                inst->AddOperand(std::move(operand));
108
0
              }
109
0
            }
110
0
          } else {
111
            // Add two memory access operands.
112
0
            inst->AddOperand({SPV_OPERAND_TYPE_MEMORY_ACCESS,
113
0
                              {uint32_t(spv::MemoryAccessMask::MaskNone)}});
114
0
            inst->AddOperand({SPV_OPERAND_TYPE_MEMORY_ACCESS,
115
0
                              {uint32_t(spv::MemoryAccessMask::MaskNone)}});
116
0
          }
117
0
        }
118
0
      }
119
0
    });
120
0
  }
121
122
0
  UpgradeMemoryAndImages();
123
0
  UpgradeAtomics();
124
0
}
125
126
0
void UpgradeMemoryModel::UpgradeMemoryAndImages() {
127
0
  for (auto& func : *get_module()) {
128
0
    func.ForEachInst([this](Instruction* inst) {
129
0
      bool is_coherent = false;
130
0
      bool is_volatile = false;
131
0
      bool src_coherent = false;
132
0
      bool src_volatile = false;
133
0
      bool dst_coherent = false;
134
0
      bool dst_volatile = false;
135
0
      uint32_t start_operand = 0u;
136
0
      spv::Scope scope = spv::Scope::QueueFamilyKHR;
137
0
      spv::Scope src_scope = spv::Scope::QueueFamilyKHR;
138
0
      spv::Scope dst_scope = spv::Scope::QueueFamilyKHR;
139
0
      switch (inst->opcode()) {
140
0
        case spv::Op::OpLoad:
141
0
        case spv::Op::OpStore:
142
0
          std::tie(is_coherent, is_volatile, scope) =
143
0
              GetInstructionAttributes(inst->GetSingleWordInOperand(0u));
144
0
          break;
145
0
        case spv::Op::OpImageRead:
146
0
        case spv::Op::OpImageSparseRead:
147
0
        case spv::Op::OpImageWrite:
148
0
          std::tie(is_coherent, is_volatile, scope) =
149
0
              GetInstructionAttributes(inst->GetSingleWordInOperand(0u));
150
0
          break;
151
0
        case spv::Op::OpCopyMemory:
152
0
        case spv::Op::OpCopyMemorySized:
153
0
          std::tie(dst_coherent, dst_volatile, dst_scope) =
154
0
              GetInstructionAttributes(inst->GetSingleWordInOperand(0u));
155
0
          std::tie(src_coherent, src_volatile, src_scope) =
156
0
              GetInstructionAttributes(inst->GetSingleWordInOperand(1u));
157
0
          break;
158
0
        default:
159
0
          break;
160
0
      }
161
162
0
      switch (inst->opcode()) {
163
0
        case spv::Op::OpLoad:
164
0
          UpgradeFlags(inst, 1u, is_coherent, is_volatile, kVisibility,
165
0
                       kMemory);
166
0
          break;
167
0
        case spv::Op::OpStore:
168
0
          UpgradeFlags(inst, 2u, is_coherent, is_volatile, kAvailability,
169
0
                       kMemory);
170
0
          break;
171
0
        case spv::Op::OpCopyMemory:
172
0
        case spv::Op::OpCopyMemorySized:
173
0
          start_operand = inst->opcode() == spv::Op::OpCopyMemory ? 2u : 3u;
174
0
          if (get_module()->version() >= SPV_SPIRV_VERSION_WORD(1, 4)) {
175
            // There are guaranteed to be two memory access operands at this
176
            // point so treat source and target separately.
177
0
            uint32_t num_access_words = MemoryAccessNumWords(
178
0
                inst->GetSingleWordInOperand(start_operand));
179
0
            UpgradeFlags(inst, start_operand, dst_coherent, dst_volatile,
180
0
                         kAvailability, kMemory);
181
0
            UpgradeFlags(inst, start_operand + num_access_words, src_coherent,
182
0
                         src_volatile, kVisibility, kMemory);
183
0
          } else {
184
0
            UpgradeFlags(inst, start_operand, dst_coherent, dst_volatile,
185
0
                         kAvailability, kMemory);
186
0
            UpgradeFlags(inst, start_operand, src_coherent, src_volatile,
187
0
                         kVisibility, kMemory);
188
0
          }
189
0
          break;
190
0
        case spv::Op::OpImageRead:
191
0
        case spv::Op::OpImageSparseRead:
192
0
          UpgradeFlags(inst, 2u, is_coherent, is_volatile, kVisibility, kImage);
193
0
          break;
194
0
        case spv::Op::OpImageWrite:
195
0
          UpgradeFlags(inst, 3u, is_coherent, is_volatile, kAvailability,
196
0
                       kImage);
197
0
          break;
198
0
        default:
199
0
          break;
200
0
      }
201
202
      // |is_coherent| is never used for the same instructions as
203
      // |src_coherent| and |dst_coherent|.
204
0
      if (is_coherent) {
205
0
        inst->AddOperand(
206
0
            {SPV_OPERAND_TYPE_SCOPE_ID, {GetScopeConstant(scope)}});
207
0
      }
208
0
      if (get_module()->version() >= SPV_SPIRV_VERSION_WORD(1, 4)) {
209
        // There are two memory access operands. The first is for the target and
210
        // the second is for the source.
211
0
        if (dst_coherent || src_coherent) {
212
0
          start_operand = inst->opcode() == spv::Op::OpCopyMemory ? 2u : 3u;
213
0
          std::vector<Operand> new_operands;
214
0
          uint32_t num_access_words =
215
0
              MemoryAccessNumWords(inst->GetSingleWordInOperand(start_operand));
216
          // The flags were already updated so subtract if we're adding a
217
          // scope.
218
0
          if (dst_coherent) --num_access_words;
219
0
          for (uint32_t i = 0; i < start_operand + num_access_words; ++i) {
220
0
            new_operands.push_back(inst->GetInOperand(i));
221
0
          }
222
          // Add the target scope if necessary.
223
0
          if (dst_coherent) {
224
0
            new_operands.push_back(
225
0
                {SPV_OPERAND_TYPE_SCOPE_ID, {GetScopeConstant(dst_scope)}});
226
0
          }
227
          // Copy the remaining current operands.
228
0
          for (uint32_t i = start_operand + num_access_words;
229
0
               i < inst->NumInOperands(); ++i) {
230
0
            new_operands.push_back(inst->GetInOperand(i));
231
0
          }
232
          // Add the source scope if necessary.
233
0
          if (src_coherent) {
234
0
            new_operands.push_back(
235
0
                {SPV_OPERAND_TYPE_SCOPE_ID, {GetScopeConstant(src_scope)}});
236
0
          }
237
0
          inst->SetInOperands(std::move(new_operands));
238
0
        }
239
0
      } else {
240
        // According to SPV_KHR_vulkan_memory_model, if both available and
241
        // visible flags are used the first scope operand is for availability
242
        // (writes) and the second is for visibility (reads).
243
0
        if (dst_coherent) {
244
0
          inst->AddOperand(
245
0
              {SPV_OPERAND_TYPE_SCOPE_ID, {GetScopeConstant(dst_scope)}});
246
0
        }
247
0
        if (src_coherent) {
248
0
          inst->AddOperand(
249
0
              {SPV_OPERAND_TYPE_SCOPE_ID, {GetScopeConstant(src_scope)}});
250
0
        }
251
0
      }
252
0
    });
253
0
  }
254
0
}
255
256
0
void UpgradeMemoryModel::UpgradeAtomics() {
257
0
  for (auto& func : *get_module()) {
258
0
    func.ForEachInst([this](Instruction* inst) {
259
0
      if (spvOpcodeIsAtomicOp(inst->opcode())) {
260
0
        bool unused_coherent = false;
261
0
        bool is_volatile = false;
262
0
        spv::Scope unused_scope = spv::Scope::QueueFamilyKHR;
263
0
        std::tie(unused_coherent, is_volatile, unused_scope) =
264
0
            GetInstructionAttributes(inst->GetSingleWordInOperand(0));
265
266
0
        UpgradeSemantics(inst, 2u, is_volatile);
267
0
        if (inst->opcode() == spv::Op::OpAtomicCompareExchange ||
268
0
            inst->opcode() == spv::Op::OpAtomicCompareExchangeWeak) {
269
0
          UpgradeSemantics(inst, 3u, is_volatile);
270
0
        }
271
0
      }
272
0
    });
273
0
  }
274
0
}
275
276
void UpgradeMemoryModel::UpgradeSemantics(Instruction* inst,
277
                                          uint32_t in_operand,
278
0
                                          bool is_volatile) {
279
0
  if (!is_volatile) return;
280
281
0
  uint32_t semantics_id = inst->GetSingleWordInOperand(in_operand);
282
0
  const analysis::Constant* constant =
283
0
      context()->get_constant_mgr()->FindDeclaredConstant(semantics_id);
284
0
  const analysis::Integer* type = constant->type()->AsInteger();
285
0
  assert(type && type->width() == 32);
286
0
  uint32_t value = 0;
287
0
  if (type->IsSigned()) {
288
0
    value = static_cast<uint32_t>(constant->GetS32());
289
0
  } else {
290
0
    value = constant->GetU32();
291
0
  }
292
293
0
  value |= uint32_t(spv::MemorySemanticsMask::Volatile);
294
0
  auto new_constant = context()->get_constant_mgr()->GetConstant(type, {value});
295
0
  auto new_semantics =
296
0
      context()->get_constant_mgr()->GetDefiningInstruction(new_constant);
297
0
  inst->SetInOperand(in_operand, {new_semantics->result_id()});
298
0
}
299
300
std::tuple<bool, bool, spv::Scope> UpgradeMemoryModel::GetInstructionAttributes(
301
0
    uint32_t id) {
302
  // |id| is a pointer used in a memory/image instruction. Need to determine if
303
  // that pointer points to volatile or coherent memory. Workgroup storage
304
  // class is implicitly coherent and cannot be decorated with volatile, so
305
  // short circuit that case.
306
0
  Instruction* inst = context()->get_def_use_mgr()->GetDef(id);
307
0
  analysis::Type* type = context()->get_type_mgr()->GetType(inst->type_id());
308
0
  if (type->AsPointer() &&
309
0
      type->AsPointer()->storage_class() == spv::StorageClass::Workgroup) {
310
0
    return std::make_tuple(true, false, spv::Scope::Workgroup);
311
0
  }
312
313
0
  bool is_coherent = false;
314
0
  bool is_volatile = false;
315
0
  std::unordered_set<uint32_t> visited;
316
0
  std::tie(is_coherent, is_volatile) =
317
0
      TraceInstruction(context()->get_def_use_mgr()->GetDef(id),
318
0
                       std::vector<uint32_t>(), &visited);
319
320
0
  return std::make_tuple(is_coherent, is_volatile, spv::Scope::QueueFamilyKHR);
321
0
}
322
323
std::pair<bool, bool> UpgradeMemoryModel::TraceInstruction(
324
    Instruction* inst, std::vector<uint32_t> indices,
325
0
    std::unordered_set<uint32_t>* visited) {
326
0
  auto iter = cache_.find(std::make_pair(inst->result_id(), indices));
327
0
  if (iter != cache_.end()) {
328
0
    return iter->second;
329
0
  }
330
331
0
  if (!visited->insert(inst->result_id()).second) {
332
0
    return std::make_pair(false, false);
333
0
  }
334
335
  // Initialize the cache before |indices| is (potentially) modified.
336
0
  auto& cached_result = cache_[std::make_pair(inst->result_id(), indices)];
337
0
  cached_result.first = false;
338
0
  cached_result.second = false;
339
340
0
  bool is_coherent = false;
341
0
  bool is_volatile = false;
342
0
  switch (inst->opcode()) {
343
0
    case spv::Op::OpVariable:
344
0
    case spv::Op::OpFunctionParameter:
345
0
      is_coherent |= HasDecoration(inst, 0, spv::Decoration::Coherent);
346
0
      is_volatile |= HasDecoration(inst, 0, spv::Decoration::Volatile);
347
0
      if (!is_coherent || !is_volatile) {
348
0
        bool type_coherent = false;
349
0
        bool type_volatile = false;
350
0
        std::tie(type_coherent, type_volatile) =
351
0
            CheckType(inst->type_id(), indices);
352
0
        is_coherent |= type_coherent;
353
0
        is_volatile |= type_volatile;
354
0
      }
355
0
      break;
356
0
    case spv::Op::OpAccessChain:
357
0
    case spv::Op::OpInBoundsAccessChain:
358
      // Store indices in reverse order.
359
0
      for (uint32_t i = inst->NumInOperands() - 1; i > 0; --i) {
360
0
        indices.push_back(inst->GetSingleWordInOperand(i));
361
0
      }
362
0
      break;
363
0
    case spv::Op::OpPtrAccessChain:
364
      // Store indices in reverse order. Skip the |Element| operand.
365
0
      for (uint32_t i = inst->NumInOperands() - 1; i > 1; --i) {
366
0
        indices.push_back(inst->GetSingleWordInOperand(i));
367
0
      }
368
0
      break;
369
0
    default:
370
0
      break;
371
0
  }
372
373
  // No point searching further.
374
0
  if (is_coherent && is_volatile) {
375
0
    cached_result.first = true;
376
0
    cached_result.second = true;
377
0
    return std::make_pair(true, true);
378
0
  }
379
380
  // Variables and function parameters are sources. Continue searching until we
381
  // reach them.
382
0
  if (inst->opcode() != spv::Op::OpVariable &&
383
0
      inst->opcode() != spv::Op::OpFunctionParameter) {
384
0
    inst->ForEachInId([this, &is_coherent, &is_volatile, &indices,
385
0
                       &visited](const uint32_t* id_ptr) {
386
0
      Instruction* op_inst = context()->get_def_use_mgr()->GetDef(*id_ptr);
387
0
      const analysis::Type* type =
388
0
          context()->get_type_mgr()->GetType(op_inst->type_id());
389
0
      if (type &&
390
0
          (type->AsPointer() || type->AsImage() || type->AsSampledImage())) {
391
0
        bool operand_coherent = false;
392
0
        bool operand_volatile = false;
393
0
        std::tie(operand_coherent, operand_volatile) =
394
0
            TraceInstruction(op_inst, indices, visited);
395
0
        is_coherent |= operand_coherent;
396
0
        is_volatile |= operand_volatile;
397
0
      }
398
0
    });
399
0
  }
400
401
0
  cached_result.first = is_coherent;
402
0
  cached_result.second = is_volatile;
403
0
  return std::make_pair(is_coherent, is_volatile);
404
0
}
405
406
std::pair<bool, bool> UpgradeMemoryModel::CheckType(
407
0
    uint32_t type_id, const std::vector<uint32_t>& indices) {
408
0
  bool is_coherent = false;
409
0
  bool is_volatile = false;
410
0
  Instruction* type_inst = context()->get_def_use_mgr()->GetDef(type_id);
411
0
  assert(type_inst->opcode() == spv::Op::OpTypePointer);
412
0
  Instruction* element_inst = context()->get_def_use_mgr()->GetDef(
413
0
      type_inst->GetSingleWordInOperand(1u));
414
0
  for (int i = (int)indices.size() - 1; i >= 0; --i) {
415
0
    if (is_coherent && is_volatile) break;
416
417
0
    if (element_inst->opcode() == spv::Op::OpTypePointer) {
418
0
      element_inst = context()->get_def_use_mgr()->GetDef(
419
0
          element_inst->GetSingleWordInOperand(1u));
420
0
    } else if (element_inst->opcode() == spv::Op::OpTypeStruct) {
421
0
      uint32_t index = indices.at(i);
422
0
      Instruction* index_inst = context()->get_def_use_mgr()->GetDef(index);
423
0
      assert(index_inst->opcode() == spv::Op::OpConstant);
424
0
      uint64_t value = GetIndexValue(index_inst);
425
0
      is_coherent |= HasDecoration(element_inst, static_cast<uint32_t>(value),
426
0
                                   spv::Decoration::Coherent);
427
0
      is_volatile |= HasDecoration(element_inst, static_cast<uint32_t>(value),
428
0
                                   spv::Decoration::Volatile);
429
0
      element_inst = context()->get_def_use_mgr()->GetDef(
430
0
          element_inst->GetSingleWordInOperand(static_cast<uint32_t>(value)));
431
0
    } else {
432
0
      assert(spvOpcodeIsComposite(element_inst->opcode()));
433
0
      element_inst = context()->get_def_use_mgr()->GetDef(
434
0
          element_inst->GetSingleWordInOperand(0u));
435
0
    }
436
0
  }
437
438
0
  if (!is_coherent || !is_volatile) {
439
0
    bool remaining_coherent = false;
440
0
    bool remaining_volatile = false;
441
0
    std::tie(remaining_coherent, remaining_volatile) =
442
0
        CheckAllTypes(element_inst);
443
0
    is_coherent |= remaining_coherent;
444
0
    is_volatile |= remaining_volatile;
445
0
  }
446
447
0
  return std::make_pair(is_coherent, is_volatile);
448
0
}
449
450
std::pair<bool, bool> UpgradeMemoryModel::CheckAllTypes(
451
0
    const Instruction* inst) {
452
0
  std::unordered_set<const Instruction*> visited;
453
0
  std::vector<const Instruction*> stack;
454
0
  stack.push_back(inst);
455
456
0
  bool is_coherent = false;
457
0
  bool is_volatile = false;
458
0
  while (!stack.empty()) {
459
0
    const Instruction* def = stack.back();
460
0
    stack.pop_back();
461
462
0
    if (!visited.insert(def).second) continue;
463
464
0
    if (def->opcode() == spv::Op::OpTypeStruct) {
465
      // Any member decorated with coherent and/or volatile is enough to have
466
      // the related operation be flagged as coherent and/or volatile.
467
0
      is_coherent |= HasDecoration(def, std::numeric_limits<uint32_t>::max(),
468
0
                                   spv::Decoration::Coherent);
469
0
      is_volatile |= HasDecoration(def, std::numeric_limits<uint32_t>::max(),
470
0
                                   spv::Decoration::Volatile);
471
0
      if (is_coherent && is_volatile)
472
0
        return std::make_pair(is_coherent, is_volatile);
473
474
      // Check the subtypes.
475
0
      for (uint32_t i = 0; i < def->NumInOperands(); ++i) {
476
0
        stack.push_back(context()->get_def_use_mgr()->GetDef(
477
0
            def->GetSingleWordInOperand(i)));
478
0
      }
479
0
    } else if (spvOpcodeIsComposite(def->opcode())) {
480
0
      stack.push_back(context()->get_def_use_mgr()->GetDef(
481
0
          def->GetSingleWordInOperand(0u)));
482
0
    } else if (def->opcode() == spv::Op::OpTypePointer) {
483
0
      stack.push_back(context()->get_def_use_mgr()->GetDef(
484
0
          def->GetSingleWordInOperand(1u)));
485
0
    }
486
0
  }
487
488
0
  return std::make_pair(is_coherent, is_volatile);
489
0
}
490
491
0
uint64_t UpgradeMemoryModel::GetIndexValue(Instruction* index_inst) {
492
0
  const analysis::Constant* index_constant =
493
0
      context()->get_constant_mgr()->GetConstantFromInst(index_inst);
494
0
  assert(index_constant->AsIntConstant());
495
0
  if (index_constant->type()->AsInteger()->IsSigned()) {
496
0
    if (index_constant->type()->AsInteger()->width() == 32) {
497
0
      return index_constant->GetS32();
498
0
    } else {
499
0
      return index_constant->GetS64();
500
0
    }
501
0
  } else {
502
0
    if (index_constant->type()->AsInteger()->width() == 32) {
503
0
      return index_constant->GetU32();
504
0
    } else {
505
0
      return index_constant->GetU64();
506
0
    }
507
0
  }
508
0
}
509
510
bool UpgradeMemoryModel::HasDecoration(const Instruction* inst, uint32_t value,
511
0
                                       spv::Decoration decoration) {
512
  // If the iteration was terminated early then an appropriate decoration was
513
  // found.
514
0
  return !context()->get_decoration_mgr()->WhileEachDecoration(
515
0
      inst->result_id(), (uint32_t)decoration, [value](const Instruction& i) {
516
0
        if (i.opcode() == spv::Op::OpDecorate ||
517
0
            i.opcode() == spv::Op::OpDecorateId) {
518
0
          return false;
519
0
        } else if (i.opcode() == spv::Op::OpMemberDecorate) {
520
0
          if (value == i.GetSingleWordInOperand(1u) ||
521
0
              value == std::numeric_limits<uint32_t>::max())
522
0
            return false;
523
0
        }
524
525
0
        return true;
526
0
      });
527
0
}
528
529
void UpgradeMemoryModel::UpgradeFlags(Instruction* inst, uint32_t in_operand,
530
                                      bool is_coherent, bool is_volatile,
531
                                      OperationType operation_type,
532
0
                                      InstructionType inst_type) {
533
0
  if (!is_coherent && !is_volatile) return;
534
535
0
  uint32_t flags = 0;
536
0
  if (inst->NumInOperands() > in_operand) {
537
0
    flags |= inst->GetSingleWordInOperand(in_operand);
538
0
  }
539
0
  if (is_coherent) {
540
0
    if (inst_type == kMemory) {
541
0
      flags |= uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR);
542
0
      if (operation_type == kVisibility) {
543
0
        flags |= uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR);
544
0
      } else {
545
0
        flags |= uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR);
546
0
      }
547
0
    } else {
548
0
      flags |= uint32_t(spv::ImageOperandsMask::NonPrivateTexelKHR);
549
0
      if (operation_type == kVisibility) {
550
0
        flags |= uint32_t(spv::ImageOperandsMask::MakeTexelVisibleKHR);
551
0
      } else {
552
0
        flags |= uint32_t(spv::ImageOperandsMask::MakeTexelAvailableKHR);
553
0
      }
554
0
    }
555
0
  }
556
557
0
  if (is_volatile) {
558
0
    if (inst_type == kMemory) {
559
0
      flags |= uint32_t(spv::MemoryAccessMask::Volatile);
560
0
    } else {
561
0
      flags |= uint32_t(spv::ImageOperandsMask::VolatileTexelKHR);
562
0
    }
563
0
  }
564
565
0
  if (inst->NumInOperands() > in_operand) {
566
0
    inst->SetInOperand(in_operand, {flags});
567
0
  } else if (inst_type == kMemory) {
568
0
    inst->AddOperand({SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS, {flags}});
569
0
  } else {
570
0
    inst->AddOperand({SPV_OPERAND_TYPE_OPTIONAL_IMAGE, {flags}});
571
0
  }
572
0
}
573
574
0
uint32_t UpgradeMemoryModel::GetScopeConstant(spv::Scope scope) {
575
0
  analysis::Integer int_ty(32, false);
576
0
  uint32_t int_id = context()->get_type_mgr()->GetTypeInstruction(&int_ty);
577
0
  const analysis::Constant* constant =
578
0
      context()->get_constant_mgr()->GetConstant(
579
0
          context()->get_type_mgr()->GetType(int_id),
580
0
          {static_cast<uint32_t>(scope)});
581
0
  return context()
582
0
      ->get_constant_mgr()
583
0
      ->GetDefiningInstruction(constant)
584
0
      ->result_id();
585
0
}
586
587
0
void UpgradeMemoryModel::CleanupDecorations() {
588
  // All of the volatile and coherent decorations have been dealt with, so now
589
  // we can just remove them.
590
0
  get_module()->ForEachInst([this](Instruction* inst) {
591
0
    if (inst->result_id() != 0) {
592
0
      context()->get_decoration_mgr()->RemoveDecorationsFrom(
593
0
          inst->result_id(), [](const Instruction& dec) {
594
0
            switch (dec.opcode()) {
595
0
              case spv::Op::OpDecorate:
596
0
              case spv::Op::OpDecorateId:
597
0
                if (spv::Decoration(dec.GetSingleWordInOperand(1u)) ==
598
0
                        spv::Decoration::Coherent ||
599
0
                    spv::Decoration(dec.GetSingleWordInOperand(1u)) ==
600
0
                        spv::Decoration::Volatile)
601
0
                  return true;
602
0
                break;
603
0
              case spv::Op::OpMemberDecorate:
604
0
                if (spv::Decoration(dec.GetSingleWordInOperand(2u)) ==
605
0
                        spv::Decoration::Coherent ||
606
0
                    spv::Decoration(dec.GetSingleWordInOperand(2u)) ==
607
0
                        spv::Decoration::Volatile)
608
0
                  return true;
609
0
                break;
610
0
              default:
611
0
                break;
612
0
            }
613
0
            return false;
614
0
          });
615
0
    }
616
0
  });
617
0
}
618
619
0
void UpgradeMemoryModel::UpgradeBarriers() {
620
0
  std::vector<Instruction*> barriers;
621
  // Collects all the control barriers in |function|. Returns true if the
622
  // function operates on the Output storage class.
623
0
  ProcessFunction CollectBarriers = [this, &barriers](Function* function) {
624
0
    bool operates_on_output = false;
625
0
    for (auto& block : *function) {
626
0
      block.ForEachInst([this, &barriers,
627
0
                         &operates_on_output](Instruction* inst) {
628
0
        if (inst->opcode() == spv::Op::OpControlBarrier) {
629
0
          barriers.push_back(inst);
630
0
        } else if (!operates_on_output) {
631
          // This instruction operates on output storage class if it is a
632
          // pointer to output type or any input operand is a pointer to output
633
          // type.
634
0
          analysis::Type* type =
635
0
              context()->get_type_mgr()->GetType(inst->type_id());
636
0
          if (type && type->AsPointer() &&
637
0
              type->AsPointer()->storage_class() == spv::StorageClass::Output) {
638
0
            operates_on_output = true;
639
0
            return;
640
0
          }
641
0
          inst->ForEachInId([this, &operates_on_output](uint32_t* id_ptr) {
642
0
            Instruction* op_inst =
643
0
                context()->get_def_use_mgr()->GetDef(*id_ptr);
644
0
            analysis::Type* op_type =
645
0
                context()->get_type_mgr()->GetType(op_inst->type_id());
646
0
            if (op_type && op_type->AsPointer() &&
647
0
                op_type->AsPointer()->storage_class() ==
648
0
                    spv::StorageClass::Output)
649
0
              operates_on_output = true;
650
0
          });
651
0
        }
652
0
      });
653
0
    }
654
0
    return operates_on_output;
655
0
  };
656
657
0
  std::queue<uint32_t> roots;
658
0
  for (auto& e : get_module()->entry_points())
659
0
    if (spv::ExecutionModel(e.GetSingleWordInOperand(0u)) ==
660
0
        spv::ExecutionModel::TessellationControl) {
661
0
      roots.push(e.GetSingleWordInOperand(1u));
662
0
      if (context()->ProcessCallTreeFromRoots(CollectBarriers, &roots)) {
663
0
        for (auto barrier : barriers) {
664
          // Add OutputMemoryKHR to the semantics of the barriers.
665
0
          uint32_t semantics_id = barrier->GetSingleWordInOperand(2u);
666
0
          Instruction* semantics_inst =
667
0
              context()->get_def_use_mgr()->GetDef(semantics_id);
668
0
          analysis::Type* semantics_type =
669
0
              context()->get_type_mgr()->GetType(semantics_inst->type_id());
670
0
          uint64_t semantics_value = GetIndexValue(semantics_inst);
671
0
          const analysis::Constant* constant =
672
0
              context()->get_constant_mgr()->GetConstant(
673
0
                  semantics_type,
674
0
                  {static_cast<uint32_t>(semantics_value) |
675
0
                   uint32_t(spv::MemorySemanticsMask::OutputMemoryKHR)});
676
0
          barrier->SetInOperand(2u, {context()
677
0
                                         ->get_constant_mgr()
678
0
                                         ->GetDefiningInstruction(constant)
679
0
                                         ->result_id()});
680
0
        }
681
0
      }
682
0
      barriers.clear();
683
0
    }
684
0
}
685
686
0
void UpgradeMemoryModel::UpgradeMemoryScope() {
687
0
  get_module()->ForEachInst([this](Instruction* inst) {
688
    // Don't need to handle all the operations that take a scope.
689
    // * Group operations can only be subgroup
690
    // * Non-uniform can only be workgroup or subgroup
691
    // * Named barriers are not supported by Vulkan
692
    // * Workgroup ops (e.g. async_copy) have at most workgroup scope.
693
0
    if (spvOpcodeIsAtomicOp(inst->opcode())) {
694
0
      if (IsDeviceScope(inst->GetSingleWordInOperand(1))) {
695
0
        inst->SetInOperand(1, {GetScopeConstant(spv::Scope::QueueFamilyKHR)});
696
0
      }
697
0
    } else if (inst->opcode() == spv::Op::OpControlBarrier) {
698
0
      if (IsDeviceScope(inst->GetSingleWordInOperand(1))) {
699
0
        inst->SetInOperand(1, {GetScopeConstant(spv::Scope::QueueFamilyKHR)});
700
0
      }
701
0
    } else if (inst->opcode() == spv::Op::OpMemoryBarrier) {
702
0
      if (IsDeviceScope(inst->GetSingleWordInOperand(0))) {
703
0
        inst->SetInOperand(0, {GetScopeConstant(spv::Scope::QueueFamilyKHR)});
704
0
      }
705
0
    }
706
0
  });
707
0
}
708
709
0
bool UpgradeMemoryModel::IsDeviceScope(uint32_t scope_id) {
710
0
  const analysis::Constant* constant =
711
0
      context()->get_constant_mgr()->FindDeclaredConstant(scope_id);
712
0
  assert(constant && "Memory scope must be a constant");
713
714
0
  const analysis::Integer* type = constant->type()->AsInteger();
715
0
  assert(type);
716
0
  assert(type->width() == 32 || type->width() == 64);
717
0
  if (type->width() == 32) {
718
0
    if (type->IsSigned())
719
0
      return static_cast<spv::Scope>(constant->GetS32()) == spv::Scope::Device;
720
0
    else
721
0
      return static_cast<spv::Scope>(constant->GetU32()) == spv::Scope::Device;
722
0
  } else {
723
0
    if (type->IsSigned())
724
0
      return static_cast<spv::Scope>(constant->GetS64()) == spv::Scope::Device;
725
0
    else
726
0
      return static_cast<spv::Scope>(constant->GetU64()) == spv::Scope::Device;
727
0
  }
728
729
0
  assert(false);
730
0
  return false;
731
0
}
732
733
0
void UpgradeMemoryModel::UpgradeExtInst(Instruction* ext_inst) {
734
0
  const bool is_modf = ext_inst->GetSingleWordInOperand(1u) == GLSLstd450Modf;
735
0
  auto ptr_id = ext_inst->GetSingleWordInOperand(3u);
736
0
  auto ptr_type_id = get_def_use_mgr()->GetDef(ptr_id)->type_id();
737
0
  auto pointee_type_id =
738
0
      get_def_use_mgr()->GetDef(ptr_type_id)->GetSingleWordInOperand(1u);
739
0
  auto element_type_id = ext_inst->type_id();
740
0
  std::vector<const analysis::Type*> element_types(2);
741
0
  element_types[0] = context()->get_type_mgr()->GetType(element_type_id);
742
0
  element_types[1] = context()->get_type_mgr()->GetType(pointee_type_id);
743
0
  analysis::Struct struct_type(element_types);
744
0
  uint32_t struct_id =
745
0
      context()->get_type_mgr()->GetTypeInstruction(&struct_type);
746
  // Change the operation
747
0
  GLSLstd450 new_op = is_modf ? GLSLstd450ModfStruct : GLSLstd450FrexpStruct;
748
0
  ext_inst->SetOperand(3u, {static_cast<uint32_t>(new_op)});
749
  // Remove the pointer argument
750
0
  ext_inst->RemoveOperand(5u);
751
  // Set the type id to the new struct.
752
0
  ext_inst->SetResultType(struct_id);
753
754
  // The result is now a struct of the original result. The zero'th element is
755
  // old result and should replace the old result. The one'th element needs to
756
  // be stored via a new instruction.
757
0
  auto where = ext_inst->NextNode();
758
0
  InstructionBuilder builder(
759
0
      context(), where,
760
0
      IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
761
0
  auto extract_0 =
762
0
      builder.AddCompositeExtract(element_type_id, ext_inst->result_id(), {0});
763
0
  context()->ReplaceAllUsesWith(ext_inst->result_id(), extract_0->result_id());
764
  // The extract's input was just changed to itself, so fix that.
765
0
  extract_0->SetInOperand(0u, {ext_inst->result_id()});
766
0
  auto extract_1 =
767
0
      builder.AddCompositeExtract(pointee_type_id, ext_inst->result_id(), {1});
768
0
  builder.AddStore(ptr_id, extract_1->result_id());
769
0
}
770
771
0
uint32_t UpgradeMemoryModel::MemoryAccessNumWords(uint32_t mask) {
772
0
  uint32_t result = 1;
773
0
  if (mask & uint32_t(spv::MemoryAccessMask::Aligned)) ++result;
774
0
  if (mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) ++result;
775
0
  if (mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) ++result;
776
0
  return result;
777
0
}
778
779
}  // namespace opt
780
}  // namespace spvtools