[llvm-branch-commits] [llvm] [AMDGPU][Attributor] Add a pass parameter `closed-world` for AMDGPUAttributor pass (PR #101760)
Shilei Tian via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Aug 6 20:22:38 PDT 2024
https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/101760
>From f9e990a43908efc2e155c95f3cd4ddadefc4d6a1 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Fri, 2 Aug 2024 18:05:44 -0400
Subject: [PATCH] [AMDGPU][Attributor] Add a pass parameter `closed-world` for
AMDGPUAttributor pass
---
llvm/lib/Target/AMDGPU/AMDGPU.h | 11 +++++--
llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp | 11 ++++---
llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def | 12 +++++++-
.../lib/Target/AMDGPU/AMDGPUTargetMachine.cpp | 29 +++++++++++++++++--
.../CodeGen/AMDGPU/simple-indirect-call-2.ll | 2 +-
.../Other/amdgpu-pass-pipeline-parsing.ll | 12 ++++++++
6 files changed, 63 insertions(+), 14 deletions(-)
create mode 100644 llvm/test/Other/amdgpu-pass-pipeline-parsing.ll
diff --git a/llvm/lib/Target/AMDGPU/AMDGPU.h b/llvm/lib/Target/AMDGPU/AMDGPU.h
index 50aef36724f705..d8ed1d9db00e59 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPU.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPU.h
@@ -283,17 +283,22 @@ class AMDGPULowerKernelArgumentsPass
PreservedAnalyses run(Function &, FunctionAnalysisManager &);
};
+struct AMDGPUAttributorOptions {
+ bool IsClosedWorld = false;
+};
+
class AMDGPUAttributorPass : public PassInfoMixin<AMDGPUAttributorPass> {
private:
TargetMachine &TM;
+ AMDGPUAttributorOptions Options;
+
/// Asserts whether we can assume whole program visibility.
bool HasWholeProgramVisibility = false;
public:
- AMDGPUAttributorPass(TargetMachine &TM,
- bool HasWholeProgramVisibility = false)
- : TM(TM), HasWholeProgramVisibility(HasWholeProgramVisibility) {};
+ AMDGPUAttributorPass(TargetMachine &TM, AMDGPUAttributorOptions Options = {})
+ : TM(TM), Options(Options) {};
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp
index 9557005721cb15..d65e0ae92308e6 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp
@@ -1025,7 +1025,7 @@ static void addPreloadKernArgHint(Function &F, TargetMachine &TM) {
}
static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
- bool HasWholeProgramVisibility) {
+ AMDGPUAttributorOptions Options) {
SetVector<Function *> Functions;
for (Function &F : M) {
if (!F.isIntrinsic())
@@ -1044,7 +1044,7 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
&AAInstanceInfo::ID});
AttributorConfig AC(CGUpdater);
- AC.IsClosedWorldModule = HasWholeProgramVisibility;
+ AC.IsClosedWorldModule = Options.IsClosedWorld;
AC.Allowed = &Allowed;
AC.IsModulePass = true;
AC.DefaultInitializeLiveInternals = false;
@@ -1114,7 +1114,7 @@ class AMDGPUAttributorLegacy : public ModulePass {
bool runOnModule(Module &M) override {
AnalysisGetter AG(this);
- return runImpl(M, AG, *TM, /*HasWholeProgramVisibility=*/false);
+ return runImpl(M, AG, *TM, /*Options=*/{});
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -1135,9 +1135,8 @@ PreservedAnalyses llvm::AMDGPUAttributorPass::run(Module &M,
AnalysisGetter AG(FAM);
// TODO: Probably preserves CFG
- return runImpl(M, AG, TM, HasWholeProgramVisibility)
- ? PreservedAnalyses::none()
- : PreservedAnalyses::all();
+ return runImpl(M, AG, TM, Options) ? PreservedAnalyses::none()
+ : PreservedAnalyses::all();
}
char AMDGPUAttributorLegacy::ID = 0;
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def b/llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def
index 57fc3314dd9709..0adf11d27a2f54 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def
+++ b/llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def
@@ -17,7 +17,6 @@
#define MODULE_PASS(NAME, CREATE_PASS)
#endif
MODULE_PASS("amdgpu-always-inline", AMDGPUAlwaysInlinePass())
-MODULE_PASS("amdgpu-attributor", AMDGPUAttributorPass(*this))
MODULE_PASS("amdgpu-lower-buffer-fat-pointers",
AMDGPULowerBufferFatPointersPass(*this))
MODULE_PASS("amdgpu-lower-ctor-dtor", AMDGPUCtorDtorLoweringPass())
@@ -26,6 +25,17 @@ MODULE_PASS("amdgpu-printf-runtime-binding", AMDGPUPrintfRuntimeBindingPass())
MODULE_PASS("amdgpu-unify-metadata", AMDGPUUnifyMetadataPass())
#undef MODULE_PASS
+#ifndef MODULE_PASS_WITH_PARAMS
+#define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)
+#endif
+MODULE_PASS_WITH_PARAMS(
+ "amdgpu-attributor", "AMDGPUAttributorPass",
+ [=](AMDGPUAttributorOptions Options) {
+ return AMDGPUAttributorPass(*this, Options);
+ },
+ parseAMDGPUAttributorPassOptions, "closed-world")
+#undef MODULE_PASS_WITH_PARAMS
+
#ifndef FUNCTION_PASS
#define FUNCTION_PASS(NAME, CREATE_PASS)
#endif
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
index 50cc2d871d4ece..700408cd55e62c 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
@@ -54,6 +54,7 @@
#include "llvm/InitializePasses.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Passes/PassBuilder.h"
+#include "llvm/Support/FormatVariadic.h"
#include "llvm/Transforms/HipStdPar/HipStdPar.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/AlwaysInliner.h"
@@ -661,6 +662,24 @@ Error AMDGPUTargetMachine::buildCodeGenPipeline(
return CGPB.buildPipeline(MPM, Out, DwoOut, FileType);
}
+Expected<AMDGPUAttributorOptions>
+parseAMDGPUAttributorPassOptions(StringRef Params) {
+ AMDGPUAttributorOptions Result;
+ while (!Params.empty()) {
+ StringRef ParamName;
+ std::tie(ParamName, Params) = Params.split(';');
+ if (ParamName == "closed-world") {
+ Result.IsClosedWorld = true;
+ } else {
+ return make_error<StringError>(
+ formatv("invalid AMDGPUAttributor pass parameter '{0}' ", ParamName)
+ .str(),
+ inconvertibleErrorCode());
+ }
+ }
+ return Result;
+}
+
void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
#define GET_PASS_REGISTRY "AMDGPUPassRegistry.def"
@@ -739,9 +758,13 @@ void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
OptimizationLevel Level,
ThinOrFullLTOPhase Phase) {
if (Level != OptimizationLevel::O0) {
- MPM.addPass(AMDGPUAttributorPass(
- *this, Phase == ThinOrFullLTOPhase::FullLTOPostLink ||
- Phase == ThinOrFullLTOPhase::ThinLTOPostLink));
+ {
+ AMDGPUAttributorOptions Options;
+ Options.IsClosedWorld =
+ (Phase == ThinOrFullLTOPhase::FullLTOPostLink) ||
+ (Phase == ThinOrFullLTOPhase::ThinLTOPostLink);
+ MPM.addPass(AMDGPUAttributorPass(*this, Options));
+ }
}
});
diff --git a/llvm/test/CodeGen/AMDGPU/simple-indirect-call-2.ll b/llvm/test/CodeGen/AMDGPU/simple-indirect-call-2.ll
index 9c3457e87dbf3f..850446c414049d 100644
--- a/llvm/test/CodeGen/AMDGPU/simple-indirect-call-2.ll
+++ b/llvm/test/CodeGen/AMDGPU/simple-indirect-call-2.ll
@@ -1,6 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-globals
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes=amdgpu-attributor %s | FileCheck --check-prefixes=CHECK,OW %s
-; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes=amdgpu-attributor -attributor-assume-closed-world=1 %s | FileCheck --check-prefixes=CHECK,CW %s
+; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes='amdgpu-attributor<closed-world>' %s | FileCheck --check-prefixes=CHECK,CW %s
target datalayout = "A5"
diff --git a/llvm/test/Other/amdgpu-pass-pipeline-parsing.ll b/llvm/test/Other/amdgpu-pass-pipeline-parsing.ll
new file mode 100644
index 00000000000000..032c2b9d297bba
--- /dev/null
+++ b/llvm/test/Other/amdgpu-pass-pipeline-parsing.ll
@@ -0,0 +1,12 @@
+; REQUIRES: amdgpu-registered-target
+
+; RUN: not opt -S -mtriple=amdgcn-amd-amdhsa -passes='amdgpu-attributor<random>' -disable-output %s 2>&1 | FileCheck %s
+
+; CHECK: amdgpu-attributor: invalid AMDGPUAttributor pass parameter 'random'
+
+define void @f() {
+entry:
+ br label %loop
+loop:
+ br label %loop
+}
More information about the llvm-branch-commits
mailing list