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_LOWERING_WRAPPED_MODULE_H 17 #define LIBLLVMBACKEND_LOWERING_WRAPPED_MODULE_H 18 19 #include "compiler/optimizer/ir/runtime_interface.h" 20 #include "debug_data_builder.h" 21 #include "llvm_ark_interface.h" 22 #include "object_code/code_info_producer.h" 23 24 #include <llvm/IR/Module.h> 25 #include <llvm/Target/TargetMachine.h> 26 27 #include <vector> 28 29 namespace ark::llvmbackend { 30 /// Wrapper around everything we need to compile an llvm::Module. 31 class WrappedModule { 32 public: 33 explicit WrappedModule(std::unique_ptr<llvm::LLVMContext> llvmContext, std::unique_ptr<llvm::Module> module, 34 std::unique_ptr<llvm::TargetMachine> targetMachine, 35 std::unique_ptr<ark::llvmbackend::LLVMArkInterface> arkInterface, 36 std::unique_ptr<ark::llvmbackend::DebugDataBuilder> debugData); 37 SetId(uint32_t id)38 void SetId(uint32_t id) 39 { 40 moduleId_ = id; 41 } 42 43 NO_COPY_SEMANTIC(WrappedModule); 44 45 DEFAULT_MOVE_SEMANTIC(WrappedModule); 46 47 ~WrappedModule() = default; 48 49 void AddMethod(ark::compiler::RuntimeInterface::MethodPtr method); 50 51 void SetCompiled(std::unique_ptr<ark::llvmbackend::CreatedObjectFile> objectFile); 52 53 bool IsCompiled(); 54 55 bool HasFunctionDefinition(ark::compiler::RuntimeInterface::MethodPtr method); 56 57 llvm::Function *GetFunctionByMethodPtr(ark::compiler::RuntimeInterface::MethodPtr method); 58 59 const std::unique_ptr<llvm::LLVMContext> &GetLLVMContext(); 60 61 const std::unique_ptr<llvm::Module> &GetModule(); 62 63 const std::unique_ptr<llvm::TargetMachine> &GetTargetMachine(); 64 65 const std::unique_ptr<ark::llvmbackend::LLVMArkInterface> &GetLLVMArkInterface(); 66 67 const std::unique_ptr<ark::llvmbackend::DebugDataBuilder> &GetDebugData(); 68 69 const std::unique_ptr<ark::llvmbackend::CodeInfoProducer> &GetCodeInfoProducer(); 70 71 const std::vector<ark::compiler::RuntimeInterface::MethodPtr> &GetMethods(); 72 73 const std::unique_ptr<ark::llvmbackend::CreatedObjectFile> &GetObjectFile(); 74 75 uint32_t GetModuleId(); 76 77 std::unique_ptr<ark::llvmbackend::CreatedObjectFile> TakeObjectFile(); 78 79 void Dump(llvm::raw_ostream *stream); 80 81 private: 82 // Do not move llvmContext_ below module, the context must be destroyed after module 83 std::unique_ptr<llvm::LLVMContext> llvmContext_; 84 std::unique_ptr<llvm::Module> module_; 85 std::unique_ptr<llvm::TargetMachine> targetMachine_; 86 std::unique_ptr<ark::llvmbackend::CreatedObjectFile> objectFile_; 87 std::unique_ptr<ark::llvmbackend::LLVMArkInterface> arkInterface_; 88 std::unique_ptr<ark::llvmbackend::DebugDataBuilder> debugData_; 89 std::unique_ptr<ark::llvmbackend::CodeInfoProducer> codeInfoProducer_; 90 std::vector<ark::compiler::RuntimeInterface::MethodPtr> methods_; 91 uint32_t moduleId_ {0}; 92 }; 93 94 } // namespace ark::llvmbackend 95 #endif // LIBLLVMBACKEND_LOWERING_WRAPPED_MODULE_H 96