1 // RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple nvptx-unknown-unknown | FileCheck %s
2 // RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple amdgcn | FileCheck %s
4 // Verifies Clang emits correct address spaces and addrspacecast instructions
7 #include "Inputs/cuda.h"
9 // CHECK: @i ={{.*}} addrspace(1) externally_initialized global
12 // CHECK: @j ={{.*}} addrspace(4) externally_initialized constant
15 // CHECK: @k ={{.*}} addrspace(3) global
23 // CHECK: @_ZZ5func0vE1a = internal addrspace(3) global %struct.MyStruct undef
24 // CHECK: @_ZZ5func1vE1a = internal addrspace(3) global float undef
25 // CHECK: @_ZZ5func2vE1a = internal addrspace(3) global [256 x float] undef
26 // CHECK: @_ZZ5func3vE1a = internal addrspace(3) global float undef
27 // CHECK: @_ZZ5func4vE1a = internal addrspace(3) global float undef
28 // CHECK: @b ={{.*}} addrspace(3) global float undef
30 __device__ void foo() {
31 // CHECK: load i32, ptr addrspacecast (ptr addrspace(1) @i to ptr)
34 // CHECK: load i32, ptr addrspacecast (ptr addrspace(4) @j to ptr)
37 // CHECK: load i32, ptr addrspacecast (ptr addrspace(3) @k to ptr)
41 // CHECK: load i32, ptr addrspacecast (ptr addrspace(3) @_ZZ3foovE2lk to ptr)
45 __device__ void func0() {
46 __shared__ MyStruct a;
47 MyStruct *ap = &a; // composite type
51 // CHECK: define{{.*}} void @_Z5func0v()
52 // CHECK: store ptr addrspacecast (ptr addrspace(3) @_ZZ5func0vE1a to ptr), ptr %{{.*}}
54 __device__ void callee(float *ap) {
58 __device__ void func1() {
60 callee(&a); // implicit cast from parameters
62 // CHECK: define{{.*}} void @_Z5func1v()
63 // CHECK: call void @_Z6calleePf(ptr noundef addrspacecast (ptr addrspace(3) @_ZZ5func1vE1a to ptr))
65 __device__ void func2() {
66 __shared__ float a[256];
67 float *ap = &a[128]; // implicit cast from a decayed array
70 // CHECK: define{{.*}} void @_Z5func2v()
71 // CHECK: store ptr getelementptr inbounds ([256 x float], ptr addrspacecast (ptr addrspace(3) @_ZZ5func2vE1a to ptr), i{{32|64}} 0, i{{32|64}} 128), ptr %{{.*}}
73 __device__ void func3() {
75 float *ap = reinterpret_cast<float *>(&a); // explicit cast
78 // CHECK: define{{.*}} void @_Z5func3v()
79 // CHECK: store ptr addrspacecast (ptr addrspace(3) @_ZZ5func3vE1a to ptr), ptr %{{.*}}
81 __device__ void func4() {
83 float *ap = (float *)&a; // explicit c-style cast
86 // CHECK: define{{.*}} void @_Z5func4v()
87 // CHECK: store ptr addrspacecast (ptr addrspace(3) @_ZZ5func4vE1a to ptr), ptr %{{.*}}
91 __device__ float *func5() {
92 return &b; // implicit cast from a return value
94 // CHECK: define{{.*}} ptr @_Z5func5v()
95 // CHECK: ret ptr addrspacecast (ptr addrspace(3) @b to ptr)