• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "emitFiles.h"
17 
18 #include <assembly-emitter.h>
19 #include <mem/arena_allocator.h>
20 
21 #include <es2panda.h>
22 #include <protobufSnapshotGenerator.h>
23 #include <util/helpers.h>
24 
25 namespace panda::es2panda::aot {
ScheduleEmitCacheJobs(EmitMergedAbcJob * emitMergedAbcJob)26 void EmitFileQueue::ScheduleEmitCacheJobs(EmitMergedAbcJob *emitMergedAbcJob)
27 {
28     for (const auto &info: progsInfo_) {
29         // generate cache protoBins and set dependencies
30         if (!info.second->needUpdateCache) {
31             continue;
32         }
33         auto outputCacheIter = options_->CompilerOptions().cacheFiles.find(info.first);
34         if (outputCacheIter != options_->CompilerOptions().cacheFiles.end()) {
35             auto emitProtoJob = new EmitCacheJob(outputCacheIter->second, info.second);
36             emitProtoJob->DependsOn(emitMergedAbcJob);
37             jobs_.push_back(emitProtoJob);
38             jobsCount_++;
39         }
40     }
41 }
42 
Schedule()43 void EmitFileQueue::Schedule()
44 {
45     ASSERT(jobsCount_ == 0);
46     std::unique_lock<std::mutex> lock(m_);
47     auto targetApi = options_->CompilerOptions().targetApiVersion;
48     auto targetSubApi = options_->CompilerOptions().targetApiSubVersion;
49 
50     if (mergeAbc_) {
51         // generate merged abc
52         auto emitMergedAbcJob = new EmitMergedAbcJob(options_->CompilerOutput(),
53             options_->CompilerOptions().transformLib, progsInfo_, targetApi, targetSubApi);
54         // Disable generating cached files when cross-program optimization is required, to prevent cached files from
55         // not being invalidated when their dependencies are changed
56         if (!options_->CompilerOptions().requireGlobalOptimization) {
57             ScheduleEmitCacheJobs(emitMergedAbcJob);
58         }
59         //  One job should be placed after those jobs which depend on it to prevent blocking
60         jobs_.push_back(emitMergedAbcJob);
61         jobsCount_++;
62     } else {
63         for (const auto &info: progsInfo_) {
64             try {
65                 // generate multi abcs
66                 auto outputFileName = options_->OutputFiles().empty() ? options_->CompilerOutput() :
67                     options_->OutputFiles().at(info.first);
68                 auto emitSingleAbcJob = new EmitSingleAbcJob(outputFileName, &(info.second->program), statp_,
69                                                              targetApi, targetSubApi);
70                 jobs_.push_back(emitSingleAbcJob);
71                 jobsCount_++;
72             } catch (std::exception &error) {
73                 throw Error(ErrorType::GENERIC, error.what());
74             }
75         }
76     }
77 
78     lock.unlock();
79     jobsAvailable_.notify_all();
80 }
81 
Run()82 void EmitSingleAbcJob::Run()
83 {
84     if (!panda::pandasm::AsmEmitter::Emit(panda::os::file::File::GetExtendedFilePath(outputFileName_), *prog_, statp_,
85         nullptr, true, nullptr, targetApiVersion_, targetApiSubVersion_)) {
86         throw Error(ErrorType::GENERIC, "Failed to emit " + outputFileName_ + ", error: " +
87             panda::pandasm::AsmEmitter::GetLastError());
88     }
89     for (auto *dependant : dependants_) {
90         dependant->Signal();
91     }
92 }
93 
Run()94 void EmitMergedAbcJob::Run()
95 {
96     std::vector<panda::pandasm::Program*> progs;
97     progs.reserve(progsInfo_.size());
98     for (const auto &info: progsInfo_) {
99         progs.push_back(&(info.second->program));
100     }
101 
102     bool success = panda::pandasm::AsmEmitter::EmitPrograms(
103         panda::os::file::File::GetExtendedFilePath(outputFileName_), progs, true,
104         targetApiVersion_, targetApiSubVersion_);
105 
106     for (auto *dependant : dependants_) {
107         dependant->Signal();
108     }
109 
110     if (!success) {
111         throw Error(ErrorType::GENERIC, "Failed to emit " + outputFileName_ + ", error: " +
112             panda::pandasm::AsmEmitter::GetLastError() +
113             "\nIf you're using any cache file generated by older version of SDK, " +
114             "please try cleaning the cache files and rebuild");
115     }
116 
117     if (!transformLib_.empty()) {
118         util::Helpers::AopTransform(outputFileName_, transformLib_);
119     }
120 }
121 
Run()122 void EmitCacheJob::Run()
123 {
124     std::unique_lock<std::mutex> lock(m_);
125     cond_.wait(lock, [this] { return dependencies_ == 0; });
126     panda::proto::ProtobufSnapshotGenerator::UpdateCacheFile(progCache_, outputProtoName_);
127 }
128 
129 }  // namespace panda::es2panda::util
130