1 /*
2  * Copyright (c) 2021-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 #ifndef ECMASCRIPT_COMPILER_PASS_MANAGER_H
17 #define ECMASCRIPT_COMPILER_PASS_MANAGER_H
18 
19 #include "ecmascript/compiler/aot_compiler_preprocessor.h"
20 #include "ecmascript/compiler/jit_compilation_env.h"
21 #include "ecmascript/compiler/bytecode_info_collector.h"
22 #include "ecmascript/compiler/compilation_driver.h"
23 #include "ecmascript/compiler/compiler_log.h"
24 #include "ecmascript/compiler/file_generators.h"
25 #include "ecmascript/compiler/ir_module.h"
26 #include "ecmascript/compiler/ir_module.h"
27 #include "ecmascript/compiler/pass_options.h"
28 #include "ecmascript/ecma_vm.h"
29 #include "ecmascript/jit/jit_profiler.h"
30 #include "ecmascript/jspandafile/method_literal.h"
31 
32 #include "ecmascript/compiler/aot_compiler_stats.h"
33 
34 namespace panda::ecmascript::kungfu {
35 class Bytecodes;
36 class CompilationConfig;
37 class PassData;
38 class CallMethodFlagMap;
39 struct AbcFileInfo;
40 class PassContext {
41 public:
42     PassContext(const std::string &triple, CompilerLog *log, BytecodeInfoCollector* collector, IRModule *aotModule,
43         PGOProfilerDecoder *decoder);
44 
GetPTManager() const45     PGOTypeManager* GetPTManager() const
46     {
47         return compilationEnv_->GetPTManager();
48     }
49 
GetByteCodes()50     Bytecodes* GetByteCodes()
51     {
52         return bytecodes_;
53     }
54 
GetCompilerConfig()55     CompilationConfig* GetCompilerConfig()
56     {
57         return &cmpCfg_;
58     }
59 
GetCompilerLog() const60     CompilerLog* GetCompilerLog() const
61     {
62         return log_;
63     }
64 
GetJSPandaFile() const65     const JSPandaFile *GetJSPandaFile() const
66     {
67         return jsPandaFile_;
68     }
69 
GetBytecodeInfoCollector() const70     BytecodeInfoCollector* GetBytecodeInfoCollector() const
71     {
72         return bcInfoCollector_;
73     }
74 
GetAOTModule() const75     IRModule* GetAOTModule() const
76     {
77         return aotModule_;
78     }
79 
FilterMethod(const MethodLiteral *methodLiteral, const MethodPcInfo &methodPCInfo) const80     bool FilterMethod(const MethodLiteral *methodLiteral, const MethodPcInfo &methodPCInfo) const
81     {
82         return bcInfoCollector_->FilterMethod(methodLiteral, methodPCInfo);
83     }
84 
IsSkippedMethod(uint32_t methodOffset) const85     bool IsSkippedMethod(uint32_t methodOffset) const
86     {
87         return bcInfoCollector_->IsSkippedMethod(methodOffset);
88     }
89 
GetBytecodeInfo()90     BCInfo& GetBytecodeInfo()
91     {
92         return bcInfoCollector_->GetBytecodeInfo();
93     }
94 
GetNativeAreaAllocator() const95     NativeAreaAllocator *GetNativeAreaAllocator() const
96     {
97         return compilationEnv_->GetNativeAreaAllocator();
98     }
99 
GetCompilationEnv() const100     CompilationEnv *GetCompilationEnv() const
101     {
102         return compilationEnv_;
103     }
104 
GetPfDecoder() const105     PGOProfilerDecoder *GetPfDecoder() const
106     {
107         return decoder_;
108     }
109 
110 private:
111     CompilationEnv *compilationEnv_ {nullptr};
112     BytecodeInfoCollector *bcInfoCollector_ {nullptr};
113     Bytecodes *bytecodes_ {nullptr};
114     CompilationConfig cmpCfg_;
115     CompilerLog *log_ {nullptr};
116     const JSPandaFile *jsPandaFile_ {nullptr};
117     IRModule *aotModule_ {nullptr};
118     PGOProfilerDecoder *decoder_ {nullptr};
119 };
120 
121 class PassManager {
122 public:
PassManager(CompilationEnv *env, std::string &triple, size_t optLevel, size_t relocMode, CompilerLog *log, AotMethodLogList *logList, size_t maxAotMethodSize, size_t maxMethodsInModule, PGOProfilerDecoder &profilerDecoder, PassOptions *passOptions, CallMethodFlagMap *callMethodFlagMap, const CVector<AbcFileInfo> &fileInfos, const CVector<std::unique_ptr<BytecodeInfoCollector>> &bcInfoCollectors, std::string optBCRange)123     explicit PassManager(CompilationEnv *env, std::string &triple, size_t optLevel, size_t relocMode,
124         CompilerLog *log, AotMethodLogList *logList, size_t maxAotMethodSize, size_t maxMethodsInModule,
125         PGOProfilerDecoder &profilerDecoder, PassOptions *passOptions, CallMethodFlagMap *callMethodFlagMap,
126         const CVector<AbcFileInfo> &fileInfos, const CVector<std::unique_ptr<BytecodeInfoCollector>> &bcInfoCollectors,
127         std::string optBCRange)
128         : compilationEnv_(env), triple_(triple), optLevel_(optLevel), relocMode_(relocMode), log_(log),
129           logList_(logList), maxAotMethodSize_(maxAotMethodSize), maxMethodsInModule_(maxMethodsInModule),
130           profilerDecoder_(profilerDecoder), passOptions_(passOptions), callMethodFlagMap_(callMethodFlagMap),
131           fileInfos_(fileInfos), bcInfoCollectors_(bcInfoCollectors), optBCRange_(optBCRange) {
132                 enableJITLog_ = compilationEnv_->GetJSOptions().GetTraceJIT();
133             };
134 
135     virtual ~PassManager() = default;
136 
137     void CompileValidFiles(AOTFileGenerator &generator, bool &ret, AotCompilerStats &compilerStats);
138 
139     bool Compile(JSPandaFile *jsPandaFile, const std::string &fileName, AOTFileGenerator &generator,
140                  AotCompilerStats &compilerStats, BytecodeInfoCollector &collector);
141 
142 protected:
143     bool IsReleasedPandaFile(const JSPandaFile *jsPandaFile) const;
144 
145     CompilationEnv *compilationEnv_ {nullptr};
146     std::string triple_ {};
147     size_t optLevel_ {3}; // 3 : default backend optimization level
148     size_t relocMode_ {2}; // 2 : default relocation mode-- PIC
149     CompilerLog *log_ {nullptr};
150     AotMethodLogList *logList_ {nullptr};
151     size_t maxAotMethodSize_ {0};
152     size_t maxMethodsInModule_ {0};
153     PGOProfilerDecoder &profilerDecoder_;
154     PassOptions *passOptions_ {nullptr};
155     CallMethodFlagMap *callMethodFlagMap_ {nullptr};
156     const CVector<AbcFileInfo> &fileInfos_;
157     const CVector<std::unique_ptr<BytecodeInfoCollector>> &bcInfoCollectors_;
158     std::string optBCRange_ {};
159     bool enableJITLog_ {false};
160 };
161 
162 class JitPassManager : public PassManager {
163 public:
JitPassManager(JitCompilationEnv *env, std::string &triple, size_t optLevel, size_t relocMode, CompilerLog *log, AotMethodLogList *logList, PGOProfilerDecoder &profilerDecoder, PassOptions *passOptions)164     JitPassManager(JitCompilationEnv *env, std::string &triple, size_t optLevel, size_t relocMode,
165         CompilerLog *log, AotMethodLogList *logList,
166         PGOProfilerDecoder &profilerDecoder, PassOptions *passOptions)
167         : PassManager(env, triple, optLevel, relocMode, log, logList, 1, 1, profilerDecoder, passOptions,
168                       nullptr, CVector<AbcFileInfo> {}, CVector<std::unique_ptr<BytecodeInfoCollector>> {}, "") { };
169 
170     bool Compile(JSHandle<ProfileTypeInfo> &profileTypeInfo, AOTFileGenerator &gen, int32_t osrOffset = -1);
171     bool RunCg();
172     virtual ~JitPassManager();
173 
174 private:
175     BytecodeInfoCollector *collector_ {nullptr};
176     LOptions *lOptions_ {nullptr};
177     JitCompilationDriver *cmpDriver_ {nullptr};
178 
179     PassContext *ctx_ {nullptr};
180     Circuit *circuit_ {nullptr};
181     BytecodeCircuitBuilder *builder_ {nullptr};
182     JITProfiler *jitProfiler_ {nullptr};
183     PassData *data_ {nullptr};
184 };
185 }
186 #endif // ECMASCRIPT_COMPILER_PASS_MANAGER_H
187