|
| 1 | +//===- PolygeistCudaRuntimeWrappers.cpp - MLIR CUDA API wrapper library ---===// |
| 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 | +// |
| 9 | +// Implements C wrappers around the CUDA library for easy linking in ORC jit. |
| 10 | +// Also adds some debugging helpers that are helpful when writing MLIR code to |
| 11 | +// run on GPUs. |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#include "mlir/ExecutionEngine/CRunnerUtils.h" |
| 16 | + |
| 17 | +#include <stdio.h> |
| 18 | + |
| 19 | +#include "cuda.h" |
| 20 | + |
| 21 | +#ifdef _WIN32 |
| 22 | +#define MLIR_CUDA_WRAPPERS_EXPORT __declspec(dllexport) |
| 23 | +#else |
| 24 | +#define MLIR_CUDA_WRAPPERS_EXPORT |
| 25 | +#endif // _WIN32 |
| 26 | + |
| 27 | +#define CUDA_REPORT_IF_ERROR(expr) \ |
| 28 | + [](CUresult result) { \ |
| 29 | + if (!result) \ |
| 30 | + return result; \ |
| 31 | + const char *name = nullptr; \ |
| 32 | + cuGetErrorName(result, &name); \ |
| 33 | + if (!name) \ |
| 34 | + name = "<unknown>"; \ |
| 35 | + fprintf(stderr, "'%s' failed with '%s'\n", #expr, name); \ |
| 36 | + return result; \ |
| 37 | + }(expr) |
| 38 | + |
| 39 | +thread_local static int32_t defaultDevice = 0; |
| 40 | + |
| 41 | +// Make the primary context of the current default device current for the |
| 42 | +// duration |
| 43 | +// of the instance and restore the previous context on destruction. |
| 44 | +class ScopedContext { |
| 45 | +public: |
| 46 | + ScopedContext() { |
| 47 | + // Static reference to CUDA primary context for device ordinal |
| 48 | + // defaultDevice. |
| 49 | + static CUcontext context = [] { |
| 50 | + CUDA_REPORT_IF_ERROR(cuInit(/*flags=*/0)); |
| 51 | + CUdevice device; |
| 52 | + CUDA_REPORT_IF_ERROR(cuDeviceGet(&device, /*ordinal=*/defaultDevice)); |
| 53 | + CUcontext ctx; |
| 54 | + // Note: this does not affect the current context. |
| 55 | + CUDA_REPORT_IF_ERROR(cuDevicePrimaryCtxRetain(&ctx, device)); |
| 56 | + return ctx; |
| 57 | + }(); |
| 58 | + |
| 59 | + CUDA_REPORT_IF_ERROR(cuCtxPushCurrent(context)); |
| 60 | + } |
| 61 | + |
| 62 | + ~ScopedContext() { CUDA_REPORT_IF_ERROR(cuCtxPopCurrent(nullptr)); } |
| 63 | +}; |
| 64 | + |
| 65 | +extern "C" MLIR_CUDA_WRAPPERS_EXPORT int32_t mgpuLaunchKernelErr( |
| 66 | + CUfunction function, intptr_t gridX, intptr_t gridY, intptr_t gridZ, |
| 67 | + intptr_t blockX, intptr_t blockY, intptr_t blockZ, int32_t smem, |
| 68 | + CUstream stream, void **params, void **extra) { |
| 69 | + ScopedContext scopedContext; |
| 70 | + return CUDA_REPORT_IF_ERROR(cuLaunchKernel(function, gridX, gridY, gridZ, |
| 71 | + blockX, blockY, blockZ, smem, |
| 72 | + stream, params, extra)); |
| 73 | +} |
0 commit comments