1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2023 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "Compiler/Optimizer/OpenCLPasses/StackOverflowDetection/StackOverflowDetection.hpp"
#include "Compiler/IGCPassSupport.h"
#include "Compiler/CISACodeGen/helper.h"
#include "Compiler/CodeGenPublic.h"
#include "Compiler/MetaDataApi/IGCMetaDataHelper.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/Module.h>
#include <llvm/IR/Instructions.h>
#include "common/LLVMWarningsPop.hpp"
using namespace llvm;
using namespace IGC;
// Register pass to igc-opt
#define PASS_FLAG "igc-stackoverflow-detection"
#define PASS_DESCRIPTION "Insert calls to stack overflow detection builtins."
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(StackOverflowDetectionPass, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_END(StackOverflowDetectionPass, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
char StackOverflowDetectionPass::ID = 0;
StackOverflowDetectionPass::StackOverflowDetectionPass() : ModulePass(ID) {
initializeStackOverflowDetectionPassPass(*PassRegistry::getPassRegistry());
}
StackOverflowDetectionPass::StackOverflowDetectionPass(Mode mode_) : StackOverflowDetectionPass() { mode = mode_; }
bool StackOverflowDetectionPass::removeDummyCalls(Module &M) {
std::vector<llvm::Instruction *> ToDeleteInstructions;
for (Function &F : M) {
for (auto &&BB : F) {
for (auto &I : BB) {
if (auto callI = llvm::dyn_cast<llvm::CallInst>(&I)) {
Function *callFunction = callI->getCalledFunction();
if (callFunction) {
auto callFunctionName = callFunction->getName();
if (callFunctionName.startswith(STACK_OVERFLOW_INIT_BUILTIN_NAME) ||
callFunctionName.startswith(STACK_OVERFLOW_DETECTION_BUILTIN_NAME)) {
ToDeleteInstructions.push_back(&I);
}
}
}
}
}
}
for (auto I : ToDeleteInstructions) {
I->eraseFromParent();
}
return !ToDeleteInstructions.empty();
}
bool StackOverflowDetectionPass::removeCallsAndFunctionsIfNoStackCallsOrVLA(Module &M, IGCMD::MetaDataUtils *pMdUtils,
ModuleMetaData *pModMD) {
bool changed = false;
bool HasStackCallsOrVLA = std::any_of(M.getFunctionList().begin(), M.getFunctionList().end(), [](auto &F) {
return F.hasFnAttribute("visaStackCall") || F.hasFnAttribute("hasVLA");
});
if (!HasStackCallsOrVLA) {
for (auto FuncName : {STACK_OVERFLOW_INIT_BUILTIN_NAME, STACK_OVERFLOW_DETECTION_BUILTIN_NAME}) {
if (auto F = M.getFunction(FuncName)) {
std::vector<llvm::CallInst *> CallersToDelete;
for (auto User : F->users()) {
if (auto I = dyn_cast<llvm::CallInst>(User)) {
CallersToDelete.push_back(I);
}
}
std::for_each(CallersToDelete.begin(), CallersToDelete.end(),
[](auto CallInst) { CallInst->eraseFromParent(); });
changed = true;
IGCMD::IGCMetaDataHelper::removeFunction(*pMdUtils, *pModMD, F);
F->removeDeadConstantUsers();
F->eraseFromParent();
}
}
}
return changed;
}
bool StackOverflowDetectionPass::runOnModule(Module &M) {
if (IGC_IS_FLAG_DISABLED(StackOverflowDetection)) {
return false;
}
bool changed = false;
auto &MDUWAnalysis = getAnalysis<MetaDataUtilsWrapper>();
auto pMdUtils = MDUWAnalysis.getMetaDataUtils();
auto pModMD = MDUWAnalysis.getModuleMetaData();
const bool isLibraryCompilation = pModMD->compOpt.IsLibraryCompilation;
// The pass is designed to be run at least two times.
// In the first run (Mode::Initialize) it inserts the detection functions
// to entry points without analysis if they are really needed,
// as it is not possible at this stage - it is meant to be called before BIImport
// and PrintfResoulution passes, as the implementation of the detection functions is in the
// builtin module. So the first run is before inlining and we are not sure if stack calls will be present.
// Also VLAs can be optimized later in the compilation.
//
// The next run (Mode::AnalyzeAndCleanup) of this pass is meant to cleanup the calls if they are not really needed.
//
// Third run (Mode::RemoveDummyCalls) removes previously inserted calls to __stackoverflow_init and
// __stackoverflow_detection. These will be inserted again in EmitVisaPass and it's impossible to do it
// correctly before this pass.
// Two dummy calls that we insterted in Mode::Initialize run were there to prevent dead code elimination
// of __stackoverflow_init and __stackoverflow_detection implementations before reaching EmitVisaPass.
if (mode == Mode::RemoveDummyCalls || mode == Mode::AnalyzeAndCleanup) {
if (mode == Mode::RemoveDummyCalls) {
changed = removeDummyCalls(M);
} else {
changed = removeCallsAndFunctionsIfNoStackCallsOrVLA(M, pMdUtils, pModMD);
}
if (changed) {
CodeGenContext *pContext = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
pMdUtils->save(*pContext->getLLVMContext());
}
return changed;
}
IGC_ASSERT(mode == Mode::Initialize);
for (Function &F : M) {
const bool isEntryFunction = isEntryFunc(pMdUtils, &F);
if (isEntryFunction || isLibraryCompilation) {
if (F.isDeclaration())
continue;
IGCLLVM::IRBuilder<> builder(&*F.begin()->begin());
if (isEntryFunction) {
auto StackOverflowInitFunc = M.getOrInsertFunction(
STACK_OVERFLOW_INIT_BUILTIN_NAME, FunctionType::get(Type::getVoidTy(M.getContext()), {}, false));
Function *Callee = cast<Function>(StackOverflowInitFunc.getCallee());
IGC_ASSERT(Callee);
auto InitCall = builder.CreateCall(Callee);
InitCall->setCallingConv(CallingConv::SPIR_FUNC);
}
auto StackOverflowDetectionFunc = M.getOrInsertFunction(
STACK_OVERFLOW_DETECTION_BUILTIN_NAME, FunctionType::get(Type::getVoidTy(M.getContext()), {}, false));
Function *Callee = cast<Function>(StackOverflowDetectionFunc.getCallee());
IGC_ASSERT(Callee);
auto CallInst = builder.CreateCall(Callee);
CallInst->setCallingConv(CallingConv::SPIR_FUNC);
changed = true;
}
}
return changed;
}
|