Home | History | Annotate | Download | only in opt
      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 "source/opt/strip_reflect_info_pass.h"
     16 
     17 #include <cstring>
     18 #include <vector>
     19 
     20 #include "source/opt/instruction.h"
     21 #include "source/opt/ir_context.h"
     22 
     23 namespace spvtools {
     24 namespace opt {
     25 
     26 Pass::Status StripReflectInfoPass::Process() {
     27   bool modified = false;
     28 
     29   std::vector<Instruction*> to_remove;
     30 
     31   bool other_uses_for_decorate_string = false;
     32   for (auto& inst : context()->module()->annotations()) {
     33     switch (inst.opcode()) {
     34       case SpvOpDecorateStringGOOGLE:
     35         if (inst.GetSingleWordInOperand(1) == SpvDecorationHlslSemanticGOOGLE) {
     36           to_remove.push_back(&inst);
     37         } else {
     38           other_uses_for_decorate_string = true;
     39         }
     40         break;
     41 
     42       case SpvOpMemberDecorateStringGOOGLE:
     43         if (inst.GetSingleWordInOperand(2) == SpvDecorationHlslSemanticGOOGLE) {
     44           to_remove.push_back(&inst);
     45         } else {
     46           other_uses_for_decorate_string = true;
     47         }
     48         break;
     49 
     50       case SpvOpDecorateId:
     51         if (inst.GetSingleWordInOperand(1) ==
     52             SpvDecorationHlslCounterBufferGOOGLE) {
     53           to_remove.push_back(&inst);
     54         }
     55         break;
     56 
     57       default:
     58         break;
     59     }
     60   }
     61 
     62   for (auto& inst : context()->module()->extensions()) {
     63     const char* ext_name =
     64         reinterpret_cast<const char*>(&inst.GetInOperand(0).words[0]);
     65     if (0 == std::strcmp(ext_name, "SPV_GOOGLE_hlsl_functionality1")) {
     66       to_remove.push_back(&inst);
     67     } else if (!other_uses_for_decorate_string &&
     68                0 == std::strcmp(ext_name, "SPV_GOOGLE_decorate_string")) {
     69       to_remove.push_back(&inst);
     70     }
     71   }
     72 
     73   for (auto* inst : to_remove) {
     74     modified = true;
     75     context()->KillInst(inst);
     76   }
     77 
     78   return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
     79 }
     80 
     81 }  // namespace opt
     82 }  // namespace spvtools
     83