Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disassemble an OpenCL kernel?

I'm not sure if it's possible. I want to study OpenCL in-depth, so I was wondering if there is a tool to disassemble an compiled OpenCL kernel.

For normal x86 executable, I can use objdump to get a disassembly view. Is there a similar tool for OpenCL kernel, yet?

like image 548
Patrick Avatar asked Jul 14 '11 06:07

Patrick


People also ask

What is an OpenCL kernel?

A kernel is essentially a function written in the OpenCL language that enables it to be compiled for execution on any device that supports OpenCL. The kernel is the only way the host can call a function that will run on a device. When the host invokes a kernel, many work items start running on the device.

Is OpenCL GPU or CPU?

OpenCL™ (Open Computing Language) is a low-level API for heterogeneous computing that runs on CUDA-powered GPUs. Using the OpenCL API, developers can launch compute kernels written using a limited subset of the C programming language on a GPU.

Does OpenCL use CPU?

 OpenCL can use CPUs as a compute device just it can for GPUs.  There is no local memory, CPUs cache is utilized in OpenCL just like any normal CPU program.


3 Answers

If you're using NVIDIA's OpenCL implementation for their GPUs, you can do the followings to disassemble an OpenCL kernel:

  1. Use clGetEventProfilingInfo() to dump the ptx code to a file, say ptxfile.ptx. Please refer to the OpenCL specification to have more details on this function.

  2. Use nvcc to compile ptx to cubin file, for example: nvcc -cubin -arch=sm_20 ptxfile.ptx will compile ptxfile.ptx onto a compute capability 2.0 device.

  3. Use cuobjdump to disassemble the cubin file into GPU instructions. For example: cuobjdump -sass ptxfile.cubin

Hope this helps.

like image 109
Zk1001 Avatar answered Oct 11 '22 10:10

Zk1001


The simplest solution, in my experience, is to use clangs OpenCL C compiler and emit SPIR. It even works on Godbolt's compiler explorer: https://godbolt.org/z/_JbXPb

Clang can also emit ptx (https://godbolt.org/z/4ARMqM) and amdhsa (https://godbolt.org/z/TduTZQ), but it may not correspond to the ptx and amdhsa assembly generated by the respective driver at runtime.

like image 24
Andreas Gravgaard Andersen Avatar answered Oct 11 '22 08:10

Andreas Gravgaard Andersen


I know that this is an old question, but in case someone comes looking here for disassembling a AMD GPU kernel, you can do the following in linux:

export GPU_DUMP_DEVICE_KERNEL=3

This make any kernel that is compiled on your machine dump the assembled code to a file in the same directory.

Source: http://dis.unal.edu.co/~gjhernandezp/TOS/GPU/ATI_Stream_SDK_OpenCL_Programming_Guide.pdf

Sections 4.2.1 and 4.2.2

like image 33
KLee1 Avatar answered Oct 11 '22 09:10

KLee1