File: ScalarizerCodeGen.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 (154 lines) | stat: -rw-r--r-- 6,689 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "Compiler/IGCPassSupport.h"
#include "Compiler/InitializePasses.h"
#include "common/Types.hpp"
#include "ScalarizerCodeGen.hpp"
#include "llvmWrapper/IR/DerivedTypes.h"

using namespace llvm;
using namespace IGC;

#define PASS_FLAG "igc-scalarizer-in-codegen"
#define PASS_DESCRIPTION "Scalarizer in codegen"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(ScalarizerCodeGen, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(ScalarizerCodeGen, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

char ScalarizerCodeGen::ID = 0;

#define DEBUG_TYPE "ScalarizerCodeGen"

ScalarizerCodeGen::ScalarizerCodeGen() : FunctionPass(ID)
{
    initializeScalarizerCodeGenPass(*PassRegistry::getPassRegistry());
}


bool ScalarizerCodeGen::runOnFunction(Function& F)
{
    llvm::IRBuilder<> builder(F.getContext());
    m_builder = &builder;

    visit(F);
    return false;
}

void ScalarizerCodeGen::visitBinaryOperator(llvm::BinaryOperator& I)
{
    // Scalarizing vector type And/Or instructions
    if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or || I.getOpcode() == Instruction::Xor)
    {
        if (I.getType()->isVectorTy())
        {
            bool isNewTypeVector = false;

            IGCLLVM::FixedVectorType* instType = cast<IGCLLVM::FixedVectorType>(I.getType());
            unsigned numElements = int_cast<unsigned>(instType->getNumElements());
            unsigned scalarSize = instType->getScalarSizeInBits();
            unsigned newScalarBits = numElements * scalarSize;
            Type* newType = nullptr;
            //  Check if the operands can be bitcasted to types int8/16/32
            if (newScalarBits == 8)
                newType = m_builder->getInt8Ty();
            else if (newScalarBits == 16)
                newType = m_builder->getInt16Ty();
            else if (newScalarBits == 32)
                newType = m_builder->getInt32Ty();
            else
            {
                // Check the suitable vector type to cast to, inorder to minimize the number of instructions
                isNewTypeVector = true;
                if (newScalarBits % 32 == 0)
                    newType = IGCLLVM::FixedVectorType::get(m_builder->getInt32Ty(), newScalarBits / 32);
                else if (newScalarBits % 16 == 0)
                    newType = IGCLLVM::FixedVectorType::get(m_builder->getInt16Ty(), newScalarBits / 16);
                else if (newScalarBits % 8 == 0)
                    newType = IGCLLVM::FixedVectorType::get(m_builder->getInt8Ty(), newScalarBits / 8);
                else
                    isNewTypeVector = false;
            }

            if (newType)
            {
                Value* src0 = I.getOperand(0);
                Value* src1 = I.getOperand(1);
                auto logicOp = I.getOpcode();
                m_builder->SetInsertPoint(&I);
                // bitcast the operands to new type
                Value* castedSrc0 = m_builder->CreateBitCast(src0, newType);
                Value* castedSrc1 = m_builder->CreateBitCast(src1, newType);
                Value* newBitCastInst;

                // Generate scalar logic operations, and then bitcast the result to a vector type
                if (!isNewTypeVector)
                {
                    Value* newLogicInst = m_builder->CreateBinOp(logicOp, castedSrc0, castedSrc1);
                    newBitCastInst = m_builder->CreateBitCast(newLogicInst, instType);
                }
                else
                {
                    IGCLLVM::FixedVectorType* newVecType = cast<IGCLLVM::FixedVectorType>(newType);
                    unsigned newVecTypeNumEle = int_cast<unsigned>(newVecType->getNumElements());
                    Value* ieLogicOp = UndefValue::get(newType);
                    for (unsigned i = 0; i < newVecTypeNumEle; i++)
                    {
                        Value* constIndex = ConstantInt::get(m_builder->getInt32Ty(), i);
                        Value* eeSrc0 = m_builder->CreateExtractElement(castedSrc0, constIndex);
                        Value* eeSrc1 = m_builder->CreateExtractElement(castedSrc1, constIndex);
                        Value* newLogicInst = m_builder->CreateBinOp(logicOp, eeSrc0, eeSrc1);
                        ieLogicOp = m_builder->CreateInsertElement(ieLogicOp, newLogicInst, constIndex);
                    }
                    newBitCastInst = m_builder->CreateBitCast(ieLogicOp, instType);
                }
                // Now replace all the instruction users with the newly bitcasted Logic Instruction
                I.replaceAllUsesWith(newBitCastInst);
                I.eraseFromParent();
            }
        }
    }
}

void ScalarizerCodeGen::visitCastInst(llvm::CastInst& I)
{
    // Scalarizing vector type Trunc/Ext instructions
    if (I.getOpcode() == Instruction::Trunc || I.getOpcode() == Instruction::ZExt || I.getOpcode() == Instruction::SExt)
    {
        if (I.getType()->isVectorTy())
        {
            IGCLLVM::FixedVectorType* instType = cast<IGCLLVM::FixedVectorType>(I.getType());
            unsigned numElements = int_cast<unsigned>(instType->getNumElements());
            Type* dstType = instType->getScalarType();
            Value* src0 = I.getOperand(0);
            auto castOp = I.getOpcode();
            m_builder->SetInsertPoint(&I);

            Value* lastOp = UndefValue::get(instType);
            for (unsigned i = 0; i < numElements; i++)
            {
                Value* constIndex = ConstantInt::get(m_builder->getInt32Ty(), i);
                Value* eeSrc0 = m_builder->CreateExtractElement(src0, constIndex);
                Value* newCastInst = nullptr;
                switch (castOp)
                {
                    case Instruction::Trunc: newCastInst = m_builder->CreateTrunc(eeSrc0, dstType); break;
                    case Instruction::ZExt: newCastInst = m_builder->CreateZExt(eeSrc0, dstType); break;
                    case Instruction::SExt: newCastInst = m_builder->CreateSExt(eeSrc0, dstType); break;
                    default: IGC_ASSERT(0);
                }
                lastOp = m_builder->CreateInsertElement(lastOp, newCastInst, constIndex);
            }

            // Now replace all the instruction users with the newly created instruction
            I.replaceAllUsesWith(lastOp);
            I.eraseFromParent();
        }
    }
}