File: LowPrecisionOptPass.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 (400 lines) | stat: -rw-r--r-- 13,539 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "Compiler/LowPrecisionOptPass.hpp"
#include "Compiler/CodeGenContextWrapper.hpp"
#include "Compiler/CodeGenPublic.h"
#include "Compiler/CISACodeGen/helper.h"
#include "Compiler/IGCPassSupport.h"

#include "llvmWrapper/IR/DerivedTypes.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/Support/CommandLine.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/InstIterator.h>
#include "common/LLVMWarningsPop.hpp"

#include "GenISAIntrinsics/GenIntrinsicInst.h"
#include "common/IGCIRBuilder.h"
#include "llvmWrapper/IR/Instructions.h"

using namespace llvm;
using namespace IGC;
using namespace IGC::IGCMD;
using namespace GenISAIntrinsic;

char LowPrecisionOpt::ID = 0;

// Register pass to igc-opt
#define PASS_FLAG "igc-low-precision-opt"
#define PASS_DESCRIPTION "Low Precision Opt"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(LowPrecisionOpt, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
IGC_INITIALIZE_PASS_END(LowPrecisionOpt, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

LowPrecisionOpt::LowPrecisionOpt() : FunctionPass(ID)
{
    initializeLowPrecisionOptPass(*PassRegistry::getPassRegistry());
    m_func_llvm_GenISA_DCL_inputVec_f16 = nullptr;
    m_func_llvm_GenISA_DCL_inputVec_f32 = nullptr;
    m_currFunction = nullptr;
    func_llvm_floor_f32 = nullptr;
}

bool LowPrecisionOpt::runOnFunction(Function& F)
{
    m_changed = false;
    CodeGenContextWrapper* pCtxWrapper = &getAnalysis<CodeGenContextWrapper>();
    CodeGenContext* ctx = pCtxWrapper->getCodeGenContext();

    MetaDataUtils* pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();

    if (pMdUtils->findFunctionsInfoItem(&F) == pMdUtils->end_FunctionsInfo())
    {
        return m_changed;
    }
    llvm::IGCIRBuilder<> builder(F.getContext());
    m_builder = &builder;
    m_currFunction = &F;
    shdrType = ctx->type;
    bundles.clear();
    m_simplifyAlu = true;
    m_changeSample = false;
    visit(F);
    // change sampler only after we simplified fext + ftrunc
    m_simplifyAlu = false;
    m_changeSample = true;
    visit(F);
    std::sort(bundles.begin(), bundles.end(), cmpOperator);
    auto bundleEnd = bundles.end();
    for (auto bundle = bundles.begin(); bundle != bundleEnd; ++bundle)
    {
        (*bundle).cInst->moveBefore(&(*(m_currFunction->getEntryBlock().begin())));
        (*bundle).fpTrunc->moveBefore(&(*(m_currFunction->getEntryBlock().begin())));
    }
    return m_changed;
}

void LowPrecisionOpt::visitFPExtInst(llvm::FPExtInst& I)
{
    if (!m_simplifyAlu)
    {
        return;
    }
    if (I.getOperand(0)->getType()->isHalfTy())
    {
        llvm::GenIntrinsicInst* callInst = llvm::dyn_cast<llvm::GenIntrinsicInst>(I.getOperand(0));

        if (callInst && callInst->hasOneUse())
        {
            GenISAIntrinsic::ID ID = callInst->getIntrinsicID();
            if (ID == GenISAIntrinsic::GenISA_DCL_ShaderInputVec || ID == GenISAIntrinsic::GenISA_DCL_inputVec)
            {
                /*
                Catches a pattern where we have a lowp input, then extend it back up. This
                generates mixed mode instructions and so it's better to keep it as PLN.
                Example if it's used directly in the sample instruction before CNL.
                */

                if (m_func_llvm_GenISA_DCL_inputVec_f32 == nullptr)
                {
                    m_func_llvm_GenISA_DCL_inputVec_f32 = llvm::GenISAIntrinsic::getDeclaration(
                        m_currFunction->getParent(),
                        ID,
                        Type::getFloatTy(m_builder->getContext()));
                }

                m_builder->SetInsertPoint(callInst);
                Value* v = m_builder->CreateCall2(m_func_llvm_GenISA_DCL_inputVec_f32, callInst->getOperand(0), callInst->getOperand(1));
#if VALUE_NAME_ENABLE
                v->setName(callInst->getName());
#endif
                I.replaceAllUsesWith(v);
                I.eraseFromParent();
                callInst->eraseFromParent();
                m_changed = true;
            }
        }
    }
}

void LowPrecisionOpt::visitFPTruncInst(llvm::FPTruncInst& I)
{
    if (!m_simplifyAlu)
    {
        return;
    }
    llvm::GenIntrinsicInst* cInst = llvm::dyn_cast<llvm::GenIntrinsicInst>(I.getOperand(0));

    if (cInst &&
        cInst->getIntrinsicID() == GenISAIntrinsic::GenISA_RuntimeValue)
    {
        if (!IGC_IS_FLAG_ENABLED(HoistPSConstBufferValues) ||
            shdrType != ShaderType::PIXEL_SHADER)
            return;
        moveBundle bundle;
        bundle.index = (uint)llvm::cast<llvm::ConstantInt>(cInst->getOperand(0))->getZExtValue();
        bundle.cInst = cInst;
        bundle.fpTrunc = &I;
        bundles.push_back(bundle);
    }
}

// If all the uses of a sampler instruction are converted to a different floating point type
// try to propagate the type in the sampler
bool LowPrecisionOpt::propagateSamplerType(llvm::GenIntrinsicInst& I)
{
    if (IGC_IS_FLAG_DISABLED(UpConvertF16Sampler) && cast<VectorType>(I.getType())->getElementType()->isHalfTy())
    {
        return false;
    }

    IGC::CodeGenContext& CGContext = *getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
    if (!CGContext.platform.supportFP16())
    {
        return false;
    }

    Type* eltTy = NULL;
    bool isFloatType = false;

    if (I.getType()->isVectorTy())
    {
        eltTy = cast<VectorType>(I.getType())->getElementType();
        isFloatType = cast<VectorType>(I.getType())->getElementType()->isFloatTy();
    }
    else
    {
        eltTy = I.getType();
        isFloatType = I.getType()->isFloatTy();
    }

    Type* newDstType = nullptr;
    if (eltTy->isFloatingPointTy())
    {
        // check that all uses are extractelement followed by fpext
        newDstType = isFloatType ?
            m_builder->getHalfTy() : m_builder->getFloatTy();
        for (auto use = I.user_begin(); use != I.user_end(); ++use)
        {
            auto extractElt = dyn_cast<ExtractElementInst>(*use);
            if (!(extractElt && extractElt->hasOneUse()))
            {
                return false;
            }
            auto fpExtOrTrunc = dyn_cast<CastInst>(*extractElt->user_begin());

            if (!(fpExtOrTrunc && fpExtOrTrunc->getType() == newDstType))
            {
                return false;
            }
        }
    }
    else if (eltTy == m_builder->getInt32Ty())
    {
        // check if we can lower the sampler return to 16-bit
        newDstType = m_builder->getInt16Ty();
        for (auto use = I.user_begin(); use != I.user_end(); ++use)
        {
            auto extractElt = dyn_cast<ExtractElementInst>(*use);
            if (!(extractElt && extractElt->hasOneUse()))
            {
                return false;
            }
            auto isUpperBitClear = [this](User* U)
            {
                // match the pattern
                // %scalar59 = extractelement <4 x i32> % 83, i32 3
                // % 84 = and i32 %scalar59, 65535
                if (U->getType() != m_builder->getInt32Ty())
                {
                    return false;
                }
                auto andInst = dyn_cast<BinaryOperator>(U);
                if (!andInst || andInst->getOpcode() != BinaryOperator::And)
                {
                    return false;
                }
                auto andSrc1 = dyn_cast<ConstantInt>(andInst->getOperand(1));
                if (!andSrc1 || andSrc1->getZExtValue() != 0xFFFF)
                {
                    return false;
                }
                return true;
            };

            auto Use = *extractElt->user_begin();
            bool isInt32to16Trunc = dyn_cast<TruncInst>(Use) && Use->getType() == m_builder->getInt16Ty();
            if (!isInt32to16Trunc && !isUpperBitClear(Use))
            {
                return false;
            }
        }
    }
    else
    {
        return false;
    }

    unsigned int numberOfElements = 1;

    if (I.getType()->isVectorTy())
    {
        numberOfElements = int_cast<unsigned int>(cast<IGCLLVM::FixedVectorType>(I.getType())->getNumElements());
    }

    llvm::SmallVector<llvm::Type*, 4> overloadTys;
    auto retTy = IGCLLVM::FixedVectorType::get(newDstType, numberOfElements);
    overloadTys.push_back(retTy);
    auto ID = I.getIntrinsicID();
    switch (ID)
    {
    case GenISAIntrinsic::GenISA_sampleptr:
    case GenISAIntrinsic::GenISA_sampleBptr:
    case GenISAIntrinsic::GenISA_sampleCptr:
    case GenISAIntrinsic::GenISA_sampleDptr:
    case GenISAIntrinsic::GenISA_sampleDCptr:
    case GenISAIntrinsic::GenISA_sampleLptr:
    case GenISAIntrinsic::GenISA_sampleLCptr:
    case GenISAIntrinsic::GenISA_sampleBCptr:
        // 4 overloaded tys: ret, arg0, resource, sampler
        overloadTys.push_back(I.getArgOperand(0)->getType());
        overloadTys.push_back(cast<SampleIntrinsic>(&I)->getTextureValue()->getType());
        overloadTys.push_back(cast<SampleIntrinsic>(&I)->getSamplerValue()->getType());
        break;
    case GenISAIntrinsic::GenISA_ldptr:
        overloadTys.push_back(cast<SamplerLoadIntrinsic>(&I)->getTextureValue()->getType());
        break;
    case GenISAIntrinsic::GenISA_ldmsptr:
        overloadTys.push_back(cast<SamplerLoadIntrinsic>(&I)->getTextureValue()->getType());
        break;
    case GenISAIntrinsic::GenISA_gather4ptr:
    case GenISAIntrinsic::GenISA_gather4Cptr:
    case GenISAIntrinsic::GenISA_gather4POptr:
    case GenISAIntrinsic::GenISA_gather4POCptr:
        // 4 overloaded tys: ret, arg0, resource, sampler
        overloadTys.push_back(I.getArgOperand(0)->getType());
        overloadTys.push_back(cast<SamplerGatherIntrinsic>(&I)->getTextureValue()->getType());
        overloadTys.push_back(cast<SamplerGatherIntrinsic>(&I)->getSamplerValue()->getType());
        break;
    default:
        return false;
    }

    Function* newSample = GenISAIntrinsic::getDeclaration(
        m_currFunction->getParent(), I.getIntrinsicID(), overloadTys);
    llvm::SmallVector<llvm::Value*, 8> newArgs;
    for (unsigned int i = 0, argSize = IGCLLVM::getNumArgOperands(&I); i < argSize; i++)
    {
        newArgs.push_back(I.getArgOperand(i));
    }
    m_builder->SetInsertPoint(&I);
    auto newCall = m_builder->CreateCall(newSample, newArgs);

    for (auto use = I.user_begin(); use != I.user_end(); ++use)
    {
        ExtractElementInst* extractElt = cast<ExtractElementInst>(*use);
        m_builder->SetInsertPoint(extractElt);

        Value* extractUse = *extractElt->user_begin();
        Value* newExtract = m_builder->CreateExtractElement(newCall, extractElt->getIndexOperand());
        if (extractUse->getType()->isFloatingPointTy())
        {
            extractUse->replaceAllUsesWith(newExtract);
        }
        else
        {
            if (dyn_cast<TruncInst>(extractUse))
            {
                // replace trunc with new extractElt
                extractUse->replaceAllUsesWith(newExtract);
            }
            else
            {
                // replace and with zext
                Value* zextInst = m_builder->CreateZExt(newExtract, m_builder->getInt32Ty());
                extractUse->replaceAllUsesWith(zextInst);
            }
        }
    }
    return true;
}

void LowPrecisionOpt::visitIntrinsicInst(llvm::IntrinsicInst& I)
{
    if (!m_simplifyAlu)
    {
        return;
    }
    if (I.getIntrinsicID() != llvm::Intrinsic::floor ||
        I.getType() != Type::getHalfTy(m_builder->getContext()))
        return;

    auto src = I.getOperand(0);
    m_builder->SetInsertPoint(&I);

    auto fpTrunc = llvm::dyn_cast <llvm::FPTruncInst>(src);
    if (fpTrunc)
    {
        src = fpTrunc->getOperand(0);
    }
    else
    {
        src = m_builder->CreateFPExt(src, m_builder->getFloatTy());
    }

    if (!func_llvm_floor_f32)
        func_llvm_floor_f32 = llvm::Intrinsic::getDeclaration(m_currFunction->getParent(), Intrinsic::floor, m_builder->getFloatTy());

    auto floor32 = m_builder->CreateCall(func_llvm_floor_f32, src);
#if VALUE_NAME_ENABLE
    floor32->setName(I.getName());
#endif

    if (I.hasOneUse())
    {
        auto hfSub = llvm::dyn_cast<llvm::BinaryOperator>(*I.user_begin());

        if (hfSub && hfSub->getOpcode() == llvm::Instruction::BinaryOps::FSub)
        {
            if (hfSub->getOperand(0) == I.getOperand(0))
            {
                auto fSub = m_builder->CreateFSub(src, floor32, hfSub->getName());
                auto fpdst = m_builder->CreateFPTrunc(fSub, Type::getHalfTy(m_builder->getContext()));
                hfSub->replaceAllUsesWith(fpdst);
            }
        }
    }
    else
    {
        auto fpdst = m_builder->CreateFPTrunc(floor32, Type::getHalfTy(m_builder->getContext()));
        I.replaceAllUsesWith(fpdst);
        I.eraseFromParent();
    }

}

/*FP16SamplerOptimization*/
void LowPrecisionOpt::visitCallInst(CallInst& I)
{
    if (!m_changeSample)
    {
        return;
    }
    if (isSampleLoadGather4InfoInstruction(&I))
    {
        bool changed = propagateSamplerType(*cast<GenIntrinsicInst>(&I));
        if (changed)
        {
            return;
        }
    }
}