fix_storage_class.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2019 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. #ifndef SOURCE_OPT_FIX_STORAGE_CLASS_H_
  15. #define SOURCE_OPT_FIX_STORAGE_CLASS_H_
  16. #include <unordered_map>
  17. #include "source/opt/ir_context.h"
  18. #include "source/opt/module.h"
  19. #include "source/opt/pass.h"
  20. namespace spvtools {
  21. namespace opt {
  22. // This pass tries to fix validation error due to a mismatch of storage classes
  23. // in instructions. There is no guarantee that all such error will be fixed,
  24. // and it is possible that in fixing these errors, it could lead to other
  25. // errors.
  26. class FixStorageClass : public Pass {
  27. public:
  28. const char* name() const override { return "fix-storage-class"; }
  29. Status Process() override;
  30. // Return the mask of preserved Analyses.
  31. IRContext::Analysis GetPreservedAnalyses() override {
  32. return IRContext::kAnalysisDefUse |
  33. IRContext::kAnalysisInstrToBlockMapping |
  34. IRContext::kAnalysisCombinators | IRContext::kAnalysisCFG |
  35. IRContext::kAnalysisDominatorAnalysis |
  36. IRContext::kAnalysisLoopAnalysis | IRContext::kAnalysisNameMap |
  37. IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
  38. }
  39. private:
  40. // Changes the storage class of the result of |inst| to |storage_class| in
  41. // appropriate, and propagates the change to the users of |inst| as well.
  42. // Returns true of any changes were made.
  43. // |seen| is used to track OpPhi instructions that should not be processed.
  44. bool PropagateStorageClass(Instruction* inst, spv::StorageClass storage_class,
  45. std::set<uint32_t>* seen);
  46. // Changes the storage class of the result of |inst| to |storage_class|.
  47. // Is it assumed that the result type of |inst| is a pointer type.
  48. // Propagates the change to the users of |inst| as well.
  49. // Returns true of any changes were made.
  50. // |seen| is used to track OpPhi instructions that should not be processed by
  51. // |PropagateStorageClass|
  52. void FixInstructionStorageClass(Instruction* inst,
  53. spv::StorageClass storage_class,
  54. std::set<uint32_t>* seen);
  55. // Changes the storage class of the result of |inst| to |storage_class|. The
  56. // result type of |inst| must be a pointer.
  57. void ChangeResultStorageClass(Instruction* inst,
  58. spv::StorageClass storage_class) const;
  59. // Returns true if the result type of |inst| is a pointer.
  60. bool IsPointerResultType(Instruction* inst);
  61. // Returns true if the result of |inst| is a pointer to storage class
  62. // |storage_class|.
  63. bool IsPointerToStorageClass(Instruction* inst,
  64. spv::StorageClass storage_class);
  65. // Change |inst| to match that operand |op_idx| now has type |type_id|, and
  66. // adjust any uses of |inst| accordingly. Returns true if the code changed.
  67. bool PropagateType(Instruction* inst, uint32_t type_id, uint32_t op_idx,
  68. std::set<uint32_t>* seen);
  69. // Changes the result type of |inst| to |new_type_id|.
  70. bool ChangeResultType(Instruction* inst, uint32_t new_type_id);
  71. // Returns the type id of the member of the type |id| that would be returned
  72. // by following the indices of the access chain instruction |inst|.
  73. uint32_t WalkAccessChainType(Instruction* inst, uint32_t id);
  74. };
  75. } // namespace opt
  76. } // namespace spvtools
  77. #endif // SOURCE_OPT_FIX_STORAGE_CLASS_H_