• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 LIBLLVMBACKEND_LLVM_IRTOC_COMPILER_H
17 #define LIBLLVMBACKEND_LLVM_IRTOC_COMPILER_H
18 
19 #include "compiler/code_info/code_info_builder.h"
20 #include "compiler/optimizer/ir/runtime_interface.h"
21 
22 #include "llvm_ark_interface.h"
23 #include "llvm_compiler.h"
24 #include "llvm_compiler_options.h"
25 #include "lowering/debug_data_builder.h"
26 #include "object_code/created_object_file.h"
27 
28 #include <llvm/Support/Error.h>
29 
30 namespace panda::compiler {
31 class CompiledMethod;
32 class Graph;
33 }  // namespace panda::compiler
34 
35 namespace panda::llvmbackend {
36 
37 // NOLINTNEXTLINE(fuchsia-multiple-inheritance)
38 class LLVMIrtocCompiler final : public LLVMCompiler, public IrtocCompilerInterface {
39 public:
40     Expected<bool, std::string> CanCompile(panda::compiler::Graph *graph) override;
41 
42     explicit LLVMIrtocCompiler(panda::compiler::RuntimeInterface *runtime, panda::ArenaAllocator *allocator,
43                                panda::Arch arch, std::string filename);
44 
45     bool AddGraph(panda::compiler::Graph *graph) override;
46 
47     void CompileAll() override;
48 
IsEmpty()49     bool IsEmpty() override
50     {
51         return methods_.empty();
52     }
53 
HasCompiledCode()54     bool HasCompiledCode() override
55     {
56         return objectFile_ != nullptr;
57     }
58 
IsIrFailed()59     bool IsIrFailed() override
60     {
61         return irFailed_;
62     }
63     void WriteObjectFile(std::string_view output) override;
64 
65     CompiledCode GetCompiledCode(std::string_view functionName) override;
66 
67 private:
68     std::string GetFastPathFeatures() const;
69 
70     static std::vector<std::string> GetFeaturesForArch(Arch arch);
71 
72     void InitializeSpecificLLVMOptions(Arch arch);
73 
74     void InitializeModule();
75 
76     size_t GetObjectFileSize() override;
77 
78 private:
79     llvm::ExitOnError exitOnErr_;
80 
81     ArenaVector<panda::Method *> methods_;
82     std::unique_ptr<llvm::Module> module_;
83     std::unique_ptr<DebugDataBuilder> debugData_;
84     std::unique_ptr<panda::llvmbackend::CreatedObjectFile> objectFile_ {nullptr};
85     std::string filename_;
86 
87     LLVMArkInterface arkInterface_;
88     bool irFailed_ {false};
89     std::unique_ptr<panda::llvmbackend::MIRCompiler> mirCompiler_;
90     std::unique_ptr<panda::llvmbackend::LLVMOptimizer> optimizer_;
91 };
92 }  // namespace panda::llvmbackend
93 #endif  // LIBLLVMBACKEND_LLVM_IRTOC_COMPILER_H
94