Skip to content

Commit 9fa222a

Browse files
lsatanovigcbot
authored andcommitted
legalizing gvload users which's further optimizations by standard LLVM passes may result in potential clobbering.
VLoad's value usage was later optimized by mem2reg in a way that resulted in genx_volatile value clobbering. Current solution to avoid this is to forbid direct usage of genx.vload(@GENX_VOLATILE_GLOBAL*) values by non-genx intrinsic. Until all the frontends are fixed, we fix-up such cases (emitting warning message in Debug build), since they are trivial to fix-up. This fix will be followed up by removal of previous fix to this issue which was based on convertion of a non-genx_volatile global used "in the context of" a genx_volatile global to genx_volatile global. This fix results in implicit conversion of "ordinary globals" to genx_volatile which is not expected by the user and increases register pressure.
1 parent 5e1bd3b commit 9fa222a

18 files changed

+408
-162
lines changed

IGC/VectorCompiler/lib/GenXCodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ set(CODEGEN_SOURCES
4343
GenXEmulate.cpp
4444
GenXExtractVectorizer.cpp
4545
GenXFixInvalidFuncName.cpp
46+
GenXLegalizeGVLoadUses.cpp
4647
GenXGASCastAnalyzer.cpp
4748
GenXGASDynamicResolution.cpp
4849
GenXGEPLowering.cpp

IGC/VectorCompiler/lib/GenXCodeGen/GenX.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ ModulePass *createGenXStructSplitterPass();
148148
FunctionPass *createGenXPredRegionLoweringPass();
149149
FunctionPass *createGenXDebugLegalizationPass();
150150
FunctionPass *createGenXFixInvalidFuncNamePass();
151+
FunctionPass *createGenXLegalizeGVLoadUsesPass();
151152
ModulePass *createGenXGASCastWrapperPass();
152153
FunctionPass *createGenXGASDynamicResolutionPass();
153154
ModulePass *createGenXInitBiFConstantsPass();

IGC/VectorCompiler/lib/GenXCodeGen/GenXBaling.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ bool GenXBaling::isRegionOKForIntrinsic(unsigned ArgInfoBits,
330330
* I operation is load-like, we don't sink it only through store-like operations
331331
*/
332332
bool GenXBaling::isSafeToMove(Instruction *Op, Instruction *From, Instruction *To) {
333-
if (!genx::isSafeToMoveInstCheckAVLoadKill(Op, To, this))
333+
if (!genx::isSafeToSink_CheckAVLoadKill(Op, To, this))
334334
return false;
335335

336336
if (DisableMemOrderCheck || !Op->mayReadOrWriteMemory())
@@ -532,7 +532,7 @@ bool GenXBaling::operandCanBeBaled(
532532
// where there are no region restrictions.)
533533
Region RdR = makeRegionFromBaleInfo(Opnd, BaleInfo());
534534
if (!isRegionOKForIntrinsic(AI.Info, RdR, canSplitBale(Inst)) ||
535-
!genx::isSafeToMoveInstCheckAVLoadKill(Opnd, Inst, this))
535+
!genx::isSafeToSink_CheckAVLoadKill(Opnd, Inst, this))
536536
return false;
537537

538538
// Do not bale in a region read with multiple uses if
@@ -657,7 +657,7 @@ void GenXBaling::processWrRegion(Instruction *Inst)
657657
GenXIntrinsic::GenXRegion::OldValueOperandNum));
658658
auto *Src1 = genx::getAVLoadSrcOrNull(L1);
659659
auto *Src2 = genx::getAVLoadSrcOrNull(L2);
660-
if (Src1 && Src1 == Src2 && !genx::loadingSameValue(L1, L2, DT))
660+
if (Src1 && Src1 == Src2 && !genx::vloadsReadSameValue(L1, L2, DT))
661661
V = nullptr;
662662
}
663663

@@ -1456,7 +1456,7 @@ void GenXBaling::processMainInst(Instruction *Inst, int IntrinID) {
14561456
break;
14571457
case GenXIntrinsicInfo::RAW:
14581458
if (isa<Instruction>(Inst->getOperand(ArgIdx)) &&
1459-
!genx::isSafeToMoveInstCheckAVLoadKill(
1459+
!genx::isSafeToSink_CheckAVLoadKill(
14601460
cast<Instruction>(Inst->getOperand(ArgIdx)), Inst, this))
14611461
continue;
14621462
// Rdregion can be baled in to a raw operand as long as it is

IGC/VectorCompiler/lib/GenXCodeGen/GenXDepressurizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ bool GenXDepressurizer::sinkOnce(Instruction *InsertBefore, Superbale *SB,
11211121
for (auto *I : SB->Bales) {
11221122
Bale B;
11231123
Baling->buildBale(I, &B);
1124-
if (!genx::isSafeToMoveBaleCheckAVLoadKill(B, InsertBefore)) {
1124+
if (!genx::isSafeToSink_CheckAVLoadKill(B, InsertBefore)) {
11251125
LLVM_DEBUG(
11261126
dbgs() << "Will not move this superbale to position of "
11271127
<< *InsertBefore

IGC/VectorCompiler/lib/GenXCodeGen/GenXGVClobberChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ bool GenXGVClobberChecker::checkGVClobberingByInterveningStore(
241241
}
242242

243243
const auto *SI = genx::getAVLoadKillOrNull(
244-
LI, UI, true, nullptr,
244+
LI, UI, false, true, nullptr,
245245
isa<PHINode>(UI) ? &PhiUserExcludeBlocksOnCfgTraversal : nullptr, SIs);
246246

247247
if (!SI)

IGC/VectorCompiler/lib/GenXCodeGen/GenXIMadPostLegalization.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ bool GenXIMadPostLegalization::runOnFunction(Function &F) {
180180
MadSumOpndInst->mayHaveSideEffects() ||
181181
isa<PHINode>(MadSumOpndInst) ||
182182
MadSumOpndInst->getNextNode() == MadInst ||
183-
!genx::isSafeToMoveInstCheckAVLoadKill(MadSumOpndInst, MadInst,
184-
Baling))
183+
!genx::isSafeToSink_CheckAVLoadKill(MadSumOpndInst, MadInst,
184+
Baling))
185185
continue;
186186
MadSumOpndInst->moveBefore(MadInst);
187187
Changed = true;
@@ -203,8 +203,8 @@ bool GenXIMadPostLegalization::runOnFunction(Function &F) {
203203
auto *UserInst = cast<Instruction>(MadSumOpndUse.getUser());
204204
if (isIntegerMadIntrinsic(UserInst) &&
205205
MadSumOpndUse.getOperandNo() == 2 &&
206-
genx::isSafeToMoveInstCheckAVLoadKill(MadSumOpndInst, UserInst,
207-
Baling)) {
206+
genx::isSafeToSink_CheckAVLoadKill(MadSumOpndInst, UserInst,
207+
Baling)) {
208208
auto *NewMadSumOpndInst = MadSumOpndInst->clone();
209209
NewMadSumOpndInst->setName(MadSumOpndInst->getName() + ".postimad");
210210
NewMadSumOpndInst->insertBefore(UserInst);
@@ -220,8 +220,8 @@ bool GenXIMadPostLegalization::runOnFunction(Function &F) {
220220
Instruction *InsertPt = nullptr;
221221
std::tie(NBB, InsertPt) = findNearestInsertPt(DT, OtherUseCases);
222222
InsertPt = InsertPt ? InsertPt : NBB->getTerminator();
223-
if (genx::isSafeToMoveInstCheckAVLoadKill(MadSumOpndInst, InsertPt,
224-
Baling))
223+
if (genx::isSafeToSink_CheckAVLoadKill(MadSumOpndInst, InsertPt,
224+
Baling))
225225
MadSumOpndInst->moveBefore(InsertPt);
226226
} else
227227
OrphanedMadSumOpndInst.push_back(MadSumOpndInst);
@@ -384,7 +384,7 @@ bool GenXIMadPostLegalization::fixMadChain(BasicBlock *BB) {
384384
// Skip movement of a bale sourcing a global volatile load predecessor
385385
// if it can result in potential clobbering by an intervening vstore.
386386
// Skip phi which is not movable.
387-
if (!genx::isSafeToMoveBaleCheckAVLoadKill(*Bale, Pos) ||
387+
if (!genx::isSafeToSink_CheckAVLoadKill(*Bale, Pos) ||
388388
isa<PHINode>(BaleInst->Inst))
389389
break;
390390
BaleInst->Inst->moveBefore(Pos);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2023 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
//
10+
/// GenXLegalizeGVLoadUses
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "GenX.h"
15+
#include "GenXUtil.h"
16+
17+
#include "llvm/IR/Function.h"
18+
#include "llvm/InitializePasses.h"
19+
#include "llvm/Pass.h"
20+
#include "llvm/Support/Debug.h"
21+
22+
#include "GenXRegionUtils.h"
23+
24+
#define DEBUG_TYPE "GENX_LEGALIZE_GVLOAD_USERS"
25+
26+
using namespace llvm;
27+
28+
class GenXLegalizeGVLoadUses : public FunctionPass
29+
{
30+
public:
31+
static char ID;
32+
explicit GenXLegalizeGVLoadUses() : FunctionPass(ID) {}
33+
llvm::StringRef getPassName() const override { return "Fix illegal gvload users."; }
34+
bool runOnFunction(Function& F) override;
35+
};
36+
37+
bool GenXLegalizeGVLoadUses::runOnFunction(Function& F) {
38+
bool Changed = false;
39+
for (auto &BB : F)
40+
for (auto II = BB.begin(); II != BB.end();) {
41+
auto *Inst = &*II++;
42+
if (genx::isAGVLoad(Inst))
43+
Changed |= genx::legalizeGVLoadForbiddenUses(Inst);
44+
}
45+
return Changed;
46+
}
47+
48+
char GenXLegalizeGVLoadUses::ID = 0;
49+
namespace llvm {
50+
void initializeGenXLegalizeGVLoadUsesPass(PassRegistry &);
51+
}
52+
INITIALIZE_PASS_BEGIN(GenXLegalizeGVLoadUses, "GenXLegalizeGVLoadUses", "GenXLegalizeGVLoadUses", false, false)
53+
INITIALIZE_PASS_END(GenXLegalizeGVLoadUses, "GenXLegalizeGVLoadUses", "GenXLegalizeGVLoadUses", false, false)
54+
FunctionPass *llvm::createGenXLegalizeGVLoadUsesPass() {
55+
initializeGenXLegalizeGVLoadUsesPass(*PassRegistry::getPassRegistry());
56+
return new GenXLegalizeGVLoadUses;
57+
}

IGC/VectorCompiler/lib/GenXCodeGen/GenXRegionCollapsing.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,6 @@ void GenXRegionCollapsing::runOnBasicBlock(BasicBlock *BB) {
230230
}
231231

232232
if (Value *V = simplifyRegionInst(Inst, DL)) {
233-
if (isa<Instruction>(V) &&
234-
!genx::isSafeToReplaceInstCheckAVLoadKill(Inst, cast<Instruction>(V)))
235-
continue;
236233
Inst->replaceAllUsesWith(V);
237234
Modified = true;
238235
continue;
@@ -530,7 +527,7 @@ void GenXRegionCollapsing::processRdRegion(Instruction *InnerRd)
530527
for (;;) {
531528
Instruction *OuterRd = dyn_cast<Instruction>(InnerRd->getOperand(0));
532529

533-
if (OuterRd && !genx::isSafeToMoveInstCheckAVLoadKill(OuterRd, InnerRd))
530+
if (OuterRd && !genx::isSafeToMove_CheckAVLoadKill(OuterRd, InnerRd, DT))
534531
break;
535532

536533
// Go through any bitcasts and up to one sext/zext if necessary to find the
@@ -767,7 +764,7 @@ void GenXRegionCollapsing::processWrRegionElim(Instruction *OuterWr)
767764
InnerWr->getOperand(GenXIntrinsic::GenXRegion::OldValueOperandNum);
768765

769766
if (auto *I = dyn_cast<Instruction>(InnerWrOldV);
770-
I && !genx::isSafeToUseAtPosCheckAVLoadKill(I, OuterWr))
767+
I && !genx::isSafeToUse_CheckAVLoadKill(I, OuterWr, DT))
771768
return;
772769

773770
Region InnerR = genx::makeRegionFromBaleInfo(InnerWr, BaleInfo(),
@@ -917,7 +914,7 @@ Instruction *GenXRegionCollapsing::processWrRegion(Instruction *OuterWr)
917914
dyn_cast<Instruction>(genx::getBitCastedValue(cast<Value>(InnerWr)));
918915

919916
if (!GenXIntrinsic::isWrRegion(InnerWr) ||
920-
!genx::isSafeToMoveInstCheckAVLoadKill(InnerWr, OuterWr))
917+
!genx::isSafeToMove_CheckAVLoadKill(InnerWr, OuterWr, DT))
921918
return OuterWr;
922919

923920
// Process inner wrregions first, recursively.
@@ -1033,7 +1030,7 @@ Instruction *GenXRegionCollapsing::processWrRegionSplat(Instruction *OuterWr)
10331030
dyn_cast<Instruction>(genx::getBitCastedValue(cast<Value>(InnerWr)));
10341031

10351032
if (!GenXIntrinsic::isWrRegion(InnerWr) ||
1036-
!genx::isSafeToMoveInstCheckAVLoadKill(InnerWr, OuterWr))
1033+
!genx::isSafeToMove_CheckAVLoadKill(InnerWr, OuterWr, DT))
10371034
return OuterWr;
10381035

10391036
// Process inner wrregions first, recursively.

0 commit comments

Comments
 (0)