Skip to content

Commit

Permalink
[nfc][amdgpu] LDS. Move selection logic up the stack.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonChesterfield committed Jul 19, 2022
1 parent a62868a commit 2224bbc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
24 changes: 13 additions & 11 deletions llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp
Expand Up @@ -148,7 +148,9 @@ class AMDGPULowerModuleLDS : public ModulePass {
bool runOnModule(Module &M) override {
CallGraph CG = CallGraph(M);
bool Changed = superAlignLDSGlobals(M);
Changed |= processUsedLDS(CG, M);
std::vector<GlobalVariable *> ModuleScopeVariables =
AMDGPU::findVariablesToLower(M, nullptr);
Changed |= processUsedLDS(CG, M, ModuleScopeVariables);

for (Function &F : M.functions()) {
if (F.isDeclaration())
Expand All @@ -157,7 +159,9 @@ class AMDGPULowerModuleLDS : public ModulePass {
// Only lower compute kernels' LDS.
if (!AMDGPU::isKernel(F.getCallingConv()))
continue;
Changed |= processUsedLDS(CG, M, &F);
std::vector<GlobalVariable *> KernelUsedVariables =
AMDGPU::findVariablesToLower(M, &F);
Changed |= processUsedLDS(CG, M, KernelUsedVariables, &F);
}

return Changed;
Expand Down Expand Up @@ -208,22 +212,20 @@ class AMDGPULowerModuleLDS : public ModulePass {
return Changed;
}

bool processUsedLDS(CallGraph const &CG, Module &M, Function *F = nullptr) {
bool processUsedLDS(CallGraph const &CG, Module &M,
std::vector<GlobalVariable *> const &LDSVarsToTransform,
Function *F = nullptr) {
LLVMContext &Ctx = M.getContext();
const DataLayout &DL = M.getDataLayout();

// Find variables to move into new struct instance
std::vector<GlobalVariable *> FoundLocalVars =
AMDGPU::findVariablesToLower(M, F);

if (FoundLocalVars.empty()) {
if (LDSVarsToTransform.empty()) {
// No variables to rewrite, no changes made.
return false;
}

SmallVector<OptimizedStructLayoutField, 8> LayoutFields;
LayoutFields.reserve(FoundLocalVars.size());
for (GlobalVariable *GV : FoundLocalVars) {
LayoutFields.reserve(LDSVarsToTransform.size());
for (GlobalVariable *GV : LDSVarsToTransform) {
OptimizedStructLayoutField F(GV, DL.getTypeAllocSize(GV->getValueType()),
AMDGPU::getAlign(DL, GV));
LayoutFields.emplace_back(F);
Expand All @@ -232,7 +234,7 @@ class AMDGPULowerModuleLDS : public ModulePass {
performOptimizedStructLayout(LayoutFields);

std::vector<GlobalVariable *> LocalVars;
LocalVars.reserve(FoundLocalVars.size()); // will be at least this large
LocalVars.reserve(LDSVarsToTransform.size()); // will be at least this large
{
// This usually won't need to insert any padding, perhaps avoid the alloc
uint64_t CurrentOffset = 0;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AMDGPU/AMDGPUReplaceLDSUseWithPointer.cpp
Expand Up @@ -141,7 +141,7 @@ class ReplaceLDSUseImpl {
std::vector<GlobalVariable *> collectLDSRequiringPointerReplace() {
// Collect LDS which requires module lowering.
std::vector<GlobalVariable *> LDSGlobals =
llvm::AMDGPU::findVariablesToLower(M);
llvm::AMDGPU::findVariablesToLower(M, nullptr);

// Remove LDS which don't qualify for replacement.
llvm::erase_if(LDSGlobals, [&](GlobalVariable *GV) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AMDGPU/Utils/AMDGPUMemoryUtils.h
Expand Up @@ -30,7 +30,7 @@ namespace AMDGPU {
Align getAlign(DataLayout const &DL, const GlobalVariable *GV);

std::vector<GlobalVariable *> findVariablesToLower(Module &M,
const Function *F = nullptr);
const Function *F);

/// Replace all uses of constant \p C with instructions in \p F.
void replaceConstantUsesInFunction(ConstantExpr *C, const Function *F);
Expand Down

0 comments on commit 2224bbc

Please sign in to comment.