@@ -1113,47 +1113,25 @@ struct AAAMDWavesPerEU : public AAAMDSizeRangeAttribute {
1113
1113
Function *F = getAssociatedFunction ();
1114
1114
auto &InfoCache = static_cast <AMDGPUInformationCache &>(A.getInfoCache ());
1115
1115
1116
- auto TakeRange = [&](std::pair<unsigned , unsigned > R) {
1117
- auto [Min, Max] = R;
1118
- ConstantRange Range (APInt (32 , Min), APInt (32 , Max + 1 ));
1119
- IntegerRangeState RangeState (Range);
1120
- clampStateAndIndicateChange (this ->getState (), RangeState);
1121
- indicateOptimisticFixpoint ();
1122
- };
1123
-
1124
- std::pair<unsigned , unsigned > MaxWavesPerEURange{
1125
- 1U , InfoCache.getMaxWavesPerEU (*F)};
1126
-
1127
1116
// If the attribute exists, we will honor it if it is not the default.
1128
1117
if (auto Attr = InfoCache.getWavesPerEUAttr (*F)) {
1118
+ std::pair<unsigned , unsigned > MaxWavesPerEURange{
1119
+ 1U , InfoCache.getMaxWavesPerEU (*F)};
1129
1120
if (*Attr != MaxWavesPerEURange) {
1130
- TakeRange (*Attr);
1121
+ auto [Min, Max] = *Attr;
1122
+ ConstantRange Range (APInt (32 , Min), APInt (32 , Max + 1 ));
1123
+ IntegerRangeState RangeState (Range);
1124
+ this ->getState () = RangeState;
1125
+ indicateOptimisticFixpoint ();
1131
1126
return ;
1132
1127
}
1133
1128
}
1134
1129
1135
- // Unlike AAAMDFlatWorkGroupSize, it's getting trickier here. Since the
1136
- // calculation of waves per EU involves flat work group size, we can't
1137
- // simply use an assumed flat work group size as a start point, because the
1138
- // update of flat work group size is in an inverse direction of waves per
1139
- // EU. However, we can still do something if it is an entry function. Since
1140
- // an entry function is a terminal node, and flat work group size either
1141
- // from attribute or default will be used anyway, we can take that value and
1142
- // calculate the waves per EU based on it. This result can't be updated by
1143
- // no means, but that could still allow us to propagate it.
1144
- if (AMDGPU::isEntryFunctionCC (F->getCallingConv ())) {
1145
- std::pair<unsigned , unsigned > FlatWorkGroupSize;
1146
- if (auto Attr = InfoCache.getFlatWorkGroupSizeAttr (*F))
1147
- FlatWorkGroupSize = *Attr;
1148
- else
1149
- FlatWorkGroupSize = InfoCache.getDefaultFlatWorkGroupSize (*F);
1150
- TakeRange (InfoCache.getEffectiveWavesPerEU (*F, MaxWavesPerEURange,
1151
- FlatWorkGroupSize));
1152
- }
1130
+ if (AMDGPU::isEntryFunctionCC (F->getCallingConv ()))
1131
+ indicatePessimisticFixpoint ();
1153
1132
}
1154
1133
1155
1134
ChangeStatus updateImpl (Attributor &A) override {
1156
- auto &InfoCache = static_cast <AMDGPUInformationCache &>(A.getInfoCache ());
1157
1135
ChangeStatus Change = ChangeStatus::UNCHANGED;
1158
1136
1159
1137
auto CheckCallSite = [&](AbstractCallSite CS) {
@@ -1162,24 +1140,21 @@ struct AAAMDWavesPerEU : public AAAMDSizeRangeAttribute {
1162
1140
LLVM_DEBUG (dbgs () << ' [' << getName () << " ] Call " << Caller->getName ()
1163
1141
<< " ->" << Func->getName () << ' \n ' );
1164
1142
1165
- const auto *CallerInfo = A.getAAFor <AAAMDWavesPerEU>(
1143
+ const auto *CallerAA = A.getAAFor <AAAMDWavesPerEU>(
1166
1144
*this , IRPosition::function (*Caller), DepClassTy::REQUIRED);
1167
- const auto *AssumedGroupSize = A.getAAFor <AAAMDFlatWorkGroupSize>(
1168
- *this , IRPosition::function (*Func), DepClassTy::REQUIRED);
1169
- if (!CallerInfo || !AssumedGroupSize || !CallerInfo->isValidState () ||
1170
- !AssumedGroupSize->isValidState ())
1145
+ if (!CallerAA || !CallerAA->isValidState ())
1171
1146
return false ;
1172
1147
1173
- unsigned Min, Max ;
1174
- std::tie (Min, Max) = InfoCache. getEffectiveWavesPerEU (
1175
- *Caller,
1176
- {CallerInfo-> getAssumed (). getLower ().getZExtValue (),
1177
- CallerInfo ->getAssumed ().getUpper ().getZExtValue () - 1 },
1178
- {AssumedGroupSize-> getAssumed (). getLower (). getZExtValue (),
1179
- AssumedGroupSize-> getAssumed (). getUpper (). getZExtValue () - 1 } );
1180
- ConstantRange CallerRange ( APInt ( 32 , Min), APInt ( 32 , Max + 1 )) ;
1181
- IntegerRangeState CallerRangeState (CallerRange);
1182
- Change |= clampStateAndIndicateChange ( this -> getState (), CallerRangeState) ;
1148
+ ConstantRange Assumed = this -> getAssumed () ;
1149
+ unsigned Min = std::max (Assumed. getLower (). getZExtValue (),
1150
+ CallerAA-> getAssumed (). getLower (). getZExtValue ());
1151
+ unsigned Max = std::max (Assumed. getUpper ().getZExtValue (),
1152
+ CallerAA ->getAssumed ().getUpper ().getZExtValue ());
1153
+ ConstantRange Range ( APInt ( 32 , Min), APInt ( 32 , Max));
1154
+ IntegerRangeState RangeState (Range );
1155
+ this -> getState () = RangeState ;
1156
+ Change |= this -> getState () == Assumed ? ChangeStatus::UNCHANGED
1157
+ : ChangeStatus::CHANGED ;
1183
1158
1184
1159
return true ;
1185
1160
};
@@ -1323,6 +1298,60 @@ struct AAAMDGPUNoAGPR
1323
1298
1324
1299
const char AAAMDGPUNoAGPR::ID = 0 ;
1325
1300
1301
+ // / The final check and update of the attribute 'amdgpu-waves-per-eu' based on
1302
+ // / the determined 'amdgpu-flat-work-group-size' attribute. We can't do this
1303
+ // / during attributor run because the two attributes grow in opposite direction,
1304
+ // / we should not use any intermediate value to calculate waves per eu until we
1305
+ // / have a determined flat workgroup size.
1306
+ static void updateWavesPerEU (Module &M, TargetMachine &TM) {
1307
+ for (Function &F : M) {
1308
+ if (F.isDeclaration ())
1309
+ continue ;
1310
+
1311
+ const GCNSubtarget &ST = TM.getSubtarget <GCNSubtarget>(F);
1312
+
1313
+ std::optional<std::pair<unsigned , std::optional<unsigned >>>
1314
+ FlatWgrpSizeAttr =
1315
+ AMDGPU::getIntegerPairAttribute (F, " amdgpu-flat-work-group-size" );
1316
+
1317
+ unsigned MinWavesPerEU = ST.getMinWavesPerEU ();
1318
+ unsigned MaxWavesPerEU = ST.getMaxWavesPerEU ();
1319
+
1320
+ unsigned MinFlatWgrpSize = ST.getMinFlatWorkGroupSize ();
1321
+ unsigned MaxFlatWgrpSize = ST.getMaxFlatWorkGroupSize ();
1322
+ if (FlatWgrpSizeAttr.has_value ()) {
1323
+ MinFlatWgrpSize = FlatWgrpSizeAttr->first ;
1324
+ MaxFlatWgrpSize = *(FlatWgrpSizeAttr->second );
1325
+ }
1326
+
1327
+ // Start with the max range.
1328
+ unsigned Min = MinWavesPerEU;
1329
+ unsigned Max = MinWavesPerEU;
1330
+
1331
+ // Compute the range from flat workgroup size. `getWavesPerEU` will also
1332
+ // account for the 'amdgpu-waves-er-eu' attribute.
1333
+ auto [MinFromFlatWgrpSize, MaxFromFlatWgrpSize] =
1334
+ ST.getWavesPerEU (F, {MinFlatWgrpSize, MaxFlatWgrpSize});
1335
+
1336
+ // For the lower bound, we have to "tighten" it.
1337
+ Min = std::max (Min, MinFromFlatWgrpSize);
1338
+ // For the upper bound, we have to "extend" it.
1339
+ Max = std::max (Max, MaxFromFlatWgrpSize);
1340
+
1341
+ // Clamp the range to the max range.
1342
+ Min = std::max (Min, MinWavesPerEU);
1343
+ Max = std::min (Max, MaxWavesPerEU);
1344
+
1345
+ // Update the attribute if it is not the max.
1346
+ if (Min != MinWavesPerEU || Max != MaxWavesPerEU) {
1347
+ SmallString<10 > Buffer;
1348
+ raw_svector_ostream OS (Buffer);
1349
+ OS << Min << ' ,' << Max;
1350
+ F.addFnAttr (" amdgpu-waves-per-eu" , OS.str ());
1351
+ }
1352
+ }
1353
+ }
1354
+
1326
1355
static bool runImpl (Module &M, AnalysisGetter &AG, TargetMachine &TM,
1327
1356
AMDGPUAttributorOptions Options,
1328
1357
ThinOrFullLTOPhase LTOPhase) {
@@ -1396,8 +1425,16 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
1396
1425
}
1397
1426
}
1398
1427
1399
- ChangeStatus Change = A.run ();
1400
- return Change == ChangeStatus::CHANGED;
1428
+ bool Changed = A.run () == ChangeStatus::CHANGED;
1429
+
1430
+ // We only update the waves-per-eu attribute at the final stage to avoid
1431
+ // setting it with intermediate values.
1432
+ if (Changed && (LTOPhase == ThinOrFullLTOPhase::None ||
1433
+ LTOPhase == ThinOrFullLTOPhase::FullLTOPostLink ||
1434
+ LTOPhase == ThinOrFullLTOPhase::ThinLTOPostLink))
1435
+ updateWavesPerEU (M, TM);
1436
+
1437
+ return Changed;
1401
1438
}
1402
1439
1403
1440
class AMDGPUAttributorLegacy : public ModulePass {
0 commit comments