File: RayTracingIntrinsicAnalysis.cpp

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 (170 lines) | stat: -rw-r--r-- 5,163 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
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
163
164
165
166
167
168
169
170
/*========================== begin_copyright_notice ============================

Copyright (C) 2019-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

//===----------------------------------------------------------------------===//
///
/// This pass and its Resolution equivalent follow the familar AdaptorOCL
/// pattern of an analysis followed by a resolution pass.  The idea early
/// on was that continuations would be invoked often as function calls so we
/// need to promote the below intrinsics to implicit args so we could later call
/// BuiltinCallGraphAnalysis to propagate the arguments through all calls.  It
/// is not currently clear how much we'll use this approach: it may be that
/// BTD + inlining of small continuations may is the way to go.
///
//===----------------------------------------------------------------------===//

#include "IGC/common/StringMacros.hpp"
#include "common/LLVMUtils.h"
#include "Compiler/IGCPassSupport.h"
#include "Compiler/CodeGenPublic.h"
#include "AdaptorCommon/ImplicitArgs.hpp"

#include "common/LLVMWarningsPush.hpp"
#include "llvm/IR/InstVisitor.h"
#include "common/LLVMWarningsPop.hpp"

using namespace IGC;
using namespace llvm;

//////////////////////////////////////////////////////////////////////////
//
// Pass detects the use of ray tracing intrinsics and prepares them for
// addition as implicit arguments
class RayTracingIntrinsicAnalysis : public ModulePass, public InstVisitor<RayTracingIntrinsicAnalysis>
{
public:
    RayTracingIntrinsicAnalysis();
    bool runOnModule(Module& M) override;
    bool runOnFunction(Function &F);
    StringRef getPassName() const override
    {
        return "RayTracingIntrinsicAnalysis";
    }
    virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override
    {
        AU.addRequired<MetaDataUtilsWrapper>();
    }

    void visitCallInst(CallInst &CI);

    static char ID;
private:
    bool hasGlobalPointer;
    bool hasLocalPointer;
    bool hasStackID;
    bool hasInlinedData;
    IGCMD::MetaDataUtils* pMdUtils;
};

#define PASS_FLAG "raytracing-intrinsic-analysis"
#define PASS_DESCRIPTION "Mark raytracing intrinsics as implicit args"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(RayTracingIntrinsicAnalysis, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_END(RayTracingIntrinsicAnalysis, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

RayTracingIntrinsicAnalysis::RayTracingIntrinsicAnalysis() : ModulePass(ID) {
    initializeRayTracingIntrinsicAnalysisPass(*PassRegistry::getPassRegistry());
}

char RayTracingIntrinsicAnalysis::ID = 0;

void RayTracingIntrinsicAnalysis::visitCallInst(CallInst &CI)
{
    if (auto *GII = dyn_cast<GenIntrinsicInst>(&CI))
    {
        switch (GII->getIntrinsicID())
        {
        case GenISAIntrinsic::GenISA_GlobalBufferPointer:
            hasGlobalPointer = true;
            return;
        case GenISAIntrinsic::GenISA_LocalBufferPointer:
            hasLocalPointer = true;
            return;
        case GenISAIntrinsic::GenISA_AsyncStackID:
            hasStackID = true;
            return;
        case GenISAIntrinsic::GenISA_InlinedData:
            hasInlinedData = true;
            return;
        default:
            break;
        }
    }
}

bool RayTracingIntrinsicAnalysis::runOnModule(Module& M)
{
    bool changed = false;
    pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
    // Run on all functions defined in this module
    for (Function& F : M)
    {
        if (F.isDeclaration())
        {
            continue;
        }
        if (runOnFunction(F))
        {
            changed = true;
        }
    }

    // Update LLVM metadata based on IGC MetadataUtils
    if (changed)
        pMdUtils->save(M.getContext());

    return changed;
}

bool RayTracingIntrinsicAnalysis::runOnFunction(Function &F)
{
    if (pMdUtils->findFunctionsInfoItem(&F) == pMdUtils->end_FunctionsInfo())
        return false;

    hasGlobalPointer = false;
    hasLocalPointer  = false;
    hasStackID       = false;
    hasInlinedData   = false;

    visit(F);

    bool changed = false;
    SmallVector<ImplicitArg::ArgType, ImplicitArg::NUM_IMPLICIT_ARGS> implicitArgs;

    if (hasGlobalPointer)
        implicitArgs.push_back(ImplicitArg::RT_GLOBAL_BUFFER_POINTER);

    if (hasLocalPointer)
        implicitArgs.push_back(ImplicitArg::RT_LOCAL_BUFFER_POINTER);

    if (hasStackID)
        implicitArgs.push_back(ImplicitArg::RT_STACK_ID);

    if (hasInlinedData)
        implicitArgs.push_back(ImplicitArg::RT_INLINED_DATA);

    if (!implicitArgs.empty())
    {
        // Create IGC metadata representing the implicit args needed by this function
        ImplicitArgs::addImplicitArgs(
            F,
            implicitArgs,
            pMdUtils);
        changed = true;
    }
    return changed;
}
namespace IGC
{
    Pass* createRayTracingIntrinsicAnalysisPass()
    {
        return new RayTracingIntrinsicAnalysis();
    }
}