1 /** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 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 16 #ifndef COMPILER_OPTIMIZER_OPTIMIZATIONS_SCHEDULER_H_ 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_SCHEDULER_H_ 18 19 #include "optimizer/ir/graph.h" 20 #include "compiler_options.h" 21 22 namespace panda::compiler { 23 class Scheduler : public Optimization { 24 public: Scheduler(Graph * graph)25 explicit Scheduler(Graph *graph) 26 : Optimization(graph), 27 sched_(graph->GetLocalAllocator()->Adapter()), 28 loads_(graph->GetLocalAllocator()->Adapter()), 29 stores_(graph->GetLocalAllocator()->Adapter()), 30 special_(graph->GetLocalAllocator()->Adapter()), 31 old_(graph->GetLocalAllocator()->Adapter()), 32 ocycle_(graph->GetLocalAllocator()->Adapter()), 33 num_deps_(graph->GetLocalAllocator()->Adapter()), 34 asap_(graph->GetLocalAllocator()->Adapter()), 35 prio_(graph->GetLocalAllocator()->Adapter()), 36 deps_(graph->GetLocalAllocator()->Adapter()) 37 { 38 } 39 40 NO_MOVE_SEMANTIC(Scheduler); 41 NO_COPY_SEMANTIC(Scheduler); 42 ~Scheduler() override = default; 43 44 bool RunImpl() override; 45 IsEnable()46 bool IsEnable() const override 47 { 48 return options.IsCompilerScheduling(); 49 } 50 GetPassName()51 const char *GetPassName() const override 52 { 53 return "Scheduler"; 54 } 55 56 private: 57 void AddDep(uint32_t *prio, Inst *from, Inst *to, uint32_t latency, Inst *barrier); 58 bool ScheduleBasicBlock(BasicBlock *bb); 59 bool BuildAllDeps(BasicBlock *bb); 60 61 void ProcessInst(Inst *inst, Marker mrk, uint32_t *num_inst, uint32_t *num_between, uint32_t *num_special, 62 Inst **last_barrier); 63 void ProcessMemory(Inst *inst, uint32_t *prio, Inst *last_barrier); 64 void ProcessSpecial(Inst *inst, uint32_t *prio, Inst *last_barrier); 65 66 bool FinalizeBB(BasicBlock *bb, uint32_t cycle); 67 void Cleanup(); 68 69 uint32_t ScheduleInstsBetweenBarriers(Inst *first, Inst *last); 70 71 using SchedulerPriorityQueue = std::priority_queue<Inst *, InstVector, std::function<bool(Inst *, Inst *)>>; 72 uint32_t SchedWithGlued(Inst *inst, SchedulerPriorityQueue *waiting, uint32_t cycle); 73 74 uint32_t oprev_ {0}; 75 uint32_t num_barriers_ {0}; 76 uint32_t max_prio_ {0}; 77 78 InstVector sched_; 79 InstVector loads_; 80 InstVector stores_; 81 InstVector special_; 82 ArenaUnorderedMap<Inst *, uint32_t> old_; 83 ArenaUnorderedMap<Inst *, uint32_t> ocycle_; 84 ArenaUnorderedMap<Inst *, uint32_t> num_deps_; 85 ArenaUnorderedMap<Inst *, uint32_t> asap_; 86 ArenaUnorderedMap<Inst *, uint32_t> prio_; 87 ArenaUnorderedMap<Inst *, ArenaUnorderedMap<Inst *, uint32_t>> deps_; 88 }; 89 } // namespace panda::compiler 90 91 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_SCHEDULER_H_ 92