transformation_add_global_variable.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "source/fuzz/transformation_add_global_variable.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. TransformationAddGlobalVariable::TransformationAddGlobalVariable(
  19. spvtools::fuzz::protobufs::TransformationAddGlobalVariable message)
  20. : message_(std::move(message)) {}
  21. TransformationAddGlobalVariable::TransformationAddGlobalVariable(
  22. uint32_t fresh_id, uint32_t type_id, spv::StorageClass storage_class,
  23. uint32_t initializer_id, bool value_is_irrelevant) {
  24. message_.set_fresh_id(fresh_id);
  25. message_.set_type_id(type_id);
  26. message_.set_storage_class(uint32_t(storage_class));
  27. message_.set_initializer_id(initializer_id);
  28. message_.set_value_is_irrelevant(value_is_irrelevant);
  29. }
  30. bool TransformationAddGlobalVariable::IsApplicable(
  31. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  32. // The result id must be fresh.
  33. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  34. return false;
  35. }
  36. // The storage class must be Private or Workgroup.
  37. auto storage_class = static_cast<spv::StorageClass>(message_.storage_class());
  38. switch (storage_class) {
  39. case spv::StorageClass::Private:
  40. case spv::StorageClass::Workgroup:
  41. break;
  42. default:
  43. assert(false && "Unsupported storage class.");
  44. return false;
  45. }
  46. // The type id must correspond to a type.
  47. auto type = ir_context->get_type_mgr()->GetType(message_.type_id());
  48. if (!type) {
  49. return false;
  50. }
  51. // That type must be a pointer type ...
  52. auto pointer_type = type->AsPointer();
  53. if (!pointer_type) {
  54. return false;
  55. }
  56. // ... with the right storage class.
  57. if (pointer_type->storage_class() != storage_class) {
  58. return false;
  59. }
  60. if (message_.initializer_id()) {
  61. // An initializer is not allowed if the storage class is Workgroup.
  62. if (storage_class == spv::StorageClass::Workgroup) {
  63. assert(false &&
  64. "By construction this transformation should not have an "
  65. "initializer when Workgroup storage class is used.");
  66. return false;
  67. }
  68. // The initializer id must be the id of a constant. Check this with the
  69. // constant manager.
  70. auto constant_id = ir_context->get_constant_mgr()->GetConstantsFromIds(
  71. {message_.initializer_id()});
  72. if (constant_id.empty()) {
  73. return false;
  74. }
  75. assert(constant_id.size() == 1 &&
  76. "We asked for the constant associated with a single id; we should "
  77. "get a single constant.");
  78. // The type of the constant must match the pointee type of the pointer.
  79. if (pointer_type->pointee_type() != constant_id[0]->type()) {
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. void TransformationAddGlobalVariable::Apply(
  86. opt::IRContext* ir_context,
  87. TransformationContext* transformation_context) const {
  88. opt::Instruction* new_instruction = fuzzerutil::AddGlobalVariable(
  89. ir_context, message_.fresh_id(), message_.type_id(),
  90. static_cast<spv::StorageClass>(message_.storage_class()),
  91. message_.initializer_id());
  92. // Inform the def-use manager about the new instruction.
  93. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction);
  94. if (message_.value_is_irrelevant()) {
  95. transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  96. message_.fresh_id());
  97. }
  98. }
  99. protobufs::Transformation TransformationAddGlobalVariable::ToMessage() const {
  100. protobufs::Transformation result;
  101. *result.mutable_add_global_variable() = message_;
  102. return result;
  103. }
  104. std::unordered_set<uint32_t> TransformationAddGlobalVariable::GetFreshIds()
  105. const {
  106. return {message_.fresh_id()};
  107. }
  108. } // namespace fuzz
  109. } // namespace spvtools