1 // RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple nvptx-unknown-unknown | FileCheck %s
3 // Verifies Clang emits correct address spaces and addrspacecast instructions
6 #include "Inputs/cuda.h"
8 // CHECK: @i = addrspace(1) externally_initialized global
11 // CHECK: @j = addrspace(4) externally_initialized global
14 // CHECK: @k = addrspace(3) global
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
27 // CHECK: @b = addrspace(3) global float undef
28 // CHECK: @c = addrspace(3) global %struct.c undef
29 // CHECK @d = addrspace(3) global %struct.d undef
31 __device__ void foo() {
32 // CHECK: load i32, i32* addrspacecast (i32 addrspace(1)* @i to i32*)
35 // CHECK: load i32, i32* addrspacecast (i32 addrspace(4)* @j to i32*)
38 // CHECK: load i32, i32* addrspacecast (i32 addrspace(3)* @k to i32*)
42 // CHECK: load i32, i32* addrspacecast (i32 addrspace(1)* @_ZZ3foovE2li to i32*)
46 // CHECK: load i32, i32* addrspacecast (i32 addrspace(4)* @_ZZ3foovE2lj to i32*)
50 // CHECK: load i32, i32* addrspacecast (i32 addrspace(3)* @_ZZ3foovE2lk to i32*)
54 __device__ void func0() {
55 __shared__ MyStruct a;
56 MyStruct *ap = &a; // composite type
60 // CHECK: define void @_Z5func0v()
61 // CHECK: store %struct.MyStruct* addrspacecast (%struct.MyStruct addrspace(3)* @_ZZ5func0vE1a to %struct.MyStruct*), %struct.MyStruct** %ap
63 __device__ void callee(float *ap) {
67 __device__ void func1() {
69 callee(&a); // implicit cast from parameters
71 // CHECK: define void @_Z5func1v()
72 // CHECK: call void @_Z6calleePf(float* addrspacecast (float addrspace(3)* @_ZZ5func1vE1a to float*))
74 __device__ void func2() {
75 __shared__ float a[256];
76 float *ap = &a[128]; // implicit cast from a decayed array
79 // CHECK: define void @_Z5func2v()
80 // 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
82 __device__ void func3() {
84 float *ap = reinterpret_cast<float *>(&a); // explicit cast
87 // CHECK: define void @_Z5func3v()
88 // CHECK: store float* addrspacecast (float addrspace(3)* @_ZZ5func3vE1a to float*), float** %ap
90 __device__ void func4() {
92 float *ap = (float *)&a; // explicit c-style cast
95 // CHECK: define void @_Z5func4v()
96 // CHECK: store float* addrspacecast (float addrspace(3)* @_ZZ5func4vE1a to float*), float** %ap
100 __device__ float *func5() {
101 return &b; // implicit cast from a return value
103 // CHECK: define float* @_Z5func5v()
104 // CHECK: ret float* addrspacecast (float addrspace(3)* @b to float*)
106 struct StructWithCtor {
107 __device__ StructWithCtor(): data(1) {}
108 __device__ StructWithCtor(const StructWithCtor &second): data(second.data) {}
109 __device__ int getData() { return data; }
113 __device__ int construct_shared_struct() {
114 // CHECK-LABEL: define i32 @_Z23construct_shared_structv()
115 __shared__ StructWithCtor s;
116 // CHECK: call void @_ZN14StructWithCtorC1Ev(%struct.StructWithCtor* addrspacecast (%struct.StructWithCtor addrspace(3)* @_ZZ23construct_shared_structvE1s to %struct.StructWithCtor*))
117 __shared__ StructWithCtor t(s);
118 // 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*))
120 // CHECK: call i32 @_ZN14StructWithCtor7getDataEv(%struct.StructWithCtor* addrspacecast (%struct.StructWithCtor addrspace(3)* @_ZZ23construct_shared_structvE1t to %struct.StructWithCtor*))
123 // Make sure we allow __shared__ structures with default or empty constructors.
127 __shared__ struct c c;
133 __shared__ struct d d;