File: BufferBoundsCheckingPatcher.cpp

package info (click to toggle)
intel-graphics-compiler 1.0.17791.18-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 102,312 kB
  • sloc: cpp: 935,343; lisp: 286,143; ansic: 16,196; python: 3,279; yacc: 2,487; lex: 1,642; pascal: 300; sh: 174; makefile: 27
file content (105 lines) | stat: -rw-r--r-- 3,521 bytes parent folder | download
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2024 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "BufferBoundsCheckingPatcher.hpp"
#include "Compiler/IGCPassSupport.h"

#include "common/LLVMWarningsPush.hpp"
#include "llvmWrapper/IR/DerivedTypes.h"
#include "llvmWrapper/IR/Instructions.h"
#include "llvmWrapper/IR/Function.h"
#include <llvm/Demangle/Demangle.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/GetElementPtrTypeIterator.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Mangler.h>
#include <llvm/IR/Module.h>
#include <llvm/Support/Regex.h>
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
#include <llvm/Transforms/Utils/Cloning.h>
#include "common/LLVMWarningsPop.hpp"

using namespace llvm;
using namespace IGC;

// Register pass to igc-opt
#define PASS_FLAG "igc-buffer-bounds-checking-patcher"
#define PASS_DESCRIPTION "Buffer bounds checking - patcher"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(BufferBoundsCheckingPatcher, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_END(BufferBoundsCheckingPatcher, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

char BufferBoundsCheckingPatcher::ID = 0;

BufferBoundsCheckingPatcher::BufferBoundsCheckingPatcher() : ModulePass(ID)
{
    initializeBufferBoundsCheckingPatcherPass(*PassRegistry::getPassRegistry());
}

bool BufferBoundsCheckingPatcher::runOnModule(Module& M)
{
    metadataUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
    for (auto& function : M)
    {
        if (function.isDeclaration())
        {
            continue;
        }

        implicitArgs = new ImplicitArgs(function, metadataUtils);
        if (!isEntryFunc(metadataUtils, &function))
        {
            return false;
        }

        for (auto& instruction : instructions(&function))
        {
            if (auto call = dyn_cast<CallInst>(&instruction))
            {
                auto function = call->getCalledFunction();
                if (!function || function->getName() != BUFFER_SIZE_PLACEHOLDER_FUNCTION_NAME)
                {
                    continue;
                }

                auto bufferSize = getBufferSizeArg(call->getFunction(), (uint32_t)cast<ConstantInt>(call->getArgOperand(0))->getSExtValue());
                //auto bufferSize = ConstantInt::get(Type::getInt64Ty(call->getContext()), 64); // Only for testing purposes
                call->replaceAllUsesWith(bufferSize);
                toRemove.push_back(call);
            }
        }
    }

    for (const auto& it : toRemove)
    {
        it->eraseFromParent();
    }

    auto bufferSizePlaceholderFunction = M.getFunction(BUFFER_SIZE_PLACEHOLDER_FUNCTION_NAME);
    if (bufferSizePlaceholderFunction)
    {
        bufferSizePlaceholderFunction->eraseFromParent();
        return true;
    }

    return !toRemove.empty();
}

Argument* BufferBoundsCheckingPatcher::getBufferSizeArg(Function* function, uint32_t n)
{
    uint32_t index = implicitArgs->getNumberedArgIndex(ImplicitArg::ArgType::BUFFER_SIZE, n);
    if (index >= implicitArgs->size())
    {
        IGC_ASSERT_MESSAGE(0, "Implicit argument for BUFFER_SIZE is out of range!");
        return nullptr;
    }

    return IGCLLVM::getArg(*function, function->arg_size() - implicitArgs->size() + index);
}