• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PANDA_PAOC_H
17 #define PANDA_PAOC_H
18 
19 #include "paoc_options.h"
20 #include "aot_builder/aot_builder.h"
21 #include "paoc_clusters.h"
22 #include "runtime/compiler.h"
23 #include "utils/expected.h"
24 #include "utils/span.h"
25 
26 namespace ark::compiler {
27 class Graph;
28 class SharedSlowPathData;
29 }  // namespace ark::compiler
30 
31 namespace ark::paoc {
32 
33 struct SkipInfo {
34     bool isFirstCompiled;
35     bool isLastCompiled;
36 };
37 
38 enum class PaocMode : uint8_t { AOT, JIT, OSR, LLVM };
39 
40 class Paoc {
41 public:
42     int Run(const ark::Span<const char *> &args);
43 
44 protected:
45     enum class LLVMCompilerStatus { FALLBACK = 0, COMPILED = 1, SKIP = 2, ERROR = 3 };
46 
47     // Wrapper for CompileJit(), CompileOsr() and CompileAot() arguments:
48     struct CompilingContext {
49         NO_COPY_SEMANTIC(CompilingContext);
50         NO_MOVE_SEMANTIC(CompilingContext);
51         CompilingContext(Method *methodPtr, size_t methodIndex, std::ofstream *statisticsDump);
52         ~CompilingContext();
53         void DumpStatistics() const;
54 
55     public:
56         Method *method {nullptr};                 // NOLINT(misc-non-private-member-variables-in-classes)
57         ark::ArenaAllocator allocator;            // NOLINT(misc-non-private-member-variables-in-classes)
58         ark::ArenaAllocator graphLocalAllocator;  // NOLINT(misc-non-private-member-variables-in-classes)
59         ark::compiler::Graph *graph {nullptr};    // NOLINT(misc-non-private-member-variables-in-classes)
60         size_t index;                             // NOLINT(misc-non-private-member-variables-in-classes)
61         std::ofstream *stats {nullptr};           // NOLINT(misc-non-private-member-variables-in-classes)
62         bool compilationStatus {true};            // NOLINT(misc-non-private-member-variables-in-classes)
63     };
64 
CreateAotBuilder()65     virtual std::unique_ptr<compiler::AotBuilder> CreateAotBuilder()
66     {
67         return std::make_unique<compiler::AotBuilder>();
68     }
69 
70 private:
71     void RunAotMode(const ark::Span<const char *> &args);
72     void StartAotFile(const panda_file::File &pfileRef);
73     bool CompileFiles();
74     bool TryLoadPandaFile(const std::string &fileName, PandaVM *vm);
75     bool CompilePandaFile(const panda_file::File &pfileRef);
76     ark::Class *ResolveClass(const panda_file::File &pfileRef, panda_file::File::EntityId classId);
77     bool PossibleToCompile(const panda_file::File &pfileRef, const ark::Class *klass,
78                            panda_file::File::EntityId classId);
79     bool Compile(Class *klass, const panda_file::File &pfileRef);
80 
81     bool Compile(Method *method, size_t methodIndex);
82     bool CompileInGraph(CompilingContext *ctx, std::string methodName, bool isOsr);
83     bool RunOptimizations(CompilingContext *ctx);
84     bool CompileJit(CompilingContext *ctx);
85     bool CompileOsr(CompilingContext *ctx);
86     bool CompileAot(CompilingContext *ctx);
87     bool FinalizeCompileAot(CompilingContext *ctx, [[maybe_unused]] uintptr_t codeAddress);
88     void PrintError(const std::string &error);
89     void PrintUsage(const ark::PandArgParser &paParser);
90     bool IsMethodInList(const std::string &methodFullName);
91     bool Skip(Method *method);
92     static std::string GetFileLocation(const panda_file::File &pfileRef, std::string location);
93     static bool CompareBootFiles(std::string filename, std::string paocLocation);
94     bool LoadPandaFiles();
95     bool TryCreateGraph(CompilingContext *ctx);
96     void BuildClassHashTable(const panda_file::File &pfileRef);
97     std::string GetFilePath(std::string fileName);
98 
IsAotMode()99     bool IsAotMode()
100     {
101         return mode_ == PaocMode::AOT || mode_ == PaocMode::LLVM;
102     }
103 
104     class ErrorHandler : public ClassLinkerErrorHandler {
OnError(ClassLinker::Error error,const PandaString & message)105         void OnError([[maybe_unused]] ClassLinker::Error error, [[maybe_unused]] const PandaString &message) override {}
106     };
107 
108     class PaocInitializer;
109 
110 protected:
AddExtraOptions(PandArgParser * parser)111     virtual void AddExtraOptions([[maybe_unused]] PandArgParser *parser) {}
ValidateExtraOptions()112     virtual void ValidateExtraOptions() {}
113 
GetPaocOptions()114     ark::paoc::Options *GetPaocOptions()
115     {
116         return paocOptions_.get();
117     }
118 
GetRuntime()119     compiler::RuntimeInterface *GetRuntime()
120     {
121         return runtime_;
122     }
GetCodeAllocator()123     ArenaAllocator *GetCodeAllocator()
124     {
125         return codeAllocator_;
126     }
127 
128     bool ShouldIgnoreFailures();
IsLLVMAotMode()129     bool IsLLVMAotMode()
130     {
131         return mode_ == PaocMode::LLVM;
132     }
133 
134     virtual void Clear(ark::mem::InternalAllocatorPtr allocator);
PrepareLLVM(const ark::Span<const char * > & args)135     virtual void PrepareLLVM([[maybe_unused]] const ark::Span<const char *> &args)
136     {
137         LOG(FATAL, COMPILER) << "--paoc-mode=llvm is not supported in this configuration";
138     }
TryLLVM(CompilingContext * ctx)139     virtual LLVMCompilerStatus TryLLVM([[maybe_unused]] CompilingContext *ctx)
140     {
141         UNREACHABLE();
142         return LLVMCompilerStatus::FALLBACK;
143     }
EndLLVM()144     virtual bool EndLLVM()
145     {
146         UNREACHABLE();
147     }
148 
149 protected:
150     std::unique_ptr<compiler::AotBuilder> aotBuilder_;  // NOLINT(misc-non-private-member-variables-in-classes)
151 
152 private:
153     std::unique_ptr<ark::RuntimeOptions> runtimeOptions_ {nullptr};
154     std::unique_ptr<ark::paoc::Options> paocOptions_ {nullptr};
155 
156     compiler::RuntimeInterface *runtime_ {nullptr};
157 
158     PaocMode mode_ {PaocMode::AOT};
159     ClassLinker *loader_ {nullptr};
160     ArenaAllocator *codeAllocator_ {nullptr};
161     std::set<std::string> methodsList_;
162     std::unordered_map<std::string, std::string> locationMapping_;
163     std::unordered_map<std::string, const panda_file::File *> preloadedFiles_;
164     size_t compilationIndex_ {0};
165     SkipInfo skipInfo_ {false, false};
166 
167     PaocClusters clustersInfo_;
168     compiler::SharedSlowPathData *slowPathData_ {nullptr};
169 
170     unsigned successMethods_ {0};
171     unsigned failedMethods_ {0};
172 
173     std::ofstream statisticsDump_;
174 };
175 
176 }  // namespace ark::paoc
177 #endif  // PANDA_PAOC_H
178