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