address-spaces.cu 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple nvptx-unknown-unknown | FileCheck %s
  2. // Verifies Clang emits correct address spaces and addrspacecast instructions
  3. // for CUDA code.
  4. #include "Inputs/cuda.h"
  5. // CHECK: @i = addrspace(1) global
  6. __device__ int i;
  7. // CHECK: @j = addrspace(4) global
  8. __constant__ int j;
  9. // CHECK: @k = addrspace(3) global
  10. __shared__ int k;
  11. struct MyStruct {
  12. int data1;
  13. int data2;
  14. };
  15. // CHECK: @_ZZ5func0vE1a = internal addrspace(3) global %struct.MyStruct zeroinitializer
  16. // CHECK: @_ZZ5func1vE1a = internal addrspace(3) global float 0.000000e+00
  17. // CHECK: @_ZZ5func2vE1a = internal addrspace(3) global [256 x float] zeroinitializer
  18. // CHECK: @_ZZ5func3vE1a = internal addrspace(3) global float 0.000000e+00
  19. // CHECK: @_ZZ5func4vE1a = internal addrspace(3) global float 0.000000e+00
  20. // CHECK: @b = addrspace(3) global float 0.000000e+00
  21. __device__ void foo() {
  22. // CHECK: load i32, i32* addrspacecast (i32 addrspace(1)* @i to i32*)
  23. i++;
  24. // CHECK: load i32, i32* addrspacecast (i32 addrspace(4)* @j to i32*)
  25. j++;
  26. // CHECK: load i32, i32* addrspacecast (i32 addrspace(3)* @k to i32*)
  27. k++;
  28. static int li;
  29. // CHECK: load i32, i32* addrspacecast (i32 addrspace(1)* @_ZZ3foovE2li to i32*)
  30. li++;
  31. __constant__ int lj;
  32. // CHECK: load i32, i32* addrspacecast (i32 addrspace(4)* @_ZZ3foovE2lj to i32*)
  33. lj++;
  34. __shared__ int lk;
  35. // CHECK: load i32, i32* addrspacecast (i32 addrspace(3)* @_ZZ3foovE2lk to i32*)
  36. lk++;
  37. }
  38. __device__ void func0() {
  39. __shared__ MyStruct a;
  40. MyStruct *ap = &a; // composite type
  41. ap->data1 = 1;
  42. ap->data2 = 2;
  43. }
  44. // CHECK: define void @_Z5func0v()
  45. // CHECK: store %struct.MyStruct* addrspacecast (%struct.MyStruct addrspace(3)* @_ZZ5func0vE1a to %struct.MyStruct*), %struct.MyStruct** %ap
  46. __device__ void callee(float *ap) {
  47. *ap = 1.0f;
  48. }
  49. __device__ void func1() {
  50. __shared__ float a;
  51. callee(&a); // implicit cast from parameters
  52. }
  53. // CHECK: define void @_Z5func1v()
  54. // CHECK: call void @_Z6calleePf(float* addrspacecast (float addrspace(3)* @_ZZ5func1vE1a to float*))
  55. __device__ void func2() {
  56. __shared__ float a[256];
  57. float *ap = &a[128]; // implicit cast from a decayed array
  58. *ap = 1.0f;
  59. }
  60. // CHECK: define void @_Z5func2v()
  61. // 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
  62. __device__ void func3() {
  63. __shared__ float a;
  64. float *ap = reinterpret_cast<float *>(&a); // explicit cast
  65. *ap = 1.0f;
  66. }
  67. // CHECK: define void @_Z5func3v()
  68. // CHECK: store float* addrspacecast (float addrspace(3)* @_ZZ5func3vE1a to float*), float** %ap
  69. __device__ void func4() {
  70. __shared__ float a;
  71. float *ap = (float *)&a; // explicit c-style cast
  72. *ap = 1.0f;
  73. }
  74. // CHECK: define void @_Z5func4v()
  75. // CHECK: store float* addrspacecast (float addrspace(3)* @_ZZ5func4vE1a to float*), float** %ap
  76. __shared__ float b;
  77. __device__ float *func5() {
  78. return &b; // implicit cast from a return value
  79. }
  80. // CHECK: define float* @_Z5func5v()
  81. // CHECK: ret float* addrspacecast (float addrspace(3)* @b to float*)
  82. struct StructWithCtor {
  83. __device__ StructWithCtor(): data(1) {}
  84. __device__ StructWithCtor(const StructWithCtor &second): data(second.data) {}
  85. __device__ int getData() { return data; }
  86. int data;
  87. };
  88. __device__ int construct_shared_struct() {
  89. // CHECK-LABEL: define i32 @_Z23construct_shared_structv()
  90. __shared__ StructWithCtor s;
  91. // CHECK: call void @_ZN14StructWithCtorC1Ev(%struct.StructWithCtor* addrspacecast (%struct.StructWithCtor addrspace(3)* @_ZZ23construct_shared_structvE1s to %struct.StructWithCtor*))
  92. __shared__ StructWithCtor t(s);
  93. // 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*))
  94. return t.getData();
  95. // CHECK: call i32 @_ZN14StructWithCtor7getDataEv(%struct.StructWithCtor* addrspacecast (%struct.StructWithCtor addrspace(3)* @_ZZ23construct_shared_structvE1t to %struct.StructWithCtor*))
  96. }