blob: 5e7ff66b6994653637da8d11069a3ffbf8a174b7 [file] [log] [blame]
Justin Holewinski83e96682012-05-24 17:43:12 +00001// RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple nvptx-unknown-unknown | FileCheck %s
Peter Collingbournef44bdf92012-05-20 21:08:35 +00002
Eli Benderskycb399432014-03-24 22:05:38 +00003// Verifies Clang emits correct address spaces and addrspacecast instructions
4// for CUDA code.
5
Eli Bendersky3468d9d2014-04-28 22:21:28 +00006#include "Inputs/cuda.h"
Peter Collingbournef44bdf92012-05-20 21:08:35 +00007
Jingyue Wu284ebe22015-08-22 05:49:28 +00008// CHECK: @i = addrspace(1) externally_initialized global
Peter Collingbournef44bdf92012-05-20 21:08:35 +00009__device__ int i;
10
Jingyue Wu284ebe22015-08-22 05:49:28 +000011// CHECK: @j = addrspace(4) externally_initialized global
Peter Collingbournef44bdf92012-05-20 21:08:35 +000012__constant__ int j;
13
Justin Holewinski83e96682012-05-24 17:43:12 +000014// CHECK: @k = addrspace(3) global
Peter Collingbournef44bdf92012-05-20 21:08:35 +000015__shared__ int k;
16
Eli Benderskycb399432014-03-24 22:05:38 +000017struct MyStruct {
18 int data1;
19 int data2;
20};
21
22// CHECK: @_ZZ5func0vE1a = internal addrspace(3) global %struct.MyStruct zeroinitializer
23// CHECK: @_ZZ5func1vE1a = internal addrspace(3) global float 0.000000e+00
24// CHECK: @_ZZ5func2vE1a = internal addrspace(3) global [256 x float] zeroinitializer
25// CHECK: @_ZZ5func3vE1a = internal addrspace(3) global float 0.000000e+00
26// CHECK: @_ZZ5func4vE1a = internal addrspace(3) global float 0.000000e+00
Jingyue Wu284ebe22015-08-22 05:49:28 +000027// CHECK: @b = addrspace(3) global float undef
Eli Benderskycb399432014-03-24 22:05:38 +000028
Peter Collingbournef44bdf92012-05-20 21:08:35 +000029__device__ void foo() {
David Blaikiea953f282015-02-27 21:19:58 +000030 // CHECK: load i32, i32* addrspacecast (i32 addrspace(1)* @i to i32*)
Peter Collingbournef44bdf92012-05-20 21:08:35 +000031 i++;
32
David Blaikiea953f282015-02-27 21:19:58 +000033 // CHECK: load i32, i32* addrspacecast (i32 addrspace(4)* @j to i32*)
Peter Collingbournef44bdf92012-05-20 21:08:35 +000034 j++;
35
David Blaikiea953f282015-02-27 21:19:58 +000036 // CHECK: load i32, i32* addrspacecast (i32 addrspace(3)* @k to i32*)
Peter Collingbournef44bdf92012-05-20 21:08:35 +000037 k++;
Peter Collingbourneee0502d2012-08-28 20:37:10 +000038
39 static int li;
David Blaikiea953f282015-02-27 21:19:58 +000040 // CHECK: load i32, i32* addrspacecast (i32 addrspace(1)* @_ZZ3foovE2li to i32*)
Peter Collingbourneee0502d2012-08-28 20:37:10 +000041 li++;
Peter Collingbournec6b08572012-08-28 20:37:50 +000042
43 __constant__ int lj;
David Blaikiea953f282015-02-27 21:19:58 +000044 // CHECK: load i32, i32* addrspacecast (i32 addrspace(4)* @_ZZ3foovE2lj to i32*)
Peter Collingbournec6b08572012-08-28 20:37:50 +000045 lj++;
46
47 __shared__ int lk;
David Blaikiea953f282015-02-27 21:19:58 +000048 // CHECK: load i32, i32* addrspacecast (i32 addrspace(3)* @_ZZ3foovE2lk to i32*)
Peter Collingbournec6b08572012-08-28 20:37:50 +000049 lk++;
Peter Collingbournef44bdf92012-05-20 21:08:35 +000050}
51
Eli Benderskycb399432014-03-24 22:05:38 +000052__device__ void func0() {
53 __shared__ MyStruct a;
54 MyStruct *ap = &a; // composite type
55 ap->data1 = 1;
56 ap->data2 = 2;
57}
58// CHECK: define void @_Z5func0v()
59// CHECK: store %struct.MyStruct* addrspacecast (%struct.MyStruct addrspace(3)* @_ZZ5func0vE1a to %struct.MyStruct*), %struct.MyStruct** %ap
60
61__device__ void callee(float *ap) {
62 *ap = 1.0f;
63}
64
65__device__ void func1() {
66 __shared__ float a;
67 callee(&a); // implicit cast from parameters
68}
69// CHECK: define void @_Z5func1v()
70// CHECK: call void @_Z6calleePf(float* addrspacecast (float addrspace(3)* @_ZZ5func1vE1a to float*))
71
72__device__ void func2() {
73 __shared__ float a[256];
74 float *ap = &a[128]; // implicit cast from a decayed array
75 *ap = 1.0f;
76}
77// CHECK: define void @_Z5func2v()
David Blaikiebdf40a62015-03-13 18:21:46 +000078// CHECK: store float* getelementptr inbounds ([256 x float], [256 x float]* addrspacecast ([256 x float] addrspace(3)* @_ZZ5func2vE1a to [256 x float]*), i32 0, i32 128), float** %ap
Eli Benderskycb399432014-03-24 22:05:38 +000079
80__device__ void func3() {
81 __shared__ float a;
82 float *ap = reinterpret_cast<float *>(&a); // explicit cast
83 *ap = 1.0f;
84}
85// CHECK: define void @_Z5func3v()
86// CHECK: store float* addrspacecast (float addrspace(3)* @_ZZ5func3vE1a to float*), float** %ap
87
88__device__ void func4() {
89 __shared__ float a;
90 float *ap = (float *)&a; // explicit c-style cast
91 *ap = 1.0f;
92}
93// CHECK: define void @_Z5func4v()
94// CHECK: store float* addrspacecast (float addrspace(3)* @_ZZ5func4vE1a to float*), float** %ap
95
96__shared__ float b;
97
98__device__ float *func5() {
99 return &b; // implicit cast from a return value
100}
101// CHECK: define float* @_Z5func5v()
102// CHECK: ret float* addrspacecast (float addrspace(3)* @b to float*)
Jingyue Wu4f7b9eb2015-03-25 20:06:28 +0000103
104struct StructWithCtor {
105 __device__ StructWithCtor(): data(1) {}
106 __device__ StructWithCtor(const StructWithCtor &second): data(second.data) {}
107 __device__ int getData() { return data; }
108 int data;
109};
110
111__device__ int construct_shared_struct() {
112// CHECK-LABEL: define i32 @_Z23construct_shared_structv()
113 __shared__ StructWithCtor s;
114// CHECK: call void @_ZN14StructWithCtorC1Ev(%struct.StructWithCtor* addrspacecast (%struct.StructWithCtor addrspace(3)* @_ZZ23construct_shared_structvE1s to %struct.StructWithCtor*))
115 __shared__ StructWithCtor t(s);
116// CHECK: call void @_ZN14StructWithCtorC1ERKS_(%struct.StructWithCtor* addrspacecast (%struct.StructWithCtor addrspace(3)* @_ZZ23construct_shared_structvE1t to %struct.StructWithCtor*), %struct.StructWithCtor* dereferenceable(4) addrspacecast (%struct.StructWithCtor addrspace(3)* @_ZZ23construct_shared_structvE1s to %struct.StructWithCtor*))
117 return t.getData();
118// CHECK: call i32 @_ZN14StructWithCtor7getDataEv(%struct.StructWithCtor* addrspacecast (%struct.StructWithCtor addrspace(3)* @_ZZ23construct_shared_structvE1t to %struct.StructWithCtor*))
119}