[Openmp-commits] [PATCH] D128896: [Libomptarget] Fix bucket selection in MemoryManager.h

Joachim Protze via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Thu Jun 30 01:57:43 PDT 2022


protze.joachim created this revision.
protze.joachim added reviewers: tianshilei1992, jdoerfert.
Herald added a project: All.
protze.joachim requested review of this revision.
Herald added a project: OpenMP.

Fixes an integer overflow in floorToPowerOfTwo for 0x8000 (32bit arch) / 0x80000000 (64bit arch) and higher.

Fixes the binary search in findBucket to return the intended bucket (e.g., 35 and 120 or 259 and 1020 went to the same bucket).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128896

Files:
  openmp/libomptarget/plugins/common/MemoryManager/MemoryManager.h


Index: openmp/libomptarget/plugins/common/MemoryManager/MemoryManager.h
===================================================================
--- openmp/libomptarget/plugins/common/MemoryManager/MemoryManager.h
+++ openmp/libomptarget/plugins/common/MemoryManager/MemoryManager.h
@@ -63,8 +63,8 @@
 #else
 #error Unsupported architecture
 #endif
-    Num += 1;
-    return Num >> 1;
+    Num = Num >> 1;
+    return Num + 1;
   }
 
   /// Find a suitable bucket
@@ -74,21 +74,23 @@
     DP("findBucket: Size %zu is floored to %zu.\n", Size, F);
 
     int L = 0, H = NumBuckets - 1;
-    while (H - L > 1) {
+    while (H >= L) {
       int M = (L + H) >> 1;
-      if (BucketSize[M] == F)
+      if (BucketSize[M] == F) {
+        DP("findBucket: Size %zu goes to bucket %d\n", Size, M);
         return M;
+      }
       if (BucketSize[M] > F)
         H = M - 1;
       else
-        L = M;
+        L = M + 1;
     }
 
-    assert(L >= 0 && L < NumBuckets && "L is out of range");
+    assert(H >= 0 && H < NumBuckets && "H is out of range");
 
-    DP("findBucket: Size %zu goes to bucket %d\n", Size, L);
+    DP("findBucket: Size %zu goes to bucket %d\n", Size, H);
 
-    return L;
+    return H;
   }
 
   /// A structure stores the meta data of a target pointer


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128896.441308.patch
Type: text/x-patch
Size: 1271 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20220630/85873062/attachment-0001.bin>


More information about the Openmp-commits mailing list