• 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 #ifndef ECMASCRIPT_COMPILER_JIT_COMPILER_H
17 #define ECMASCRIPT_COMPILER_JIT_COMPILER_H
18 
19 #include "ecmascript/compiler/pass_manager.h"
20 #include "ecmascript/compiler/jit_compilation_env.h"
21 #include "ecmascript/ecma_vm.h"
22 #include "ecmascript/jit/jit_task.h"
23 #include "ecmascript/js_runtime_options.h"
24 #include "ecmascript/pgo_profiler/pgo_profiler.h"
25 #include "ecmascript/compiler/baseline/baseline_compiler.h"
26 
27 namespace panda::ecmascript::kungfu {
28 extern "C" {
29 PUBLIC_API void InitJitCompiler(JSRuntimeOptions options);
30 PUBLIC_API void *CreateJitCompilerTask(JitTask *jitTask);
31 PUBLIC_API bool JitCompile(void *compiler, JitTask *jitTask);
32 PUBLIC_API bool JitFinalize(void *compiler, JitTask *jitTask);
33 PUBLIC_API void DeleteJitCompilerTask(void *handle);
34 };
35 
36 struct JitCompilationOptions {
37     JitCompilationOptions(JSRuntimeOptions options);
38     JitCompilationOptions() = default;
39 
40     std::string triple_;
41     std::string outputFileName_;
42     size_t optLevel_;
43     size_t relocMode_;
44     std::string logOption_;
45     std::string logMethodsList_;
46     bool compilerLogTime_;
47     bool deviceIsScreenOff_;
48     uint32_t hotnessThreshold_;
49     int32_t deviceThermalLevel_;
50     std::string profilerIn_;
51     bool isEnableArrayBoundsCheckElimination_;
52     bool isEnableTypeLowering_;
53     bool isEnableEarlyElimination_;
54     bool isEnableLaterElimination_;
55     bool isEnableValueNumbering_;
56     bool isEnableOptInlining_;
57     bool isEnableOptString_;
58     bool isEnableOptPGOType_;
59     bool isEnableOptTrackField_;
60     bool isEnableOptLoopPeeling_;
61     bool isEnableOptOnHeapCheck_;
62     bool isEnableOptLoopInvariantCodeMotion_;
63     bool isEnableOptConstantFolding_;
64     bool isEnableLexenvSpecialization_;
65     bool isEnableNativeInline_;
66     bool isEnableLoweringBuiltin_;
67 };
68 
69 class JitCompilerTask final {
70 public:
JitCompilerTask(JitTask * jitTask)71     JitCompilerTask(JitTask *jitTask) : jsFunction_(jitTask->GetJsFunction()), offset_(jitTask->GetOffset()),
72         jitCompilationEnv_(new JitCompilationEnv(jitTask->GetCompilerVM(), jitTask->GetHostVM(), jsFunction_)),
73         profileTypeInfo_(jitTask->GetProfileTypeInfo()),
74         compilerTier_(jitTask->GetCompilerTier()), baselineCompiler_(nullptr),
75         passManager_(nullptr), jitCodeGenerator_(nullptr) { };
76     static JitCompilerTask *CreateJitCompilerTask(JitTask *jitTask);
77 
78     bool Compile();
79     bool Finalize(JitTask *jitTask);
80 
81     void ReleaseJitPassManager();
82 
83 private:
84     JSHandle<JSFunction> jsFunction_;
85     int32_t offset_;
86     std::unique_ptr<JitCompilationEnv> jitCompilationEnv_;
87     JSHandle<ProfileTypeInfo> profileTypeInfo_;
88     CompilerTier compilerTier_;
89     std::unique_ptr<BaselineCompiler> baselineCompiler_;
90     std::unique_ptr<JitPassManager> passManager_;
91     // need refact AOTFileGenerator to JitCodeGenerator
92     std::unique_ptr<AOTFileGenerator> jitCodeGenerator_;
93 };
94 
95 class JitCompiler final {
96 public:
JitCompiler(JSRuntimeOptions * options)97     explicit JitCompiler(JSRuntimeOptions *options) : jitOptions_(*options),
98         log_(jitOptions_.logOption_),
99         logList_(jitOptions_.logMethodsList_),
100         profilerDecoder_(jitOptions_.profilerIn_, jitOptions_.hotnessThreshold_) { }
101     ~JitCompiler() = default;
102     void Init(JSRuntimeOptions options);
103 
104     static JitCompiler *GetInstance(JSRuntimeOptions *options = nullptr);
GetJitOptions()105     JitCompilationOptions &GetJitOptions()
106     {
107         return jitOptions_;
108     }
109 
GetCompilerLog()110     CompilerLog &GetCompilerLog()
111     {
112         return log_;
113     }
114 
GetLogList()115     AotMethodLogList &GetLogList()
116     {
117         return logList_;
118     }
119 
GetProfilerDecoder()120     PGOProfilerDecoder &GetProfilerDecoder()
121     {
122         return profilerDecoder_;
123     }
GetPassOptions()124     PassOptions &GetPassOptions()
125     {
126         return passOptions_;
127     }
128     void UpdatePassOptions(CompilationEnv *env);
129 
130     static ARK_INLINE bool AllocFromFortAndCopy(CompilationEnv &compilationEnv,
131                                                 MachineCodeDesc &desc, RelocMap &relocInfo);
132 private:
133     JitCompilationOptions jitOptions_;
134     CompilerLog log_;
135     AotMethodLogList logList_;
136     PGOProfilerDecoder profilerDecoder_;
137     PassOptions passOptions_;
138 };
139 
140 }  // namespace panda::ecmascript::kungfu
141 #endif  // ECMASCRIPT_COMPILER_JIT_COMPILER_H
142