Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 26 additions & 31 deletions llvm/lib/SYCLLowerIR/ESIMD/LowerESIMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,20 @@ translateSpirvGlobalUses(LoadInst *LI, StringRef SpirvGlobalName,
}
}

static void translateGlobalUse(Value *Use, StringRef SpirvGlobalName,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wondering, how did this issue come up? do we have an internal tracker for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is nice to have suggestion for original work to lower spirv globals from ~ 3 years ago. Theoretically it should improve compilation performance and reduce amount of code, but I doubt anybody will notice that. It was included into tracker to move ESIMD passes/split LowerESIMD pass to Function Passes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, thanks

SmallVectorImpl<Instruction *> &InstsToErase) {
LoadInst *LI = dyn_cast<LoadInst>(Use);
ConstantExpr *CE = dyn_cast<ConstantExpr>(Use);
GetElementPtrConstantExpr *GEPCE = dyn_cast<GetElementPtrConstantExpr>(Use);
if (LI != nullptr) {
translateSpirvGlobalUses(LI, SpirvGlobalName, InstsToErase);
} else if (CE != nullptr || GEPCE != nullptr) {
for (User *U : (CE == nullptr ? GEPCE : CE)->users()) {
translateGlobalUse(U, SpirvGlobalName, InstsToErase);
}
}
}

static void createESIMDIntrinsicArgs(const ESIMDIntrinDesc &Desc,
SmallVector<Value *, 16> &GenXArgs,
CallInst &CI, id::FunctionEncoding *FE) {
Expand Down Expand Up @@ -2090,6 +2104,18 @@ PreservedAnalyses SYCLLowerESIMDPass::run(Module &M,
MPM.run(M, MAM);
}

SmallVector<Instruction *, 8> ToErase;
constexpr size_t PrefLen = StringRef(SPIRV_INTRIN_PREF).size();
for (GlobalVariable &Global : M.globals()) {
if (!Global.getName().starts_with(SPIRV_INTRIN_PREF))
continue;

for (User *U : Global.users())
translateGlobalUse(U, Global.getName().drop_front(PrefLen), ToErase);
}
for (auto *CI : ToErase)
CI->eraseFromParent();

generateKernelMetadata(M);
// This function needs to run after generateKernelMetadata, as it
// uses the generated metadata:
Expand Down Expand Up @@ -2244,37 +2270,6 @@ size_t SYCLLowerESIMDPass::runOnFunction(Function &F,
// this is ESIMD intrinsic - record for later translation
ESIMDIntrCalls.push_back(CI);
}

// Translate loads from SPIRV builtin globals into GenX intrinsics
auto *LI = dyn_cast<LoadInst>(&I);
if (LI) {
Value *LoadPtrOp = LI->getPointerOperand();
Value *SpirvGlobal = nullptr;
// Look through constant expressions to find SPIRV builtin globals
// It may come with or without cast.
auto *CE = dyn_cast<ConstantExpr>(LoadPtrOp);
auto *GEPCE = dyn_cast<GetElementPtrConstantExpr>(LoadPtrOp);
if (GEPCE) {
SpirvGlobal = GEPCE->getOperand(0);
} else if (CE) {
assert(CE->isCast() && "ConstExpr should be a cast");
SpirvGlobal = CE->getOperand(0);
} else {
SpirvGlobal = LoadPtrOp;
}

if (!isa<GlobalVariable>(SpirvGlobal) ||
!SpirvGlobal->getName().starts_with(SPIRV_INTRIN_PREF))
continue;

auto PrefLen = StringRef(SPIRV_INTRIN_PREF).size();

// Translate all uses of the load instruction from SPIRV builtin global.
// Replaces the original global load and it is uses and stores the old
// instructions to ToErase.
translateSpirvGlobalUses(LI, SpirvGlobal->getName().drop_front(PrefLen),
ToErase);
}
}
// Now demangle and translate found ESIMD intrinsic calls
for (auto *CI : ESIMDIntrCalls) {
Expand Down