Skip to content

Commit

Permalink
Regards #679:
Browse files Browse the repository at this point in the history
* Added demangling support using NVIDIA's (nearly-hidden) libcufilt
* Updated the `execution_control` example to not fail needlessly when getting the full signature as the demangled name
* Clarified kernel-name-mismatch error messages in `execution_control`
  • Loading branch information
eyalroz committed Oct 26, 2024
1 parent 8c442e7 commit 1348e5b
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 4 deletions.
16 changes: 15 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 12.4)
endif()
endforeach()
endif()
if (NOT TARGET CUDA::cufilt)
_CUDAToolkit_find_and_add_import_lib(cufilt)
endif()

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)



set(targets runtime-and-driver nvtx rtc fatbin)
set(prefixed-targets "")
set(caw_namespace "cuda-api-wrappers")
Expand Down Expand Up @@ -75,7 +80,7 @@ foreach(wrapper_lib ${targets})
WINDOWS_EXPORT_ALL_SYMBOLS ON
)
endforeach()
add_library("${caw_namespace}::driver-and-runtime" ALIAS caw_runtime-and-driver)
add_library("cuda-api-wrappers::driver-and-runtime" ALIAS caw_runtime-and-driver)
target_link_libraries(caw_rtc INTERFACE cuda-api-wrappers::runtime-and-driver CUDA::nvrtc)
if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.1)
if (TARGET CUDA::nvptxcompiler)
Expand All @@ -99,6 +104,15 @@ if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 12.4)
else()
message(WARNING "Could not locate a valid NVIDIA fatbin creator target or library file")
endif()

if (TARGET CUDA::cufilt)
target_link_libraries(caw_runtime-and-driver INTERFACE CUDA::cufilt)
elseif(EXISTS "${CUDA_cufilt_LIBRARY}")
target_link_libraries(caw_runtime-and-driver INTERFACE "${CUDA_cufilt_LIBRARY}")
else()
message(WARNING "Could not locate the cufilt demangling library")
endif()

endif()

target_link_libraries(caw_fatbin INTERFACE cuda-api-wrappers::runtime-and-driver)
Expand Down
10 changes: 7 additions & 3 deletions examples/by_api_module/execution_control.cu
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ int main(int argc, char **argv)

const auto kernel_function = foo;
const auto kernel_name = "foo"; // no reflection, sadly...
#if CUDA_VERSION >= 12030
const auto full_kernel_name = "foo(int)"; // no reflection, sadly...
#endif

auto kernel = cuda::kernel::get(device, kernel_function);

Expand All @@ -59,10 +62,11 @@ int main(int argc, char **argv)
auto mangled_name_from_kernel = base_ref.mangled_name();
#ifdef __GNUC__
auto demangled_name = demangle(mangled_name_from_kernel);
if (strcmp(demangled_name.c_str(), kernel_name) != 0) {
std::cout
if (strcmp(demangled_name.c_str(), full_kernel_name) != 0) {
std::cerr
<< "CUDA reports a different name for kernel \"" << kernel_name
<< "\" via its handle: \"" << demangled_name << "\"" << std::endl;
<< "\" via its handle: \"" << demangled_name << "\"\n"
<< "FAILURE\n";
return EXIT_FAILURE;
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions src/cuda/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#include "api/event.hpp"
#include "api/kernel.hpp"
#include "api/module.hpp"
#if CUDA_VERSION >= 11030
#include "api/demangle.hpp"
#endif
#if CUDA_VERSION >= 12000
#include "api/library.hpp"
#include "api/kernels/in_library.hpp"
Expand Down
79 changes: 79 additions & 0 deletions src/cuda/api/demangle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @file
*
* @brief Functions related to mangling & demangling identifiers
*
*/
#ifndef CUDA_API_WRAPPERS_MANGLING_HPP_
#define CUDA_API_WRAPPERS_MANGLING_HPP_

#if CUDA_VERSION >= 11030

#include "detail/span.hpp"
#include "detail/unique_span.hpp"
#include <nv_decode.h>

namespace cuda {

namespace detail_ {

using mangle_status_t = int;

inline void validate_mangling_status(int status)
{
switch (status) {
// 0 is fine
case -1: throw ::std::runtime_error("Memory allocation failure by __cu_demangle for a demangled CUDA identifier");
case -2: throw ::std::invalid_argument("Mangled identifier passed for demangling was invalid");
case -3: throw ::std::invalid_argument("Validation of one of the input arguments for a __cu_demangle() call failed");
}
return;
}

// TODO: Assuming the length _does_ include the trailing '\0'
inline char* demangle(const char* mangled_identifier, char* buffer, size_t& allocated_size)
{
int status;
char* result = __cu_demangle(mangled_identifier, buffer, &allocated_size, &status);
validate_mangling_status(status);
return result;
}

inline unique_span<char> demangle(const char* mangled_identifier)
{
size_t allocated_size { 0 };
auto demangled = demangle(mangled_identifier, nullptr, allocated_size);
#ifndef NDEBUG
if (allocated_size <= 1) {
throw ::std::logic_error("Invalid allocation size returned by __cu_demangle()");
}
#endif
return unique_span<char>{demangled, allocated_size - 1, c_free_deleter<char> };
}


} // namespace detail_

inline unique_span<char> demangle(const char* mangled_identifier)
{
return detail_::demangle(mangled_identifier);
}

template<typename T>
T demangle_as(const char* mangled_identifier)
{
auto demangled = detail_::demangle(mangled_identifier);
return { demangled.data(), demangled.data() + demangled.size() };
}

template<>
inline ::std::string demangle_as<::std::string>(const char* mangled_identifier)
{
auto demangled = detail_::demangle(mangled_identifier);
return { demangled.data(), demangled.size() };
}

} // namespace cuda

#endif // CUDA_VERSION >= 11030
#endif // CUDA_API_WRAPPERS_MANGLING_HPP_
7 changes: 7 additions & 0 deletions src/cuda/api/detail/unique_span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ inline void default_span_deleter(span<T> sp)
delete[] sp.data();
}

template <typename T>
inline void c_free_deleter(span<T> sp)
{
::std::free(sp.data());
}


} // namespace detail_


Expand Down

0 comments on commit 1348e5b

Please sign in to comment.