| 1 |
// REQUIRES: x86-registered-target |
| 2 |
// REQUIRES: amdgpu-registered-target |
| 3 |
|
| 4 |
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fcuda-is-device -emit-llvm -x hip %s -o - | FileCheck --check-prefixes=COMMON,CHECK %s |
| 5 |
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fcuda-is-device -emit-llvm -x hip %s -disable-O0-optnone -o - | opt -S -O2 | FileCheck %s --check-prefixes=COMMON,OPT |
| 6 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -x hip %s -o - | FileCheck -check-prefix=HOST %s |
| 7 |
|
| 8 |
#include "Inputs/cuda.h" |
| 9 |
|
| 10 |
// Coerced struct from `struct S` without all generic pointers lowered into |
| 11 |
// global ones. |
| 12 |
|
| 13 |
// On the host-side compilation, generic pointer won't be coerced. |
| 14 |
// HOST-NOT: %struct.S.coerce |
| 15 |
// HOST-NOT: %struct.T.coerce |
| 16 |
|
| 17 |
// HOST: define{{.*}} void @_Z22__device_stub__kernel1Pi(ptr noundef %x) |
| 18 |
// COMMON-LABEL: define{{.*}} amdgpu_kernel void @_Z7kernel1Pi(ptr addrspace(1){{.*}} %x.coerce) |
| 19 |
// CHECK-NOT: ={{.*}} addrspacecast ptr addrspace(1) %{{.*}} to ptr |
| 20 |
// OPT: [[VAL:%.*]] = load i32, ptr addrspace(1) %x.coerce, align 4{{$}} |
| 21 |
// OPT: [[INC:%.*]] = add nsw i32 [[VAL]], 1 |
| 22 |
// OPT: store i32 [[INC]], ptr addrspace(1) %x.coerce, align 4 |
| 23 |
// OPT: ret void |
| 24 |
__global__ void kernel1(int *x) { |
| 25 |
x[0]++; |
| 26 |
} |
| 27 |
|
| 28 |
// HOST: define{{.*}} void @_Z22__device_stub__kernel2Ri(ptr noundef nonnull align 4 dereferenceable(4) %x) |
| 29 |
// COMMON-LABEL: define{{.*}} amdgpu_kernel void @_Z7kernel2Ri(ptr addrspace(1){{.*}} nonnull align 4 dereferenceable(4) %x.coerce) |
| 30 |
// CHECK-NOT: ={{.*}} addrspacecast ptr addrspace(1) %{{.*}} to ptr |
| 31 |
// OPT: [[VAL:%.*]] = load i32, ptr addrspace(1) %x.coerce, align 4{{$}} |
| 32 |
// OPT: [[INC:%.*]] = add nsw i32 [[VAL]], 1 |
| 33 |
// OPT: store i32 [[INC]], ptr addrspace(1) %x.coerce, align 4 |
| 34 |
// OPT: ret void |
| 35 |
__global__ void kernel2(int &x) { |
| 36 |
x++; |
| 37 |
} |
| 38 |
|
| 39 |
// HOST: define{{.*}} void @_Z22__device_stub__kernel3PU3AS2iPU3AS1i(ptr addrspace(2) noundef %x, ptr addrspace(1) noundef %y) |
| 40 |
// CHECK-LABEL: define{{.*}} amdgpu_kernel void @_Z7kernel3PU3AS2iPU3AS1i(ptr addrspace(2){{.*}} %x, ptr addrspace(1){{.*}} %y) |
| 41 |
// CHECK-NOT: ={{.*}} addrspacecast ptr addrspace(1) %{{.*}} to ptr |
| 42 |
__global__ void kernel3(__attribute__((address_space(2))) int *x, |
| 43 |
__attribute__((address_space(1))) int *y) { |
| 44 |
y[0] = x[0]; |
| 45 |
} |
| 46 |
|
| 47 |
// COMMON-LABEL: define{{.*}} void @_Z4funcPi(ptr{{.*}} %x) |
| 48 |
// CHECK-NOT: ={{.*}} addrspacecast ptr addrspace(1) %{{.*}} to ptr |
| 49 |
__device__ void func(int *x) { |
| 50 |
x[0]++; |
| 51 |
} |
| 52 |
|
| 53 |
struct S { |
| 54 |
int *x; |
| 55 |
float *y; |
| 56 |
}; |
| 57 |
// `by-val` struct is passed by-indirect-alias (a mix of by-ref and indirect |
| 58 |
// by-val). However, the enhanced address inferring pass should be able to |
| 59 |
// assume they are global pointers. |
| 60 |
// |
| 61 |
// HOST: define{{.*}} void @_Z22__device_stub__kernel41S(ptr %s.coerce0, ptr %s.coerce1) |
| 62 |
// COMMON-LABEL: define{{.*}} amdgpu_kernel void @_Z7kernel41S(ptr addrspace(4){{.*}} byref(%struct.S) align 8 %0) |
| 63 |
// OPT: [[P0:%.*]] = load ptr, ptr addrspace(4) %0, align 8 |
| 64 |
// OPT: [[G0:%.*]] ={{.*}} addrspacecast ptr [[P0]] to ptr addrspace(1) |
| 65 |
// OPT: [[R1:%.*]] = getelementptr inbounds i8, ptr addrspace(4) %0, i64 8 |
| 66 |
// OPT: [[P1:%.*]] = load ptr, ptr addrspace(4) [[R1]], align 8 |
| 67 |
// OPT: [[G1:%.*]] ={{.*}} addrspacecast ptr [[P1]] to ptr addrspace(1) |
| 68 |
// OPT: [[V0:%.*]] = load i32, ptr addrspace(1) [[G0]], align 4, !amdgpu.noclobber ![[MD:[0-9]+]] |
| 69 |
// OPT: [[INC:%.*]] = add nsw i32 [[V0]], 1 |
| 70 |
// OPT: store i32 [[INC]], ptr addrspace(1) [[G0]], align 4 |
| 71 |
// OPT: [[V1:%.*]] = load float, ptr addrspace(1) [[G1]], align 4 |
| 72 |
// OPT: [[ADD:%.*]] = fadd contract float [[V1]], 1.000000e+00 |
| 73 |
// OPT: store float [[ADD]], ptr addrspace(1) [[G1]], align 4 |
| 74 |
// OPT: ret void |
| 75 |
__global__ void kernel4(struct S s) { |
| 76 |
s.x[0]++; |
| 77 |
s.y[0] += 1.f; |
| 78 |
} |
| 79 |
|
| 80 |
// If a pointer to struct is passed, only the pointer itself is coerced into the global one. |
| 81 |
// HOST: define{{.*}} void @_Z22__device_stub__kernel5P1S(ptr noundef %s) |
| 82 |
// COMMON-LABEL: define{{.*}} amdgpu_kernel void @_Z7kernel5P1S(ptr addrspace(1){{.*}} %s.coerce) |
| 83 |
__global__ void kernel5(struct S *s) { |
| 84 |
s->x[0]++; |
| 85 |
s->y[0] += 1.f; |
| 86 |
} |
| 87 |
|
| 88 |
struct T { |
| 89 |
float *x[2]; |
| 90 |
}; |
| 91 |
// `by-val` array is passed by-indirect-alias (a mix of by-ref and indirect |
| 92 |
// by-val). However, the enhanced address inferring pass should be able to |
| 93 |
// assume they are global pointers. |
| 94 |
// |
| 95 |
// HOST: define{{.*}} void @_Z22__device_stub__kernel61T(ptr %t.coerce0, ptr %t.coerce1) |
| 96 |
// COMMON-LABEL: define{{.*}} amdgpu_kernel void @_Z7kernel61T(ptr addrspace(4){{.*}} byref(%struct.T) align 8 %0) |
| 97 |
// OPT: [[P0:%.*]] = load ptr, ptr addrspace(4) %0, align 8 |
| 98 |
// OPT: [[G0:%.*]] ={{.*}} addrspacecast ptr [[P0]] to ptr addrspace(1) |
| 99 |
// OPT: [[R1:%.*]] = getelementptr inbounds i8, ptr addrspace(4) %0, i64 8 |
| 100 |
// OPT: [[P1:%.*]] = load ptr, ptr addrspace(4) [[R1]], align 8 |
| 101 |
// OPT: [[G1:%.*]] ={{.*}} addrspacecast ptr [[P1]] to ptr addrspace(1) |
| 102 |
// OPT: [[V0:%.*]] = load float, ptr addrspace(1) [[G0]], align 4, !amdgpu.noclobber ![[MD]] |
| 103 |
// OPT: [[ADD0:%.*]] = fadd contract float [[V0]], 1.000000e+00 |
| 104 |
// OPT: store float [[ADD0]], ptr addrspace(1) [[G0]], align 4 |
| 105 |
// OPT: [[V1:%.*]] = load float, ptr addrspace(1) [[G1]], align 4 |
| 106 |
// OPT: [[ADD1:%.*]] = fadd contract float [[V1]], 2.000000e+00 |
| 107 |
// OPT: store float [[ADD1]], ptr addrspace(1) [[G1]], align 4 |
| 108 |
// OPT: ret void |
| 109 |
__global__ void kernel6(struct T t) { |
| 110 |
t.x[0][0] += 1.f; |
| 111 |
t.x[1][0] += 2.f; |
| 112 |
} |
| 113 |
|
| 114 |
// Check that coerced pointers retain the noalias attribute when qualified with __restrict. |
| 115 |
// HOST: define{{.*}} void @_Z22__device_stub__kernel7Pi(ptr noalias noundef %x) |
| 116 |
// COMMON-LABEL: define{{.*}} amdgpu_kernel void @_Z7kernel7Pi(ptr addrspace(1) noalias{{.*}} %x.coerce) |
| 117 |
__global__ void kernel7(int *__restrict x) { |
| 118 |
x[0]++; |
| 119 |
} |
| 120 |
|
| 121 |
// Single element struct. |
| 122 |
struct SS { |
| 123 |
float *x; |
| 124 |
}; |
| 125 |
// HOST: define{{.*}} void @_Z22__device_stub__kernel82SS(ptr %a.coerce) |
| 126 |
// COMMON-LABEL: define{{.*}} amdgpu_kernel void @_Z7kernel82SS(ptr addrspace(1){{.*}} %a.coerce) |
| 127 |
// CHECK-NOT: ={{.*}} addrspacecast ptr addrspace(1) %{{.*}} to ptr |
| 128 |
// OPT: [[VAL:%.*]] = load float, ptr addrspace(1) %a.coerce, align 4{{$}} |
| 129 |
// OPT: [[INC:%.*]] = fadd contract float [[VAL]], 3.000000e+00 |
| 130 |
// OPT: store float [[INC]], ptr addrspace(1) %a.coerce, align 4 |
| 131 |
// OPT: ret void |
| 132 |
__global__ void kernel8(struct SS a) { |
| 133 |
*a.x += 3.f; |
| 134 |
} |