Skip to content

Commit 657fcc9

Browse files
mmereckiigcbot
authored andcommitted
Update PatternMatch to genreate saturation more often
Detect more cases when `.sat` qualifier can be used
1 parent 3b7490e commit 657fcc9

File tree

4 files changed

+100
-11
lines changed

4 files changed

+100
-11
lines changed

IGC/Compiler/CISACodeGen/EmitVISAPass.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ void EmitPass::EmitSimpleAlu(Instruction* inst, CVariable* dst, CVariable* src0,
23912391
EmitSimpleAlu(GetOpCode(inst), dst, src0, src1);
23922392
}
23932393

2394-
void EmitPass::EmitSimpleAlu(EOPCODE opCode, const SSource sources[2], const DstModifier& modifier)
2394+
void EmitPass::EmitSimpleAlu(EOPCODE opCode, const SSource sources[2], const DstModifier& modifier, bool isUnsigned)
23952395
{
23962396
CVariable* srcs[2] = { nullptr, nullptr };
23972397

@@ -2403,8 +2403,13 @@ void EmitPass::EmitSimpleAlu(EOPCODE opCode, const SSource sources[2], const Dst
24032403
srcs[1] = GetSrcVariable(sources[1], sources[1].fromConstantPool);
24042404
SetSourceModifiers(1, sources[1]);
24052405
}
2406+
CVariable* dst = m_destination;
2407+
if (isUnsigned)
2408+
{
2409+
dst = m_currShader->BitCast(m_destination, GetUnsignedType(m_destination->GetType()));
2410+
}
24062411
m_encoder->SetDstModifier(modifier);
2407-
EmitSimpleAlu(opCode, m_destination, srcs[0], srcs[1]);
2412+
EmitSimpleAlu(opCode, dst, srcs[0], srcs[1]);
24082413
}
24092414

24102415
void EmitPass::EmitSimpleAlu(EOPCODE opCode, CVariable* dst, CVariable* src0, CVariable* src1)
@@ -2786,12 +2791,14 @@ void EmitPass::EmitIntegerTruncWithSat(bool isSignedDst, bool isSignedSrc, const
27862791

27872792
// Emits 4 conversion instructions that downconvert 4 values to a packed
27882793
// <4 x i8> value.
2789-
void EmitPass::EmitPack4i8(const std::array<SSource, 4>& sources, const DstModifier& dstMod)
2794+
void EmitPass::EmitPack4i8(const std::array<SSource, 4>& sources, const std::array<bool, 4> isSat, const DstModifier& dstMod)
27902795
{
2791-
CVariable* dst = m_currShader->GetNewAlias(m_destination, ISA_TYPE_UB, 0, 0);
2796+
CVariable* dst = m_currShader->GetNewAlias(m_destination, ISA_TYPE_B, 0, 0);
27922797
for (uint32_t i = 0; i < 4; ++i)
27932798
{
2794-
m_encoder->SetDstModifier(dstMod);
2799+
DstModifier mod = dstMod;
2800+
mod.sat = isSat[i];
2801+
m_encoder->SetDstModifier(mod);
27952802
CVariable* src = GetSrcVariable(sources[i]);
27962803

27972804
m_encoder->SetDstRegion(4);
@@ -2801,7 +2808,7 @@ void EmitPass::EmitPack4i8(const std::array<SSource, 4>& sources, const DstModif
28012808
}
28022809
}
28032810

2804-
// Emits a conversion instruction that upconverts an 8-bit value from
2811+
// Emits a conversion instruction that upconverts an 8-bit value from
28052812
// a packed <4 x i8> value.
28062813
void EmitPass::EmitUnpack4i8(const SSource& source, uint32_t index, bool isUnsigned, const DstModifier& dstMod)
28072814
{

IGC/Compiler/CISACodeGen/EmitVISAPass.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class EmitPass : public llvm::FunctionPass
132132
void EmitAluIntrinsic(llvm::CallInst* I, const SSource source[2], const DstModifier& modifier);
133133
void EmitSimpleAlu(llvm::Instruction* inst, const SSource source[2], const DstModifier& modifier);
134134
void EmitSimpleAlu(llvm::Instruction* inst, CVariable* dst, CVariable* src0, CVariable* src1);
135-
void EmitSimpleAlu(EOPCODE opCode, const SSource source[2], const DstModifier& modifier);
135+
void EmitSimpleAlu(EOPCODE opCode, const SSource source[2], const DstModifier& modifier, bool isUnsigned = false);
136136
void EmitSimpleAlu(EOPCODE opCode, CVariable* dst, CVariable* src0, CVariable* src1);
137137
void EmitMinMax(bool isMin, bool isUnsigned, const SSource source[2], const DstModifier& modifier);
138138
void EmitUAdd(llvm::BinaryOperator* inst, const DstModifier& modifier);
@@ -143,7 +143,7 @@ class EmitPass : public llvm::FunctionPass
143143
void EmitGenIntrinsicMessage(llvm::GenIntrinsicInst* inst);
144144
void EmitSIToFPZExt(const SSource& source, const DstModifier& dstMod);
145145
void EmitIntegerTruncWithSat(bool isSignedDst, bool isSignedSrc, const SSource& source, const DstModifier& dstMod);
146-
void EmitPack4i8(const std::array<SSource, 4>& sources, const DstModifier& dstMod);
146+
void EmitPack4i8(const std::array<SSource, 4>& sources, const std::array<bool, 4> isSat, const DstModifier& dstMod);
147147
void EmitUnpack4i8(const SSource& source, uint32_t index, bool isUnsigned, const DstModifier& dstMod);
148148
void EmitRepack4i8(const std::array<SSource, 4>& sources, const std::array<uint32_t, 4>& mappings, const DstModifier& dstMod);
149149
void EmitAddPair(llvm::GenIntrinsicInst* GII, const SSource Sources[4], const DstModifier& DstMod);

IGC/Compiler/CISACodeGen/PatternMatchPass.cpp

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,46 @@ namespace IGC
961961
return true;
962962
}
963963

964+
bool CodeGenPatternMatch::MatchShrSatModifier(llvm::SelectInst& I)
965+
{
966+
struct ShrSatPattern : public Pattern
967+
{
968+
SSource sources[2];
969+
Instruction* inst;
970+
virtual void Emit(EmitPass* pass, const DstModifier& modifier)
971+
{
972+
DstModifier mod = modifier;
973+
mod.sat = true;
974+
pass->EmitSimpleAlu(GetOpCode(inst), sources, mod, true /*isUnsigned*/);
975+
}
976+
};
977+
978+
Value* LHS = nullptr, * RHS = nullptr;
979+
if (isMax(&I, LHS, RHS))
980+
{
981+
if (isa<ConstantInt>(LHS) || isa<ConstantInt>(RHS))
982+
{
983+
ConstantInt* c = isa<ConstantInt>(LHS) ? cast<ConstantInt>(LHS) : cast<ConstantInt>(RHS);
984+
if (c->getZExtValue() == 0)
985+
{
986+
Value* v = isa<ConstantInt>(LHS) ? RHS : LHS;
987+
if (isa<LShrOperator>(v) || isa<AShrOperator>(v))
988+
{
989+
Instruction* inst = cast<Instruction>(v);
990+
ShrSatPattern* pattern = new (m_allocator) ShrSatPattern();
991+
pattern->inst = inst;
992+
pattern->sources[0] = GetSource(inst->getOperand(0), false, false, IsSourceOfSample(&I));
993+
pattern->sources[1] = GetSource(inst->getOperand(1), false, false, IsSourceOfSample(&I));
994+
AddPattern(pattern);
995+
996+
return true;
997+
}
998+
}
999+
}
1000+
}
1001+
return false;
1002+
}
1003+
9641004
void CodeGenPatternMatch::visitFPToSIInst(llvm::FPToSIInst& I) {
9651005
bool match = MatchFPToIntegerWithSaturation(I) || MatchModifier(I);
9661006
IGC_ASSERT_MESSAGE(match, "Pattern match Failed");
@@ -1162,6 +1202,7 @@ namespace IGC
11621202
{
11631203
bool match = MatchFloatingPointSatModifier(I) ||
11641204
MatchIntegerTruncSatModifier(I) ||
1205+
MatchShrSatModifier(I) ||
11651206
MatchAbsNeg(I) ||
11661207
MatchFPToIntegerWithSaturation(I) ||
11671208
MatchMinMax(I) ||
@@ -3236,9 +3277,10 @@ namespace IGC
32363277
struct PackPattern : public Pattern
32373278
{
32383279
std::array<SSource, 4> sources;
3280+
std::array<bool, 4> isSat;
32393281
virtual void Emit(EmitPass* pass, const DstModifier& modifier)
32403282
{
3241-
pass->EmitPack4i8(sources, modifier);
3283+
pass->EmitPack4i8(sources, isSat, modifier);
32423284
}
32433285
};
32443286

@@ -3247,6 +3289,7 @@ namespace IGC
32473289
isa<InsertElementInst>(I.getOperand(0)))
32483290
{
32493291
Value* sources[4] = {};
3292+
bool isSat[4] = {};
32503293
auto ie = cast<InsertElementInst>(I.getOperand(0));
32513294
uint32_t elemsFound = 0;
32523295
while (ie)
@@ -3274,11 +3317,47 @@ namespace IGC
32743317
}
32753318
}
32763319
if (elemsFound == 4)
3320+
{
3321+
// Check if the sources can be promoted to the "sat" qualifier
3322+
for (uint32_t i = 0; i < 4; ++i)
3323+
{
3324+
// Lambda checks if one of the source values is a constant
3325+
// value equal to `imm` and returns the other source value.
3326+
auto GetOtherSource = [&](const auto& src, uint32_t imm)->Value*
3327+
{
3328+
if (isa<ConstantInt>(src[0]) || isa<ConstantInt>(src[1]))
3329+
{
3330+
ConstantInt* c = isa<ConstantInt>(src[0]) ? cast<ConstantInt>(src[0]) : cast<ConstantInt>(src[1]);
3331+
if (c->getZExtValue() == imm)
3332+
{
3333+
return isa<ConstantInt>(src[0]) ? src[1] : src[0];
3334+
}
3335+
}
3336+
return nullptr;
3337+
};
3338+
Value* minSources[2];
3339+
if (isMin(sources[i], minSources[0], minSources[1]))
3340+
{
3341+
if (Value* minVal = GetOtherSource(minSources, 127))
3342+
{
3343+
Value* maxSources[2];
3344+
if (isMax(minVal, maxSources[0], maxSources[1]) &&
3345+
GetOtherSource(maxSources, 0))
3346+
{
3347+
sources[i] = minVal;
3348+
isSat[i] = true;
3349+
}
3350+
}
3351+
}
3352+
}
3353+
}
3354+
if (elemsFound == 4)
32773355
{
32783356
PackPattern* pattern = new (m_allocator) PackPattern();
32793357
for (uint32_t i = 0; i < 4; ++i)
32803358
{
32813359
pattern->sources[i] = GetSource(sources[i], false, false, IsSourceOfSample(&I));
3360+
pattern->isSat[i] = isSat[i];
32823361
}
32833362
AddPattern(pattern);
32843363
return true;
@@ -6204,7 +6283,7 @@ namespace IGC
62046283
return found;
62056284
}
62066285

6207-
inline bool isMax(llvm::Value* max, llvm::Value*& source0, llvm::Value*& source1)
6286+
bool isMax(llvm::Value* max, llvm::Value*& source0, llvm::Value*& source1)
62086287
{
62096288
bool isMin, isUnsigned;
62106289
llvm::Value* maxSource0;
@@ -6221,7 +6300,7 @@ namespace IGC
62216300
return false;
62226301
}
62236302

6224-
inline bool isMin(llvm::Value* min, llvm::Value*& source0, llvm::Value*& source1)
6303+
bool isMin(llvm::Value* min, llvm::Value*& source0, llvm::Value*& source1)
62256304
{
62266305
bool isMin, isUnsigned;
62276306
llvm::Value* maxSource0;

IGC/Compiler/CISACodeGen/PatternMatchPass.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ namespace IGC
244244
std::tuple<llvm::Value*, unsigned, VISA_Type> isFPToSignedIntSatWithInexactConstant(llvm::SelectInst* I);
245245
std::tuple<llvm::Value*, unsigned, VISA_Type> isFPToUnsignedIntSatWithInexactConstant(llvm::SelectInst* I);
246246
bool MatchIntegerTruncSatModifier(llvm::SelectInst& I);
247+
bool MatchShrSatModifier(llvm::SelectInst& I);
247248
std::tuple<llvm::Value*, bool, bool> isIntegerSatTrunc(llvm::SelectInst*);
248249

249250
bool MatchSIToFPZExt(llvm::SIToFPInst* S2FI);
@@ -362,6 +363,8 @@ namespace IGC
362363
bool isSat(llvm::Instruction* sat, llvm::Value*& source, bool& isUnsigned);
363364
bool isOne(llvm::Value* zero);
364365
bool isMinOrMax(llvm::Value* inst, llvm::Value*& source0, llvm::Value*& source1, bool& isMin, bool& isUnsigned);
366+
bool isMax(llvm::Value* max, llvm::Value*& source0, llvm::Value*& source1);
367+
bool isMin(llvm::Value* min, llvm::Value*& source0, llvm::Value*& source1);
365368
bool isCandidateForConstantPool(llvm::Value * val);
366369
e_modifier CombineModifier(e_modifier mod1, e_modifier mod2);
367370

0 commit comments

Comments
 (0)