Skip to content

Commit fe771f7

Browse files
skachkov-inteligcbot
authored andcommitted
Small refactor in SIMDCF Conformance pass
Use IRBuilder for goto and join lowering in SimdCFConformance pass
1 parent 99eb18a commit fe771f7

File tree

1 file changed

+41
-66
lines changed

1 file changed

+41
-66
lines changed

IGC/VectorCompiler/lib/GenXCodeGen/GenXSimdCFConformance.cpp

Lines changed: 41 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,9 @@ class GenXSimdCFConformance {
395395
BasicBlock *PhiPredBlock = nullptr);
396396
Value *lowerPHIUse(PHINode *PN, SetVector<Value *> &ToRemove);
397397
Value *lowerArgumentUse(Argument *Arg);
398-
Value *insertCond(Value *OldVal, Value *NewVal, const Twine &Name, Instruction *InsertBefore, const DebugLoc &DL);
399-
Value *truncateCond(Value *In, Type *Ty, const Twine &Name, Instruction *InsertBefore, const DebugLoc &DL);
398+
Value *insertCond(Value *OldVal, Value *NewVal, const Twine &Name,
399+
IRBuilder<> &IRB);
400+
Value *truncateCond(Value *In, Type *Ty, const Twine &Name, IRBuilder<> &IRB);
400401
void lowerGoto(CallInst *Goto);
401402
void lowerJoin(CallInst *Join);
402403
void replaceGotoJoinUses(CallInst *GotoJoin, ArrayRef<Value *> Vals);
@@ -3484,8 +3485,7 @@ void GenXSimdCFConformance::checkInterference(SetVector<SimpleValue> *Vals,
34843485
* Return: value, possibly the same as the input value
34853486
*/
34863487
Value *GenXSimdCFConformance::insertCond(Value *OldVal, Value *NewVal,
3487-
const Twine &Name, Instruction *InsertBefore, const DebugLoc &DL)
3488-
{
3488+
const Twine &Name, IRBuilder<> &IRB) {
34893489
unsigned OldWidth =
34903490
cast<IGCLLVM::FixedVectorType>(OldVal->getType())->getNumElements();
34913491
unsigned NewWidth =
@@ -3497,27 +3497,24 @@ Value *GenXSimdCFConformance::insertCond(Value *OldVal, Value *NewVal,
34973497
// GenXLowering decides whether this is suitable to lower to wrpredregion, or
34983498
// needs to be lowered to something less efficient.
34993499
SmallVector<Constant *, 32> Indices;
3500-
Type *I32Ty = Type::getInt32Ty(InsertBefore->getContext());
35013500
unsigned i;
35023501
for (i = 0; i != NewWidth; ++i)
3503-
Indices.push_back(ConstantInt::get(I32Ty, i));
3504-
auto UndefIndex = UndefValue::get(I32Ty);
3502+
Indices.push_back(IRB.getInt32(i));
3503+
auto UndefIndex = UndefValue::get(IRB.getInt32Ty());
35053504
for (; i != OldWidth; ++i)
35063505
Indices.push_back(UndefIndex);
3507-
auto SV1 = new ShuffleVectorInst(NewVal, UndefValue::get(NewVal->getType()),
3508-
ConstantVector::get(Indices), NewVal->getName() + ".extend", InsertBefore);
3509-
SV1->setDebugLoc(DL);
3506+
auto SV = IRB.CreateShuffleVector(NewVal, UndefValue::get(NewVal->getType()),
3507+
ConstantVector::get(Indices),
3508+
NewVal->getName() + ".extend");
35103509
if (isa<UndefValue>(OldVal))
3511-
return SV1;
3510+
return SV;
35123511
Indices.clear();
35133512
for (i = 0; i != NewWidth; ++i)
3514-
Indices.push_back(ConstantInt::get(I32Ty, i + OldWidth));
3513+
Indices.push_back(IRB.getInt32(i + OldWidth));
35153514
for (; i != OldWidth; ++i)
3516-
Indices.push_back(ConstantInt::get(I32Ty, i));
3517-
auto SV2 = new ShuffleVectorInst(OldVal, SV1, ConstantVector::get(Indices),
3518-
Name, InsertBefore);
3519-
SV2->setDebugLoc(DL);
3520-
return SV2;
3515+
Indices.push_back(IRB.getInt32(i));
3516+
return IRB.CreateShuffleVector(OldVal, SV, ConstantVector::get(Indices),
3517+
Name);
35213518
}
35223519

35233520
/***********************************************************************
@@ -3532,23 +3529,19 @@ Value *GenXSimdCFConformance::insertCond(Value *OldVal, Value *NewVal,
35323529
* Return: value, possibly the same as the input value
35333530
*/
35343531
Value *GenXSimdCFConformance::truncateCond(Value *In, Type *Ty,
3535-
const Twine &Name, Instruction *InsertBefore, const DebugLoc &DL)
3536-
{
3532+
const Twine &Name,
3533+
IRBuilder<> &IRB) {
35373534
unsigned InWidth =
35383535
cast<IGCLLVM::FixedVectorType>(In->getType())->getNumElements();
35393536
unsigned TruncWidth = cast<IGCLLVM::FixedVectorType>(Ty)->getNumElements();
35403537
if (InWidth == TruncWidth)
35413538
return In;
35423539
// Do the truncate with shufflevector. GenXLowering lowers it to rdpredregion.
3543-
SmallVector<Constant *, 32> Indices;
3544-
Type *I32Ty = Type::getInt32Ty(InsertBefore->getContext());
3545-
unsigned i;
3546-
for (i = 0; i != TruncWidth; ++i)
3547-
Indices.push_back(ConstantInt::get(I32Ty, i));
3548-
auto SV = new ShuffleVectorInst(In, UndefValue::get(In->getType()),
3549-
ConstantVector::get(Indices), Name, InsertBefore);
3550-
SV->setDebugLoc(DL);
3551-
return SV;
3540+
SmallVector<Constant *, 32> Indices(TruncWidth);
3541+
for (unsigned i = 0; i < TruncWidth; ++i)
3542+
Indices.push_back(IRB.getInt32(i));
3543+
return IRB.CreateShuffleVector(In, UndefValue::get(In->getType()),
3544+
ConstantVector::get(Indices), Name);
35523545
}
35533546

35543547
/***********************************************************************
@@ -3562,44 +3555,33 @@ Value *GenXSimdCFConformance::truncateCond(Value *In, Type *Ty,
35623555
void GenXSimdCFConformance::lowerGoto(CallInst *Goto)
35633556
{
35643557
LLVM_DEBUG(dbgs() << "lowerGoto: " << *Goto << "\n");
3565-
const DebugLoc &DL = Goto->getDebugLoc();
3558+
IRBuilder<> IRB(Goto);
35663559
if (EnableGenXGotoJoin && !lowerSimdCF)
35673560
DiagnosticInfoSimdCF::emit(Goto, "failed to optimize SIMD branch", DS_Warning);
35683561
Value *Results[3];
35693562
auto EM = Goto->getOperand(0);
35703563
auto Cond = Goto->getOperand(2);
35713564
// EM is always 32 bit. Extract SubEM, of the same width as Cond, from it.
3572-
auto OldSubEM = truncateCond(EM, Cond->getType(),
3573-
EM->getName() + ".sub", Goto, DL);
3565+
auto OldSubEM =
3566+
truncateCond(EM, Cond->getType(), EM->getName() + ".sub", IRB);
35743567
// Result 1: NewRM = OldRM | (SubEM & ~Cond)
3575-
auto NotCond = BinaryOperator::Create(Instruction::Xor, Cond,
3576-
Constant::getAllOnesValue(Cond->getType()),
3577-
Goto->getName() + ".notcond", Goto);
3578-
NotCond->setDebugLoc(DL);
3579-
auto NotCondAndSubEM = BinaryOperator::Create(Instruction::And, NotCond,
3580-
OldSubEM, Goto->getName() + ".disabling", Goto);
3581-
NotCondAndSubEM->setDebugLoc(DL);
3568+
auto NotCond = IRB.CreateXor(Cond, Constant::getAllOnesValue(Cond->getType()),
3569+
Goto->getName() + ".notcond");
3570+
auto NotCondAndSubEM =
3571+
IRB.CreateAnd(NotCond, OldSubEM, Goto->getName() + ".disabling");
35823572
Value *OldRM = Goto->getArgOperand(1);
3583-
auto NewRM = BinaryOperator::Create(Instruction::Or, OldRM, NotCondAndSubEM,
3584-
Goto->getName() + ".newRM", Goto);
3585-
NewRM->setDebugLoc(DL);
3573+
auto NewRM = IRB.CreateOr(OldRM, NotCondAndSubEM, Goto->getName() + ".newRM");
35863574
Results[1] = NewRM;
35873575
// And SubEM with Cond.
3588-
auto SubEM = BinaryOperator::Create(Instruction::And, OldSubEM, Cond,
3589-
Goto->getName() + ".subEM", Goto);
3590-
SubEM->setDebugLoc(DL);
3576+
auto SubEM = IRB.CreateAnd(OldSubEM, Cond, Goto->getName() + ".subEM");
35913577
// Insert that back into EM. That is result 0.
3592-
Results[0] = EM = insertCond(EM, SubEM, Goto->getName() + ".EM", Goto, DL);
3578+
Results[0] = EM = insertCond(EM, SubEM, Goto->getName() + ".EM", IRB);
35933579
// Result 2: BranchCond = !any(SubEM)
35943580
Function *AnyFunc = GenXIntrinsic::getGenXDeclaration(M, GenXIntrinsic::genx_any,
35953581
SubEM->getType());
3596-
auto Any = CallInst::Create(AnyFunc, SubEM,
3597-
SubEM->getName() + ".any", Goto);
3598-
Any->setDebugLoc(DL);
3599-
auto Not = BinaryOperator::Create(Instruction::Xor, Any,
3600-
Constant::getAllOnesValue(Any->getType()),
3601-
Any->getName() + ".not", Goto);
3602-
Not->setDebugLoc(DL);
3582+
auto Any = IRB.CreateCall(AnyFunc, SubEM, SubEM->getName() + ".any");
3583+
auto Not = IRB.CreateXor(Any, Constant::getAllOnesValue(Any->getType()),
3584+
Any->getName() + ".not");
36033585
Results[2] = Not;
36043586
// Replace uses.
36053587
replaceGotoJoinUses(Goto, Results);
@@ -3613,29 +3595,22 @@ void GenXSimdCFConformance::lowerGoto(CallInst *Goto)
36133595
void GenXSimdCFConformance::lowerJoin(CallInst *Join)
36143596
{
36153597
LLVM_DEBUG(dbgs() << "lowerJoin: " << *Join << "\n");
3616-
const DebugLoc &DL = Join->getDebugLoc();
3598+
IRBuilder<> IRB(Join);
36173599
Value *Results[2];
36183600
auto EM = Join->getOperand(0);
36193601
auto RM = Join->getOperand(1);
36203602
// EM is always 32 bit. Extract SubEM, of the same width as RM, from it.
3621-
auto OldSubEM = truncateCond(EM, RM->getType(), EM->getName() + ".sub",
3622-
Join, DL);
3603+
auto OldSubEM = truncateCond(EM, RM->getType(), EM->getName() + ".sub", IRB);
36233604
// Or it with RM.
3624-
auto SubEM = BinaryOperator::Create(Instruction::Or, OldSubEM, RM,
3625-
Join->getName() + ".subEM", Join);
3626-
SubEM->setDebugLoc(DL);
3605+
auto SubEM = IRB.CreateOr(OldSubEM, RM, Join->getName() + ".subEM");
36273606
// Insert that back into EM. That is result 0.
3628-
Results[0] = EM = insertCond(EM, SubEM, Join->getName() + ".EM", Join, DL);
3607+
Results[0] = EM = insertCond(EM, SubEM, Join->getName() + ".EM", IRB);
36293608
// Result 1: BranchCond = !any(SubEM)
36303609
Function *AnyFunc = GenXIntrinsic::getGenXDeclaration(M, GenXIntrinsic::genx_any,
36313610
SubEM->getType());
3632-
auto Any = CallInst::Create(AnyFunc, SubEM,
3633-
SubEM->getName() + ".any", Join);
3634-
Any->setDebugLoc(DL);
3635-
auto Not = BinaryOperator::Create(Instruction::Xor, Any,
3636-
Constant::getAllOnesValue(Any->getType()),
3637-
Any->getName() + ".not", Join);
3638-
Not->setDebugLoc(DL);
3611+
auto Any = IRB.CreateCall(AnyFunc, SubEM, SubEM->getName() + ".any");
3612+
auto Not = IRB.CreateXor(Any, Constant::getAllOnesValue(Any->getType()),
3613+
Any->getName() + ".not");
36393614
Results[1] = Not;
36403615
// Replace uses.
36413616
replaceGotoJoinUses(Join, Results);

0 commit comments

Comments
 (0)