Skip to content

Commit c5bba7e

Browse files
committed
[HIP][LLVM][Opt] Add LLVM support for hipstdpar
This patch adds the LLVM changes needed for enabling HIP parallel algorithm offload on AMDGPU targets. What we do here is add two passes, one mandatory and one optional: 1. HipStdParAcceleratorCodeSelectionPass is mandatory, depends on CallGraphAnalysis, and implements the following transform: - Traverse the call-graph, and check for functions that are roots for accelerator execution (at the moment, these are GPU kernels exclusively, and would originate in the accelerator specific algorithm library the toolchain uses as an implementation detail); - Starting from a root, do a BFS to find all functions that are reachable (called directly or indirectly via a call- chain) and record them; - After having done the above for all roots in the Module, we have the computed the set of reachable functions, which is the union of roots and functions reachable from roots; - All functions that are not in the reachable set are removed; for the special case where the reachable set is empty we completely clear the module; 2. HipStdParAllocationInterpositionPass is optional, is meant as a fallback with restricted functionality for cases where on-demand paging is unavailable on a platform, and implements the following transform: - Iterate all functions in a Module; - If a function's name is in a predefined set of allocation / deallocation that the runtime implementation is allowed and expected to interpose, replace all its uses with the equivalent accelerator aware function, iff the latter is available; - If the accelerator aware equivalent is unavailable we warn, but compilation will go ahead, which means that it is possible to get issues around the accelerator trying to access inaccessible memory at run time; - We rely on direct name matching as opposed to using the new alloc-kind family of attributes and / or the LibCall analysis pass because some of the legacy functions that need replacing would not carry the former or be identified by the latter. Reviewed by: JonChesterfield, yaxunl Differential Revision: https://reviews.llvm.org/D155856
1 parent cacfac4 commit c5bba7e

17 files changed

+938
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===--------- HipStdPar.h - Standard Parallelism passes --------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
///
10+
/// AcceleratorCodeSelection - Identify all functions reachable from a kernel,
11+
/// removing those that are unreachable.
12+
///
13+
/// AllocationInterposition - Forward calls to allocation / deallocation
14+
// functions to runtime provided equivalents that allocate memory that is
15+
// accessible for an accelerator
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef LLVM_TRANSFORMS_HIPSTDPAR_HIPSTDPAR_H
19+
#define LLVM_TRANSFORMS_HIPSTDPAR_HIPSTDPAR_H
20+
21+
#include "llvm/IR/PassManager.h"
22+
23+
namespace llvm {
24+
25+
class Module;
26+
class ModuleAnaysisManager;
27+
28+
class HipStdParAcceleratorCodeSelectionPass
29+
: public PassInfoMixin<HipStdParAcceleratorCodeSelectionPass> {
30+
public:
31+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
32+
33+
static bool isRequired() { return true; }
34+
};
35+
36+
class HipStdParAllocationInterpositionPass
37+
: public PassInfoMixin<HipStdParAllocationInterpositionPass> {
38+
public:
39+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
40+
41+
static bool isRequired() { return true; }
42+
};
43+
44+
} // namespace llvm
45+
46+
#endif // LLVM_TRANSFORMS_HIPSTDPAR_HIPSTDPAR_H

llvm/lib/Passes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ add_llvm_component_library(LLVMPasses
1919
CodeGen
2020
Core
2121
Coroutines
22+
HipStdPar
2223
IPO
2324
InstCombine
2425
IRPrinter

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
#include "llvm/Transforms/Coroutines/CoroEarly.h"
9595
#include "llvm/Transforms/Coroutines/CoroElide.h"
9696
#include "llvm/Transforms/Coroutines/CoroSplit.h"
97+
#include "llvm/Transforms/HipStdPar/HipStdPar.h"
9798
#include "llvm/Transforms/IPO/AlwaysInliner.h"
9899
#include "llvm/Transforms/IPO/Annotation2Metadata.h"
99100
#include "llvm/Transforms/IPO/ArgumentPromotion.h"

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "llvm/Transforms/Coroutines/CoroEarly.h"
3838
#include "llvm/Transforms/Coroutines/CoroElide.h"
3939
#include "llvm/Transforms/Coroutines/CoroSplit.h"
40+
#include "llvm/Transforms/HipStdPar/HipStdPar.h"
4041
#include "llvm/Transforms/IPO/AlwaysInliner.h"
4142
#include "llvm/Transforms/IPO/Annotation2Metadata.h"
4243
#include "llvm/Transforms/IPO/ArgumentPromotion.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ MODULE_PASS("forceattrs", ForceFunctionAttrsPass())
6464
MODULE_PASS("function-import", FunctionImportPass())
6565
MODULE_PASS("globalopt", GlobalOptPass())
6666
MODULE_PASS("globalsplit", GlobalSplitPass())
67+
MODULE_PASS("hipstdpar-select-accelerator-code",
68+
HipStdParAcceleratorCodeSelectionPass())
69+
MODULE_PASS("hipstdpar-interpose-alloc", HipStdParAllocationInterpositionPass())
6770
MODULE_PASS("hotcoldsplit", HotColdSplittingPass())
6871
MODULE_PASS("inferattrs", InferFunctionAttrsPass())
6972
MODULE_PASS("inliner-wrapper", ModuleInlinerWrapperPass())

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "llvm/InitializePasses.h"
5151
#include "llvm/MC/TargetRegistry.h"
5252
#include "llvm/Passes/PassBuilder.h"
53+
#include "llvm/Transforms/HipStdPar/HipStdPar.h"
5354
#include "llvm/Transforms/IPO.h"
5455
#include "llvm/Transforms/IPO/AlwaysInliner.h"
5556
#include "llvm/Transforms/IPO/GlobalDCE.h"
@@ -348,6 +349,11 @@ static cl::opt<bool> EnableRewritePartialRegUses(
348349
cl::desc("Enable rewrite partial reg uses pass"), cl::init(false),
349350
cl::Hidden);
350351

352+
static cl::opt<bool> EnableHipStdPar(
353+
"amdgpu-enable-hipstdpar",
354+
cl::desc("Enable HIP Standard Parallelism Offload support"), cl::init(false),
355+
cl::Hidden);
356+
351357
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
352358
// Register the target
353359
RegisterTargetMachine<R600TargetMachine> X(getTheR600Target());
@@ -699,6 +705,8 @@ void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
699705
if (EnableLibCallSimplify && Level != OptimizationLevel::O0)
700706
FPM.addPass(AMDGPUSimplifyLibCallsPass());
701707
PM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
708+
if (EnableHipStdPar)
709+
PM.addPass(HipStdParAcceleratorCodeSelectionPass());
702710
});
703711

704712
PB.registerPipelineEarlySimplificationEPCallback(

llvm/lib/Target/AMDGPU/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ add_llvm_target(AMDGPUCodeGen
176176
CodeGenTypes
177177
Core
178178
GlobalISel
179+
HIPStdPar
179180
IPO
180181
MC
181182
MIRParser

llvm/lib/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ add_subdirectory(Hello)
99
add_subdirectory(ObjCARC)
1010
add_subdirectory(Coroutines)
1111
add_subdirectory(CFGuard)
12+
add_subdirectory(HipStdPar)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
add_llvm_component_library(LLVMHipStdPar
2+
HipStdPar.cpp
3+
4+
ADDITIONAL_HEADER_DIRS
5+
${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms/HipStdPar
6+
7+
DEPENDS
8+
intrinsics_gen
9+
LLVMAnalysis
10+
11+
LINK_COMPONENTS
12+
Analysis
13+
Core
14+
Support
15+
TransformUtils)

0 commit comments

Comments
 (0)