HIP icon indicating copy to clipboard operation
HIP copied to clipboard

Bug in the IR generated for __longlong_as_double

Open deven-amd opened this issue 8 years ago • 9 comments

When the routine __longlong_as_double is used in kernel code, the IR generated for it implements the routine using the "sitofp" instruction instead of a "bitcast" instruction. We believe this is a bug.

Please use the provided testcase (at the end of this message) to see it. IR corresponding to the buggy routine is also provided

Re-run the testcase with "IMPLEMENT_WORKAROUND" defined to see the expected/correct functionality.

Ping me if you need more information.

Compile cmd -

/opt/rocm/hip/bin/hipcc -Wall src/double_check_longlong_as_double.cpp -o run_check.exe

Testcase -

// src/double_check_longlong_as_double.cpp
#include <stdio.h>

// hip header file
#include "hip/hip_runtime.h"
#include "hip/hip_fp16.h"

//#define IMPLEMENT_WORKAROUND

#define NUM (8)

using input_type_t = long long;
using output_type_t = double;

void print_values(input_type_t in, output_type_t out)
{
  printf ("%016llx : %f\n", in, out);
}


__global__ void gpuKernel(hipLaunchParm lp, output_type_t* out, input_type_t* in) {
    int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x;
    int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y;
    int idx = x + y;

#ifdef IMPLEMENT_WORKAROUND    

    output_type_t* addr = reinterpret_cast<output_type_t*>(&in[idx]);
    out[idx] =  *addr;

#else

    out[idx] = __longlong_as_double(in[idx]);

#endif
}


int main() {

    hipDeviceProp_t devProp;
    hipGetDeviceProperties(&devProp, 0);

    std::cout << "Device name " << devProp.name << std::endl;

    // initialize the input data
    input_type_t* input = (input_type_t*) malloc (NUM * sizeof(input_type_t));
    input[0] = 0x0000000000000000ull;  // bit-pattern for +0.0
    input[1] = 0x3ff0000000000000ull;  // bit-pattern for 1.0
    input[2] = 0x4000000000000000ull;  // bit-pattern for 2.0
    input[3] = 0x4008000000000000ull;  // bit-pattern for 3.0
    input[4] = 0x4010000000000000ull;  // bit-pattern for 4.0
    input[5] = 0x4014000000000000ull;  // bit-pattern for 5.0
    input[6] = 0x7ff0000000000000ull;  // bit-pattern for +inf
    input[7] = 0x7ff8000000000000ull;  // bit-pattern for nan
    
    // allocate the memory on the device side
    input_type_t* inputOnGPU = nullptr;
    output_type_t* resultOnGPU = nullptr;
    hipMalloc((void**)&inputOnGPU, NUM * sizeof(input_type_t));
    hipMalloc((void**)&resultOnGPU, NUM * sizeof(output_type_t));

    // allocate memory on the host for the result
    output_type_t* result = (output_type_t*)malloc(NUM * sizeof(output_type_t));

    // Memory transfer from host to device
    hipMemcpy(inputOnGPU, input, NUM * sizeof(input_type_t), hipMemcpyHostToDevice);

    // Lauching kernel from host
    hipLaunchKernel(gpuKernel, dim3(1), dim3(NUM), 0, 0, resultOnGPU, inputOnGPU);

    // Memory transfer from device to host
    hipMemcpy(result, resultOnGPU, NUM * sizeof(output_type_t), hipMemcpyDeviceToHost);

    // free the resources on device side
    hipFree(inputOnGPU);
    hipFree(resultOnGPU);

    for (int i=0; i<NUM; i++) {
      print_values(input[i], result[i]);
    }
    
    // free the resources on host side
    free(input);
    free(result);

    return 0;
}

// IR for kernel

; Function Attrs: alwaysinline norecurse nounwind
define weak_odr amdgpu_kernel void @_Z9gpuKernelN8hip_impl17Empty_launch_parmEPdPx(%"struct.std::less" %lp.coerce, double* %out, i64* %in) #20 {
  %call = tail call i32 @hc_get_group_size(i32 0) #28
  %call2 = tail call i32 @hc_get_group_id(i32 0) #28
  %mul = mul i32 %call2, %call
  %call3 = tail call i32 @hc_get_workitem_id(i32 0) #28
  %call4 = tail call i32 @hc_get_group_size(i32 1) #28
  %call5 = tail call i32 @hc_get_group_id(i32 1) #28
  %mul6 = mul i32 %call5, %call4
  %call7 = tail call i32 @hc_get_workitem_id(i32 1) #28
  %add8 = add i32 %mul, %call3
  %add = add i32 %add8, %call7
  %add9 = add i32 %add, %mul6
  %idxprom = sext i32 %add9 to i64
  %arrayidx = getelementptr inbounds i64, i64* %in, i64 %idxprom
  %1 = load i64, i64* %arrayidx, align 8, !tbaa !2962
  %conv.i = sitofp i64 %1 to double
  %arrayidx12 = getelementptr inbounds double, double* %out, i64 %idxprom
  store double %conv.i, double* %arrayidx12, align 8, !tbaa !2943
  ret void
}

deven-amd avatar May 08 '18 17:05 deven-amd

Can you post the full IR, just the function is incomplete

arsenm avatar May 09 '18 17:05 arsenm

Nevermind, this is a library problem

arsenm avatar May 09 '18 17:05 arsenm

@mangupta @jedwards-AMD this appears to be due to faulty packaging of libhip_device.a. The version we're shipping (and have shipped) appears to archive the old, broken versions of the conversion functions, which were present before https://github.com/ROCm-Developer-Tools/HIP/pull/267 was merged. Could you please look into why that is the case? Thank you.

AlexVlx avatar May 24 '18 13:05 AlexVlx

Do you know what version of the hip_hcc package was installed? The fix for #267 was made in November of 2017.

jedwards-AMD avatar May 24 '18 13:05 jedwards-AMD

@jithunnair-amd is on 1.7.x, but this applies to 1.8.0 as well, we verified that last evening with @jlgreathouse.

AlexVlx avatar May 24 '18 14:05 AlexVlx

I ran into this issue on ROCm 1.9.211 today. An atomicAdd(double* address, double value), such as the one in /opt/rocm/hip/include/hip/hcc_detail/hip_atomic.h, implemented with a CAS loop using __longlong_as_double, gives incorrect results. Replacing __longlong_as_double with a chained reinterpret_cast solves the problem.

rwvo avatar Sep 28 '18 23:09 rwvo

Could someone please post an update of where things are with this issue?

jithunnair-amd avatar Nov 09 '18 18:11 jithunnair-amd

Looks like this has been fixed and merged into master, so ROCm 2.0 should have the fix. https://github.com/ROCm-Developer-Tools/HIP/pull/667

rwvo avatar Nov 09 '18 19:11 rwvo

Closing this since the fix has been merged into master branch.

mangupta avatar Nov 27 '18 15:11 mangupta