• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 #include "runtime/compiler.h"
17 #include "runtime/compiler_task_manager_worker.h"
18 #include "compiler/background_task_runner.h"
19 #include "compiler/compiler_task_runner.h"
20 
21 namespace ark {
22 
CompilerTaskManagerWorker(mem::InternalAllocatorPtr internalAllocator,Compiler * compiler)23 CompilerTaskManagerWorker::CompilerTaskManagerWorker(mem::InternalAllocatorPtr internalAllocator, Compiler *compiler)
24     : CompilerWorker(internalAllocator, compiler)
25 {
26     compilerTaskManagerQueue_ = taskmanager::TaskManager::CreateTaskQueue<decltype(internalAllocator_->Adapter())>(
27         taskmanager::MIN_QUEUE_PRIORITY);
28     ASSERT(compilerTaskManagerQueue_ != nullptr);
29 }
30 
JoinWorker()31 void CompilerTaskManagerWorker::JoinWorker()
32 {
33     {
34         os::memory::LockHolder lock(taskQueueLock_);
35         compilerWorkerJoined_ = true;
36     }
37     compilerTaskManagerQueue_->WaitBackgroundTasks();
38 }
39 
AddTask(CompilerTask && task)40 void CompilerTaskManagerWorker::AddTask(CompilerTask &&task)
41 {
42     {
43         os::memory::LockHolder lock(taskQueueLock_);
44         if (compilerWorkerJoined_) {
45             return;
46         }
47         if (compilerTaskDeque_.empty()) {
48             CompilerTask emptyTask;
49             compilerTaskDeque_.emplace_back(std::move(emptyTask));
50         } else {
51             compilerTaskDeque_.emplace_back(std::move(task));
52             return;
53         }
54     }
55     // This means that this is first task in queue, so we can compile it in-place.
56     BackgroundCompileMethod(std::move(task));
57 }
58 
BackgroundCompileMethod(CompilerTask && ctx)59 void CompilerTaskManagerWorker::BackgroundCompileMethod(CompilerTask &&ctx)
60 {
61     auto threadDeleter = [this](Thread *thread) { internalAllocator_->Delete(thread); };
62     compiler::BackgroundCompilerContext::CompilerThread compilerThread(
63         internalAllocator_->New<Thread>(ctx.GetVM(), Thread::ThreadType::THREAD_TYPE_COMPILER),
64         std::move(threadDeleter));
65 
66     auto taskDeleter = [this](CompilerTask *task) { internalAllocator_->Delete(task); };
67     compiler::BackgroundCompilerContext::CompilerTask compilerTask(
68         internalAllocator_->New<CompilerTask>(std::move(ctx)), std::move(taskDeleter));
69 
70     compiler::BackgroundCompilerTaskRunner taskRunner(compilerTaskManagerQueue_, compilerThread.get(),
71                                                       compiler_->GetRuntimeInterface());
72     auto &compilerCtx = taskRunner.GetContext();
73     compilerCtx.SetCompilerThread(std::move(compilerThread));
74     compilerCtx.SetCompilerTask(std::move(compilerTask));
75 
76     // Callback to compile next method from compiler_task_deque_
77     taskRunner.AddFinalize([this]([[maybe_unused]] compiler::BackgroundCompilerContext &taskContext) {
78         CompilerTask nextTask;
79         {
80             os::memory::LockHolder lock(taskQueueLock_);
81             ASSERT(!compilerTaskDeque_.empty());
82             // compilation of current task is complete
83             compilerTaskDeque_.pop_front();
84             if (compilerTaskDeque_.empty()) {
85                 return;
86             }
87             // now queue has empty task which will be popped at the end of compilation
88             nextTask = std::move(compilerTaskDeque_.front());
89         }
90         BackgroundCompileMethod(std::move(nextTask));
91     });
92 
93     auto backgroundTask = [this](compiler::BackgroundCompilerTaskRunner runner) {
94         if (runner.GetContext().GetMethod()->AtomicSetCompilationStatus(Method::WAITING, Method::COMPILATION)) {
95             compiler_->StartCompileMethod<compiler::BACKGROUND_MODE>(std::move(runner));
96             return;
97         }
98         compiler::BackgroundCompilerTaskRunner::EndTask(std::move(runner), false);
99     };
100     compiler::BackgroundCompilerTaskRunner::StartTask(std::move(taskRunner), std::move(backgroundTask));
101 }
102 
103 }  // namespace ark
104