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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2024 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
//
/// GenXPromotePredicate
/// --------------------
///
/// GenXPromotePredicate is an optimization pass that promotes vector operations
/// on predicates (n x i1) to operations on wider integer types (<n x i16>).
/// This often reduces flag register pressure and improves code quality.
///
//===----------------------------------------------------------------------===//
#include "GenX.h"
#include "GenXSubtarget.h"
#include "GenXTargetMachine.h"
#include "GenXUtil.h"
#include "llvm/ADT/EquivalenceClasses.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvmWrapper/IR/DerivedTypes.h"
#define DEBUG_TYPE "genx-promote-predicate"
using namespace llvm;
using namespace genx;
static cl::opt<unsigned> LogicOpsThreshold(
"logical-ops-threshold", cl::init(5), cl::Hidden,
cl::desc("Number of logical predicate operations to apply GRF promotion"));
STATISTIC(NumCollectedPredicateWebs, "Number of collected predicate webs");
STATISTIC(NumPromotedPredicateWebs, "Number of GRF-promoted predicate webs");
namespace {
class GenXPromotePredicate : public FunctionPass {
public:
static char ID;
GenXPromotePredicate() : FunctionPass(ID) {}
bool runOnFunction(Function &F) override;
StringRef getPassName() const override { return "GenXPromotePredicate"; }
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<TargetPassConfig>();
AU.setPreservesCFG();
}
};
} // namespace
char GenXPromotePredicate::ID = 0;
namespace llvm {
void initializeGenXPromotePredicatePass(PassRegistry &);
}
INITIALIZE_PASS_BEGIN(GenXPromotePredicate, "GenXPromotePredicate",
"GenXPromotePredicate", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
INITIALIZE_PASS_END(GenXPromotePredicate, "GenXPromotePredicate",
"GenXPromotePredicate", false, false)
FunctionPass *llvm::createGenXPromotePredicatePass() {
initializeGenXPromotePredicatePass(*PassRegistry::getPassRegistry());
return new GenXPromotePredicate;
}
static constexpr unsigned PromotedPredicateWidth = 16;
// Get predicate value with grf type.
static Value *getExtendedValue(Value *Val, Instruction *InsertBefore) {
IGC_ASSERT(Val->getType()->isIntOrIntVectorTy(1));
IRBuilder<> IRB(InsertBefore);
Type *NewTy =
IGCLLVM::getWithNewBitWidth(Val->getType(), PromotedPredicateWidth);
return IRB.CreateSExt(Val, NewTy, Val->getName() + ".widened");
}
// Get grf value with predicate type.
static Value *getTruncatedValue(Value *Val, Instruction *InsertBefore) {
IGC_ASSERT(Val->getType()->isIntOrIntVectorTy(PromotedPredicateWidth));
IRBuilder<> IRB(InsertBefore);
Type *NewTy = IGCLLVM::getWithNewBitWidth(Val->getType(), 1);
return IRB.CreateTrunc(Val, NewTy, Val->getName() + ".truncated");
}
static Value *scalarizeInputPredicate(Value *V, Type *Ty, IRBuilder<> &IRB) {
if (auto *C = dyn_cast<Constant>(V)) {
if (C->isAllOnesValue())
return Constant::getAllOnesValue(Ty);
if (C->isNullValue())
return Constant::getNullValue(Ty);
}
return IRB.CreateBitCast(V, Ty);
}
// Promote a vector predicate operation into scalar integer one.
static Value *promoteInstToScalar(Instruction *Inst) {
IRBuilder<> IRB(Inst);
auto *VTy = cast<IGCLLVM::FixedVectorType>(Inst->getType());
auto Width = VTy->getNumElements();
IGC_ASSERT(VTy->isIntOrIntVectorTy(1));
IGC_ASSERT(Width == 8 || Width == 16 || Width == 32);
auto *STy = IRB.getIntNTy(Width);
// Special case - phi node.
if (auto *Phi = dyn_cast<PHINode>(Inst)) {
auto *ScalarPhi = IRB.CreatePHI(STy, Phi->getNumIncomingValues(),
Phi->getName() + ".scalar");
for (unsigned I = 0; I < Phi->getNumIncomingValues(); ++I) {
auto *IncomingBlock = Phi->getIncomingBlock(I);
auto *IncomingValue = Phi->getIncomingValue(I);
IRB.SetInsertPoint(IncomingBlock->getTerminator());
auto *ScalarValue = scalarizeInputPredicate(IncomingValue, STy, IRB);
ScalarPhi->addIncoming(ScalarValue, IncomingBlock);
}
IRB.SetInsertPoint(Phi->getParent()->getFirstNonPHI());
return IRB.CreateBitCast(ScalarPhi, VTy);
}
IGC_ASSERT(isa<BinaryOperator>(Inst));
auto *Op1 = scalarizeInputPredicate(Inst->getOperand(0), STy, IRB);
auto *Op2 = scalarizeInputPredicate(Inst->getOperand(1), STy, IRB);
auto *ScalarInst = IRB.CreateBinOp(cast<BinaryOperator>(Inst)->getOpcode(),
Op1, Op2, Inst->getName() + ".scalar");
return IRB.CreateBitCast(ScalarInst, VTy);
}
// Promote one predicate instruction to grf - promote all its operands and
// instruction itself, and then sink the result back to predicate.
static Value *promoteInst(Instruction *Inst, bool AllowScalarPromotion) {
if (auto *VTy = dyn_cast<IGCLLVM::FixedVectorType>(Inst->getType());
VTy && AllowScalarPromotion) {
IGC_ASSERT(VTy->isIntOrIntVectorTy(1));
auto Width = VTy->getNumElements();
if (Width == 8 || Width == 16 || Width == 32)
return promoteInstToScalar(Inst);
}
IRBuilder<> IRB(Inst);
// Special case - phi node.
if (auto *Phi = dyn_cast<PHINode>(Inst)) {
auto *WidenedPhi = IRB.CreatePHI(
IGCLLVM::getWithNewBitWidth(Phi->getType(), PromotedPredicateWidth),
Phi->getNumIncomingValues(), Phi->getName() + ".promoted");
for (unsigned i = 0; i < Phi->getNumIncomingValues(); ++i) {
auto IncomingValue = Phi->getIncomingValue(i);
auto IncomingBlock = Phi->getIncomingBlock(i);
WidenedPhi->addIncoming(
getExtendedValue(IncomingValue, IncomingBlock->getTerminator()),
IncomingBlock);
}
return getTruncatedValue(WidenedPhi, Phi->getParent()->getFirstNonPHI());
}
// Process binary operators.
IGC_ASSERT(isa<BinaryOperator>(Inst));
Value *Op1 = getExtendedValue(Inst->getOperand(0), Inst),
*Op2 = getExtendedValue(Inst->getOperand(1), Inst);
Value *PromotedInst =
IRB.CreateBinOp(cast<BinaryOperator>(Inst)->getOpcode(), Op1, Op2,
Inst->getName() + ".promoted");
return getTruncatedValue(PromotedInst, Inst);
}
// Cleanup trunc->sext chains and lower trunc if there are remaining uses:
// trunc <n x i16> %val to <n x i1> => icmp %val, 0
// This is done in assumption that all bits in truncated value are the same
// (this is always true in predicate web).
static void foldTruncAndSExt(TruncInst *TI) {
Value *SrcVal = TI->getOperand(0);
Type *SrcTy = TI->getSrcTy();
SmallVector<Instruction *, 4> ToErase;
for (auto U : TI->users()) {
auto *SI = dyn_cast<SExtInst>(U);
if (!SI || SI->getDestTy() != SrcTy)
continue;
ToErase.push_back(SI);
}
for (auto SI : ToErase) {
SI->replaceAllUsesWith(SrcVal);
SI->eraseFromParent();
}
if (!TI->user_empty()) {
auto Cmp =
IRBuilder<>(TI).CreateICmpNE(SrcVal, Constant::getNullValue(SrcTy));
TI->replaceAllUsesWith(Cmp);
}
TI->eraseFromParent();
}
static void foldBitcast(BitCastInst *Cast) {
auto *Src = Cast->getOperand(0);
auto *SrcTy = Src->getType();
SmallVector<User *, 4> ToErase;
llvm::copy_if(Cast->users(), std::back_inserter(ToErase), [SrcTy](auto *U) {
return isa<BitCastInst>(U) && U->getType() == SrcTy;
});
for (auto *U : ToErase) {
auto *I = cast<Instruction>(U);
I->replaceAllUsesWith(Src);
I->eraseFromParent();
}
if (Cast->user_empty())
Cast->eraseFromParent();
}
class PredicateWeb {
public:
template <class InputIt>
PredicateWeb(InputIt First, InputIt Last, bool AllowScalarAllAny)
: Web(First, Last), AllowScalarAllAny(AllowScalarAllAny) {}
void print(llvm::raw_ostream &O) const {
for (auto Inst : Web)
O << *Inst << '\n';
}
void dump() const { print(dbgs()); }
bool isBeneficialToPromote() const {
unsigned NumBinaryOps =
std::count_if(Web.begin(), Web.end(),
[](auto *Inst) { return isa<BinaryOperator>(Inst); });
return NumBinaryOps >= LogicOpsThreshold;
}
void doPromotion() const {
auto AllowScalar = true;
if (!AllowScalarAllAny)
AllowScalar = llvm::none_of(Web, [](auto *Inst) {
return llvm::any_of(Inst->users(), [](auto *U) {
auto IID = vc::getAnyIntrinsicID(U);
return IID == GenXIntrinsic::genx_any ||
IID == GenXIntrinsic::genx_all;
});
});
// Do promotion.
SmallVector<Instruction *, 8> Worklist;
for (auto *Inst : Web) {
auto *PromotedInst = promoteInst(Inst, AllowScalar);
if (isa<TruncInst>(PromotedInst) || isa<BitCastInst>(PromotedInst))
Worklist.push_back(cast<Instruction>(PromotedInst));
Inst->replaceAllUsesWith(PromotedInst);
Inst->eraseFromParent();
}
// Do cleanup.
for (auto *I : Worklist)
if (auto *Trunc = dyn_cast<TruncInst>(I))
foldTruncAndSExt(Trunc);
else if (auto *Cast = dyn_cast<BitCastInst>(I))
foldBitcast(Cast);
}
private:
SmallPtrSet<Instruction *, 16> Web;
bool AllowScalarAllAny;
};
constexpr const char IdxMDName[] = "pred.index";
// Comparator to keep sequence of instructions - otherwise it will compare
// dynamically allocated pointers
struct Comparator {
long getMd(Instruction *const &I) const {
auto *IMd = I->getMetadata(IdxMDName);
IGC_ASSERT_EXIT(IMd);
return cast<ConstantInt>(
cast<ConstantAsMetadata>(IMd->getOperand(0).get())->getValue())
->getZExtValue();
}
bool operator()(Instruction *const &Lhs, Instruction *const &Rhs) const {
return getMd(Lhs) > getMd(Rhs);
}
};
bool GenXPromotePredicate::runOnFunction(Function &F) {
auto &ST = getAnalysis<TargetPassConfig>()
.getTM<GenXTargetMachine>()
.getGenXSubtarget();
bool AllowScalarAllAny = !ST.hasFusedEU();
// Put every predicate instruction into its own equivalence class.
long Idx = 0;
llvm::EquivalenceClasses<Instruction *, Comparator> PredicateWebs;
for (auto &I : instructions(F)) {
if (!genx::isPredicate(&I))
continue;
if (!I.isBitwiseLogicOp() && !isa<PHINode>(&I))
continue;
auto &Ctx = I.getContext();
auto *MD = ConstantAsMetadata::get(
ConstantInt::get(Ctx, llvm::APInt(64, ++Idx, false)));
I.setMetadata(IdxMDName, MDNode::get(Ctx, MD));
PredicateWebs.insert(&I);
}
// Connect data-flow related instructions together.
for (auto &EC : PredicateWebs) {
Instruction *Inst = EC.getData();
for (auto &Op : Inst->operands()) {
Instruction *In = dyn_cast<Instruction>(Op);
if (!In || !In->hasMetadata(IdxMDName))
continue;
PredicateWebs.unionSets(Inst, In);
}
}
// Promote web if it is big enough (likely to cause flag spills).
bool Modified = false;
for (auto I = PredicateWebs.begin(), E = PredicateWebs.end(); I != E; ++I) {
if (!I->isLeader())
continue;
PredicateWeb Web(PredicateWebs.member_begin(I), PredicateWebs.member_end(),
AllowScalarAllAny);
LLVM_DEBUG(dbgs() << "Predicate web:\n"; Web.dump());
++NumCollectedPredicateWebs;
if (!Web.isBeneficialToPromote())
continue;
LLVM_DEBUG(dbgs() << "Beneficial to promote\n");
Web.doPromotion();
++NumPromotedPredicateWebs;
Modified = true;
}
return Modified;
}
|