ROCm / HIPIFY

HIPIFY: Convert CUDA to Portable C++ Code
https://rocm.docs.amd.com/projects/HIPIFY/en/latest/
MIT License
524 stars 75 forks source link

[HIPIFY] Macros not evaluated #281

Closed ymeiron closed 3 years ago

ymeiron commented 3 years ago

The code I am trying to hipify looks like that

// test.cu
void func()
{
#if 0
    int var;
#else
    int var;
#endif
}

The command I issue is hipify-clang test.cu --cuda-path=/usr/local/cuda and the error message is:

/tmp/test.cu-d6adf8.hip:6:9: error: redefinition of 'var'
    int var;
        ^
/tmp/test.cu-d6adf8.hip:4:9: note: previous definition is here
    int var;
        ^
1 error generated when compiling for host.
Error while processing /tmp/test.cu-d6adf8.hip.

Obviously clang tries to evaluate the #if 0 code block, which is incorrect. I installed version 12.0.0 from the ROCm repository on Centos 7.

emankov commented 3 years ago

It is intentionally so; the point is to hipify all preprocessor conditional branches presented in the source CUDA code, and it is the default (hipify-clang's, not clang's) behaviour. To switch to the compiler's default behaviour, when blocks under false conditional branches are removed by the preprocessor from the further compilation, the hipify-clang's option --skip-excluded-preprocessor-conditional-blocks should be specified. Please, run hipify-clang --help for more information.

P.S. The above functionality in clang's preprocessor was specially implemented under RetainExcludedConditionalBlocks option, which is just the opposite to the hipify-clang's --skip-excluded-preprocessor-conditional-blocks.

ymeiron commented 3 years ago

Thanks for the explanation. Of course it would have been much nicer if all preprocessor conditional branches were evaluated separately such that redefining a variable in the #else block wouldn't cause an error, it is valid C++ after all. But for my needs choosing a branch and using --skip-excluded-preprocessor-conditional-blocks did the job.

emankov commented 3 years ago

Hipify-clang is not actually a compiler even it is based on clang's FE. It is a converter and its main task is to convert as much as possible from one source code written in CUDA to another, written in HIP, including the code under conditional macros.

But you're right, that this aspect is not obvious, when you get a compilation error on a valid C++ code. After implementing https://github.com/ROCm-Developer-Tools/HIPIFY/issues/1 the issue you reported will go away regardless of either --skip-excluded-preprocessor-conditional-blocks option is set or not. Btw, your case is mentioned in #1 among three main cases.

emankov commented 3 years ago

Closing as answered.