diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp --- a/clang/lib/Sema/SemaOpenMP.cpp +++ b/clang/lib/Sema/SemaOpenMP.cpp @@ -16372,8 +16372,12 @@ ExprResult Allocator = DefaultLvalueConversion(A); if (Allocator.isInvalid()) return nullptr; - Allocator = PerformImplicitConversion(Allocator.get(), - DSAStack->getOMPAllocatorHandleT(), + QualType AllocatorHT = DSAStack->getOMPAllocatorHandleT(); + if (!AllocatorHT->isEnumeralType() && AllocatorHT->isIntegerType()) + // allocator_handler_t is enum type. For int type convert to + // UnsignedPointerDiffType avoid truncate. + AllocatorHT = Context.getUnsignedPointerDiffType(); + Allocator = PerformImplicitConversion(Allocator.get(), AllocatorHT, Sema::AA_Initializing, /*AllowExplicit=*/true); if (Allocator.isInvalid()) diff --git a/clang/test/OpenMP/omp_init_allocator.c b/clang/test/OpenMP/omp_init_allocator.c new file mode 100644 --- /dev/null +++ b/clang/test/OpenMP/omp_init_allocator.c @@ -0,0 +1,61 @@ +// RUN: %clang_cc1 -isystem %S/Inputs -emit-llvm -o - -fopenmp \ +// RUN: -fopenmp-version=50 -triple x86_64-unknown-linux-gnu %s | FileCheck %s + +typedef enum { + omp_atk_sync_hint = 1, + omp_atk_alignment = 2 +} omp_alloctrait_key_t; +typedef unsigned long int uintptr_t; +typedef uintptr_t omp_uintptr_t; +typedef struct { + omp_alloctrait_key_t key; + omp_uintptr_t value; +} omp_alloctrait_t; +typedef enum omp_allocator_handle_t { + omp_null_allocator = 0, + omp_default_mem_alloc = 1, + omp_large_cap_mem_alloc = 2, + omp_const_mem_alloc = 3, + omp_high_bw_mem_alloc = 4, + omp_low_lat_mem_alloc = 5, + omp_cgroup_mem_alloc = 6, + omp_pteam_mem_alloc = 7, + omp_thread_mem_alloc = 8, + + omp_target_host_mem_alloc = 100, + omp_target_shared_mem_alloc = 101, + omp_target_device_mem_alloc = 102, + KMP_ALLOCATOR_MAX_HANDLE = (18446744073709551615UL) +} omp_allocator_handle_t; +typedef enum omp_memspace_handle_t { + omp_default_mem_space = 0, + omp_large_cap_mem_space = 1, + omp_const_mem_space = 2, + omp_high_bw_mem_space = 3, + omp_low_lat_mem_space = 4, + + omp_target_host_mem_space = 100, + omp_target_shared_mem_space = 101, + omp_target_device_mem_space = 102, + KMP_MEMSPACE_MAX_HANDLE = (18446744073709551615UL) +} omp_memspace_handle_t; + +extern omp_allocator_handle_t omp_init_allocator(omp_memspace_handle_t m, + int ntraits, + omp_alloctrait_t traits[]); +//CHECK-LABEL: test_allocate_allocator +void test_allocate_allocator() { + omp_alloctrait_t x_traits[1] = {{omp_atk_alignment, 64}}; + omp_allocator_handle_t x_alloc = + omp_init_allocator(omp_default_mem_space, 1, x_traits); + { + int x; +// CHECK: [[RET:%.+]] = call i64 @omp_init_allocator +// CHECK-NEXT: store i64 [[RET]], ptr [[X_ALLOC:%x_alloc]] +// CHECK: [[TMP:%.+]] = load i64, ptr [[X_ALLOC]] +// CHECK-NEXT: [[CONV:%conv]] = inttoptr i64 [[TMP]] to ptr +// CHECK-NEXT: call ptr @__kmpc_alloc(i32 %0, i64 4, ptr [[CONV]]) +// CHECK-NOT: trunc +#pragma omp allocate(x) allocator(x_alloc) + } +} diff --git a/openmp/runtime/test/api/omp_init_allocator.c b/openmp/runtime/test/api/omp_init_allocator.c new file mode 100644 --- /dev/null +++ b/openmp/runtime/test/api/omp_init_allocator.c @@ -0,0 +1,20 @@ +// RUN: %libomp-compile-and-run + +#include +#include + +void test_allocate_allocator() { + omp_alloctrait_t x_traits[1] = {{omp_atk_alignment, 64}}; + omp_allocator_handle_t x_alloc = + omp_init_allocator(omp_default_mem_space, 1, x_traits); + { + int x; +#pragma omp allocate(x) allocator(x_alloc) + } + omp_destroy_allocator(x_alloc); +} + +int main() { + test_allocate_allocator(); + printf("passed\n"); +}