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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2024 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "GenX.h"
#include "vc/InternalIntrinsics/InternalIntrinsics.h"
#include "vc/Utils/GenX/Intrinsics.h"
#include "vc/Utils/GenX/KernelInfo.h"
#include "vc/Utils/General/Types.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include <queue>
#define DEBUG_TYPE "genx-count-indirect-stateless"
using namespace llvm;
namespace {
class GenXCountIndirectStateless : public ModulePass {
public:
static char ID;
GenXCountIndirectStateless() : ModulePass(ID) {}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<CallGraphWrapperPass>();
AU.setPreservesAll();
}
StringRef getPassName() const override {
return "GenX count indirect stateless";
}
bool runOnModule(Module &M) override;
private:
void countIndirectStateless(Function &F);
unsigned getIndirectStatelessCount(Function &F) const;
SmallDenseMap<Function *, unsigned> IndirectStatelessCount;
};
class CountIndirectStatelessVisitor
: public InstVisitor<CountIndirectStatelessVisitor> {
public:
unsigned getCount() const { return Count; }
void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CI) {
analyzePointer(CI.getPointerOperand());
}
void visitAtomicRMWInst(AtomicRMWInst &AI) {
analyzePointer(AI.getPointerOperand());
}
void visitLoadInst(LoadInst &LI) { analyzePointer(LI.getPointerOperand()); }
void visitStoreInst(StoreInst &SI) { analyzePointer(SI.getPointerOperand()); }
void visitCallInst(CallInst &CI);
private:
bool analyzePointer(Value *Ptr);
static bool isMemLoadIntrinsic(Value *V);
SmallDenseMap<Instruction *, bool> IsPointer;
unsigned Count = 0;
};
} // namespace
namespace llvm {
void initializeGenXCountIndirectStatelessPass(PassRegistry &);
} // namespace llvm
char GenXCountIndirectStateless::ID = 0;
INITIALIZE_PASS_BEGIN(GenXCountIndirectStateless, "GenXCountIndirectStateless",
"GenXCountIndirectStateless", false, false)
INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
INITIALIZE_PASS_END(GenXCountIndirectStateless, "GenXCountIndirectStateless",
"GenXCountIndirectStateless", false, false)
ModulePass *llvm::createGenXCountIndirectStatelessPass() {
initializeGenXCountIndirectStatelessPass(*PassRegistry::getPassRegistry());
return new GenXCountIndirectStateless();
}
bool GenXCountIndirectStateless::runOnModule(Module &M) {
for (auto &F : M)
countIndirectStateless(F);
if (IndirectStatelessCount.empty())
return false;
auto Kernels = make_filter_range(M, [](auto &F) { return vc::isKernel(F); });
for (auto &F : Kernels) {
const unsigned Count = getIndirectStatelessCount(F);
vc::KernelMetadata MD(&F);
MD.updateIndirectCountMD(Count);
}
return true;
}
void GenXCountIndirectStateless::countIndirectStateless(Function &F) {
if (F.isDeclaration())
return;
CountIndirectStatelessVisitor Visitor;
Visitor.visit(F);
auto Count = Visitor.getCount();
if (Count > 0)
IndirectStatelessCount.try_emplace(&F, Count);
}
unsigned
GenXCountIndirectStateless::getIndirectStatelessCount(Function &F) const {
const auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
std::queue<const Function *> WorkList;
SmallPtrSet<const Function *, 8> Visited;
WorkList.push(&F);
unsigned Count = 0;
while (!WorkList.empty()) {
auto *Curr = WorkList.front();
WorkList.pop();
if (Curr->isDeclaration() || !Visited.insert(Curr).second)
continue;
Count += IndirectStatelessCount.lookup(Curr);
const auto *Node = CG[Curr];
for (const auto &Edge : *Node) {
const auto *F = Edge.second->getFunction();
if (F && !Visited.contains(F))
WorkList.push(F);
}
}
return Count;
}
void CountIndirectStatelessVisitor::visitCallInst(CallInst &CI) {
if (vc::InternalIntrinsic::isStatelessIntrinsic(&CI)) {
analyzePointer(vc::InternalIntrinsic::getMemoryAddressOperand(&CI));
return;
}
auto IID = vc::getAnyIntrinsicID(&CI);
switch (IID) {
default:
return;
case Intrinsic::masked_gather:
case Intrinsic::masked_load:
analyzePointer(CI.getArgOperand(0));
break;
case Intrinsic::masked_scatter:
case Intrinsic::masked_store:
analyzePointer(CI.getArgOperand(1));
break;
case GenXIntrinsic::genx_svm_block_ld:
case GenXIntrinsic::genx_svm_block_ld_unaligned:
case GenXIntrinsic::genx_svm_block_st:
analyzePointer(CI.getArgOperand(0));
break;
case GenXIntrinsic::genx_svm_gather:
case GenXIntrinsic::genx_svm_scatter:
analyzePointer(CI.getArgOperand(2));
break;
case GenXIntrinsic::genx_svm_gather4_scaled:
case GenXIntrinsic::genx_svm_scatter4_scaled:
analyzePointer(CI.getArgOperand(3));
analyzePointer(CI.getArgOperand(4));
break;
case GenXIntrinsic::genx_svm_atomic_add:
case GenXIntrinsic::genx_svm_atomic_and:
case GenXIntrinsic::genx_svm_atomic_cmpxchg:
case GenXIntrinsic::genx_svm_atomic_dec:
case GenXIntrinsic::genx_svm_atomic_imax:
case GenXIntrinsic::genx_svm_atomic_imin:
case GenXIntrinsic::genx_svm_atomic_inc:
case GenXIntrinsic::genx_svm_atomic_max:
case GenXIntrinsic::genx_svm_atomic_min:
case GenXIntrinsic::genx_svm_atomic_or:
case GenXIntrinsic::genx_svm_atomic_sub:
case GenXIntrinsic::genx_svm_atomic_xchg:
case GenXIntrinsic::genx_svm_atomic_xor:
analyzePointer(CI.getArgOperand(1));
break;
}
}
bool CountIndirectStatelessVisitor::analyzePointer(Value *Ptr) {
auto *Inst = dyn_cast<Instruction>(Ptr);
if (!Inst)
return false;
if (auto It = IsPointer.find(Inst); It != IsPointer.end()) {
if (It->second)
++Count;
return It->second;
}
if (auto [It, Inserted] = IsPointer.try_emplace(Inst, false); !Inserted)
return It->second;
auto *Ty = Inst->getType();
const bool IsInt64 = Ty->isIntOrIntVectorTy(64);
if (Ty->isFPOrFPVectorTy()) {
return false;
}
if (auto *PTy = dyn_cast<PointerType>(Ty)) {
const auto AS = PTy->getAddressSpace();
if (AS == vc::AddrSpace::Local || AS == vc::AddrSpace::CodeSectionINTEL ||
AS == vc::AddrSpace::GlobalA32)
return false;
}
if (isa<LoadInst>(Inst) || isMemLoadIntrinsic(Inst) ||
(isa<CallInst>(Inst) && !vc::isAnyNonTrivialIntrinsic(Inst))) {
IsPointer[Inst] = true;
++Count;
return true;
}
if (isa<AtomicRMWInst>(Inst)) {
IsPointer[Inst] = IsInt64;
Count += IsInt64;
return IsInt64;
}
if (isa<AtomicCmpXchgInst>(Inst)) {
auto *ValTy = cast<StructType>(Ty)->getElementType(0);
const auto IsInt64 = ValTy->isIntOrIntVectorTy(64);
IsPointer[Inst] = IsInt64;
Count += IsInt64;
return IsInt64;
}
const auto Opcode = Inst->getOpcode();
switch (Opcode) {
default:
break;
case Instruction::Trunc:
case Instruction::SExt:
case Instruction::ZExt:
// Trunc, ZExt and SExt instructions cannot produce a pointer value.
case Instruction::Mul:
case Instruction::UDiv:
case Instruction::SDiv:
case Instruction::URem:
case Instruction::SRem:
case Instruction::Shl:
case Instruction::LShr:
case Instruction::AShr:
// Mul-like and div-like instructions cannot produce a pointer value.
return false;
}
if (isa<GetElementPtrInst>(Inst) || isa<ExtractElementInst>(Inst)) {
const bool IsIndirect = analyzePointer(Inst->getOperand(0));
IsPointer[Inst] = IsIndirect;
return IsIndirect;
}
for (auto &Op : Inst->operands()) {
if (analyzePointer(Op.get())) {
IsPointer[Inst] = true;
return true;
}
}
return false;
}
bool CountIndirectStatelessVisitor::isMemLoadIntrinsic(Value *V) {
if (vc::InternalIntrinsic::isInternalMemoryIntrinsic(V))
return true;
auto IID = vc::getAnyIntrinsicID(V);
switch (IID) {
default:
break;
case Intrinsic::masked_gather:
case Intrinsic::masked_load:
case GenXIntrinsic::genx_svm_block_ld:
case GenXIntrinsic::genx_svm_block_ld_unaligned:
case GenXIntrinsic::genx_svm_gather:
return true;
case GenXIntrinsic::genx_svm_atomic_add:
case GenXIntrinsic::genx_svm_atomic_cmpxchg:
case GenXIntrinsic::genx_svm_atomic_dec:
case GenXIntrinsic::genx_svm_atomic_inc:
case GenXIntrinsic::genx_svm_atomic_sub:
case GenXIntrinsic::genx_svm_atomic_xchg:
return V->getType()->isIntOrIntVectorTy(64);
}
return false;
}
|