xref: /llvm-project/clang/test/Driver/cuda-cross-compiling.c (revision 0c71fdd1575b826cbb3c252ee0b15fc84559abec)
1 // Tests the driver when targeting the NVPTX architecture directly without a
2 // host toolchain to perform CUDA mappings.
3 
4 //
5 // Test the generated phases when targeting NVPTX.
6 //
7 // RUN: %clang -target nvptx64-nvidia-cuda -ccc-print-phases %s 2>&1 \
8 // RUN:   | FileCheck -check-prefix=PHASES %s
9 
10 //      PHASES: 0: input, "[[INPUT:.+]]", c
11 // PHASES-NEXT: 1: preprocessor, {0}, cpp-output
12 // PHASES-NEXT: 2: compiler, {1}, ir
13 // PHASES-NEXT: 3: backend, {2}, assembler
14 // PHASES-NEXT: 4: assembler, {3}, object
15 // PHASES-NEXT: 5: linker, {4}, image
16 
17 //
18 // Test the generated bindings when targeting NVPTX.
19 //
20 // RUN: %clang -target nvptx64-nvidia-cuda -ccc-print-bindings %s 2>&1 \
21 // RUN:   | FileCheck -check-prefix=BINDINGS %s
22 
23 //      BINDINGS: "nvptx64-nvidia-cuda" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[PTX:.+]].s"
24 // BINDINGS-NEXT: "nvptx64-nvidia-cuda" - "NVPTX::Assembler", inputs: ["[[PTX]].s"], output: "[[CUBIN:.+]].o"
25 // BINDINGS-NEXT: "nvptx64-nvidia-cuda" - "NVPTX::Linker", inputs: ["[[CUBIN]].o"], output: "a.out"
26 
27 //
28 // Test the generated arguments to the CUDA binary utils when targeting NVPTX.
29 // Ensure that the '.o' files are converted to '.cubin' if produced internally.
30 //
31 // RUN: %clang -target nvptx64-nvidia-cuda -march=sm_61 -### %s 2>&1 \
32 // RUN:   | FileCheck -check-prefix=ARGS %s
33 
34 //      ARGS: -cc1" "-triple" "nvptx64-nvidia-cuda" "-S" {{.*}} "-target-cpu" "sm_61" "-target-feature" "+ptx{{[0-9]+}}" {{.*}} "-o" "[[PTX:.+]].s"
35 // ARGS-NEXT: ptxas{{.*}}"-m64" "-O0" "--gpu-name" "sm_61" "--output-file" "[[CUBIN:.+]].o" "[[PTX]].s" "-c"
36 // ARGS-NEXT: clang-nvlink-wrapper{{.*}}"-o" "a.out" "-arch" "sm_61"{{.*}}"[[CUBIN]].o"
37 
38 //
39 // Test the generated arguments to the CUDA binary utils when targeting NVPTX.
40 // Ensure that we emit '.o' files if compiled with '-c'
41 //
42 // RUN: %clang -target nvptx64-nvidia-cuda -march=sm_61 -c -### %s 2>&1 \
43 // RUN:   | FileCheck -check-prefix=OBJECT %s
44 // RUN: %clang -target nvptx64-nvidia-cuda -save-temps -march=sm_61 -c -### %s 2>&1 \
45 // RUN:   | FileCheck -check-prefix=OBJECT %s
46 
47 //      OBJECT: -cc1" "-triple" "nvptx64-nvidia-cuda" "-S" {{.*}} "-target-cpu" "sm_61" "-target-feature" "+ptx{{[0-9]+}}" {{.*}} "-o" "[[PTX:.+]].s"
48 // OBJECT-NEXT: ptxas{{.*}}"-m64" "-O0" "--gpu-name" "sm_61" "--output-file" "[[OBJ:.+]].o" "[[PTX]].s" "-c"
49 
50 //
51 // Test the generated arguments to the CUDA binary utils when targeting NVPTX.
52 // Ensure that we copy input '.o' files to '.cubin' files when linking.
53 //
54 // RUN: touch %t.o
55 // RUN: %clang -target nvptx64-nvidia-cuda -march=sm_61 -### %t.o 2>&1 \
56 // RUN:   | FileCheck -check-prefix=LINK %s
57 
58 // LINK: clang-nvlink-wrapper{{.*}}"-o" "a.out" "-arch" "sm_61"{{.*}}[[CUBIN:.+]].o
59 
60 //
61 // Test to ensure that we enable handling global constructors in a freestanding
62 // Nvidia compilation.
63 //
64 // RUN: %clang -target nvptx64-nvidia-cuda -march=sm_70 %s -### 2>&1 \
65 // RUN:   | FileCheck -check-prefix=LOWERING %s
66 // RUN: %clang -target nvptx64-nvidia-cuda -march=sm_70 -flto -c %s -### 2>&1 \
67 // RUN:   | FileCheck -check-prefix=LOWERING-LTO %s
68 
69 // LOWERING: -cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}} "-mllvm" "--nvptx-lower-global-ctor-dtor"
70 // LOWERING: clang-nvlink-wrapper{{.*}} "-mllvm" "--nvptx-lower-global-ctor-dtor"
71 // LOWERING-LTO-NOT: "--nvptx-lower-global-ctor-dtor"
72 
73 //
74 // Test passing arguments directly to nvlink.
75 //
76 // RUN: %clang -target nvptx64-nvidia-cuda -Wl,-v -Wl,a,b -march=sm_52 -### %s 2>&1 \
77 // RUN:   | FileCheck -check-prefix=LINKER-ARGS %s
78 
79 // LINKER-ARGS: clang-nvlink-wrapper{{.*}}"-v"{{.*}}"a" "b"
80 
81 // Tests for handling a missing architecture.
82 //
83 // RUN: not %clang -target nvptx64-nvidia-cuda %s -### 2>&1 \
84 // RUN:   | FileCheck -check-prefix=MISSING %s
85 // RUN: not %clang -target nvptx64-nvidia-cuda -march=generic %s -### 2>&1 \
86 // RUN:   | FileCheck -check-prefix=MISSING %s
87 
88 // MISSING: error: must pass in an explicit nvptx64 gpu architecture to 'ptxas'
89 // MISSING: error: must pass in an explicit nvptx64 gpu architecture to 'nvlink'
90 
91 // Do not error when performing LTO.
92 //
93 // RUN: %clang -target nvptx64-nvidia-cuda -flto %s -### 2>&1 \
94 // RUN:   | FileCheck -check-prefix=MISSING-LTO %s
95 
96 // MISSING-LTO-NOT: error: must pass in an explicit nvptx64 gpu architecture to 'nvlink'
97 
98 // RUN: %clang -target nvptx64-nvidia-cuda -flto -c %s -### 2>&1 \
99 // RUN:   | FileCheck -check-prefix=GENERIC %s
100 // RUN: %clang -target nvptx64-nvidia-cuda -march=sm_52 -march=generic -flto -c %s -### 2>&1 \
101 // RUN:   | FileCheck -check-prefix=GENERIC %s
102 
103 // GENERIC-NOT: -cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}} "-target-cpu"
104 
105 //
106 // Test forwarding the necessary +ptx feature.
107 //
108 // RUN: %clang -target nvptx64-nvidia-cuda --cuda-feature=+ptx63 -march=sm_52 -### %s 2>&1 \
109 // RUN:   | FileCheck -check-prefix=FEATURE %s
110 
111 // FEATURE: clang-nvlink-wrapper{{.*}}"--plugin-opt=-mattr=+ptx63"
112 
113 //
114 // Test including the libc startup files and libc
115 //
116 // RUN: %clang -target nvptx64-nvidia-cuda -march=sm_61 -stdlib -startfiles \
117 // RUN:   -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
118 
119 // STARTUP: clang-nvlink-wrapper{{.*}}"-lc" "-lm" "{{.*}}crt1.o"
120