diff --git a/mlir/lib/Conversion/GPUToCUDA/ConvertLaunchFuncToCudaCalls.cpp b/mlir/lib/Conversion/GPUToCUDA/ConvertLaunchFuncToCudaCalls.cpp index d93324284256..9d8c8942051e 100644 --- a/mlir/lib/Conversion/GPUToCUDA/ConvertLaunchFuncToCudaCalls.cpp +++ b/mlir/lib/Conversion/GPUToCUDA/ConvertLaunchFuncToCudaCalls.cpp @@ -49,7 +49,7 @@ static constexpr const char *cuModuleGetFunctionName = "mcuModuleGetFunction"; static constexpr const char *cuLaunchKernelName = "mcuLaunchKernel"; static constexpr const char *cuGetStreamHelperName = "mcuGetStreamHelper"; static constexpr const char *cuStreamSynchronizeName = "mcuStreamSynchronize"; -static constexpr const char *kMcuMemHostRegisterPtr = "mcuMemHostRegisterPtr"; +static constexpr const char *kMcuMemHostRegister = "mcuMemHostRegister"; static constexpr const char *kCubinAnnotation = "nvvm.cubin"; static constexpr const char *kCubinStorageSuffix = "_cubin_cst"; @@ -228,13 +228,13 @@ void GpuLaunchFuncToCudaCallsPass::declareCudaFunctions(Location loc) { getPointerType() /* CUstream stream */, /*isVarArg=*/false)); } - if (!module.lookupSymbol(kMcuMemHostRegisterPtr)) { + if (!module.lookupSymbol(kMcuMemHostRegister)) { builder.create( - loc, kMcuMemHostRegisterPtr, + loc, kMcuMemHostRegister, LLVM::LLVMType::getFunctionTy(getVoidType(), { getPointerType(), /* void *ptr */ - getInt32Type() /* int32 flags*/ + getInt64Type() /* int64 sizeBytes*/ }, /*isVarArg=*/false)); } @@ -277,12 +277,14 @@ GpuLaunchFuncToCudaCallsPass::setupParamsArray(gpu::LaunchFuncOp launchOp, // the descriptor pointer is registered via @mcuMemHostRegisterPtr if (llvmType.isStructTy()) { auto registerFunc = - getModule().lookupSymbol(kMcuMemHostRegisterPtr); - auto zero = builder.create( - loc, getInt32Type(), builder.getI32IntegerAttr(0)); + getModule().lookupSymbol(kMcuMemHostRegister); + auto nullPtr = builder.create(loc, llvmType.getPointerTo()); + auto gep = builder.create(loc, llvmType.getPointerTo(), + ArrayRef{nullPtr, one}); + auto size = builder.create(loc, getInt64Type(), gep); builder.create(loc, ArrayRef{}, builder.getSymbolRefAttr(registerFunc), - ArrayRef{casted, zero}); + ArrayRef{casted, size}); Value *memLocation = builder.create( loc, getPointerPointerType(), one, /*alignment=*/1); builder.create(loc, casted, memLocation); diff --git a/mlir/test/mlir-cuda-runner/all-reduce-op.mlir b/mlir/test/mlir-cuda-runner/all-reduce-op.mlir index 6356db0791a0..c863623a360b 100644 --- a/mlir/test/mlir-cuda-runner/all-reduce-op.mlir +++ b/mlir/test/mlir-cuda-runner/all-reduce-op.mlir @@ -4,12 +4,11 @@ func @main() { %arg = alloc() : memref<13x4x2xf32> %dst = memref_cast %arg : memref<13x4x2xf32> to memref - %zero = constant 0 : i32 %one = constant 1 : index %sx = dim %dst, 0 : memref %sy = dim %dst, 1 : memref %sz = dim %dst, 2 : memref - call @mcuMemHostRegister(%dst, %zero) : (memref, i32) -> () + call @mcuMemHostRegisterMemRef3dFloat(%dst) : (memref) -> () gpu.launch blocks(%bx, %by, %bz) in (%grid_x = %one, %grid_y = %one, %grid_z = %one) threads(%tx, %ty, %tz) in (%block_x = %sx, %block_y = %sy, %block_z = %sz) args(%kernel_dst = %dst) : memref { @@ -27,5 +26,5 @@ func @main() { return } -func @mcuMemHostRegister(%ptr : memref, %flags : i32) +func @mcuMemHostRegisterMemRef3dFloat(%ptr : memref) func @mcuPrintFloat(%ptr : memref) diff --git a/mlir/test/mlir-cuda-runner/all-reduce-region.mlir b/mlir/test/mlir-cuda-runner/all-reduce-region.mlir index 1a3a4595d13f..5cb803cfb7d6 100644 --- a/mlir/test/mlir-cuda-runner/all-reduce-region.mlir +++ b/mlir/test/mlir-cuda-runner/all-reduce-region.mlir @@ -4,10 +4,9 @@ func @main() { %arg = alloc() : memref<35xf32> %dst = memref_cast %arg : memref<35xf32> to memref - %zero = constant 0 : i32 %one = constant 1 : index %sx = dim %dst, 0 : memref - call @mcuMemHostRegister(%dst, %zero) : (memref, i32) -> () + call @mcuMemHostRegisterMemRef1dFloat(%dst) : (memref) -> () gpu.launch blocks(%bx, %by, %bz) in (%grid_x = %one, %grid_y = %one, %grid_z = %one) threads(%tx, %ty, %tz) in (%block_x = %sx, %block_y = %one, %block_z = %one) args(%kernel_dst = %dst) : memref { @@ -25,5 +24,5 @@ func @main() { return } -func @mcuMemHostRegister(%ptr : memref, %flags : i32) +func @mcuMemHostRegisterMemRef1dFloat(%ptr : memref) func @mcuPrintFloat(%ptr : memref) diff --git a/mlir/test/mlir-cuda-runner/gpu-to-cubin.mlir b/mlir/test/mlir-cuda-runner/gpu-to-cubin.mlir index 871327ff819d..98e0832d5ba2 100644 --- a/mlir/test/mlir-cuda-runner/gpu-to-cubin.mlir +++ b/mlir/test/mlir-cuda-runner/gpu-to-cubin.mlir @@ -15,10 +15,9 @@ func @other_func(%arg0 : f32, %arg1 : memref) { // CHECK: [1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00] func @main() { %arg0 = alloc() : memref<5xf32> - %20 = constant 0 : i32 %21 = constant 5 : i32 %22 = memref_cast %arg0 : memref<5xf32> to memref - call @mcuMemHostRegister(%22, %20) : (memref, i32) -> () + call @mcuMemHostRegisterMemRef1dFloat(%22) : (memref) -> () call @mcuPrintFloat(%22) : (memref) -> () %24 = constant 1.0 : f32 call @other_func(%24, %22) : (f32, memref) -> () @@ -26,5 +25,5 @@ func @main() { return } -func @mcuMemHostRegister(%ptr : memref, %flags : i32) +func @mcuMemHostRegisterMemRef1dFloat(%ptr : memref) func @mcuPrintFloat(%ptr : memref) diff --git a/mlir/tools/mlir-cuda-runner/cuda-runtime-wrappers.cpp b/mlir/tools/mlir-cuda-runner/cuda-runtime-wrappers.cpp index 31b6f6f62093..ac772589282e 100644 --- a/mlir/tools/mlir-cuda-runner/cuda-runtime-wrappers.cpp +++ b/mlir/tools/mlir-cuda-runner/cuda-runtime-wrappers.cpp @@ -21,8 +21,8 @@ // //===----------------------------------------------------------------------===// -#include -#include +#include +#include #include "llvm/Support/raw_ostream.h" @@ -80,6 +80,13 @@ extern "C" int32_t mcuStreamSynchronize(void *stream) { /// Helper functions for writing mlir example code +// Allows to register byte array with the CUDA runtime. Helpful until we have +// transfer functions implemented. +extern "C" void mcuMemHostRegister(void *ptr, uint64_t sizeBytes) { + reportErrorIfAny(cuMemHostRegister(ptr, sizeBytes, /*flags=*/0), + "MemHostRegister"); +} + // A struct that corresponds to how MLIR represents memrefs. template struct MemRefType { T *basePtr; @@ -89,23 +96,22 @@ template struct MemRefType { int64_t strides[N]; }; -// Allows to register a pointer with the CUDA runtime. Helpful until -// we have transfer functions implemented. -extern "C" void mcuMemHostRegister(const MemRefType *arg, - int32_t flags) { - reportErrorIfAny( - cuMemHostRegister(arg->data, arg->sizes[0] * sizeof(float), flags), - "MemHostRegister"); - for (int pos = 0; pos < arg->sizes[0]; pos++) { - arg->data[pos] = 1.23f; - } +// Allows to register a MemRef with the CUDA runtime. Initializes array with +// value. Helpful until we have transfer functions implemented. +template +void mcuMemHostRegisterMemRef(const MemRefType *arg, T value) { + auto count = std::accumulate(arg->sizes, arg->sizes + N, 1, + std::multiplies()); + std::fill_n(arg->data, count, value); + mcuMemHostRegister(arg->data, count * sizeof(T)); } - -// Allows to register a pointer with the CUDA runtime. Helpful until -// we have transfer functions implemented. -extern "C" void mcuMemHostRegisterPtr(void *ptr, int32_t flags) { - reportErrorIfAny(cuMemHostRegister(ptr, sizeof(void *), flags), - "MemHostRegister"); +extern "C" void +mcuMemHostRegisterMemRef1dFloat(const MemRefType *arg) { + mcuMemHostRegisterMemRef(arg, 1.23f); +} +extern "C" void +mcuMemHostRegisterMemRef3dFloat(const MemRefType *arg) { + mcuMemHostRegisterMemRef(arg, 1.23f); } /// Prints the given float array to stderr.