forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRocmRuntimeWrappers.cpp
201 lines (169 loc) · 7.31 KB
/
RocmRuntimeWrappers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//===- RocmRuntimeWrappers.cpp - MLIR ROCM runtime wrapper library --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Implements C wrappers around the ROCM library for easy linking in ORC jit.
// Also adds some debugging helpers that are helpful when writing MLIR code to
// run on GPUs.
//
//===----------------------------------------------------------------------===//
#include <cassert>
#include <numeric>
#include "mlir/ExecutionEngine/CRunnerUtils.h"
#include "llvm/ADT/ArrayRef.h"
#include "hip/hip_runtime.h"
#define HIP_REPORT_IF_ERROR(expr) \
[](hipError_t result) { \
if (!result) \
return; \
const char *name = hipGetErrorName(result); \
if (!name) \
name = "<unknown>"; \
fprintf(stderr, "'%s' failed with '%s'\n", #expr, name); \
}(expr)
// Sets the `Context` for the duration of the instance and restores the previous
// context on destruction.
class ScopedContext {
public:
ScopedContext() {
// Static reference to HIP primary context for device ordinal 0.
static hipCtx_t context = [] {
HIP_REPORT_IF_ERROR(hipInit(/*flags=*/0));
hipDevice_t device;
HIP_REPORT_IF_ERROR(hipDeviceGet(&device, /*ordinal=*/0));
hipCtx_t ctx;
HIP_REPORT_IF_ERROR(hipDevicePrimaryCtxRetain(&ctx, device));
return ctx;
}();
HIP_REPORT_IF_ERROR(hipCtxPushCurrent(context));
}
~ScopedContext() { HIP_REPORT_IF_ERROR(hipCtxPopCurrent(nullptr)); }
};
extern "C" hipModule_t mgpuModuleLoad(void *data) {
ScopedContext scopedContext;
hipModule_t module = nullptr;
HIP_REPORT_IF_ERROR(hipModuleLoadData(&module, data));
return module;
}
extern "C" void mgpuModuleUnload(hipModule_t module) {
HIP_REPORT_IF_ERROR(hipModuleUnload(module));
}
extern "C" hipFunction_t mgpuModuleGetFunction(hipModule_t module,
const char *name) {
hipFunction_t function = nullptr;
HIP_REPORT_IF_ERROR(hipModuleGetFunction(&function, module, name));
return function;
}
// The wrapper uses intptr_t instead of ROCM's unsigned int to match
// the type of MLIR's index type. This avoids the need for casts in the
// generated MLIR code.
extern "C" void mgpuLaunchKernel(hipFunction_t function, intptr_t gridX,
intptr_t gridY, intptr_t gridZ,
intptr_t blockX, intptr_t blockY,
intptr_t blockZ, int32_t smem,
hipStream_t stream, void **params,
void **extra) {
ScopedContext scopedContext;
HIP_REPORT_IF_ERROR(hipModuleLaunchKernel(function, gridX, gridY, gridZ,
blockX, blockY, blockZ, smem,
stream, params, extra));
}
extern "C" hipStream_t mgpuStreamCreate() {
ScopedContext scopedContext;
hipStream_t stream = nullptr;
HIP_REPORT_IF_ERROR(hipStreamCreate(&stream));
return stream;
}
extern "C" void mgpuStreamDestroy(hipStream_t stream) {
HIP_REPORT_IF_ERROR(hipStreamDestroy(stream));
}
extern "C" void mgpuStreamSynchronize(hipStream_t stream) {
return HIP_REPORT_IF_ERROR(hipStreamSynchronize(stream));
}
extern "C" void mgpuStreamWaitEvent(hipStream_t stream, hipEvent_t event) {
HIP_REPORT_IF_ERROR(hipStreamWaitEvent(stream, event, /*flags=*/0));
}
extern "C" hipEvent_t mgpuEventCreate() {
ScopedContext scopedContext;
hipEvent_t event = nullptr;
HIP_REPORT_IF_ERROR(hipEventCreateWithFlags(&event, hipEventDisableTiming));
return event;
}
extern "C" void mgpuEventDestroy(hipEvent_t event) {
HIP_REPORT_IF_ERROR(hipEventDestroy(event));
}
extern "C" void mgpuEventSynchronize(hipEvent_t event) {
HIP_REPORT_IF_ERROR(hipEventSynchronize(event));
}
extern "C" void mgpuEventRecord(hipEvent_t event, hipStream_t stream) {
HIP_REPORT_IF_ERROR(hipEventRecord(event, stream));
}
extern "C" void *mgpuMemAlloc(uint64_t sizeBytes, hipStream_t /*stream*/) {
ScopedContext scopedContext;
void *ptr;
HIP_REPORT_IF_ERROR(hipMalloc(&ptr, sizeBytes));
return ptr;
}
extern "C" void mgpuMemFree(void *ptr, hipStream_t /*stream*/) {
HIP_REPORT_IF_ERROR(hipFree(ptr));
}
extern "C" void mgpuMemcpy(void *dst, void *src, size_t sizeBytes,
hipStream_t stream) {
HIP_REPORT_IF_ERROR(
hipMemcpyAsync(dst, src, sizeBytes, hipMemcpyDefault, stream));
}
extern "C" void mgpuMemset32(void *dst, int value, size_t count,
hipStream_t stream) {
HIP_REPORT_IF_ERROR(hipMemsetD32Async(reinterpret_cast<hipDeviceptr_t>(dst),
value, count, stream));
}
/// Helper functions for writing mlir example code
// Allows to register byte array with the ROCM runtime. Helpful until we have
// transfer functions implemented.
extern "C" void mgpuMemHostRegister(void *ptr, uint64_t sizeBytes) {
ScopedContext scopedContext;
HIP_REPORT_IF_ERROR(hipHostRegister(ptr, sizeBytes, /*flags=*/0));
}
// Allows to register a MemRef with the ROCm runtime. Helpful until we have
// transfer functions implemented.
extern "C" void
mgpuMemHostRegisterMemRef(int64_t rank, StridedMemRefType<char, 1> *descriptor,
int64_t elementSizeBytes) {
llvm::SmallVector<int64_t, 4> denseStrides(rank);
llvm::ArrayRef<int64_t> sizes(descriptor->sizes, rank);
llvm::ArrayRef<int64_t> strides(sizes.end(), rank);
std::partial_sum(sizes.rbegin(), sizes.rend(), denseStrides.rbegin(),
std::multiplies<int64_t>());
auto sizeBytes = denseStrides.front() * elementSizeBytes;
// Only densely packed tensors are currently supported.
std::rotate(denseStrides.begin(), denseStrides.begin() + 1,
denseStrides.end());
denseStrides.back() = 1;
assert(strides == llvm::makeArrayRef(denseStrides));
auto ptr = descriptor->data + descriptor->offset * elementSizeBytes;
mgpuMemHostRegister(ptr, sizeBytes);
}
template <typename T>
void mgpuMemGetDevicePointer(T *hostPtr, T **devicePtr) {
HIP_REPORT_IF_ERROR(hipSetDevice(0));
HIP_REPORT_IF_ERROR(
hipHostGetDevicePointer((void **)devicePtr, hostPtr, /*flags=*/0));
}
extern "C" StridedMemRefType<float, 1>
mgpuMemGetDeviceMemRef1dFloat(float *allocated, float *aligned, int64_t offset,
int64_t size, int64_t stride) {
float *devicePtr = nullptr;
mgpuMemGetDevicePointer(aligned, &devicePtr);
return {devicePtr, devicePtr, offset, {size}, {stride}};
}
extern "C" StridedMemRefType<int32_t, 1>
mgpuMemGetDeviceMemRef1dInt32(int32_t *allocated, int32_t *aligned,
int64_t offset, int64_t size, int64_t stride) {
int32_t *devicePtr = nullptr;
mgpuMemGetDevicePointer(aligned, &devicePtr);
return {devicePtr, devicePtr, offset, {size}, {stride}};
}