Skip to content

Commit 438cebe

Browse files
committed
[AMDGPU] SelectionDAG divergence tracking should take into account Target divergency.
1 parent 8631b4f commit 438cebe

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

llvm/include/llvm/CodeGen/SelectionDAG.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ class SelectionDAG {
238238
LLVMContext *Context;
239239
CodeGenOptLevel OptLevel;
240240

241+
bool DivergentTarget = false;
242+
241243
UniformityInfo *UA = nullptr;
242244
FunctionLoweringInfo * FLI = nullptr;
243245

@@ -471,14 +473,16 @@ class SelectionDAG {
471473
Pass *PassPtr, const TargetLibraryInfo *LibraryInfo,
472474
UniformityInfo *UA, ProfileSummaryInfo *PSIin,
473475
BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI,
474-
FunctionVarLocs const *FnVarLocs);
476+
FunctionVarLocs const *FnVarLocs, bool HasDivergency);
475477

476478
void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE,
477479
MachineFunctionAnalysisManager &AM,
478480
const TargetLibraryInfo *LibraryInfo, UniformityInfo *UA,
479481
ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin,
480-
MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs) {
481-
init(NewMF, NewORE, nullptr, LibraryInfo, UA, PSIin, BFIin, MMI, FnVarLocs);
482+
MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs,
483+
bool HasDivergency) {
484+
init(NewMF, NewORE, nullptr, LibraryInfo, UA, PSIin, BFIin, MMI, FnVarLocs,
485+
HasDivergency);
482486
MFAM = &AM;
483487
}
484488

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ void SelectionDAG::init(MachineFunction &NewMF,
13681368
const TargetLibraryInfo *LibraryInfo,
13691369
UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
13701370
BlockFrequencyInfo *BFIin, MachineModuleInfo &MMIin,
1371-
FunctionVarLocs const *VarLocs) {
1371+
FunctionVarLocs const *VarLocs, bool HasDivergency) {
13721372
MF = &NewMF;
13731373
SDAGISelPass = PassPtr;
13741374
ORE = &NewORE;
@@ -1381,6 +1381,7 @@ void SelectionDAG::init(MachineFunction &NewMF,
13811381
BFI = BFIin;
13821382
MMI = &MMIin;
13831383
FnVarLocs = VarLocs;
1384+
DivergentTarget = HasDivergency;
13841385
}
13851386

13861387
SelectionDAG::~SelectionDAG() {
@@ -2327,7 +2328,8 @@ SDValue SelectionDAG::getRegister(Register Reg, EVT VT) {
23272328
return SDValue(E, 0);
23282329

23292330
auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2330-
N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2331+
N->SDNodeBits.IsDivergent =
2332+
DivergentTarget ? TLI->isSDNodeSourceOfDivergence(N, FLI, UA) : false;
23312333
CSEMap.InsertNode(N, IP);
23322334
InsertNode(N);
23332335
return SDValue(N, 0);
@@ -10929,7 +10931,8 @@ SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
1092910931
// Now we update the operands.
1093010932
N->OperandList[0].set(Op);
1093110933

10932-
updateDivergence(N);
10934+
if (DivergentTarget)
10935+
updateDivergence(N);
1093310936
// If this gets put into a CSE map, add it.
1093410937
if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1093510938
return N;
@@ -10958,7 +10961,8 @@ SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
1095810961
if (N->OperandList[1] != Op2)
1095910962
N->OperandList[1].set(Op2);
1096010963

10961-
updateDivergence(N);
10964+
if (DivergentTarget)
10965+
updateDivergence(N);
1096210966
// If this gets put into a CSE map, add it.
1096310967
if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1096410968
return N;
@@ -11009,7 +11013,8 @@ UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
1100911013
if (N->OperandList[i] != Ops[i])
1101011014
N->OperandList[i].set(Ops[i]);
1101111015

11012-
updateDivergence(N);
11016+
if (DivergentTarget)
11017+
updateDivergence(N);
1101311018
// If this gets put into a CSE map, add it.
1101411019
if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1101511020
return N;
@@ -11796,8 +11801,9 @@ void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
1179611801
SDUse &Use = *UI;
1179711802
++UI;
1179811803
Use.set(To);
11799-
if (To->isDivergent() != From->isDivergent())
11800-
updateDivergence(User);
11804+
if (DivergentTarget)
11805+
if (To->isDivergent() != From->isDivergent())
11806+
updateDivergence(User);
1180111807
} while (UI != UE && UI->getUser() == User);
1180211808
// Now that we have modified User, add it back to the CSE maps. If it
1180311809
// already exists there, recursively merge the results together.
@@ -11854,8 +11860,9 @@ void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
1185411860
SDUse &Use = *UI;
1185511861
++UI;
1185611862
Use.setNode(To);
11857-
if (To->isDivergent() != From->isDivergent())
11858-
updateDivergence(User);
11863+
if (DivergentTarget)
11864+
if (To->isDivergent() != From->isDivergent())
11865+
updateDivergence(User);
1185911866
} while (UI != UE && UI->getUser() == User);
1186011867

1186111868
// Now that we have modified User, add it back to the CSE maps. If it
@@ -11907,8 +11914,9 @@ void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
1190711914
To_IsDivergent |= ToOp->isDivergent();
1190811915
} while (UI != UE && UI->getUser() == User);
1190911916

11910-
if (To_IsDivergent != From->isDivergent())
11911-
updateDivergence(User);
11917+
if (DivergentTarget)
11918+
if (To_IsDivergent != From->isDivergent())
11919+
updateDivergence(User);
1191211920

1191311921
// Now that we have modified User, add it back to the CSE maps. If it
1191411922
// already exists there, recursively merge the results together.
@@ -11968,8 +11976,9 @@ void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
1196811976

1196911977
++UI;
1197011978
Use.set(To);
11971-
if (To->isDivergent() != From->isDivergent())
11972-
updateDivergence(User);
11979+
if (DivergentTarget)
11980+
if (To->isDivergent() != From->isDivergent())
11981+
updateDivergence(User);
1197311982
} while (UI != UE && UI->getUser() == User);
1197411983
// We are iterating over all uses of the From node, so if a use
1197511984
// doesn't use the specific value, no changes are made.

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,10 @@ void SelectionDAGISel::initializeAnalysisResults(
482482
MachineModuleInfo &MMI =
483483
MAMP.getCachedResult<MachineModuleAnalysis>(*Fn.getParent())->getMMI();
484484

485-
CurDAG->init(*MF, *ORE, MFAM, LibInfo, UA, PSI, BFI, MMI, FnVarLocs);
485+
TTI = &FAM.getResult<TargetIRAnalysis>(Fn);
486+
487+
CurDAG->init(*MF, *ORE, MFAM, LibInfo, UA, PSI, BFI, MMI, FnVarLocs,
488+
TTI->hasBranchDivergence());
486489

487490
// Now get the optional analyzes if we want to.
488491
// This is based on the possibly changed OptLevel (after optnone is taken
@@ -500,10 +503,6 @@ void SelectionDAGISel::initializeAnalysisResults(
500503
BatchAA = std::nullopt;
501504

502505
SP = &FAM.getResult<SSPLayoutAnalysis>(Fn);
503-
504-
#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
505-
TTI = &FAM.getResult<TargetIRAnalysis>(Fn);
506-
#endif
507506
}
508507

509508
void SelectionDAGISel::initializeAnalysisResults(MachineFunctionPass &MFP) {
@@ -539,7 +538,10 @@ void SelectionDAGISel::initializeAnalysisResults(MachineFunctionPass &MFP) {
539538
MachineModuleInfo &MMI =
540539
MFP.getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
541540

542-
CurDAG->init(*MF, *ORE, &MFP, LibInfo, UA, PSI, BFI, MMI, FnVarLocs);
541+
TTI = &MFP.getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn);
542+
543+
CurDAG->init(*MF, *ORE, &MFP, LibInfo, UA, PSI, BFI, MMI, FnVarLocs,
544+
TTI->hasBranchDivergence());
543545

544546
// Now get the optional analyzes if we want to.
545547
// This is based on the possibly changed OptLevel (after optnone is taken
@@ -558,10 +560,6 @@ void SelectionDAGISel::initializeAnalysisResults(MachineFunctionPass &MFP) {
558560
BatchAA = std::nullopt;
559561

560562
SP = &MFP.getAnalysis<StackProtector>().getLayoutInfo();
561-
562-
#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
563-
TTI = &MFP.getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn);
564-
#endif
565563
}
566564

567565
bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {

0 commit comments

Comments
 (0)