From c27f530d4c6306b2010306131f66e771d6a66594 Mon Sep 17 00:00:00 2001 From: Shilei Tian Date: Sat, 12 Feb 2022 22:02:47 -0500 Subject: [PATCH] [OpenMP][Offloading] Fix infinite loop in applyToShadowMapEntries This patch fixes the issue that the for loop in `applyToShadowMapEntries` is infinite because `Itr` is not incremented in `CB`. Fixes #53727. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D119471 --- openmp/libomptarget/src/omptarget.cpp | 2 + openmp/libomptarget/test/offloading/bug53727.cpp | 57 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 openmp/libomptarget/test/offloading/bug53727.cpp diff --git a/openmp/libomptarget/src/omptarget.cpp b/openmp/libomptarget/src/omptarget.cpp index c8504c8..304091e 100644 --- a/openmp/libomptarget/src/omptarget.cpp +++ b/openmp/libomptarget/src/omptarget.cpp @@ -889,6 +889,7 @@ static int targetDataContiguous(ident_t *loc, DeviceTy &Device, void *ArgsBase, DP("Restoring original host pointer value " DPxMOD " for host pointer " DPxMOD "\n", DPxPTR(Itr->second.HstPtrVal), DPxPTR(ShadowHstPtrAddr)); + ++Itr; return OFFLOAD_SUCCESS; }; applyToShadowMapEntries(Device, CB, HstPtrBegin, ArgSize, TPR); @@ -911,6 +912,7 @@ static int targetDataContiguous(ident_t *loc, DeviceTy &Device, void *ArgsBase, sizeof(void *), AsyncInfo); if (Ret != OFFLOAD_SUCCESS) REPORT("Copying data to device failed.\n"); + ++Itr; return Ret; }; applyToShadowMapEntries(Device, CB, HstPtrBegin, ArgSize, TPR); diff --git a/openmp/libomptarget/test/offloading/bug53727.cpp b/openmp/libomptarget/test/offloading/bug53727.cpp new file mode 100644 index 0000000..4744024 --- /dev/null +++ b/openmp/libomptarget/test/offloading/bug53727.cpp @@ -0,0 +1,57 @@ +// RUN: %libomptarget-compilexx-and-run-generic + +#include +#include + +constexpr const int N = 10; + +struct T { + int a; + int *p; +}; + +struct S { + int b; + T t; +}; + +int main(int argc, char *argv[]) { + S s; + s.t.p = new int[N]; + for (int i = 0; i < N; ++i) { + s.t.p[i] = i; + } + +#pragma omp target enter data map(to : s, s.t.p[:N]) + +#pragma omp target + { + for (int i = 0; i < N; ++i) { + s.t.p[i] += i; + } + } + +#pragma omp target update from(s.t.p[:N]) + + for (int i = 0; i < N; ++i) { + assert(s.t.p[i] == 2 * i); + s.t.p[i] += i; + } + +#pragma omp target update to(s.t.p[:N]) + +#pragma omp target + { + for (int i = 0; i < N; ++i) { + s.t.p[i] += i; + } + } + +#pragma omp target exit data map(from : s, s.t.p[:N]) + + for (int i = 0; i < N; ++i) { + assert(s.t.p[i] == 4 * i); + } + + return 0; +} -- 2.7.4