Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

Why is the preprocessing output of clang duplicated for cuda files?

Giving a file the extension .cu causes the preprocessing output (-E) of clang to be duplicated:

$ cat main.cu
int main(){}

$ clang -E -nocudalib -nocudainc main.cu
# 1 "main.cu"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 666 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "main.cu" 2
int main(){}
# 1 "main.cu"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 665 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "main.cu" 2
int main(){}

It seems normal when using .c:

$ cat main.c
int main(){}

$ clang -E -nocudalib -nocudainc main.c
clang: warning: argument unused during compilation: '-nocudainc' [-Wunused-command-line-argument]
# 1 "main.c"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 341 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "main.c" 2
int main(){}

Based on the warning I inferred that clang in fact correctly detects that it is a CUDA file. I can also do a compilation without -nocudainc -nocudalib, for example clang -E --cuda-gpu-arch=sm_50 main.cu. This output is also almost entirely duplicate, but 10,000s of lines.

Compiling the file normally as either .cu or .c works. If I save the preprocessor output as a .i file, I can compile the output from the .c file, but not from the .cu file:

$ clang main.i
main.cu:1:5: error: redefinition of 'main'
int main(){}
    ^
main.cu:1:5: note: previous definition is here
int main(){}
    ^
1 error generated.

I would like to know why the output is duplicated, and if I can avoid it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The output is duplicated because clang compiles CUDA both for the host and the device (GPU) separately. You can use --cuda-device-only or --cuda-host-only to only output the gpu or host code respectively.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...