Skip to content

Commit af1ecdf

Browse files
ViacheslavRbigcbot
authored andcommitted
Adding Cast handler to CodeGen Scalarizer
Adding handler for Trunc/Zext/SExt operation to CodeGen Scalarizer.
1 parent 6f50bc2 commit af1ecdf

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

IGC/Compiler/CISACodeGen/ScalarizerCodeGen.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,40 @@ void ScalarizerCodeGen::visitBinaryOperator(llvm::BinaryOperator& I)
115115
}
116116
}
117117
}
118+
119+
void ScalarizerCodeGen::visitCastInst(llvm::CastInst& I)
120+
{
121+
// Scalarizing vector type Trunc/Ext instructions
122+
if (I.getOpcode() == Instruction::Trunc || I.getOpcode() == Instruction::ZExt || I.getOpcode() == Instruction::SExt)
123+
{
124+
if (I.getType()->isVectorTy())
125+
{
126+
IGCLLVM::FixedVectorType* instType = cast<IGCLLVM::FixedVectorType>(I.getType());
127+
unsigned numElements = int_cast<unsigned>(instType->getNumElements());
128+
Type* dstType = instType->getScalarType();
129+
Value* src0 = I.getOperand(0);
130+
auto castOp = I.getOpcode();
131+
m_builder->SetInsertPoint(&I);
132+
133+
Value* lastOp = UndefValue::get(instType);
134+
for (unsigned i = 0; i < numElements; i++)
135+
{
136+
Value* constIndex = ConstantInt::get(m_builder->getInt32Ty(), i);
137+
Value* eeSrc0 = m_builder->CreateExtractElement(src0, constIndex);
138+
Value* newCastInst = nullptr;
139+
switch (castOp)
140+
{
141+
case Instruction::Trunc: newCastInst = m_builder->CreateTrunc(eeSrc0, dstType); break;
142+
case Instruction::ZExt: newCastInst = m_builder->CreateZExt(eeSrc0, dstType); break;
143+
case Instruction::SExt: newCastInst = m_builder->CreateSExt(eeSrc0, dstType); break;
144+
default: IGC_ASSERT(0);
145+
}
146+
lastOp = m_builder->CreateInsertElement(lastOp, newCastInst, constIndex);
147+
}
148+
149+
// Now replace all the instruction users with the newly created instruction
150+
I.replaceAllUsesWith(lastOp);
151+
I.eraseFromParent();
152+
}
153+
}
154+
}

IGC/Compiler/CISACodeGen/ScalarizerCodeGen.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace IGC
3131

3232
virtual bool runOnFunction(llvm::Function& F);
3333
void visitBinaryOperator(llvm::BinaryOperator& I);
34+
void visitCastInst(llvm::CastInst& I);
3435

3536
private:
3637
llvm::IRBuilder<>* m_builder;

0 commit comments

Comments
 (0)