chipStar icon indicating copy to clipboard operation
chipStar copied to clipboard

Naming conflict between built-in functions of CUDA and OCL

Open MathisonBao opened this issue 1 year ago • 4 comments

The void sincosf(float, float *, float *) is a CUDA built-in function, the float sincos(float, float *) is a OCL built-in function but not CUDA. So the user could define a function as same as the OCL one.

In the implementation of sincosf function, sincos will be called. (in order to use the SPIR-V Instruction: OpExtInst sincos), but if a user defines a function, named sincos, the sincosf will return the wrong result (sincosf will call the function that the user defined).

For example:

__device__
float sincos(float x, float &y) {
    *y = x + 1.0;
    return x + 2.0;
}

__global__
void test(float *IN, float *OUT0, float *OUT1, int num) {
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < num) {
        float x, y;
        x = sincos(IN[i], &y);
        float a, b;
        sincosf(IN[i], &a, &b);
        OUT0[i] = x + y;
        OUT1[i] = a + b;
    }
}

The data in OUT0 and the data in OUT1 will be the same. Is this a bug? Is there any solution? Thanks a lot.

MathisonBao avatar May 10 '24 06:05 MathisonBao

A bug. We might need to rename the conflicting user functions using macros.

pjaaskel avatar May 10 '24 09:05 pjaaskel

Hi, pjaaskel:

Thanks for your reply. Could you explain in detail how to solve it using macro? I'm not sure how to use macros to differentiate between device or host functions with the same function name.

Thanks, Mathison

MathisonBao avatar May 11 '24 05:05 MathisonBao

I meant that hipcc could (force) include a header that has lines such as ``#define sincos _user_sincos` that avoid the name clash. But after a bit of thinking, that's not an ideal solution as it renames user facing symbols, so we should do something similar internally for the sincos() builtin calls.

pjaaskel avatar May 13 '24 06:05 pjaaskel

A proper way to fix this issue is to use SPIR-V builtins instead of OpenCL built-ins in the headers. E.g. HIP's sincosf would be mapped to __spirv_ocl_sincos.

linehill avatar May 13 '24 10:05 linehill

Okay, thanks pjaaskel and linehill!

MathisonBao avatar May 26 '24 02:05 MathisonBao