1 /* 2 * Copyright (c) 2021 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/bytecode_info_collector.h" 20 #include "ecmascript/compiler/compiler_log.h" 21 #include "ecmascript/compiler/file_generators.h" 22 #include "ecmascript/ecma_vm.h" 23 #include "ecmascript/pgo_profiler/pgo_profiler_decoder.h" 24 #include "ecmascript/pgo_profiler/pgo_profiler_manager.h" 25 #include "ecmascript/ts_types/ts_manager.h" 26 27 namespace panda::ecmascript::kungfu { 28 class Bytecodes; 29 class LexEnvManager; 30 class CompilationConfig; 31 32 class PassContext { 33 public: PassContext(const std::string & triple,CompilerLog * log,BytecodeInfoCollector * collector,LLVMModule * aotModule,PGOProfilerDecoder * decoder)34 PassContext(const std::string &triple, CompilerLog *log, BytecodeInfoCollector* collector, LLVMModule *aotModule, 35 PGOProfilerDecoder *decoder) 36 : vm_(collector->GetVM()), 37 bcInfoCollector_(collector), 38 tsManager_(vm_->GetJSThread()->GetCurrentEcmaContext()->GetTSManager()), 39 bytecodes_(collector->GetByteCodes()), 40 lexEnvManager_(bcInfoCollector_->GetEnvManager()), 41 cmpCfg_(triple, &vm_->GetJSOptions()), 42 log_(log), 43 jsPandaFile_(collector->GetJSPandaFile()), 44 aotModule_(aotModule), 45 decoder_(decoder) 46 { 47 } 48 GetTSManager()49 TSManager* GetTSManager() const 50 { 51 return tsManager_; 52 } 53 GetByteCodes()54 Bytecodes* GetByteCodes() 55 { 56 return bytecodes_; 57 } 58 GetLexEnvManager()59 LexEnvManager* GetLexEnvManager() const 60 { 61 return lexEnvManager_; 62 } 63 GetCompilerConfig()64 CompilationConfig* GetCompilerConfig() 65 { 66 return &cmpCfg_; 67 } 68 GetCompilerLog()69 CompilerLog* GetCompilerLog() const 70 { 71 return log_; 72 } 73 GetJSPandaFile()74 const JSPandaFile *GetJSPandaFile() const 75 { 76 return jsPandaFile_; 77 } 78 GetBytecodeInfoCollector()79 BytecodeInfoCollector* GetBytecodeInfoCollector() const 80 { 81 return bcInfoCollector_; 82 } 83 GetAOTModule()84 LLVMModule* GetAOTModule() const 85 { 86 return aotModule_; 87 } 88 IsSkippedMethod(uint32_t methodOffset)89 bool IsSkippedMethod(uint32_t methodOffset) const 90 { 91 return bcInfoCollector_->IsSkippedMethod(methodOffset); 92 } 93 GetBytecodeInfo()94 BCInfo& GetBytecodeInfo() 95 { 96 return bcInfoCollector_->GetBytecodeInfo(); 97 } 98 GetNativeAreaAllocator()99 NativeAreaAllocator *GetNativeAreaAllocator() const 100 { 101 return vm_->GetNativeAreaAllocator(); 102 } 103 GetEcmaVM()104 EcmaVM *GetEcmaVM() const 105 { 106 return vm_; 107 } 108 GetPfDecoder()109 PGOProfilerDecoder *GetPfDecoder() const 110 { 111 return decoder_; 112 } 113 114 private: 115 EcmaVM *vm_ {nullptr}; 116 BytecodeInfoCollector *bcInfoCollector_ {nullptr}; 117 TSManager *tsManager_ {nullptr}; 118 Bytecodes *bytecodes_ {nullptr}; 119 LexEnvManager *lexEnvManager_ {nullptr}; 120 CompilationConfig cmpCfg_; 121 CompilerLog *log_ {nullptr}; 122 const JSPandaFile *jsPandaFile_ {nullptr}; 123 LLVMModule *aotModule_ {nullptr}; 124 PGOProfilerDecoder *decoder_ {nullptr}; 125 }; 126 127 class PassOptions { 128 public: PassOptions(bool enableTypeLowering,bool enableEarlyElimination,bool enableLaterElimination,bool enableValueNumbering,bool enableTypeInfer,bool enableOptInlining,bool enableOptPGOType,bool enableOptTrackField,bool enableOptLoopPeeling)129 PassOptions(bool enableTypeLowering, bool enableEarlyElimination, bool enableLaterElimination, 130 bool enableValueNumbering, bool enableTypeInfer, bool enableOptInlining, 131 bool enableOptPGOType, bool enableOptTrackField, bool enableOptLoopPeeling) 132 : enableTypeLowering_(enableTypeLowering), 133 enableEarlyElimination_(enableEarlyElimination), 134 enableLaterElimination_(enableLaterElimination), 135 enableValueNumbering_(enableValueNumbering), 136 enableTypeInfer_(enableTypeInfer), 137 enableOptInlining_(enableOptInlining), 138 enableOptPGOType_(enableOptPGOType), 139 enableOptTrackField_(enableOptTrackField), 140 enableOptLoopPeeling_(enableOptLoopPeeling) 141 { 142 } 143 144 #define OPTION_LIST(V) \ 145 V(TypeLowering, true) \ 146 V(EarlyElimination, true) \ 147 V(LaterElimination, true) \ 148 V(ValueNumbering, false) \ 149 V(TypeInfer, false) \ 150 V(OptInlining, false) \ 151 V(OptNoGCCall, false) \ 152 V(OptPGOType, false) \ 153 V(NoCheck, false) \ 154 V(OptTrackField, false) \ 155 V(OptLoopPeeling, false) 156 157 #define DECL_OPTION(NAME, DEFAULT) \ 158 public: \ 159 bool Enable##NAME() const \ 160 { \ 161 return enable##NAME##_; \ 162 } \ 163 \ 164 private: \ 165 bool enable##NAME##_ {DEFAULT}; 166 167 OPTION_LIST(DECL_OPTION) 168 #undef ENABLE_OPTION 169 #undef OPTION_LIST 170 }; 171 172 class PassManager { 173 public: PassManager(EcmaVM * vm,std::string & entry,std::string & triple,size_t optLevel,size_t relocMode,CompilerLog * log,AotMethodLogList * logList,size_t maxAotMethodSize,size_t maxMethodsInModule,const std::string & profIn,uint32_t hotnessThreshold,PassOptions * passOptions)174 PassManager(EcmaVM* vm, std::string &entry, std::string &triple, size_t optLevel, size_t relocMode, 175 CompilerLog *log, AotMethodLogList *logList, size_t maxAotMethodSize, size_t maxMethodsInModule, 176 const std::string &profIn, uint32_t hotnessThreshold, PassOptions *passOptions) 177 : vm_(vm), entry_(entry), triple_(triple), optLevel_(optLevel), relocMode_(relocMode), log_(log), 178 logList_(logList), maxAotMethodSize_(maxAotMethodSize), maxMethodsInModule_(maxMethodsInModule), 179 profilerDecoder_(profIn, hotnessThreshold), passOptions_(passOptions) {}; 180 PassManager() = default; 181 ~PassManager() = default; 182 183 bool Compile(JSPandaFile *jsPandaFile, const std::string &fileName, AOTFileGenerator &generator); 184 185 private: 186 JSPandaFile *CreateAndVerifyJSPandaFile(const CString &fileName); 187 void ProcessConstantPool(BytecodeInfoCollector *collector); 188 bool IsReleasedPandaFile(const JSPandaFile *jsPandaFile) const; 189 void ResolveModule(const JSPandaFile *jsPandaFile, const std::string &fileName); 190 bool ShouldCollect() const; 191 192 EcmaVM *vm_ {nullptr}; 193 std::string entry_ {}; 194 std::string triple_ {}; 195 size_t optLevel_ {3}; // 3 : default backend optimization level 196 size_t relocMode_ {2}; // 2 : default relocation mode-- PIC 197 CompilerLog *log_ {nullptr}; 198 AotMethodLogList *logList_ {nullptr}; 199 size_t maxAotMethodSize_ {0}; 200 size_t maxMethodsInModule_ {0}; 201 PGOProfilerDecoder profilerDecoder_; 202 PassOptions *passOptions_ {nullptr}; 203 }; 204 } 205 #endif // ECMASCRIPT_COMPILER_PASS_MANAGER_H 206