File: ModuleAllocaAnalysis.hpp

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (108 lines) | stat: -rw-r--r-- 3,781 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
106
107
108
/*========================== begin_copyright_notice ============================

Copyright (C) 2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/
#pragma once

#include "common/LLVMWarningsPush.hpp"
#include <llvm/Pass.h>
#include "common/LLVMWarningsPop.hpp"
#include "llvmWrapper/Support/Alignment.h"

namespace llvm{
    class AllocaInst;
    class Function;
}

namespace IGC
{
    namespace IGCMD
    {
        class MetaDataUtils;
    }
    class GenXFunctionGroupAnalysis;
    class FunctionGroup;

    /// \brief Analyze alloca instructions and determine the size and offset of
    /// each alloca and the total amount of private memory needed by each kernel.
    class ModuleAllocaAnalysis : public llvm::ModulePass
    {
    public:
        // Pass identification, replacement for typeid
        static char ID;

        ModuleAllocaAnalysis();
        ~ModuleAllocaAnalysis();

        virtual llvm::StringRef getPassName() const override;

        virtual bool runOnModule(llvm::Module& M) override;

        /// @brief  Adds the analysis required by this pass
        virtual void getAnalysisUsage(llvm::AnalysisUsage& AU) const override;

        /// Initialize setup like UseScratchSpacePrivateMemory.
        bool safeToUseScratchSpace() const;

        /// \brief Return the offset of alloca instruction in private memory buffer.
        //  This function should not be called when AI is variable length alloca
        unsigned getConstBufferOffset(llvm::AllocaInst* AI) const;

        /// \brief Return the size of alloca instruction in private memory buffer.
        //  This function should not be called when AI is variable length alloca
        unsigned getConstBufferSize(llvm::AllocaInst* AI) const;

        /// \brief Return all alloca instructions of a given function.
        llvm::SmallVector<llvm::AllocaInst*, 8>& getAllocaInsts(llvm::Function* F) const;

        /// \brief Return the total private memory size per WI of a given function.
        unsigned getTotalPrivateMemPerWI(llvm::Function* F) const;

    private:
        struct FunctionAllocaInfo {
            void setAllocaDesc(llvm::AllocaInst* AI, unsigned Offset, unsigned Size)
            {
                AllocaDesc[AI] = std::make_pair(Offset, Size);
            }

            /// \brief Total amount of private memory size per kernel. All functions in
            /// a kernel will have the same size.
            unsigned TotalSize = 0;

            /// \brief Alloca instructions for a function.
            llvm::SmallVector<llvm::AllocaInst*, 8> Allocas;

            /// \brief Alloca instruction, its offset and size in buffer.
            llvm::DenseMap<llvm::AllocaInst*, std::pair<unsigned, unsigned>> AllocaDesc;
        };

        /// \brief The module being analyzed.
        llvm::Module* M = nullptr;

        /// \brief The optional function group analysis.
        GenXFunctionGroupAnalysis* FGA = nullptr;

        /// \brief Each function has an entry that describes its private memory
        /// usage information.
        llvm::DenseMap<llvm::Function*, FunctionAllocaInfo*> InfoMap;

        FunctionAllocaInfo* getFuncAllocaInfo(llvm::Function* F) const;

        FunctionAllocaInfo* getOrCreateFuncAllocaInfo(llvm::Function* F);

        /// \brief Analyze the module and fill function alloca info map.
        void analyze();

        /// \brief When function group analysis is available, visit group by group.
        void analyze(IGC::FunctionGroup* FG);

        /// \brief Analyze individual functions.
        void analyze(llvm::Function* F, unsigned& gOffset, alignment_t& gAlignment);
    };

    void initializeModuleAllocaAnalysisPass(llvm::PassRegistry&);

}