Skip to content

Commit 9abc457

Browse files
committed
[NewPM][AMDGPU] Port amdgpu-simplifylib/amdgpu-usenative
And add them to the pipeline via AMDGPUTargetMachine::registerPassBuilderCallbacks(), which mirrors AMDGPUTargetMachine::adjustPassManager(). These passes can't be unconditionally added to PassRegistry.def since they are only present when the AMDGPU backend is enabled. And there are no target-specific headers in llvm/include, so parsing these pass names must occur somewhere in the AMDGPU directory. I decided the best place was inside the TargetMachine, since the PassBuilder invokes TargetMachine::registerPassBuilderCallbacks() anyway. If we come up with a cleaner solution for target-specific passes in the future that's fine, but there aren't too many target-specific IR passes living in target-specific directories so it shouldn't be too bad to change in the future. Reviewed By: ychen, arsenm Differential Revision: https://reviews.llvm.org/D93863
1 parent 7b00e9f commit 9abc457

File tree

8 files changed

+113
-1
lines changed

8 files changed

+113
-1
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPU_H
1111
#define LLVM_LIB_TARGET_AMDGPU_AMDGPU_H
1212

13-
#include "llvm/IR/IntrinsicsR600.h" // TODO: Sink this.
1413
#include "llvm/IR/IntrinsicsAMDGPU.h" // TODO: Sink this.
14+
#include "llvm/IR/IntrinsicsR600.h" // TODO: Sink this.
15+
#include "llvm/IR/PassManager.h"
1516
#include "llvm/Support/CodeGen.h"
1617

1718
namespace llvm {
@@ -75,6 +76,14 @@ ModulePass *createAMDGPUPropagateAttributesLatePass(const TargetMachine *);
7576
FunctionPass *createAMDGPURewriteOutArgumentsPass();
7677
FunctionPass *createSIModeRegisterPass();
7778

79+
struct AMDGPUSimplifyLibCallsPass : PassInfoMixin<AMDGPUSimplifyLibCallsPass> {
80+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
81+
};
82+
83+
struct AMDGPUUseNativeCallsPass : PassInfoMixin<AMDGPUUseNativeCallsPass> {
84+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
85+
};
86+
7887
void initializeAMDGPUDAGToDAGISelPass(PassRegistry&);
7988

8089
void initializeAMDGPUMachineCFGStructurizerPass(PassRegistry&);

llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/IR/Intrinsics.h"
2727
#include "llvm/IR/LLVMContext.h"
2828
#include "llvm/IR/Module.h"
29+
#include "llvm/IR/PassManager.h"
2930
#include "llvm/IR/ValueSymbolTable.h"
3031
#include "llvm/InitializePasses.h"
3132
#include "llvm/Support/Debug.h"
@@ -1750,6 +1751,40 @@ bool AMDGPUSimplifyLibCalls::runOnFunction(Function &F) {
17501751
return Changed;
17511752
}
17521753

1754+
PreservedAnalyses AMDGPUSimplifyLibCallsPass::run(Function &F,
1755+
FunctionAnalysisManager &AM) {
1756+
AMDGPULibCalls Simplifier;
1757+
Simplifier.initNativeFuncs();
1758+
1759+
bool Changed = false;
1760+
auto AA = &AM.getResult<AAManager>(F);
1761+
1762+
LLVM_DEBUG(dbgs() << "AMDIC: process function ";
1763+
F.printAsOperand(dbgs(), false, F.getParent()); dbgs() << '\n';);
1764+
1765+
for (auto &BB : F) {
1766+
for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E;) {
1767+
// Ignore non-calls.
1768+
CallInst *CI = dyn_cast<CallInst>(I);
1769+
++I;
1770+
// Ignore intrinsics that do not become real instructions.
1771+
if (!CI || isa<DbgInfoIntrinsic>(CI) || CI->isLifetimeStartOrEnd())
1772+
continue;
1773+
1774+
// Ignore indirect calls.
1775+
Function *Callee = CI->getCalledFunction();
1776+
if (Callee == 0)
1777+
continue;
1778+
1779+
LLVM_DEBUG(dbgs() << "AMDIC: try folding " << *CI << "\n";
1780+
dbgs().flush());
1781+
if (Simplifier.fold(CI, AA))
1782+
Changed = true;
1783+
}
1784+
}
1785+
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
1786+
}
1787+
17531788
bool AMDGPUUseNativeCalls::runOnFunction(Function &F) {
17541789
if (skipFunction(F) || UseNative.empty())
17551790
return false;
@@ -1772,3 +1807,32 @@ bool AMDGPUUseNativeCalls::runOnFunction(Function &F) {
17721807
}
17731808
return Changed;
17741809
}
1810+
1811+
PreservedAnalyses AMDGPUUseNativeCallsPass::run(Function &F,
1812+
FunctionAnalysisManager &AM) {
1813+
if (UseNative.empty())
1814+
return PreservedAnalyses::all();
1815+
1816+
AMDGPULibCalls Simplifier;
1817+
Simplifier.initNativeFuncs();
1818+
1819+
bool Changed = false;
1820+
for (auto &BB : F) {
1821+
for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E;) {
1822+
// Ignore non-calls.
1823+
CallInst *CI = dyn_cast<CallInst>(I);
1824+
++I;
1825+
if (!CI)
1826+
continue;
1827+
1828+
// Ignore indirect calls.
1829+
Function *Callee = CI->getCalledFunction();
1830+
if (Callee == 0)
1831+
continue;
1832+
1833+
if (Simplifier.useNative(CI))
1834+
Changed = true;
1835+
}
1836+
}
1837+
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
1838+
}

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@
4040
#include "llvm/IR/Attributes.h"
4141
#include "llvm/IR/Function.h"
4242
#include "llvm/IR/LegacyPassManager.h"
43+
#include "llvm/IR/PassManager.h"
4344
#include "llvm/InitializePasses.h"
4445
#include "llvm/Pass.h"
46+
#include "llvm/Passes/PassBuilder.h"
4547
#include "llvm/Support/CommandLine.h"
4648
#include "llvm/Support/Compiler.h"
4749
#include "llvm/Support/TargetRegistry.h"
@@ -52,6 +54,7 @@
5254
#include "llvm/Transforms/Scalar.h"
5355
#include "llvm/Transforms/Scalar/GVN.h"
5456
#include "llvm/Transforms/Utils.h"
57+
#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
5558
#include "llvm/Transforms/Vectorize.h"
5659
#include <memory>
5760

@@ -482,6 +485,33 @@ void AMDGPUTargetMachine::adjustPassManager(PassManagerBuilder &Builder) {
482485
});
483486
}
484487

488+
void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB,
489+
bool DebugPassManager) {
490+
PB.registerPipelineParsingCallback(
491+
[](StringRef PassName, FunctionPassManager &PM,
492+
ArrayRef<PassBuilder::PipelineElement>) {
493+
if (PassName == "amdgpu-simplifylib") {
494+
PM.addPass(AMDGPUSimplifyLibCallsPass());
495+
return true;
496+
}
497+
if (PassName == "amdgpu-usenative") {
498+
PM.addPass(AMDGPUUseNativeCallsPass());
499+
return true;
500+
}
501+
return false;
502+
});
503+
504+
PB.registerPipelineStartEPCallback([DebugPassManager](
505+
ModulePassManager &PM,
506+
PassBuilder::OptimizationLevel Level) {
507+
FunctionPassManager FPM(DebugPassManager);
508+
FPM.addPass(AMDGPUUseNativeCallsPass());
509+
if (EnableLibCallSimplify && Level != PassBuilder::OptimizationLevel::O0)
510+
FPM.addPass(AMDGPUSimplifyLibCallsPass());
511+
PM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
512+
});
513+
}
514+
485515
//===----------------------------------------------------------------------===//
486516
// R600 Target Machine (R600 -> Cayman)
487517
//===----------------------------------------------------------------------===//

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class AMDGPUTargetMachine : public LLVMTargetMachine {
5656

5757
void adjustPassManager(PassManagerBuilder &) override;
5858

59+
void registerPassBuilderCallbacks(PassBuilder &PB,
60+
bool DebugPassManager) override;
61+
5962
/// Get the integer value of a null pointer in the given address space.
6063
static int64_t getNullPointerValue(unsigned AddrSpace) {
6164
return (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||

llvm/lib/Target/AMDGPU/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ add_llvm_target(AMDGPUCodeGen
152152
Core
153153
IPO
154154
MC
155+
Passes
155156
AMDGPUDesc
156157
AMDGPUInfo
157158
AMDGPUUtils

llvm/test/CodeGen/AMDGPU/simplify-libcalls.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
; RUN: opt -S -O1 -mtriple=amdgcn-- -amdgpu-simplify-libcall < %s | FileCheck -enable-var-scope -check-prefix=GCN -check-prefix=GCN-POSTLINK %s
22
; RUN: opt -S -O1 -mtriple=amdgcn-- -amdgpu-simplify-libcall -amdgpu-prelink <%s | FileCheck -enable-var-scope -check-prefix=GCN -check-prefix=GCN-PRELINK %s
33
; RUN: opt -S -O1 -mtriple=amdgcn-- -amdgpu-use-native -amdgpu-prelink < %s | FileCheck -enable-var-scope -check-prefix=GCN -check-prefix=GCN-NATIVE %s
4+
; RUN: opt -S -passes='default<O1>' -mtriple=amdgcn-- -amdgpu-simplify-libcall < %s | FileCheck -enable-var-scope -check-prefix=GCN -check-prefix=GCN-POSTLINK %s
5+
; RUN: opt -S -passes='default<O1>' -mtriple=amdgcn-- -amdgpu-simplify-libcall -amdgpu-prelink <%s | FileCheck -enable-var-scope -check-prefix=GCN -check-prefix=GCN-PRELINK %s
6+
; RUN: opt -S -passes='default<O1>' -mtriple=amdgcn-- -amdgpu-use-native -amdgpu-prelink < %s | FileCheck -enable-var-scope -check-prefix=GCN -check-prefix=GCN-NATIVE %s
47

58
; GCN-LABEL: {{^}}define amdgpu_kernel void @test_sincos
69
; GCN-POSTLINK: call fast float @_Z3sinf(

llvm/test/CodeGen/AMDGPU/simplify-libcalls2.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; REQUIRES: asserts
22
; RUN: opt -S -amdgpu-simplifylib -debug-only=amdgpu-simplifylib -mtriple=amdgcn-unknown-amdhsa -disable-output < %s 2>&1 | FileCheck %s
3+
; RUN: opt -S -passes=amdgpu-simplifylib -debug-only=amdgpu-simplifylib -mtriple=amdgcn-unknown-amdhsa -disable-output < %s 2>&1 | FileCheck %s
34

45
; CHECK-NOT: AMDIC: try folding call void @llvm.lifetime.start.p0i8
56
; CHECK-NOT: AMDIC: try folding call void @llvm.lifetime.end.p0i8

llvm/utils/gn/secondary/llvm/lib/Target/AMDGPU/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ static_library("LLVMAMDGPUCodeGen") {
112112
"//llvm/lib/CodeGen/SelectionDAG",
113113
"//llvm/lib/IR",
114114
"//llvm/lib/MC",
115+
"//llvm/lib/Passes",
115116
"//llvm/lib/Support",
116117
"//llvm/lib/Target",
117118
"//llvm/lib/Transforms/IPO",

0 commit comments

Comments
 (0)