From 6780d5675b7cd75279d8fc13ee1a1cc272087613 Mon Sep 17 00:00:00 2001 From: Alexey Bataev Date: Tue, 4 Aug 2020 11:42:26 -0400 Subject: [PATCH] [LIBOMPTARGET]Fix order of mapper data for targetDataEnd function. targetDataMapper function fills arrays with the mapping data in the direct order. When this function is called by targetDataBegin or tgt_target_update functions, it works as expected. But targetDataEnd function processes mapped data in reverse order. In this case, the base pointer might be deleted before the associated data is deleted. Need to reverse data, mapped by mapper, too, since it always adds data that must be deleted at the end of the buffer. Fixes the test declare_mapper_target_update.cpp. Also, reduces the memry fragmentation by preallocation the memory buffers. Differential Revision: https://reviews.llvm.org/D85216 --- openmp/libomptarget/src/omptarget.cpp | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/openmp/libomptarget/src/omptarget.cpp b/openmp/libomptarget/src/omptarget.cpp index 5f8a3a6..4656ec0 100644 --- a/openmp/libomptarget/src/omptarget.cpp +++ b/openmp/libomptarget/src/omptarget.cpp @@ -230,22 +230,26 @@ int targetDataMapper(DeviceTy &Device, void *arg_base, void *arg, // Construct new arrays for args_base, args, arg_sizes and arg_types // using the information in MapperComponents and call the corresponding // target_data_* function using these new arrays. - std::vector mapper_args_base; - std::vector mapper_args; - std::vector mapper_arg_sizes; - std::vector mapper_arg_types; - - for (auto& C : MapperComponents.Components) { - mapper_args_base.push_back(C.Base); - mapper_args.push_back(C.Begin); - mapper_arg_sizes.push_back(C.Size); - mapper_arg_types.push_back(C.Type); + std::vector MapperArgsBase(MapperComponents.Components.size()); + std::vector MapperArgs(MapperComponents.Components.size()); + std::vector MapperArgSizes(MapperComponents.Components.size()); + std::vector MapperArgTypes(MapperComponents.Components.size()); + + for (unsigned I = 0, E = MapperComponents.Components.size(); I < E; ++I) { + auto &C = + MapperComponents + .Components[target_data_function == targetDataEnd ? I : E - I - 1]; + MapperArgsBase[I] = C.Base; + MapperArgs[I] = C.Begin; + MapperArgSizes[I] = C.Size; + MapperArgTypes[I] = C.Type; } int rc = target_data_function(Device, MapperComponents.Components.size(), - mapper_args_base.data(), mapper_args.data(), mapper_arg_sizes.data(), - mapper_arg_types.data(), /*arg_mappers*/ nullptr, - /*__tgt_async_info*/ nullptr); + MapperArgsBase.data(), MapperArgs.data(), + MapperArgSizes.data(), MapperArgTypes.data(), + /*arg_mappers*/ nullptr, + /*__tgt_async_info*/ nullptr); return rc; } -- 2.7.4