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 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 ark::compiler { 31 class CompiledMethod; 32 class Graph; 33 } // namespace ark::compiler 34 35 namespace ark::llvmbackend { 36 37 // NOLINTNEXTLINE(fuchsia-multiple-inheritance) 38 class LLVMIrtocCompiler final : public LLVMCompiler, public IrtocCompilerInterface { 39 public: 40 explicit LLVMIrtocCompiler(ark::compiler::RuntimeInterface *runtime, ark::ArenaAllocator *allocator, ark::Arch arch, 41 std::string filename); 42 43 Expected<bool, std::string> TryAddGraph(ark::compiler::Graph *graph) override; 44 45 void FinishCompile() override; 46 IsEmpty()47 bool IsEmpty() override 48 { 49 return methods_.empty(); 50 } 51 HasCompiledCode()52 bool HasCompiledCode() override 53 { 54 return objectFile_ != nullptr; 55 } 56 IsIrFailed()57 bool IsIrFailed() override 58 { 59 return false; 60 } 61 void WriteObjectFile(std::string_view output) override; 62 63 CompiledCode GetCompiledCode(std::string_view functionName) override; 64 65 private: 66 std::string GetFastPathFeatures() const; 67 68 static std::vector<std::string> GetFeaturesForArch(Arch arch); 69 70 void InitializeSpecificLLVMOptions(Arch arch); 71 72 void InitializeModule(); 73 74 size_t GetObjectFileSize() override; 75 76 private: 77 llvm::ExitOnError exitOnErr_; 78 79 ArenaVector<ark::Method *> methods_; 80 std::unique_ptr<llvm::Module> module_; 81 std::unique_ptr<DebugDataBuilder> debugData_; 82 std::unique_ptr<ark::llvmbackend::CreatedObjectFile> objectFile_ {nullptr}; 83 std::string filename_; 84 85 LLVMArkInterface arkInterface_; 86 std::unique_ptr<ark::llvmbackend::MIRCompiler> mirCompiler_; 87 std::unique_ptr<ark::llvmbackend::LLVMOptimizer> optimizer_; 88 std::unique_ptr<llvm::TargetMachine> targetMachine_ {nullptr}; 89 }; 90 } // namespace ark::llvmbackend 91 #endif // LIBLLVMBACKEND_LLVM_IRTOC_COMPILER_H 92