Skip to content

[CUDA][HIP] Report error about global/device function while device code has already been generated #117831

@aywala

Description

@aywala
#include <hip/hip_runtime.h>
#include <stdio.h>

#ifdef __HIP_DEVICE_COMPILE__
    #define HD __host__ __device__
#else
    #define HD
#endif

HD void foo(){
    printf("execute foo\n");
}

__global__ void kernel(){
    foo();
}

int main() {
    kernel<<<1,1>>>();
    hipDeviceSynchronize();
}
}

This code sample is error with clang when compiling for host target.

hip_macro.cpp:19:5: error: no matching function for call to 'foo'
    foo();
    ^~~
hip_macro.cpp:10:9: note: candidate function not viable: call to __host__ function from __global__ function
HD void foo(){
#include <stdio.h>

#ifdef __CUDA_ARCH__
    #define HD __host__ __device__
#else
    #define HD
#endif

HD void foo() {
    printf("execute foo\n");
}

__global__  void kernel(){
    foo(); 
}

int main() {
    kernel<<<1,1>>>();
    cudaDeviceSynchronize();
}

This code sample is ok with nvcc.

Why does clang parse device function once again when compiling for host target? And send an error for device function but the device code has already been generated. That does not make sense.

Real world problem:
Some header files have something like

#ifdef __CUDA_ARCH__
#define HD __host__ __device__
#else
    #define HD
#endif

When these header files are included in cuda source file, clang can not handle correctly. I know __CUDACC__ will be ok, but __CUDA_ARCH__ should be ok too. This is a problem caused by the design of compiler not caused by the language standard.

Metadata

Metadata

Assignees

No one assigned

    Labels

    cudainvalidResolved as invalid, i.e. not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions