Skip to content

Commit 3a75551

Browse files
committed
Reland "[NFC][AMDGPULowerModuleLDSPass] Factorize repetead sort code"
Fixed compilation error and reudndant copy warning Differential Revision: https://reviews.llvm.org/D154977
1 parent f7684d8 commit 3a75551

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp

+17-25
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ bool isKernelLDS(const Function *F) {
245245
return AMDGPU::isKernel(F->getCallingConv());
246246
}
247247

248+
template <typename T> std::vector<T> sortByName(std::vector<T> &&V) {
249+
llvm::sort(V.begin(), V.end(), [](const auto *L, const auto *R) {
250+
return L->getName() < R->getName();
251+
});
252+
return {std::move(V)};
253+
}
254+
248255
class AMDGPULowerModuleLDS : public ModulePass {
249256

250257
static void
@@ -733,10 +740,7 @@ class AMDGPULowerModuleLDS : public ModulePass {
733740
}
734741

735742
// Put them in an arbitrary but reproducible order
736-
llvm::sort(OrderedKernels.begin(), OrderedKernels.end(),
737-
[](const Function *lhs, const Function *rhs) -> bool {
738-
return lhs->getName() < rhs->getName();
739-
});
743+
OrderedKernels = sortByName(std::move(OrderedKernels));
740744

741745
// Annotate the kernels with their order in this vector
742746
LLVMContext &Ctx = M->getContext();
@@ -1185,13 +1189,8 @@ class AMDGPULowerModuleLDS : public ModulePass {
11851189

11861190
// The order must be consistent between lookup table and accesses to
11871191
// lookup table
1188-
std::vector<GlobalVariable *> TableLookupVariablesOrdered(
1189-
TableLookupVariables.begin(), TableLookupVariables.end());
1190-
llvm::sort(TableLookupVariablesOrdered.begin(),
1191-
TableLookupVariablesOrdered.end(),
1192-
[](const GlobalVariable *lhs, const GlobalVariable *rhs) {
1193-
return lhs->getName() < rhs->getName();
1194-
});
1192+
auto TableLookupVariablesOrdered = sortByName(std::vector(
1193+
TableLookupVariables.begin(), TableLookupVariables.end()));
11951194

11961195
GlobalVariable *LookupTable = buildLookupTable(
11971196
M, TableLookupVariablesOrdered, OrderedKernels, KernelToReplacement);
@@ -1338,12 +1337,9 @@ class AMDGPULowerModuleLDS : public ModulePass {
13381337
// The order of fields in this struct depends on the order of
13391338
// varables in the argument which varies when changing how they
13401339
// are identified, leading to spurious test breakage.
1341-
std::vector<GlobalVariable *> Sorted(LDSVarsToTransform.begin(),
1342-
LDSVarsToTransform.end());
1343-
llvm::sort(Sorted.begin(), Sorted.end(),
1344-
[](const GlobalVariable *lhs, const GlobalVariable *rhs) {
1345-
return lhs->getName() < rhs->getName();
1346-
});
1340+
auto Sorted = sortByName(
1341+
std::vector(LDSVarsToTransform.begin(), LDSVarsToTransform.end()));
1342+
13471343
for (GlobalVariable *GV : Sorted) {
13481344
OptimizedStructLayoutField F(GV,
13491345
DL.getTypeAllocSize(GV->getValueType()),
@@ -1424,19 +1420,15 @@ class AMDGPULowerModuleLDS : public ModulePass {
14241420
template <typename PredicateTy>
14251421
static void replaceLDSVariablesWithStruct(
14261422
Module &M, DenseSet<GlobalVariable *> const &LDSVarsToTransformArg,
1427-
LDSVariableReplacement Replacement, PredicateTy Predicate) {
1423+
const LDSVariableReplacement &Replacement, PredicateTy Predicate) {
14281424
LLVMContext &Ctx = M.getContext();
14291425
const DataLayout &DL = M.getDataLayout();
14301426

14311427
// A hack... we need to insert the aliasing info in a predictable order for
14321428
// lit tests. Would like to have them in a stable order already, ideally the
14331429
// same order they get allocated, which might mean an ordered set container
1434-
std::vector<GlobalVariable *> LDSVarsToTransform(
1435-
LDSVarsToTransformArg.begin(), LDSVarsToTransformArg.end());
1436-
llvm::sort(LDSVarsToTransform.begin(), LDSVarsToTransform.end(),
1437-
[](const GlobalVariable *lhs, const GlobalVariable *rhs) {
1438-
return lhs->getName() < rhs->getName();
1439-
});
1430+
std::vector<GlobalVariable *> LDSVarsToTransform = sortByName(std::vector(
1431+
LDSVarsToTransformArg.begin(), LDSVarsToTransformArg.end()));
14401432

14411433
// Create alias.scope and their lists. Each field in the new structure
14421434
// does not alias with all other fields.
@@ -1458,7 +1450,7 @@ class AMDGPULowerModuleLDS : public ModulePass {
14581450
// field of the instance that will be allocated by AMDGPUMachineFunction
14591451
for (size_t I = 0; I < NumberVars; I++) {
14601452
GlobalVariable *GV = LDSVarsToTransform[I];
1461-
Constant *GEP = Replacement.LDSVarsToConstantGEP[GV];
1453+
Constant *GEP = Replacement.LDSVarsToConstantGEP.at(GV);
14621454

14631455
GV->replaceUsesWithIf(GEP, Predicate);
14641456

0 commit comments

Comments
 (0)