-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[AMDGPU][Attributor] Rework update of AAAMDWavesPerEU
#123995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1113,47 +1113,25 @@ struct AAAMDWavesPerEU : public AAAMDSizeRangeAttribute { | |
Function *F = getAssociatedFunction(); | ||
auto &InfoCache = static_cast<AMDGPUInformationCache &>(A.getInfoCache()); | ||
|
||
auto TakeRange = [&](std::pair<unsigned, unsigned> R) { | ||
auto [Min, Max] = R; | ||
ConstantRange Range(APInt(32, Min), APInt(32, Max + 1)); | ||
IntegerRangeState RangeState(Range); | ||
clampStateAndIndicateChange(this->getState(), RangeState); | ||
indicateOptimisticFixpoint(); | ||
}; | ||
|
||
std::pair<unsigned, unsigned> MaxWavesPerEURange{ | ||
1U, InfoCache.getMaxWavesPerEU(*F)}; | ||
|
||
// If the attribute exists, we will honor it if it is not the default. | ||
if (auto Attr = InfoCache.getWavesPerEUAttr(*F)) { | ||
std::pair<unsigned, unsigned> MaxWavesPerEURange{ | ||
1U, InfoCache.getMaxWavesPerEU(*F)}; | ||
if (*Attr != MaxWavesPerEURange) { | ||
TakeRange(*Attr); | ||
auto [Min, Max] = *Attr; | ||
ConstantRange Range(APInt(32, Min), APInt(32, Max + 1)); | ||
IntegerRangeState RangeState(Range); | ||
this->getState() = RangeState; | ||
indicateOptimisticFixpoint(); | ||
return; | ||
} | ||
} | ||
|
||
// Unlike AAAMDFlatWorkGroupSize, it's getting trickier here. Since the | ||
// calculation of waves per EU involves flat work group size, we can't | ||
// simply use an assumed flat work group size as a start point, because the | ||
// update of flat work group size is in an inverse direction of waves per | ||
// EU. However, we can still do something if it is an entry function. Since | ||
// an entry function is a terminal node, and flat work group size either | ||
// from attribute or default will be used anyway, we can take that value and | ||
// calculate the waves per EU based on it. This result can't be updated by | ||
// no means, but that could still allow us to propagate it. | ||
if (AMDGPU::isEntryFunctionCC(F->getCallingConv())) { | ||
std::pair<unsigned, unsigned> FlatWorkGroupSize; | ||
if (auto Attr = InfoCache.getFlatWorkGroupSizeAttr(*F)) | ||
FlatWorkGroupSize = *Attr; | ||
else | ||
FlatWorkGroupSize = InfoCache.getDefaultFlatWorkGroupSize(*F); | ||
TakeRange(InfoCache.getEffectiveWavesPerEU(*F, MaxWavesPerEURange, | ||
FlatWorkGroupSize)); | ||
} | ||
if (AMDGPU::isEntryFunctionCC(F->getCallingConv())) | ||
indicatePessimisticFixpoint(); | ||
} | ||
|
||
ChangeStatus updateImpl(Attributor &A) override { | ||
auto &InfoCache = static_cast<AMDGPUInformationCache &>(A.getInfoCache()); | ||
ChangeStatus Change = ChangeStatus::UNCHANGED; | ||
|
||
auto CheckCallSite = [&](AbstractCallSite CS) { | ||
|
@@ -1162,24 +1140,21 @@ struct AAAMDWavesPerEU : public AAAMDSizeRangeAttribute { | |
LLVM_DEBUG(dbgs() << '[' << getName() << "] Call " << Caller->getName() | ||
<< "->" << Func->getName() << '\n'); | ||
|
||
const auto *CallerInfo = A.getAAFor<AAAMDWavesPerEU>( | ||
const auto *CallerAA = A.getAAFor<AAAMDWavesPerEU>( | ||
*this, IRPosition::function(*Caller), DepClassTy::REQUIRED); | ||
const auto *AssumedGroupSize = A.getAAFor<AAAMDFlatWorkGroupSize>( | ||
*this, IRPosition::function(*Func), DepClassTy::REQUIRED); | ||
if (!CallerInfo || !AssumedGroupSize || !CallerInfo->isValidState() || | ||
!AssumedGroupSize->isValidState()) | ||
if (!CallerAA || !CallerAA->isValidState()) | ||
return false; | ||
|
||
unsigned Min, Max; | ||
std::tie(Min, Max) = InfoCache.getEffectiveWavesPerEU( | ||
*Caller, | ||
{CallerInfo->getAssumed().getLower().getZExtValue(), | ||
CallerInfo->getAssumed().getUpper().getZExtValue() - 1}, | ||
{AssumedGroupSize->getAssumed().getLower().getZExtValue(), | ||
AssumedGroupSize->getAssumed().getUpper().getZExtValue() - 1}); | ||
ConstantRange CallerRange(APInt(32, Min), APInt(32, Max + 1)); | ||
IntegerRangeState CallerRangeState(CallerRange); | ||
Change |= clampStateAndIndicateChange(this->getState(), CallerRangeState); | ||
ConstantRange Assumed = getAssumed(); | ||
unsigned Min = std::max(Assumed.getLower().getZExtValue(), | ||
CallerAA->getAssumed().getLower().getZExtValue()); | ||
unsigned Max = std::max(Assumed.getUpper().getZExtValue(), | ||
CallerAA->getAssumed().getUpper().getZExtValue()); | ||
ConstantRange Range(APInt(32, Min), APInt(32, Max)); | ||
IntegerRangeState RangeState(Range); | ||
getState() = RangeState; | ||
Change |= getState() == Assumed ? ChangeStatus::UNCHANGED | ||
: ChangeStatus::CHANGED; | ||
|
||
return true; | ||
}; | ||
|
@@ -1323,6 +1298,74 @@ struct AAAMDGPUNoAGPR | |
|
||
const char AAAMDGPUNoAGPR::ID = 0; | ||
|
||
/// Performs the final check and updates the 'amdgpu-waves-per-eu' attribute | ||
/// based on the finalized 'amdgpu-flat-work-group-size' attribute. | ||
/// Both attributes start with narrow ranges that expand during iteration. | ||
/// However, a narrower flat-workgroup-size leads to a wider waves-per-eu range, | ||
/// preventing optimal updates later. Therefore, waves-per-eu can't be updated | ||
/// with intermediate values during the attributor run. We defer the | ||
/// finalization of waves-per-eu until after the flat-workgroup-size is | ||
/// finalized. | ||
/// TODO: Remove this and move similar logic back into the attributor run once | ||
/// we have a better representation for waves-per-eu. | ||
static bool updateWavesPerEU(Module &M, TargetMachine &TM) { | ||
bool Changed = false; | ||
|
||
LLVMContext &Ctx = M.getContext(); | ||
|
||
for (Function &F : M) { | ||
if (F.isDeclaration()) | ||
continue; | ||
|
||
const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(F); | ||
shiltian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
std::optional<std::pair<unsigned, std::optional<unsigned>>> | ||
FlatWgrpSizeAttr = | ||
AMDGPU::getIntegerPairAttribute(F, "amdgpu-flat-work-group-size"); | ||
|
||
unsigned MinWavesPerEU = ST.getMinWavesPerEU(); | ||
unsigned MaxWavesPerEU = ST.getMaxWavesPerEU(); | ||
|
||
unsigned MinFlatWgrpSize = ST.getMinFlatWorkGroupSize(); | ||
unsigned MaxFlatWgrpSize = ST.getMaxFlatWorkGroupSize(); | ||
if (FlatWgrpSizeAttr.has_value()) { | ||
MinFlatWgrpSize = FlatWgrpSizeAttr->first; | ||
MaxFlatWgrpSize = *(FlatWgrpSizeAttr->second); | ||
} | ||
shiltian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Start with the "best" range. | ||
unsigned Min = MinWavesPerEU; | ||
unsigned Max = MinWavesPerEU; | ||
|
||
// Compute the range from flat workgroup size. `getWavesPerEU` will also | ||
// account for the 'amdgpu-waves-er-eu' attribute. | ||
auto [MinFromFlatWgrpSize, MaxFromFlatWgrpSize] = | ||
ST.getWavesPerEU(F, {MinFlatWgrpSize, MaxFlatWgrpSize}); | ||
|
||
// For the lower bound, we have to "tighten" it. | ||
Min = std::max(Min, MinFromFlatWgrpSize); | ||
// For the upper bound, we have to "extend" it. | ||
Max = std::max(Max, MaxFromFlatWgrpSize); | ||
|
||
// Clamp the range to the max range. | ||
Min = std::max(Min, MinWavesPerEU); | ||
Max = std::min(Max, MaxWavesPerEU); | ||
|
||
// Update the attribute if it is not the max. | ||
if (Min != MinWavesPerEU || Max != MaxWavesPerEU) { | ||
SmallString<10> Buffer; | ||
raw_svector_ostream OS(Buffer); | ||
OS << Min << ',' << Max; | ||
Attribute OldAttr = F.getFnAttribute("amdgpu-waves-per-eu"); | ||
Attribute NewAttr = Attribute::get(Ctx, "amdgpu-waves-per-eu", OS.str()); | ||
F.addFnAttr(NewAttr); | ||
Changed |= OldAttr == NewAttr; | ||
} | ||
} | ||
|
||
return Changed; | ||
} | ||
|
||
static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM, | ||
AMDGPUAttributorOptions Options, | ||
ThinOrFullLTOPhase LTOPhase) { | ||
|
@@ -1396,8 +1439,11 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM, | |
} | ||
} | ||
|
||
ChangeStatus Change = A.run(); | ||
return Change == ChangeStatus::CHANGED; | ||
bool Changed = A.run() == ChangeStatus::CHANGED; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be more refined on waves per eu or flat workgroup size changing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't follow your question. Can you expatiate it? |
||
|
||
Changed |= updateWavesPerEU(M, TM); | ||
|
||
return Changed; | ||
} | ||
|
||
class AMDGPUAttributorLegacy : public ModulePass { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.