modify_maximal_reconvergence.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) 2024 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. #include "modify_maximal_reconvergence.h"
  15. #include "source/opt/ir_context.h"
  16. #include "source/util/make_unique.h"
  17. namespace spvtools {
  18. namespace opt {
  19. Pass::Status ModifyMaximalReconvergence::Process() {
  20. bool changed = false;
  21. if (add_) {
  22. changed = AddMaximalReconvergence();
  23. } else {
  24. changed = RemoveMaximalReconvergence();
  25. }
  26. return changed ? Pass::Status::SuccessWithChange
  27. : Pass::Status::SuccessWithoutChange;
  28. }
  29. bool ModifyMaximalReconvergence::AddMaximalReconvergence() {
  30. bool changed = false;
  31. bool has_extension = false;
  32. bool has_shader =
  33. context()->get_feature_mgr()->HasCapability(spv::Capability::Shader);
  34. for (auto extension : context()->extensions()) {
  35. if (extension.GetOperand(0).AsString() == "SPV_KHR_maximal_reconvergence") {
  36. has_extension = true;
  37. break;
  38. }
  39. }
  40. std::unordered_set<uint32_t> entry_points_with_mode;
  41. for (auto mode : get_module()->execution_modes()) {
  42. if (spv::ExecutionMode(mode.GetSingleWordInOperand(1)) ==
  43. spv::ExecutionMode::MaximallyReconvergesKHR) {
  44. entry_points_with_mode.insert(mode.GetSingleWordInOperand(0));
  45. }
  46. }
  47. for (auto entry_point : get_module()->entry_points()) {
  48. const uint32_t id = entry_point.GetSingleWordInOperand(1);
  49. if (!entry_points_with_mode.count(id)) {
  50. changed = true;
  51. if (!has_extension) {
  52. context()->AddExtension("SPV_KHR_maximal_reconvergence");
  53. has_extension = true;
  54. }
  55. if (!has_shader) {
  56. context()->AddCapability(spv::Capability::Shader);
  57. has_shader = true;
  58. }
  59. context()->AddExecutionMode(MakeUnique<Instruction>(
  60. context(), spv::Op::OpExecutionMode, 0, 0,
  61. std::initializer_list<Operand>{
  62. {SPV_OPERAND_TYPE_ID, {id}},
  63. {SPV_OPERAND_TYPE_EXECUTION_MODE,
  64. {static_cast<uint32_t>(
  65. spv::ExecutionMode::MaximallyReconvergesKHR)}}}));
  66. entry_points_with_mode.insert(id);
  67. }
  68. }
  69. return changed;
  70. }
  71. bool ModifyMaximalReconvergence::RemoveMaximalReconvergence() {
  72. bool changed = false;
  73. std::vector<Instruction*> to_remove;
  74. Instruction* mode = &*get_module()->execution_mode_begin();
  75. while (mode) {
  76. if (mode->opcode() != spv::Op::OpExecutionMode &&
  77. mode->opcode() != spv::Op::OpExecutionModeId) {
  78. break;
  79. }
  80. if (spv::ExecutionMode(mode->GetSingleWordInOperand(1)) ==
  81. spv::ExecutionMode::MaximallyReconvergesKHR) {
  82. mode = context()->KillInst(mode);
  83. changed = true;
  84. } else {
  85. mode = mode->NextNode();
  86. }
  87. }
  88. changed |=
  89. context()->RemoveExtension(Extension::kSPV_KHR_maximal_reconvergence);
  90. return changed;
  91. }
  92. } // namespace opt
  93. } // namespace spvtools