Skip to content

[FEA]: Add a utility to programmatically dump PTX/SASS for a kernel/device function #4849

@jrhemstad

Description

@jrhemstad

Is this a duplicate?

Area

General CCCL

Is your feature request related to a problem? Please describe.

As a CUDA C++ developer, I would like the ability to programmatically query the generated PTX/SASS for a specified kernel/device function. This would be useful for a variety of things, like verifying the absence of local memory instructions in CI/testing.

Describe the solution you'd like

In an ideal world, I would like an API that I can just pass a pointer to a __global__ or __device__ function and get the PTX/SASS as a string:

__device__ void foo(){...}

template <typename T>
__global__ void kernel(){...}

std::string ptx = cuda::get_ptx(foo);
std::string sass = cuda::get_sass(kernel<int>);

I'm not sure if this is actually possible with the APIs the driver/runtime exposes today.

Describe alternatives you've considered

No response

Additional context

@gevtushenko built a version of this for our own testing in c.parallel:

inline std::string inspect_sass(const void* cubin, size_t cubin_size)
{
namespace fs = std::filesystem;
fs::path temp_dir = fs::temp_directory_path();
fs::path temp_in_filename = temp_dir / "temp_in_file.cubin";
fs::path temp_out_filename = temp_dir / "temp_out_file.sass";
std::ofstream temp_in_file(temp_in_filename, std::ios::binary);
if (!temp_in_file)
{
throw std::runtime_error("Failed to create temporary file.");
}
temp_in_file.write(static_cast<const char*>(cubin), cubin_size);
temp_in_file.close();
std::string command = "nvdisasm -gi ";
command += temp_in_filename;
command += " > ";
command += temp_out_filename;
int exec_code = std::system(command.c_str());
if (!fs::remove(temp_in_filename))
{
throw std::runtime_error("Failed to remove temporary file.");
}
if (exec_code != 0)
{
throw std::runtime_error("Failed to execute command.");
}
std::ifstream temp_out_file(temp_out_filename, std::ios::binary);
if (!temp_out_file)
{
throw std::runtime_error("Failed to create temporary file.");
}
const std::string sass{std::istreambuf_iterator<char>(temp_out_file), std::istreambuf_iterator<char>()};
if (!fs::remove(temp_out_filename))
{
throw std::runtime_error("Failed to remove temporary file.");
}
return sass;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions