1 /* 2 * Copyright (c) 2021-2024 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 ark::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 ssWithRuntimeCall_(graph->GetLocalAllocator()->Adapter()), 32 old_(graph->GetLocalAllocator()->Adapter()), 33 ocycle_(graph->GetLocalAllocator()->Adapter()), 34 numDeps_(graph->GetLocalAllocator()->Adapter()), 35 asap_(graph->GetLocalAllocator()->Adapter()), 36 prio_(graph->GetLocalAllocator()->Adapter()), 37 deps_(graph->GetLocalAllocator()->Adapter()) 38 { 39 } 40 41 NO_MOVE_SEMANTIC(Scheduler); 42 NO_COPY_SEMANTIC(Scheduler); 43 ~Scheduler() override = default; 44 45 bool RunImpl() override; 46 IsEnable()47 bool IsEnable() const override 48 { 49 return g_options.IsCompilerScheduling(); 50 } 51 GetPassName()52 const char *GetPassName() const override 53 { 54 return "Scheduler"; 55 } 56 57 private: 58 void AddDep(uint32_t *prio, Inst *from, Inst *to, uint32_t latency, Inst *barrier); 59 bool ScheduleBasicBlock(BasicBlock *bb); 60 bool BuildAllDeps(BasicBlock *bb); 61 62 void ProcessInst(Inst *inst, uint32_t *numInst, uint32_t *numBetween, uint32_t *numSpecial, Inst **lastBarrier); 63 void ProcessMemory(Inst *inst, uint32_t *prio, Inst *lastBarrier); 64 void ProcessSpecial(Inst *inst, uint32_t *prio, Inst *lastBarrier); 65 void ProcessSpecialBoundsCheckI(Inst *inst, uint32_t *prio, Inst *lastBarrier); 66 void ProcessRefInst(Inst *inst, uint32_t *prio, Inst *lastBarrier); 67 68 bool FinalizeBB(BasicBlock *bb, uint32_t cycle); 69 void Cleanup(); 70 71 void ScheduleBarrierInst(Inst **inst); 72 uint32_t ScheduleInstsBetweenBarriers(Inst *first, Inst *last); 73 74 using SchedulerPriorityQueue = std::priority_queue<Inst *, InstVector, std::function<bool(Inst *, Inst *)>>; 75 uint32_t SchedWithGlued(Inst *inst, SchedulerPriorityQueue *waiting, uint32_t cycle); 76 77 Marker mrk_ {}; 78 uint32_t oprev_ {0}; 79 uint32_t numBarriers_ {0}; 80 uint32_t maxPrio_ {0}; 81 82 InstVector sched_; 83 InstVector loads_; 84 InstVector stores_; 85 InstVector special_; 86 InstVector ssWithRuntimeCall_; 87 ArenaUnorderedMap<Inst *, uint32_t> old_; 88 ArenaUnorderedMap<Inst *, uint32_t> ocycle_; 89 ArenaUnorderedMap<Inst *, uint32_t> numDeps_; 90 ArenaUnorderedMap<Inst *, uint32_t> asap_; 91 ArenaUnorderedMap<Inst *, uint32_t> prio_; 92 ArenaUnorderedMap<Inst *, ArenaUnorderedMap<Inst *, uint32_t>> deps_; 93 }; 94 } // namespace ark::compiler 95 96 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_SCHEDULER_H 97