1#ifndef PROTEUS_FRONTEND_DISPATCHER_HIP_H
2#define PROTEUS_FRONTEND_DISPATCHER_HIP_H
13#include <llvm/Bitcode/BitcodeReader.h>
14#include <llvm/Linker/Linker.h>
15#include <llvm/Support/FileSystem.h>
16#include <llvm/Support/MemoryBuffer.h>
20class DispatcherHIP :
public Dispatcher {
22 static DispatcherHIP &instance() {
23 static DispatcherHIP D;
27 std::unique_ptr<MemoryBuffer> compile(std::unique_ptr<LLVMContext> Ctx,
28 std::unique_ptr<Module> Mod,
29 const HashT &ModuleHash,
30 bool DisableIROpt =
false)
override {
34 auto CtxOwner = std::move(Ctx);
35 auto ModOwner = std::move(Mod);
38 auto LoadBitcode = [&](
const llvm::SmallString<256> &Path) {
39 auto BufferOrErr = llvm::MemoryBuffer::getFile(Path);
40 if (!BufferOrErr || !BufferOrErr.get())
42 Path.str().str() +
" (" + Toolchain.Origin +
")");
43 auto Parsed = llvm::parseBitcodeFile(
44 BufferOrErr->get()->getMemBufferRef(), ModOwner->getContext());
47 Path.str().str() +
" (" + Toolchain.Origin +
")");
48 return std::move(Parsed.get());
51 auto AppendBitcodePath =
52 [&](llvm::SmallVectorImpl<llvm::SmallString<256>> &Paths,
53 llvm::StringRef Filename) {
54 llvm::SmallString<256> Path{Toolchain.DeviceLibDir};
55 llvm::sys::path::append(Path, Filename);
56 Paths.push_back(std::move(Path));
59 auto Exists = [&](llvm::StringRef Filename) ->
bool {
60 llvm::SmallString<256> Path{Toolchain.DeviceLibDir};
61 llvm::sys::path::append(Path, Filename);
62 return llvm::sys::fs::exists(Path);
65 auto PickFirstExisting =
66 [&](std::initializer_list<llvm::StringRef> Candidates)
68 for (
auto C : Candidates) {
77 llvm::SmallVector<llvm::SmallString<256>, 8> LibsToLink;
78 AppendBitcodePath(LibsToLink,
"ocml.bc");
79 AppendBitcodePath(LibsToLink,
"ockl.bc");
82 if (
auto Abi = PickFirstExisting({
"oclc_abi_version_600.bc",
83 "oclc_abi_version_500.bc",
84 "oclc_abi_version_400.bc"});
86 AppendBitcodePath(LibsToLink, Abi);
89 std::string(
"DispatchHIP: missing oclc ABI bitcode under ") +
90 Toolchain.DeviceLibDir +
" (" + Toolchain.Origin +
91 "; expected oclc_abi_version_{600,500,400}.bc)");
95 const std::string DeviceArch =
Jit.getDeviceArch().str();
96 if (!llvm::StringRef{DeviceArch}.starts_with(
"gfx"))
99 const llvm::StringRef IsaSuffix = llvm::StringRef{DeviceArch}.drop_front(3);
100 const std::string IsaFile = (
"oclc_isa_version_" + IsaSuffix +
".bc").str();
101 if (!Exists(IsaFile))
103 IsaFile +
" under " + Toolchain.DeviceLibDir +
" (" +
104 Toolchain.Origin +
"; DeviceArch=" + DeviceArch +
")");
105 AppendBitcodePath(LibsToLink, IsaFile);
108 AppendBitcodePath(LibsToLink,
"oclc_unsafe_math_off.bc");
109 AppendBitcodePath(LibsToLink,
"oclc_finite_only_off.bc");
110 AppendBitcodePath(LibsToLink,
"oclc_daz_opt_off.bc");
111 AppendBitcodePath(LibsToLink,
"oclc_correctly_rounded_sqrt_on.bc");
114 const bool IsWave32 = llvm::StringRef{DeviceArch}.starts_with(
"gfx10") ||
115 llvm::StringRef{DeviceArch}.starts_with(
"gfx11") ||
116 llvm::StringRef{DeviceArch}.starts_with(
"gfx12");
117 AppendBitcodePath(LibsToLink, IsWave32 ?
"oclc_wavefrontsize64_off.bc"
118 :
"oclc_wavefrontsize64_on.bc");
120 llvm::Linker Linker{*ModOwner};
121 for (
const auto &Path : LibsToLink) {
122 auto LibMod = LoadBitcode(Path);
123 Linker.linkInModule(std::move(LibMod),
124 llvm::Linker::Flags::LinkOnlyNeeded);
127 std::unique_ptr<MemoryBuffer> ObjectModule =
128 Jit.compileOnly(*ModOwner, DisableIROpt);
133 ModuleHash, CacheEntry::staticObject(ObjectModule->getMemBufferRef()));
138 std::unique_ptr<CompiledLibrary>
139 lookupCompiledLibrary(
const HashT &ModuleHash)
override {
140 return ObjectCache->lookup(ModuleHash);
143 DispatchResult launch(
void *KernelFunc,
LaunchDims GridDim,
145 uint64_t ShmemSize,
void *Stream)
override {
147 dim3 HipGridDim = {GridDim.
X, GridDim.
Y, GridDim.
Z};
148 dim3 HipBlockDim = {BlockDim.
X, BlockDim.
Y, BlockDim.
Z};
149 hipStream_t HipStream =
reinterpret_cast<hipStream_t
>(Stream);
152 reinterpret_cast<hipFunction_t
>(KernelFunc), HipGridDim, HipBlockDim,
153 KernelArgs, ShmemSize, HipStream);
156 StringRef getDeviceArch()
const override {
return Jit.getDeviceArch(); }
159 CodeCache.printStats();
160 CodeCache.printKernelTrace();
161 ObjectCache->printStats();
164 void *getFunctionAddress(
const std::string &
KernelName,
165 const HashT &ModuleHash,
166 CompiledLibrary &Library)
override {
167 TIMESCOPE(DispatcherHIP, getFunctionAddress);
168 auto GetKernelFunc = [&]() {
172 if (
auto KernelFunc = CodeCache.lookup(HashValue))
176 KernelName, Library.ObjectModule->getBufferStart(),
180 CodeCache.insert(HashValue, KernelFunc,
KernelName);
185 auto KernelFunc = GetKernelFunc();
189 void registerDynamicLibrary(
const HashT &,
const std::string &)
override {
193 void registerObject(
const HashT &HashValue,
194 const llvm::MemoryBufferRef &Obj)
override {
195 ObjectCache->store(HashValue, CacheEntry::staticObject(Obj));
199 JitEngineDeviceHIP &
Jit;
202 Jit(JitEngineDeviceHIP::instance()) {}
203 MemoryCache<hipFunction_t> CodeCache{
"DispatcherHIP"};
void char * KernelName
Definition CompilerInterfaceDevice.cpp:59
JitEngineHost & Jit
Definition CompilerInterfaceHost.cpp:26
#define TIMESCOPE(...)
Definition TimeTracing.h:66
Definition MemoryCache.h:27
TargetModelType
Definition TargetModel.h:8
HashT hash(FirstT &&First, RestTs &&...Rest)
Definition Hashing.h:168
void reportFatalError(const llvm::Twine &Reason, const char *FILE, unsigned Line)
Definition Error.cpp:14
cudaError_t launchKernelFunction(CUfunction KernelFunc, dim3 GridDim, dim3 BlockDim, void **KernelArgs, uint64_t ShmemSize, CUstream Stream)
Definition CoreDeviceCUDA.h:78
const ResolvedHIPToolchain & resolveHIPToolchain()
Definition HIPToolchain.cpp:273
CUfunction getKernelFunctionFromImage(StringRef KernelName, const void *Image, bool RelinkGlobalsByCopy, const std::unordered_map< std::string, GlobalVarInfo > &VarNameToGlobalInfo)
Definition CoreDeviceCUDA.h:50
Definition Dispatcher.h:22
unsigned Z
Definition Dispatcher.h:23
unsigned Y
Definition Dispatcher.h:23
unsigned X
Definition Dispatcher.h:23