llvm-project/clang/test/OpenMP/copy-gaps-6.cpp
Joseph Huber 4376fbd793
[OpenMP] Move OpenMP implicit argument to the end and reformat (#185989)
Summary:
We use this `dyn_ptr` argument in Clang/OpenMP to handle the
`KernelLaunchEnvironment`. This is a per-kernel argument used to share
some information. Currenetly, it's prepended to the argument list and we
generate storage for it in the runtime.

This is bad for a few reasons:
1. It changes the ABI by shifting user arguments
2. It cannot be trivially be left uninitialized if unused
3. The runtime must allocate its own memory for it

This PR changes it to be appended instead. Additionally, space for this
is always emitted. This means the OMPIRBuilder itself will provide the
storage, we simply need to populate it in the runtime if it is used.
This means that if it's unused we don't always pay the cost and it's
easier for non-OpenMP users to ignore it.

Backward compatibility is maintained by auto-upgrading the kernel
arguments. In `libomptarget` we completely allocate a new buffer to
store this in the new format. The plugins still need to respect the old
ABI of the called device object, so we simply rotate it if it's the old
version.
2026-03-12 18:08:22 -05:00

95 lines
3.9 KiB
C++

// RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp -emit-llvm %s -o - | FileCheck %s
// expected-no-diagnostics
struct S {
int x;
int *arr;
int y;
int z;
};
int main() {
S v;
// &v, &v, sizeof(v), ALLOC | PARAM
// &v, &v.x + sizeof(int), sizeof(v.arr to v.y), TO | FROM | MEMBER_OF_1
// &v, &v.x, sizeof(v.x), TO | FROM | MEMBER_OF_1
// &v, &v.z, sizeof(v.z), TO | FROM | MEMBER_OF_1
#pragma omp target map(tofrom: v, v.x, v.z)
{
v.x++;
v.y += 2;
v.z += 3;
}
// &v, &v, sizeof(v), ALLOC | PARAM
// &v, &v.x + sizeof(int), sizeof(v.arr to v.z), TO | FROM | MEMBER_OF_1
// &v, &v.x, sizeof(v.x), TO | FROM | MEMBER_OF_1
// &v.arr[0], &v.arr[0], 4 * sizeof(int), TO | FROM
// &v.arr, &v.arr[0], sizeof(void*), ATTACH
#pragma omp target map(tofrom: v, v.x, v.arr[:1])
{
v.x++;
v.y += 2;
v.arr[0] += 2;
v.z += 4;
}
// &v, &v, sizeof(v), TO | FROM | PARAM
// &v.arr[0], &v.arr[0], 4 * sizeof(int), TO | FROM
// &v.arr, &v.arr[0], sizeof(void*), ATTACH
#pragma omp target map(tofrom: v, v.arr[:1])
{
v.x++;
v.y += 2;
v.arr[0] += 2;
v.z += 4;
}
return 0;
}
// CHECK: [[CSTSZ0:@.+]] = private {{.*}}constant [5 x i64] [i64 0, i64 0, i64 4, i64 4, i64 0]
// CHECK: [[CSTTY0:@.+]] = private {{.*}}constant [5 x i64] [i64 [[#0x20]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 288]
// CHECK: [[CSTSZ1:@.+]] = private {{.*}}constant [6 x i64] [i64 0, i64 0, i64 4, i64 4, i64 8, i64 0]
// CHECK: [[CSTTY1:@.+]] = private {{.*}}constant [6 x i64] [i64 [[#0x20]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x3]], i64 [[#0x4000]], i64 288]
// CHECK: [[CSTSZ2:@.+]] = private {{.*}}constant [4 x i64] [i64 24, i64 4, i64 8, i64 0]
// CHECK: [[CSTTY2:@.+]] = private {{.*}}constant [4 x i64] [i64 [[#0x23]], i64 [[#0x3]], i64 [[#0x4000]], i64 288]
// CHECK-DAG: call i32 @__tgt_target_kernel(ptr @{{.+}}, i64 -1, i32 -1, i32 0, ptr @.{{.+}}.region_id, ptr [[ARGS:%.+]])
// CHECK-DAG: [[KSIZE:%.+]] = getelementptr inbounds {{.+}}[[ARGS]], i32 0, i32 4
// CHECK-DAG: store ptr [[SZBASE:%.+]], ptr [[KSIZE]], align 8
// CHECK-DAG: [[SZBASE]] = getelementptr inbounds [5 x i64], ptr [[SIZES:%[^,]*]], i32 0, i32 0
// Fill two non-constant size elements here: the whole struct size, and the
// region covering v.arr and v.y.
// CHECK-DAG: [[STR:%.+]] = getelementptr inbounds [5 x i64], ptr [[SIZES]], i32 0, i32 0
// CHECK-DAG: store i64 %{{.+}}, ptr [[STR]], align 8
// CHECK-DAG: [[ARRY:%.+]] = getelementptr inbounds [5 x i64], ptr [[SIZES]], i32 0, i32 1
// CHECK-DAG: store i64 %{{.+}}, ptr [[ARRY]], align 8
// CHECK: call void
// CHECK-DAG: call i32 @__tgt_target_kernel(ptr @{{.+}}, i64 -1, i32 -1, i32 0, ptr @.{{.+}}.region_id, ptr [[ARGS:%.+]])
// CHECK-DAG: [[KSIZE:%.+]] = getelementptr inbounds {{.+}}[[ARGS]], i32 0, i32 4
// CHECK-DAG: store ptr [[SZBASE:%.+]], ptr [[KSIZE]], align 8
// CHECK-DAG: [[SZBASE]] = getelementptr inbounds [6 x i64], ptr [[SIZES:%[^,]*]], i32 0, i32 0
// Fill two non-constant size elements here: the whole struct size, and the
// region covering v.arr, v.y and v.z.
// CHECK-DAG: [[STR:%.+]] = getelementptr inbounds [6 x i64], ptr [[SIZES]], i32 0, i32 0
// CHECK-DAG: store i64 %{{.+}}, ptr [[STR]], align 8
// CHECK-DAG: [[ARRYZ:%.+]] = getelementptr inbounds [6 x i64], ptr [[SIZES]], i32 0, i32 1
// CHECK-DAG: store i64 %{{.+}}, ptr [[ARRYZ]], align 8
// CHECK: call void
// CHECK-DAG: call i32 @__tgt_target_kernel(ptr @{{.+}}, i64 -1, i32 -1, i32 0, ptr @.{{.+}}.region_id, ptr [[ARGS:%.+]])
// CHECK-DAG: [[KSIZE:%.+]] = getelementptr inbounds {{.+}}[[ARGS]], i32 0, i32 4
// No overlap, so no non-constant size element here.
// CHECK-NOT: store ptr [[CSTSZ2]], ptr [[KSIZE]], align 8