Debian Codegraph @

clang/test/SemaCUDA/deduction-guide-overload.cu

raw
1 // RUN: %clang_cc1 -std=c++20 -triple nvptx64-nvidia-cuda -fsyntax-only \
2 // RUN: -fcuda-is-device -verify %s
3 // RUN: %clang_cc1 -std=c++20 -triple nvptx64-nvidia-cuda -fsyntax-only \
4 // RUN: -verify %s
5 // expected-no-diagnostics
6
7 #include "Inputs/cuda.h"
8
9 // This test exercises class template argument deduction (CTAD) when there are
10 // multiple constructors that differ only by constraints. In CUDA/HIP mode, the
11 // implementation must *not* collapse implicit deduction guides that have the
12 // same function type but different constraints; otherwise, CTAD can lose viable
13 // candidates.
14
15 template <typename T>
16 concept Signed = __is_signed(T);
17
18 template <typename T>
19 concept NotSigned = !Signed<T>;
20
21 // 1) Constrained ctors with different constraints: ensure we keep
22 // deduction guides that differ only by constraints.
23
24 template <typename T>
25 struct OverloadCTAD {
26 __host__ __device__ OverloadCTAD(T) requires Signed<T>;
27 __host__ __device__ OverloadCTAD(T) requires NotSigned<T>;
28 };
29
30 __host__ __device__ void use_overload_ctad_hd() {
31 OverloadCTAD a(1); // T = int, uses Signed-constrained guide
32 OverloadCTAD b(1u); // T = unsigned int, uses NotSigned-constrained guide
33 }
34
35 __device__ void use_overload_ctad_dev() {
36 OverloadCTAD c(1);
37 OverloadCTAD d(1u);
38 }
39
40 __global__ void use_overload_ctad_global() {
41 OverloadCTAD e(1);
42 OverloadCTAD f(1u);
43 }
44
45 // 2) Add a pair of constructors that have the same signature and the same
46 // constraint but differ only by CUDA target attributes. This exercises the
47 // case where two implicit deduction guides would be identical except for
48 // their originating constructor's CUDA target.
49
50 template <typename T>
51 struct OverloadCTADTargets {
52 __host__ OverloadCTADTargets(T) requires Signed<T>;
53 __device__ OverloadCTADTargets(T) requires Signed<T>;
54 };
55
56 __host__ void use_overload_ctad_targets_host() {
57 OverloadCTADTargets g(1);
58 }
59
60 __device__ void use_overload_ctad_targets_device() {
61 OverloadCTADTargets h(1);
62 }
63
64 // 3) Unconstrained host/device duplicates: identical signatures and no
65 // constraints, differing only by CUDA target attributes.
66
67 template <typename T>
68 struct UnconstrainedHD {
69 __host__ UnconstrainedHD(T);
70 __device__ UnconstrainedHD(T);
71 };
72
73 __host__ __device__ void use_unconstrained_hd_hd() {
74 UnconstrainedHD u1(1);
75 }
76
77 __device__ void use_unconstrained_hd_dev() {
78 UnconstrainedHD u2(1);
79 }
80
81 __global__ void use_unconstrained_hd_global() {
82 UnconstrainedHD u3(1);
83 }
84
85 // 4) Constrained vs unconstrained ctors with the same signature: guides
86 // must not be collapsed away when constraints differ.
87
88 template <typename T>
89 concept IsInt = __is_same(T, int);
90
91 template <typename T>
92 struct ConstrainedVsUnconstrained {
93 __host__ __device__ ConstrainedVsUnconstrained(T);
94 __host__ __device__ ConstrainedVsUnconstrained(T) requires IsInt<T>;
95 };
96
97 __host__ __device__ void use_constrained_vs_unconstrained_hd() {
98 ConstrainedVsUnconstrained a(1); // T = int, constrained guide viable
99 ConstrainedVsUnconstrained b(1u); // T = unsigned, only unconstrained guide
100 }
101
102 __device__ void use_constrained_vs_unconstrained_dev() {
103 ConstrainedVsUnconstrained c(1);
104 ConstrainedVsUnconstrained d(1u);
105 }
106
107 __global__ void use_constrained_vs_unconstrained_global() {
108 ConstrainedVsUnconstrained e(1);
109 ConstrainedVsUnconstrained f(1u);
110 }
111