File: UniformAssumptions.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 (233 lines) | stat: -rw-r--r-- 8,283 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*========================== begin_copyright_notice ============================

Copyright (C) 2019-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "Compiler/CodeGenPublic.h"
#include "Compiler/CISACodeGen/helper.h"
#include "Compiler/IGCPassSupport.h"
#include "common/IGCIRBuilder.h"
#include "LLVM3DBuilder/BuiltinsFrontend.hpp"
#include "UniformAssumptions.hpp"
#include "Probe/Assertion.h"

using namespace llvm;

namespace IGC {

    char UniformAssumptions::ID = 0;
#define PASS_FLAG "UniformAssumptions"
#define PASS_DESCRIPTION "Detect non-uniform usage of textures and samplers and check if they may be assumed uniform."
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
    IGC_INITIALIZE_PASS_BEGIN(UniformAssumptions, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
        IGC_INITIALIZE_PASS_DEPENDENCY(WIAnalysis)
        IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
        IGC_INITIALIZE_PASS_END(UniformAssumptions, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

        UniformAssumptions::UniformAssumptions() : llvm::FunctionPass(ID)
    {
        initializeUniformAssumptionsPass(*PassRegistry::getPassRegistry());
    }

    bool UniformAssumptions::runOnFunction(Function& F)
    {
        m_pCodeGenContext = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
        m_WIAnalysis = &getAnalysis<WIAnalysis>();
        if (m_WIAnalysis == nullptr || m_pCodeGenContext == nullptr)
        {
            IGC_ASSERT(0);
            return false;
        }

        // Propagate assumptions backwards (if safe) to increase the coverage.
        HoistAssumptions(F);
        // Try optimizing non-uniform resource accesses to uniform (to prevent from adding ResourceLoops)
        OptimizeResourceAccesses(F);

        return m_changed;
    }

    void UniformAssumptions::visitCallInst(CallInst& CI)
    {
        if (m_collectingAssumptions)
        {
            if (llvm::GenIntrinsicInst * pIntr = llvm::dyn_cast<llvm::GenIntrinsicInst>(&CI))
            {
                if (pIntr->getIntrinsicID() == GenISAIntrinsic::GenISA_is_uniform)
                {
                    m_assumptions.push_back(pIntr);
                }
            }
            return;
        }

        if (llvm::GenIntrinsicInst * pIntr = llvm::dyn_cast<llvm::GenIntrinsicInst>(&CI))
        {
            // Figure out the intrinsic operands for texture & sampler
            llvm::Value* pTextureValue = nullptr;
            llvm::Value*pSamplerValue = nullptr;
            IGC::getTextureAndSamplerOperands(
                pIntr,
                pTextureValue,
                pSamplerValue);

            if (pTextureValue && pTextureValue->getType()->isPointerTy() &&
                !m_WIAnalysis->isUniform(pTextureValue))
            {
                // Check assumptions for texture:
                if (IsAssumedUniform(pTextureValue))
                {
                    MakeUniformResourceOperand(pTextureValue, &CI);
                }
            }

            if (pSamplerValue && pSamplerValue->getType()->isPointerTy() &&
                !m_WIAnalysis->isUniform(pSamplerValue))
            {
                // Check assumptions for sampler:
                if (IsAssumedUniform(pSamplerValue))
                {
                    MakeUniformResourceOperand(pSamplerValue, &CI);
                }
            }
        }
    }

    void UniformAssumptions::OptimizeResourceAccesses(llvm::Function& F)
    {
        IGC_ASSERT(m_collectingAssumptions == false);
        visit(F);
    }

    bool UniformAssumptions::IsAssumedUniform(llvm::Value* V, int recursionLevel) const
    {
        if (recursionLevel >= s_cMaxRecursion)
        {
            // Limit level of recursion.
            return false;
        }

        // Check if marked as uniform by WIAnalysis:
        if (m_WIAnalysis->isUniform(V))
        {
            return true;
        }

        // Check if value can be assumed uniform:
        for (auto UI = V->use_begin(), UE = V->use_end(); UI != UE; ++UI)
        {
            if (llvm::GenIntrinsicInst * pIntr = llvm::dyn_cast<llvm::GenIntrinsicInst>(UI->getUser()))
            {
                if (pIntr->getIntrinsicID() == GenISAIntrinsic::GenISA_is_uniform)
                {
                    return true;
                }
            }
        }

        // This is very conservative simplification of rules implemented in Uniform Analysis (WIANalysis pass).
        // Uniform Analysis additionally tries to prove some instructions to be uniform even if they do not
        // have all uniform operands. Here, we simply assume that:
        // - CallInst, AllocaInst, VAArgInst and PHINode that were marked by UA as non-uniform remain non-uniform.
        // - Other instructions marked by UA as non-uniform can only be assumed uniform if all of its operands can be assumed uniform.
        // In the future, it would be better to reuse Uniform Analysis logic here - it would be more reliable
        // and would also optimize more cases.
        if (llvm::isa<llvm::CallInst>(V) ||
            llvm::isa<llvm::AllocaInst>(V) ||
            llvm::isa<llvm::VAArgInst>(V) ||
            llvm::isa<llvm::PHINode>(V))
        {
            return false;
        }

        // Check if all operands are uniform:
        if (auto inst = dyn_cast<Instruction>(V))
        {
            for (auto& op : inst->operands())
            {
                if (!IsAssumedUniform(op, recursionLevel + 1))
                {
                    return false;
                }
            }
            return true;
        }

        return false;
    }

    void UniformAssumptions::MakeUniformResourceOperand(llvm::Value* V, llvm::CallInst* CI)
    {
        // Add readFirstLane to make value uniform.
        LLVM3DBuilder<> builder(*m_pCodeGenContext->getLLVMContext(), m_pCodeGenContext->platform.getPlatformInfo());
        builder.SetInsertPoint(CI);
        Value* uniformTexture = builder.readFirstLane(V);
        for (unsigned i = 0; i < CI->getNumOperands(); i++)
        {
            if (CI->getOperand(i) == V)
            {
                CI->setOperand(i, uniformTexture);
            }
        }

        m_changed = true;
    }

    void UniformAssumptions::CollectAssumptions(llvm::Function& F)
    {
        m_assumptions.clear();
        m_collectingAssumptions = true;
        visit(F);
        m_collectingAssumptions = false;
    }

    void UniformAssumptions::HoistAssumptions(llvm::Function& F)
    {
        CollectAssumptions(F);

        // Try to propagate assumptions up.
        for (auto A : m_assumptions)
        {
            auto src = A->getOperand(0);
            bool check_further = true;
            while (check_further)
            {
                check_further = false;
                if (auto castInst = dyn_cast<CastInst>(src))
                {
                    // if ext(A) is uniform then A is uniform too.
                    if (castInst->getOpcode() == Instruction::ZExt ||
                        castInst->getOpcode() == Instruction::SExt)
                    {
                        Instruction* newAssumption = A->clone();
                        src = castInst->getOperand(0);
                        newAssumption->setOperand(0, src);
                        if (auto srcInst = dyn_cast<Instruction>(src))
                        {
                            if (isa<PHINode>(srcInst))
                            {
                                newAssumption->insertBefore(
                                    srcInst->getParent()->getFirstNonPHI());
                            }
                            else
                            {
                                newAssumption->insertAfter(srcInst);
                            }
                        }
                        else
                        {
                            newAssumption->insertBefore(
                                F.getEntryBlock().getFirstNonPHI());
                        }
                        check_further = true;
                    }
                }
            }
        }
    }

} // namespace IGC