| 33 | transformations, ignore_inapplicable_transformations) {} |
| 34 | |
| 35 | void FuzzerPassReplaceIrrelevantIds::Apply() { |
| 36 | // Keep track of the irrelevant ids. This includes all the ids that are |
| 37 | // irrelevant according to the fact manager and that are still present in the |
| 38 | // module (some of them may have been removed by previously-run |
| 39 | // transformations). |
| 40 | std::vector<uint32_t> irrelevant_ids; |
| 41 | |
| 42 | // Keep a map from the type ids of irrelevant ids to all the ids with that |
| 43 | // type. |
| 44 | std::unordered_map<uint32_t, std::vector<uint32_t>> types_to_ids; |
| 45 | |
| 46 | // Find all the irrelevant ids that still exist in the module and all the |
| 47 | // types for which irrelevant ids exist. |
| 48 | for (auto id : |
| 49 | GetTransformationContext()->GetFactManager()->GetIrrelevantIds()) { |
| 50 | // Check that the id still exists in the module. |
| 51 | auto declaration = GetIRContext()->get_def_use_mgr()->GetDef(id); |
| 52 | if (!declaration) { |
| 53 | continue; |
| 54 | } |
| 55 | |
| 56 | irrelevant_ids.push_back(id); |
| 57 | |
| 58 | // If the type of this id has not been seen before, add a mapping from this |
| 59 | // type id to an empty list in |types_to_ids|. The list will be filled later |
| 60 | // on. |
| 61 | if (types_to_ids.count(declaration->type_id()) == 0) { |
| 62 | types_to_ids.insert({declaration->type_id(), {}}); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // If no irrelevant ids were found, return. |
| 67 | if (irrelevant_ids.empty()) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // For every type for which we have at least one irrelevant id, record all ids |
| 72 | // in the module which have that type. Skip ids of OpFunction instructions as |
| 73 | // we cannot use these as replacements. |
| 74 | for (const auto& pair : GetIRContext()->get_def_use_mgr()->id_to_defs()) { |
| 75 | uint32_t type_id = pair.second->type_id(); |
| 76 | if (pair.second->opcode() != spv::Op::OpFunction && type_id && |
| 77 | types_to_ids.count(type_id)) { |
| 78 | types_to_ids[type_id].push_back(pair.first); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Keep a list of all the transformations to perform. We avoid applying the |
| 83 | // transformations while traversing the uses since applying the transformation |
| 84 | // invalidates all analyses, and we want to avoid invalidating and recomputing |
| 85 | // them every time. |
| 86 | std::vector<TransformationReplaceIrrelevantId> transformations_to_apply; |
| 87 | |
| 88 | // Loop through all the uses of irrelevant ids, check that the id can be |
| 89 | // replaced and randomly decide whether to apply the transformation. |
| 90 | for (auto irrelevant_id : irrelevant_ids) { |
| 91 | uint32_t type_id = |
| 92 | GetIRContext()->get_def_use_mgr()->GetDef(irrelevant_id)->type_id(); |
nothing calls this directly
no test coverage detected