decuda 0.1.1

CUDA to HIP, SYCL, OpenCL, and Rust GPU migration tool — automatic source-code translator for porting CUDA C++ kernels to AMD ROCm HIP, Intel oneAPI SYCL, Khronos OpenCL, and Rust GPU (cust / rust-gpu)
Documentation
// Generated by decuda. Edit with care.
// HIP is mostly source-compatible with CUDA at the kernel level.
// Compare against the original .cu file for sanity.

// Advanced fixture: inline PTX assembly constructs.
//
// Exercises:
//   - asm("...") with output/input operands
//   - asm volatile("...") with no operands
//   - asm with memory clobber
//   - All PTX constructs are flagged as warnings (NVIDIA-specific, no
//     equivalent in HIP/SYCL/OpenCL/Rust — manual rewrite required)
//   - __global__ kernel, threadIdx.x, blockIdx.x, blockDim.x
//   - cuda_runtime.h header
#include <hip/hip_runtime.h> /* was: cuda_runtime.h */

__global__ void ptx_bswap(int* data, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i >= n) return;

    int x = data[i];

    // Byte-swap via PTX `prmt` instruction.
    int result;
    asm("prmt.b32 %0, %1, 0, 0x0123;" : "=r"(result) : "r"(x));
    data[i] = result;
}

__global__ void ptx_membar(int* data, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i >= n) return;

    // Memory barrier via PTX.
    asm volatile("membar.gl;");
    data[i] += 1;
}

__global__ void ptx_clock(unsigned long long* cycles) {
    // Read the GPU clock counter via PTX.
    unsigned long long c;
    asm volatile("mov.u64 %0, %%clock64;" : "=l"(c));
    if (blockIdx.x * blockDim.x + threadIdx.x == 0) {
        *cycles = c;
    }
}

__global__ void ptx_lanemask(unsigned int* mask) {
    // Get the active lane mask via PTX.
    unsigned int m;
    asm volatile("activemask.b32 %0;" : "=r"(m));
    if (blockIdx.x * blockDim.x + threadIdx.x == 0) {
        *mask = m;
    }
}

int main(void) {
    const int N = 1024;
    int* d_data = nullptr;
    unsigned long long* d_cycles = nullptr;
    unsigned int* d_mask = nullptr;

    hipMalloc((void**)&d_data, N * sizeof(int));
    hipMalloc((void**)&d_cycles, sizeof(unsigned long long));
    hipMalloc((void**)&d_mask, sizeof(unsigned int));

    dim3 grid(N / 256);
    dim3 block(256);

    hipLaunchKernelGGL(ptx_bswap, dim3(grid), dim3(block), 0, 0, d_data, N);
    hipLaunchKernelGGL(ptx_membar, dim3(grid), dim3(block), 0, 0, d_data, N);
    hipLaunchKernelGGL(ptx_clock, dim3(1), dim3(1), 0, 0, d_cycles);
    hipLaunchKernelGGL(ptx_lanemask, dim3(1), dim3(1), 0, 0, d_mask);

    hipDeviceSynchronize();
    hipFree(d_data);
    hipFree(d_cycles);
    hipFree(d_mask);
    return 0;
}