2012-05-25 01:43:12 +08:00
// RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple nvptx-unknown-unknown | FileCheck %s
2018-04-03 01:38:24 +08:00
// RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple amdgcn | FileCheck %s
2012-05-21 05:08:35 +08:00
2014-03-25 06:05:38 +08:00
// Verifies Clang emits correct address spaces and addrspacecast instructions
// for CUDA code.
2014-04-29 06:21:28 +08:00
#include "Inputs/cuda.h"
2012-05-21 05:08:35 +08:00
2015-08-22 13:49:28 +08:00
// CHECK: @i = addrspace(1) externally_initialized global
2012-05-21 05:08:35 +08:00
__device__ int i;
2015-08-22 13:49:28 +08:00
// CHECK: @j = addrspace(4) externally_initialized global
2012-05-21 05:08:35 +08:00
__constant__ int j;
2012-05-25 01:43:12 +08:00
// CHECK: @k = addrspace(3) global
2012-05-21 05:08:35 +08:00
__shared__ int k;
2014-03-25 06:05:38 +08:00
struct MyStruct {
int data1;
int data2;
};
2018-04-03 01:38:24 +08:00
// CHECK: @_ZZ5func0vE1a = internal addrspace(3) global %struct.MyStruct undef
// CHECK: @_ZZ5func1vE1a = internal addrspace(3) global float undef
// CHECK: @_ZZ5func2vE1a = internal addrspace(3) global [256 x float] undef
// CHECK: @_ZZ5func3vE1a = internal addrspace(3) global float undef
// CHECK: @_ZZ5func4vE1a = internal addrspace(3) global float undef
2015-08-22 13:49:28 +08:00
// CHECK: @b = addrspace(3) global float undef
2014-03-25 06:05:38 +08:00
2012-05-21 05:08:35 +08:00
__device__ void foo() {
2015-02-28 05:19:58 +08:00
// CHECK: load i32, i32* addrspacecast (i32 addrspace(1)* @i to i32*)
2012-05-21 05:08:35 +08:00
i++;
2015-02-28 05:19:58 +08:00
// CHECK: load i32, i32* addrspacecast (i32 addrspace(4)* @j to i32*)
2012-05-21 05:08:35 +08:00
j++;
2015-02-28 05:19:58 +08:00
// CHECK: load i32, i32* addrspacecast (i32 addrspace(3)* @k to i32*)
2012-05-21 05:08:35 +08:00
k++;
2012-08-29 04:37:10 +08:00
2012-08-29 04:37:50 +08:00
__shared__ int lk;
2015-02-28 05:19:58 +08:00
// CHECK: load i32, i32* addrspacecast (i32 addrspace(3)* @_ZZ3foovE2lk to i32*)
2012-08-29 04:37:50 +08:00
lk++;
2012-05-21 05:08:35 +08:00
}
2014-03-25 06:05:38 +08:00
__device__ void func0() {
__shared__ MyStruct a;
MyStruct *ap = &a; // composite type
ap->data1 = 1;
ap->data2 = 2;
}
// CHECK: define void @_Z5func0v()
2018-04-03 01:38:24 +08:00
// CHECK: store %struct.MyStruct* addrspacecast (%struct.MyStruct addrspace(3)* @_ZZ5func0vE1a to %struct.MyStruct*), %struct.MyStruct** %{{.*}}
2014-03-25 06:05:38 +08:00
__device__ void callee(float *ap) {
*ap = 1.0f;
}
__device__ void func1() {
__shared__ float a;
callee(&a); // implicit cast from parameters
}
// CHECK: define void @_Z5func1v()
// CHECK: call void @_Z6calleePf(float* addrspacecast (float addrspace(3)* @_ZZ5func1vE1a to float*))
__device__ void func2() {
__shared__ float a[256];
float *ap = &a[128]; // implicit cast from a decayed array
*ap = 1.0f;
}
// CHECK: define void @_Z5func2v()
2018-04-03 01:38:24 +08:00
// CHECK: store float* getelementptr inbounds ([256 x float], [256 x float]* addrspacecast ([256 x float] addrspace(3)* @_ZZ5func2vE1a to [256 x float]*), i{{32|64}} 0, i{{32|64}} 128), float** %{{.*}}
2014-03-25 06:05:38 +08:00
__device__ void func3() {
__shared__ float a;
float *ap = reinterpret_cast<float *>(&a); // explicit cast
*ap = 1.0f;
}
// CHECK: define void @_Z5func3v()
2018-04-03 01:38:24 +08:00
// CHECK: store float* addrspacecast (float addrspace(3)* @_ZZ5func3vE1a to float*), float** %{{.*}}
2014-03-25 06:05:38 +08:00
__device__ void func4() {
__shared__ float a;
float *ap = (float *)&a; // explicit c-style cast
*ap = 1.0f;
}
// CHECK: define void @_Z5func4v()
2018-04-03 01:38:24 +08:00
// CHECK: store float* addrspacecast (float addrspace(3)* @_ZZ5func4vE1a to float*), float** %{{.*}}
2014-03-25 06:05:38 +08:00
__shared__ float b;
__device__ float *func5() {
return &b; // implicit cast from a return value
}
// CHECK: define float* @_Z5func5v()
// CHECK: ret float* addrspacecast (float addrspace(3)* @b to float*)