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 PANDA_COMPILER_INPLACE_TASK_RUNNER_H 17 #define PANDA_COMPILER_INPLACE_TASK_RUNNER_H 18 19 #include <memory> 20 21 #include "libpandabase/task_runner.h" 22 #include "libpandabase/mem/arena_allocator.h" 23 24 namespace panda { 25 26 class Method; 27 class PandaVM; 28 } // namespace panda 29 30 namespace panda::compiler { 31 32 class Pipeline; 33 class Graph; 34 35 class InPlaceCompilerContext { 36 public: SetMethod(Method * method)37 void SetMethod(Method *method) 38 { 39 method_ = method; 40 } 41 SetOsr(bool isOsr)42 void SetOsr(bool isOsr) 43 { 44 isOsr_ = isOsr; 45 } 46 SetVM(PandaVM * pandaVm)47 void SetVM(PandaVM *pandaVm) 48 { 49 pandaVm_ = pandaVm; 50 } 51 SetAllocator(ArenaAllocator * allocator)52 void SetAllocator(ArenaAllocator *allocator) 53 { 54 allocator_ = allocator; 55 } 56 SetLocalAllocator(ArenaAllocator * localAllocator)57 void SetLocalAllocator(ArenaAllocator *localAllocator) 58 { 59 localAllocator_ = localAllocator; 60 } 61 SetMethodName(std::string methodName)62 void SetMethodName(std::string methodName) 63 { 64 methodName_ = std::move(methodName); 65 } 66 SetGraph(Graph * graph)67 void SetGraph(Graph *graph) 68 { 69 graph_ = graph; 70 } 71 SetPipeline(Pipeline * pipeline)72 void SetPipeline(Pipeline *pipeline) 73 { 74 pipeline_ = pipeline; 75 } 76 SetCompilationStatus(bool compilationStatus)77 void SetCompilationStatus(bool compilationStatus) 78 { 79 compilationStatus_ = compilationStatus; 80 } 81 GetMethod()82 Method *GetMethod() const 83 { 84 return method_; 85 } 86 IsOsr()87 bool IsOsr() const 88 { 89 return isOsr_; 90 } 91 GetVM()92 PandaVM *GetVM() const 93 { 94 return pandaVm_; 95 } 96 GetAllocator()97 ArenaAllocator *GetAllocator() const 98 { 99 return allocator_; 100 } 101 GetLocalAllocator()102 ArenaAllocator *GetLocalAllocator() const 103 { 104 return localAllocator_; 105 } 106 GetMethodName()107 const std::string &GetMethodName() const 108 { 109 return methodName_; 110 } 111 GetGraph()112 Graph *GetGraph() const 113 { 114 return graph_; 115 } 116 GetPipeline()117 Pipeline *GetPipeline() const 118 { 119 return pipeline_; 120 } 121 GetCompilationStatus()122 bool GetCompilationStatus() const 123 { 124 return compilationStatus_; 125 } 126 127 private: 128 Method *method_ {nullptr}; 129 bool isOsr_ {false}; 130 PandaVM *pandaVm_ {nullptr}; 131 ArenaAllocator *allocator_ {nullptr}; 132 ArenaAllocator *localAllocator_ {nullptr}; 133 std::string methodName_; 134 Graph *graph_ {nullptr}; 135 Pipeline *pipeline_ {nullptr}; 136 // Used only in JIT Compilation 137 bool compilationStatus_ {false}; 138 }; 139 140 class InPlaceCompilerTaskRunner : public panda::TaskRunner<InPlaceCompilerTaskRunner, InPlaceCompilerContext> { 141 public: GetContext()142 InPlaceCompilerContext &GetContext() override 143 { 144 return taskCtx_; 145 } 146 147 /** 148 * @brief Starts task in-place 149 * @param task_runner - Current TaskRunner containing context and callbacks 150 * @param task_func - task which will be executed with @param task_runner 151 */ 152 // NOLINTNEXTLINE(performance-unnecessary-value-param) StartTask(InPlaceCompilerTaskRunner taskRunner,TaskRunner::TaskFunc taskFunc)153 static void StartTask(InPlaceCompilerTaskRunner taskRunner, TaskRunner::TaskFunc taskFunc) 154 { 155 taskFunc(std::move(taskRunner)); 156 } 157 158 private: 159 InPlaceCompilerContext taskCtx_; 160 }; 161 162 } // namespace panda::compiler 163 164 #endif // PANDA_COMPILER_INPLACE_TASK_RUNNER_H 165