Skip to content

Commit

Permalink
Fix multiple annotations with RDC (#78)
Browse files Browse the repository at this point in the history
Co-authored-by: Giorgis Georgakoudis <[email protected]>
  • Loading branch information
davidbeckingsale and ggeorgakoudis authored Dec 23, 2024
1 parent 20b2677 commit 25133d7
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 5 deletions.
13 changes: 11 additions & 2 deletions lib/JitEngineDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,17 @@ void JitEngineDevice<ImplT>::registerFunction(void *Handle, void *Kernel,
int32_t NumRCs) {
DBG(Logger::logs("proteus")
<< "Register function " << Kernel << " To Handle " << Handle << "\n");
assert(!KernelToHandleMap.contains(Kernel) &&
"Expected kernel inserted only once in the map");
// NOTE: HIP RDC might call multiple times the registerFunction for the same
// kernel, which has weak linkage, when it comes from different translation
// units. Either the first or the second call can prevail and should be
// equivalent. We let the first one prevail.
if (KernelToHandleMap.contains(Kernel)) {
DBG(Logger::logs("proteus")
<< "Warning: duplicate register function for kernel " +
std::string(KernelName)
<< "\n");
return;
}
KernelToHandleMap[Kernel] = Handle;

JITKernelInfoMap[Kernel] =
Expand Down
8 changes: 6 additions & 2 deletions pass/ProteusPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,12 @@ class ProteusJitPassImpl {
" to be a kernel function!");
}

if (JitFunctionInfoMap.contains(Fn))
FATAL_ERROR("Duplicate jit annotation for Fn " + Fn->getName());
if (JitFunctionInfoMap.contains(Fn)) {
DEBUG(Logger::logs("proteus-pass")
<< "Warning: Duplicate jit annotation for Fn " + Fn->getName() +
"\n");
continue;
}

DEBUG(Logger::logs("proteus-pass")
<< "JIT Function " << Fn->getName() << "\n");
Expand Down
2 changes: 2 additions & 0 deletions tests/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ CREATE_GPU_TEST(kernel_unused_gvar kernel_unused_gvar.cpp kernel_unused_gvar_def
CREATE_GPU_TEST(kernel_repeat kernel_repeat.cpp)
CREATE_GPU_TEST(kernel_launch_exception kernel_launch_exception.cpp)
CREATE_GPU_TEST(kernel_preset_bounds kernel_preset_bounds.cpp)
CREATE_GPU_TEST(multi_file_launcher file1_kernel_launcher.cpp file2_kernel_launcher.cpp)

CREATE_GPU_TEST_RDC(kernel kernel.cpp)
CREATE_GPU_TEST_RDC(kernel_cache kernel_cache.cpp)
Expand All @@ -203,6 +204,7 @@ CREATE_GPU_TEST_RDC(kernel_calls_func kernel_calls_func.cpp device_func.cpp)
CREATE_GPU_TEST_RDC(kernel_repeat kernel_repeat.cpp)
CREATE_GPU_TEST_RDC(kernel_launch_exception kernel_launch_exception.cpp)
CREATE_GPU_TEST_RDC(kernel_preset_bounds kernel_preset_bounds.cpp)
CREATE_GPU_TEST_RDC(multi_file_launcher file1_kernel_launcher.cpp file2_kernel_launcher.cpp)

CREATE_GPU_LIBRARY(device_func_lib device_func.cpp)
CREATE_GPU_TEST_RDC_LIBS(kernel_calls_func_lib device_func_lib kernel_calls_func_lib.cpp)
28 changes: 28 additions & 0 deletions tests/gpu/file1_kernel_launcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// clang-format off
// RUN: ./multi_file_launcher.%ext | FileCheck %s --check-prefixes=CHECK,CHECK-FIRST
// Second run uses the object cache.
// RUN: ./multi_file_launcher.%ext | FileCheck %s --check-prefixes=CHECK,CHECK-SECOND
// clang-format on
#include <climits>
#include <cstdio>

#include "gpu_common.h"
#include "launcher.hpp"

void foo();

int main() {
gpuErrCheck(launcher(kernel_body));
gpuErrCheck(gpuDeviceSynchronize());
foo();
gpuErrCheck(gpuDeviceSynchronize());
return 0;
}

// CHECK: Kernel body
// CHECK: Kernel body
// CHECK: JitCache hits 0 total 2
// CHECK: HashValue {{[0-9]+}} NumExecs 1 NumHits 0
// CHECK: HashValue {{[0-9]+}} NumExecs 1 NumHits 0
// CHECK-FIRST: JitStorageCache hits 0 total 2
// CHECK-SECOND: JitStorageCache hits 2 total 2
6 changes: 6 additions & 0 deletions tests/gpu/file2_kernel_launcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

#include "gpu_common.h"
#include "launcher.hpp"

void foo() { gpuErrCheck(launcher(kernel_body)); }
2 changes: 1 addition & 1 deletion tests/gpu/gpu_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
gpuGetErrorString(err)); \
abort(); \
} \
}
}
16 changes: 16 additions & 0 deletions tests/gpu/launcher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
struct kernel_body_t {
__device__ void operator()() { printf("Kernel body\n"); }
};

const kernel_body_t kernel_body{};

template <typename LB>
__global__ __attribute__((annotate("jit"))) void kernel(LB lb) {
lb();
}

template <typename T> gpuError_t __attribute__((always_inline)) launcher(T lb) {
auto func = reinterpret_cast<const void *>(&kernel<T>);
void *args[] = {(void *)&lb};
return gpuLaunchKernel(func, 1, 1, args, 0, 0);
}

0 comments on commit 25133d7

Please sign in to comment.