1 /* 2 * Copyright (c) 2023 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 MAPLE_PHASE_INCLUDE_PHASEDRIVER_H 17 #define MAPLE_PHASE_INCLUDE_PHASEDRIVER_H 18 #include "mir_module.h" 19 #include "mir_function.h" 20 #include "mpl_scheduler.h" 21 22 namespace maple { 23 class PhaseDriverImpl : public MplTaskParam { 24 public: 25 PhaseDriverImpl() = default; 26 virtual ~PhaseDriverImpl() = default; 27 GlobalInit()28 virtual void GlobalInit() {} 29 LocalInit()30 virtual void LocalInit() {} 31 ProcessRun(uint32,void *,void *)32 virtual void ProcessRun(uint32, void *, void *) {} 33 ProcessFinish(uint32,void *,void *)34 virtual void ProcessFinish(uint32, void *, void *) {} 35 }; 36 37 class PhaseDriver : public MplScheduler { 38 public: 39 class Task : public MplTask { 40 public: target(currTarget)41 Task(void *currTarget, void *currParamEx = nullptr) : target(currTarget), paramException(currParamEx) {} 42 43 ~Task() = default; 44 45 protected: RunImpl(MplTaskParam * param)46 int RunImpl(MplTaskParam *param) override 47 { 48 CHECK_NULL_FATAL(param); 49 static_cast<PhaseDriverImpl *>(param)->ProcessRun(taskId, target, paramException); 50 return 0; 51 } 52 FinishImpl(MplTaskParam * param)53 int FinishImpl(MplTaskParam *param) override 54 { 55 CHECK_NULL_FATAL(param); 56 static_cast<PhaseDriverImpl *>(param)->ProcessFinish(taskId, target, paramException); 57 return 0; 58 } 59 void *target; 60 void *paramException; 61 }; 62 63 explicit PhaseDriver(const std::string &phaseName); 64 virtual ~PhaseDriver() = default; 65 66 virtual void RunAll(MIRModule *module, int thread, bool bSeq = false); 67 virtual void RunSerial(); 68 virtual void RunParallel(int thread, bool bSeq = false); 69 virtual PhaseDriverImpl *NewPhase() = 0; 70 virtual void RegisterTasks() = 0; 71 72 protected: 73 virtual void CallbackThreadMainStart(); 74 virtual void CallbackThreadMainEnd(); CallbackGetTaskRunParam()75 virtual MplTaskParam *CallbackGetTaskRunParam() const 76 { 77 return phaseImplLocal; 78 } 79 CallbackGetTaskFinishParam()80 virtual MplTaskParam *CallbackGetTaskFinishParam() const 81 { 82 return phaseImplLocal; 83 } 84 85 MIRModule *module; 86 PhaseDriverImpl *phaseImpl; 87 thread_local static PhaseDriverImpl *phaseImplLocal; 88 std::string phaseName; 89 }; 90 } // namespace maple 91 #endif // MAPLE_PHASE_INCLUDE_PHASEDRIVER_H 92