Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions util/tracer_nvbit/tracer_tool/tracer_tool.cu
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@

#define TRACER_VERSION "5"

static int get_attr_with_kernel_fallback(CUfunction func,
CUfunction_attribute attr) {
int value = 0;
CUresult res = cuFuncGetAttribute(&value, attr, func);
if (res == CUDA_ERROR_INVALID_HANDLE) {
CUdevice dev = 0;
if (cuCtxGetDevice(&dev) == CUDA_SUCCESS) {
cuKernelGetAttribute(&value, attr, (CUkernel)func, dev);
Comment on lines +43 to +44
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If cuCtxGetDevice fails, the function skips the cuKernelGetAttribute call and returns 0. However, it's unclear whether this is the intended behavior. Consider checking if cuCtxGetDevice can legitimately fail in scenarios where cuFuncGetAttribute returns INVALID_HANDLE, and whether silently returning 0 is appropriate in such cases.

Suggested change
if (cuCtxGetDevice(&dev) == CUDA_SUCCESS) {
cuKernelGetAttribute(&value, attr, (CUkernel)func, dev);
CUresult ctx_res = cuCtxGetDevice(&dev);
if (ctx_res == CUDA_SUCCESS) {
cuKernelGetAttribute(&value, attr, (CUkernel)func, dev);
} else {
fprintf(stderr,
"get_attr_with_kernel_fallback: cuCtxGetDevice failed with "
"error code %d\n",
(int)ctx_res);

Copilot uses AI. Check for mistakes.
}
}
Comment on lines +41 to +46
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of cuKernelGetAttribute is not checked. If this call fails, value might remain uninitialized or contain incorrect data, which would then be returned and used in trace headers. The original code used CUDA_SAFECALL which would have caught such errors. Consider checking the return value and handling failures appropriately, or at least logging when the fallback also fails.

Suggested change
if (res == CUDA_ERROR_INVALID_HANDLE) {
CUdevice dev = 0;
if (cuCtxGetDevice(&dev) == CUDA_SUCCESS) {
cuKernelGetAttribute(&value, attr, (CUkernel)func, dev);
}
}
if (res == CUDA_SUCCESS) {
return value;
}
if (res == CUDA_ERROR_INVALID_HANDLE) {
CUdevice dev = 0;
CUresult dev_res = cuCtxGetDevice(&dev);
if (dev_res == CUDA_SUCCESS) {
CUresult kern_res = cuKernelGetAttribute(&value, attr, (CUkernel)func, dev);
if (kern_res != CUDA_SUCCESS) {
fprintf(stderr,
"tracer_tool: cuKernelGetAttribute failed (attr=%d) with error %d\n",
(int)attr, (int)kern_res);
}
} else {
fprintf(stderr,
"tracer_tool: cuCtxGetDevice failed with error %d while getting attribute %d\n",
(int)dev_res, (int)attr);
}
} else {
fprintf(stderr,
"tracer_tool: cuFuncGetAttribute failed (attr=%d) with error %d\n",
(int)attr, (int)res);
}
/* On any failure, return the default value (0) */

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function should document its behavior when both cuFuncGetAttribute and cuKernelGetAttribute fail. Currently, it silently returns 0 (the initialized value) without any indication that the attribute query failed. This differs from the original implementation which used CUDA_SAFECALL to enforce error handling. Consider adding a comment explaining that 0 is returned on failure, or adding optional error logging.

Suggested change
}
}
/* If both attribute queries fail, this function returns 0 (the default
* initialized value) without reporting an error. Callers should treat 0
* as a possible "attribute unavailable" result. */

Copilot uses AI. Check for mistakes.
return value;
}

/* Channel used to communicate from GPU to CPU receiving thread */
#define CHANNEL_SIZE (1l << 20)
static __managed__ ChannelDev channel_dev;
Expand Down Expand Up @@ -502,16 +515,12 @@ static void enter_kernel_launch(CUcontext ctx, CUfunction func,
}

// Get the number of registers and shared memory size for the kernel
int nregs;
CUDA_SAFECALL(cuFuncGetAttribute(&nregs, CU_FUNC_ATTRIBUTE_NUM_REGS, func));

int shmem_static_nbytes;
CUDA_SAFECALL(cuFuncGetAttribute(&shmem_static_nbytes,
CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES, func));

int binary_version;
CUDA_SAFECALL(cuFuncGetAttribute(&binary_version,
CU_FUNC_ATTRIBUTE_BINARY_VERSION, func));
int nregs =
get_attr_with_kernel_fallback(func, CU_FUNC_ATTRIBUTE_NUM_REGS);
int shmem_static_nbytes = get_attr_with_kernel_fallback(
func, CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES);
int binary_version =
get_attr_with_kernel_fallback(func, CU_FUNC_ATTRIBUTE_BINARY_VERSION);

// Instrument the kernel if needed
instrument_function_if_needed(ctx, func);
Expand Down