Home | History | Annotate | Download | only in spirit
      1 /*
      2  * Copyright 2017, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef RSOV_COMPILER_SPIRIT_PASS_QUEUE_H
     18 #define RSOV_COMPILER_SPIRIT_PASS_QUEUE_H
     19 
     20 #include "module.h"
     21 #include "pass.h"
     22 #include "stl_util.h"
     23 
     24 #include <stdint.h>
     25 
     26 #include <memory>
     27 #include <set>
     28 #include <vector>
     29 
     30 namespace android {
     31 namespace spirit {
     32 
     33 // A FIFO of passes. Passes are appended to the end of the FIFO and run in the
     34 // first-in first-out order. Once appended to a pass queue, Passes are owned by
     35 // the queue.
     36 class PassQueue {
     37 public:
     38   PassQueue() : mPassesDeleter(mPassSet) {}
     39 
     40   // Appends a pass to the end of the queue
     41   bool append(Pass *pass);
     42 
     43   // Runs all passes in the queue in the first-in first-out order on a Module.
     44   // Returns the result Module after all passes have run.
     45   // If argument error is not null, sets the error code. On a successful run,
     46   // error code is set to zero.
     47   Module *run(Module *module, int *error = nullptr);
     48 
     49   // Deserialize the input vector of words into a Module, and runs all passes in
     50   // the queue in the first-in first-out order on the Module.
     51   // for a serialized Module.
     52   // After all the passes have run, returns the words from the serialized result
     53   // Module.
     54   // If argument error is not null, sets the error code. On a successful run,
     55   // error code is set to zero.
     56   std::vector<uint32_t> run(const std::vector<uint32_t> &spirvWords,
     57                             int *error = nullptr);
     58 
     59   // Runs all passes in the queue in the first-in first-out order on a Module.
     60   // After all the passes have run, serializes the result Module, and returns
     61   // the words as a vector.
     62   // If argument error is not null, sets the error code. On a successful run,
     63   // error code is set to zero.
     64   std::vector<uint32_t> runAndSerialize(Module *module, int *error = nullptr);
     65 
     66 private:
     67   std::vector<Pass *> mPasses;
     68   // Keep all passes in a set so that we can delete them on destruction without
     69   // worrying about duplicates
     70   std::set<Pass *> mPassSet;
     71   ContainerDeleter<std::set<Pass *>> mPassesDeleter;
     72 };
     73 
     74 } // spirit
     75 } // android
     76 
     77 #endif // RSOV_COMPILER_SPIRIT_PASS_QUEUE_H
     78