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 #include "mark_always_inline.h"
17 #include "llvm_ark_interface.h"
18 #include "llvm_compiler_options.h"
19
20 #include <llvm/Pass.h>
21 #include <llvm/IR/InlineAsm.h>
22 #include <llvm/IR/Instructions.h>
23 #include <llvm/IR/Module.h>
24 #include <llvm/IR/Verifier.h>
25 #include <llvm/Transforms/Utils/BasicBlockUtils.h>
26 #include <llvm/Transforms/IPO/FunctionImport.h>
27
28 #include "transforms/transform_utils.h"
29
30 #define DEBUG_TYPE "mark-always-inline"
31
32 using llvm::Attribute;
33 using llvm::AttributeList;
34 using llvm::CallInst;
35 using llvm::dyn_cast;
36 using llvm::Function;
37 using llvm::StringRef;
38
39 namespace ark::llvmbackend::passes {
40
ShouldInsert(const ark::llvmbackend::LLVMCompilerOptions * options)41 bool MarkAlwaysInline::ShouldInsert(const ark::llvmbackend::LLVMCompilerOptions *options)
42 {
43 return options->doIrtocInline;
44 }
45
run(llvm::Function & function,llvm::FunctionAnalysisManager &)46 llvm::PreservedAnalyses MarkAlwaysInline::run(llvm::Function &function, llvm::FunctionAnalysisManager & /*AM*/)
47 {
48 if (function.hasMetadata(LLVMArkInterface::FUNCTION_MD_INLINE_MODULE)) {
49 return llvm::PreservedAnalyses::all();
50 }
51
52 // Experiments showed best improvement on this inlining level
53 static constexpr int32_t DEFAULT_MAX_INLINING_LEVEL = 2;
54
55 auto changed = InlineCallTree(&function, 0, DEFAULT_MAX_INLINING_LEVEL);
56 return changed ? llvm::PreservedAnalyses::none() : llvm::PreservedAnalyses::all();
57 }
58
InlineCallTree(Function * function,int32_t level,int32_t maxLevel)59 bool MarkAlwaysInline::InlineCallTree(Function *function, int32_t level, int32_t maxLevel)
60 {
61 ASSERT(function != nullptr);
62
63 if (level == maxLevel) {
64 return false;
65 }
66 ASSERT(level < maxLevel);
67
68 bool changed = false;
69 for (auto &basicBlock : *function) {
70 for (auto &instruction : basicBlock) {
71 auto call = dyn_cast<CallInst>(&instruction);
72 if (call == nullptr) {
73 continue;
74 }
75 if (call->hasFnAttr(Attribute::AlwaysInline) || call->hasFnAttr(Attribute::NoInline)) {
76 continue;
77 }
78 auto calledFunction = call->getCalledFunction();
79 static constexpr std::array EXCLUSIONS = {
80 // Because:
81 // 1. Called in all interpreter handlers
82 // 2. ~x5 compilation time increase
83 StringRef("DebugPrintEntrypoint"), //
84 };
85 if (calledFunction == nullptr || calledFunction->isDeclaration() ||
86 std::find(EXCLUSIONS.cbegin(), EXCLUSIONS.cend(), calledFunction->getName()) != EXCLUSIONS.cend()) {
87 continue;
88 }
89 call->addAttributeAtIndex(AttributeList::FunctionIndex, Attribute::AlwaysInline);
90 changed = true;
91 LLVM_DEBUG(llvm::dbgs() << "Set AlwaysInline to a call. Caller = '" << call->getFunction()->getName()
92 << "', callee = '" << call->getCalledFunction()->getName() << "'\n");
93 InlineCallTree(call->getCalledFunction(), level + 1, maxLevel);
94 }
95 }
96 return changed;
97 }
98 } // namespace ark::llvmbackend::passes
99