File: SampleMultiversioning.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 (364 lines) | stat: -rw-r--r-- 11,606 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*========================== begin_copyright_notice ============================

Copyright (C) 2019-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "SampleMultiversioning.hpp"
#include "Compiler/CodeGenPublic.h"
#include "Compiler/CodeGenPublicEnums.h"
#include "Compiler/IGCPassSupport.h"
#include "Compiler/InitializePasses.h"
#include "common/secure_mem.h"
#include "Probe/Assertion.h"

using namespace llvm;
using namespace IGC;

// Register pass to igc-opt
#define PASS_FLAG "igc-sample-multiversioning"
#define PASS_DESCRIPTION "sample multiversioning"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(SampleMultiversioning, PASS_FLAG, PASS_DESCRIPTION,
    PASS_CFG_ONLY, PASS_ANALYSIS)
    IGC_INITIALIZE_PASS_END(SampleMultiversioning, PASS_FLAG, PASS_DESCRIPTION,
        PASS_CFG_ONLY, PASS_ANALYSIS)

    char SampleMultiversioning::ID = 0;

#define DEBUG_TYPE "SampleMultiversioning"

SampleMultiversioning::SampleMultiversioning(CodeGenContext* pContext)
    : FunctionPass(ID), pContext(pContext)
{
    initializeSampleMultiversioningPass(*PassRegistry::getPassRegistry());
}

SampleMultiversioning::SampleMultiversioning() : FunctionPass(ID), pContext(nullptr)
{
    initializeSampleMultiversioningPass(*PassRegistry::getPassRegistry());
}

bool SampleMultiversioning::isOnlyExtractedAfterSample(Value* SampleInst, SmallVector<Instruction*, 4> & ExtrVals)
{
    SmallVector<Instruction*, 4> TmpExtrVals;
    for (auto* Use : SampleInst->users())
    {
        if (auto * EI = dyn_cast<ExtractElementInst>(Use))
        {
            TmpExtrVals.push_back(EI);
        }
        else
        {
            return false;
        }
    }
    for (auto val : TmpExtrVals)
    {
        ExtrVals.push_back(val);
    }
    return true;
}

bool SampleMultiversioning::isOnlyMultiplied(Instruction* Sample, Instruction* Val, SmallSet<Instruction*, 4> & MulVals)
{
    SmallSet<Instruction*, 4> TmpMulVals;
    for (auto* Use2 : Val->users())
    {
        if (auto * BOP = dyn_cast<BinaryOperator>(Use2))
        {
            if (BOP->getOpcode() == Instruction::FMul)
            {
                Value* Dep = BOP->getOperand(1);
                if (Dep == Val)
                {
                    Dep = BOP->getOperand(0);
                }

                // Multiplication by a constant is not supported
                if (isa<Constant>(Dep))
                {
                    return false;
                }

                // Make sure multiplied value dominates the Sample
                auto DepI = dyn_cast<Instruction>(Dep);
                if (Sample->getParent() != DepI->getParent())
                {
                    if (!DT->dominates(DepI->getParent(), Sample->getParent()))
                    {
                        return false;
                    }
                }

                TmpMulVals.insert(DepI);
            }
            else
            {
                return false;
            }

        }
        else
        {
            return false;
        }
    }
    for (auto val : TmpMulVals)
    {
        MulVals.insert(val);
    }
    return true;
};

// @TODO
// Eliminate recursion

Instruction* SampleMultiversioning::getPureFunction(Value* Val)
{
    if (auto I = dyn_cast<Instruction>(Val))
    {
        if (isa<UnaryInstruction>(I) || isa<BinaryOperator>(I) ||
            isa<CmpInst>(I) || isa<ExtractElementInst>(I) || isa<CastInst>(I) ||
            isa<PHINode>(I) || IsMathIntrinsic(GetOpCode(I)) ||
            isa<SelectInst>(I))
        {
            return I;
        }
    }
    return nullptr;
};

bool SampleMultiversioning::isOnlyMultipliedAfterSample(Instruction* Val,
    SmallSet<Instruction*, 4> & MulVals)
{
    SmallSet<Instruction*, 4> TmpMulVals;

    SmallVector<Instruction*, 4> ExtrVals;
    if (isOnlyExtractedAfterSample(Val, ExtrVals))
    {
        for (auto ExtrVal : ExtrVals)
        {
            if (!isOnlyMultiplied(Val, ExtrVal, TmpMulVals))
            {
                return false;
            }
        }
    }
    else
    {
        return false;
    }
    for (auto val : TmpMulVals)
    {
        MulVals.insert(val);
    }
    return true;
};

bool SampleMultiversioning::runOnFunction(Function& F)
{
    DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();

    struct MulAfterSample
    {
        Instruction* Sample;
        SmallSet<Instruction*, 4> MulVals;
    };
    SmallVector<MulAfterSample, 4> SampleInsts;

    SmallSet<Instruction*, 4> TmpMulVals;
    SmallSet<Instruction*, 4> MulVals;
    for (BasicBlock& BB : F)
    {
        for (Instruction& Inst : BB)
        {
            if (isSampleLoadGather4InfoInstruction(&Inst) ||
                isa<LdRawIntrinsic>(Inst))
            {
                if (isOnlyMultipliedAfterSample(&Inst, TmpMulVals))
                {
                    for (auto Val : TmpMulVals)
                    {
                        MulVals.insert(Val);
                    }
                    SampleInsts.push_back({ &Inst, MulVals });
                    MulVals.clear();
                }
                TmpMulVals.clear();
            }
        }
    }

    if (SampleInsts.size() < 4)
    {
        // @TODO
        // bitcast after sample
        // And instead of Mul
        // Reorder instructions if sample dominates the dependendency
        for (auto& SI : SampleInsts)
        {
            Instruction* Sample = SI.Sample;
            BasicBlock* Parent = Sample->getParent();
            IGC_ASSERT(SI.MulVals.size());

            // Check if some multipliers are redundant or duplicated
            SmallSet<Instruction*, 4> ToRemove;
            for (auto CurrMulVal : SI.MulVals)
            {
                if (Instruction * dep = dyn_cast<Instruction>(CurrMulVal))
                {
                    if (dep->getOpcode() == Instruction::FMul)
                    {
                        SmallVector<Instruction*, 4> ToCheck;
                        ToCheck.push_back(dep);
                        while (!ToCheck.empty())
                        {
                            Instruction* Current = ToCheck.pop_back_val();

                            Instruction* op0 = dyn_cast<Instruction>(Current->getOperand(0));
                            if (op0 && (SI.MulVals.find(op0) != SI.MulVals.end()))
                            {
                                ToRemove.insert(dep);
                                break;
                            }

                            Instruction* op1 = dyn_cast<Instruction>(Current->getOperand(1));
                            if (op1 && (SI.MulVals.find(op1) != SI.MulVals.end()))
                            {
                                ToRemove.insert(dep);
                                break;
                            }

                            if (op0 && op0->getOpcode() == Instruction::FMul)
                            {
                                ToCheck.push_back(op0);
                            }

                            if (op1 && op1->getOpcode() == Instruction::FMul)
                            {
                                ToCheck.push_back(op1);
                            }
                        }
                    }
                }
            }

            // Erase redundant multipliers
            for (auto remInsn : ToRemove)
            {
                SI.MulVals.erase(remInsn);
            }

            bool skipOpt = false;
            // Consider instructions after sample blocking optimization for hoisting
            SmallVector<Instruction*, 4> toCheckUses;
            SmallSet<Instruction*, 4> toHoist;
            for (auto val : SI.MulVals)
            {
                if (DT->dominates(SI.Sample, val))
                {
                    if (IGC_IS_FLAG_DISABLED(EnableSMRescheduling))
                        skipOpt = true;
                    toHoist.insert(val);
                    toCheckUses.push_back(val);
                }
            }

           // Consider hoisting some instruction after the sample if they block opt.
            while (toCheckUses.size() > 0)
            {
                Instruction* checkVal = toCheckUses.pop_back_val();
                for (auto op = checkVal->op_begin(); op != checkVal->op_end(); ++op)
                {
                    Value* val = op->get();
                    Instruction* val_insn = dyn_cast<Instruction>(val);
                    if (val_insn)
                    {
                        if ((val_insn->getParent() != Parent) || DT->dominates(val_insn, SI.Sample))
                        {
                            continue;
                        }
                        else
                        {
                            for (auto tempSI : SampleInsts)
                            {
                                // Corner case where two samplers can be branched for one Mul.
                                if (val_insn == tempSI.Sample)
                                    skipOpt = true;
                            }
                            toHoist.insert(val_insn);
                            toCheckUses.push_back(val_insn);
                        }
                    }
                }
            }

            if (skipOpt)
                continue;

            int hoistSize = toHoist.size();
            Instruction * iterInsn = SI.Sample->getNextNode();

            while (hoistSize > 0)
            {
                Instruction* tempInsn = iterInsn;
                iterInsn = iterInsn->getNextNode();
                if (toHoist.find(tempInsn) != toHoist.end())
                {
                    tempInsn->moveBefore(SI.Sample);
                    hoistSize--;
                }
            }

            BasicBlock* BB1 = Parent->splitBasicBlock(Sample);
            Parent->getTerminator()->eraseFromParent();
            BasicBlock* BB2 =
                Sample->getParent()->splitBasicBlock(Sample->getNextNode());

            PHINode* Phi;
            IRBuilder<> PHIBuilder(BB2);
            PHIBuilder.SetInsertPoint(&*BB2->begin());
            Phi = PHIBuilder.CreatePHI(Sample->getType(), 2);
            Sample->replaceAllUsesWith(Phi);

            IRBuilder<> AndBuilder(Parent);

            Value* PrevCmp = nullptr;
            Value* And = nullptr;
            for (auto Dep : SI.MulVals)
            {
                auto Cmp = AndBuilder.CreateFCmp(
                    CmpInst::Predicate::FCMP_OEQ, Dep,
                    ConstantFP::get(F.getContext(), APFloat(0.0f)));
                if (PrevCmp)
                {
                    And = AndBuilder.CreateAnd(PrevCmp, Cmp);
                }
                PrevCmp = Cmp;
            }

            if (!And)
            {
                And = PrevCmp;
            }
            AndBuilder.CreateCondBr(And, BB2, Sample->getParent());

            Phi->addIncoming(Sample, BB1);
            Phi->addIncoming(ConstantVector::get({
                               ConstantFP::get(F.getContext(), APFloat(0.0f)),
                               ConstantFP::get(F.getContext(), APFloat(0.0f)),
                               ConstantFP::get(F.getContext(), APFloat(0.0f)),
                               ConstantFP::get(F.getContext(), APFloat(0.0f)),
                }),
                Parent);
            DT->recalculate(F);
        }
        pContext->m_instrTypes.hasMultipleBB = true;
        return true;
    }
    return false;
}