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_COMPILER_H 17 #define LIBLLVMBACKEND_LLVM_COMPILER_H 18 19 #include "compiler_interface.h" 20 #include "mir_compiler.h" 21 #include "llvm_compiler_options.h" 22 #include "transforms/llvm_optimizer.h" 23 24 #include <llvm/ADT/Triple.h> 25 #include <llvm/IR/LLVMContext.h> 26 #include <llvm/Support/CommandLine.h> 27 #include <llvm/Support/ManagedStatic.h> 28 29 namespace ark::llvmbackend { 30 31 class LLVMCompiler : public CompilerInterface { 32 public: 33 explicit LLVMCompiler(Arch arch); 34 GetArch()35 Arch GetArch() const 36 { 37 return arch_; 38 } 39 40 protected: 41 bool IsInliningDisabled(); 42 bool IsInliningDisabled(ark::compiler::Graph *graph); 43 44 bool IsInliningDisabled(compiler::RuntimeInterface *runtime, compiler::RuntimeInterface::MethodPtr method); 45 46 ark::llvmbackend::LLVMCompilerOptions InitializeLLVMCompilerOptions(); 47 void InitializeDefaultLLVMOptions(); 48 void InitializeLLVMOptions(); 49 50 void SetLLVMOption(const char *option, const std::string &value); 51 52 static llvm::Triple GetTripleForArch(Arch arch); 53 static std::string GetCPUForArch(Arch arch); 54 55 static void InitializeLLVMTarget(Arch arch); 56 static void InitializeLLVMPasses(); 57 GetLLVMContext()58 llvm::LLVMContext *GetLLVMContext() 59 { 60 return &context_; 61 } 62 63 private: 64 Arch arch_; 65 llvm::LLVMContext context_; 66 std::string llvmPreOptions_; 67 #ifndef NDEBUG 68 bool parsedOptions_ {false}; 69 #endif 70 }; 71 } // namespace ark::llvmbackend 72 #endif // LIBLLVMBACKEND_LLVM_COMPILER_H 73