aboutsummaryrefslogtreecommitdiff
path: root/clang/test/CodeGenCUDA/device-use-host-var.cu
blob: 64de57e41b4b9f5a3db5fa80b3951ba8078ea57f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// RUN: %clang_cc1 -std=c++14 -triple amdgcn-amd-amdhsa \
// RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck %s
// RUN: %clang_cc1 -std=c++14 -triple amdgcn-amd-amdhsa \
// RUN:   -fcuda-is-device -emit-llvm -o - -x hip %s | FileCheck -check-prefix=NEG %s

#include "Inputs/cuda.h"

struct A {
  int x;
};

// Check the situation of B<T> has empty ctor but B<int> has non-empty ctor.
// Make sure const B<int> variables are not promoted to constant variables.
template<typename T>
struct B {
  T x;
  B() {}
  B(T _x) { x = _x; }
  static const B<T> y;
};

template<>
struct B<int> {
  int x;
  B() { x = 1; }
  static const B<int> y;
};

template<typename T>
const B<T> B<T>::y;

const B<int> B<int>::y;

template<typename T>
T temp_fun(T x) {
  return B<T>::y.x;
}

// Check template variable with empty default ctor but non-empty initializer
// ctor is not promoted.
template<typename T>
const B<T> b = B<T>(-1);

constexpr int constexpr_var = 1;
constexpr A constexpr_struct{2};
constexpr A constexpr_array[4] = {0, 0, 0, 3};
constexpr char constexpr_str[] = "abcd";
const int const_var = 4;
const A const_struct{5};
const A const_array[] = {0, 0, 0, 6};
const char const_str[] = "xyz";

// Check const variables used by host only are not emitted.
const int var_host_only = 7;

// CHECK-DAG: @_ZL13constexpr_str.const = private unnamed_addr addrspace(4) constant [5 x i8] c"abcd\00"
// CHECK-DAG: @_ZL13constexpr_var = internal addrspace(4) constant i32 1
// CHECK-DAG: @_ZL16constexpr_struct = internal addrspace(4) constant %struct.A { i32 2 }
// CHECK-DAG: @_ZL15constexpr_array = internal addrspace(4) constant [4 x %struct.A] [%struct.A zeroinitializer, %struct.A zeroinitializer, %struct.A zeroinitializer, %struct.A { i32 3 }]
// CHECK-DAG: @_ZL9const_var = internal addrspace(4) constant i32 4
// CHECK-DAG: @_ZL12const_struct = internal addrspace(4) constant %struct.A { i32 5 }
// CHECK-DAG: @_ZL11const_array = internal addrspace(4) constant [4 x %struct.A] [%struct.A zeroinitializer, %struct.A zeroinitializer, %struct.A zeroinitializer, %struct.A { i32 6 }]
// CHECK-DAG: @_ZL9const_str = internal addrspace(4) constant [4 x i8] c"xyz\00"

// NEG-NOT: @_ZN1BIiE1yE
// NEG-NOT: @_Z1bIdE
// NEG-NOT: @_ZL13var_host_only
// NEG-NOT: {{^}}@{{.*}} = external

// CHECK-LABEL: define{{.*}}@_Z7dev_funPiPPKi
// CHECK: store i32 1
// CHECK: store i32 2
// CHECK: store i32 3
// CHECK: load i8, ptr getelementptr {{.*}} @_ZL13constexpr_str.const
// CHECK: store i32 4
// CHECK: store i32 5
// CHECK: store i32 6
// CHECK: load i8, ptr getelementptr {{.*}} @_ZL9const_str
// CHECK: store ptr {{.*}}@_ZL13constexpr_var
// CHECK: store ptr {{.*}} @_ZL16constexpr_struct
// CHECK: store ptr getelementptr {{.*}} @_ZL15constexpr_array
// CHECK: store ptr {{.*}}@_ZL9const_var
// CHECK: store ptr {{.*}} @_ZL12const_struct
// CHECK: store ptr getelementptr {{.*}} @_ZL11const_array
__device__ void dev_fun(int *out, const int **out2) {
  *out = constexpr_var;
  *out = constexpr_struct.x;
  *out = constexpr_array[3].x;
  *out = constexpr_str[3];
  *out = const_var;
  *out = const_struct.x;
  *out = const_array[3].x;
  *out = const_str[3];
  *out2 = &constexpr_var;
  *out2 = &constexpr_struct.x;
  *out2 = &constexpr_array[3].x;
  *out2 = &const_var;
  *out2 = &const_struct.x;
  *out2 = &const_array[3].x;
}

void fun() {
  temp_fun(1);
  (void) b<double>;
  (void) var_host_only;
}