[Openmp-commits] [PATCH] D150882: [OpenMP] remove usage of std::abs in the new loop collapse support code

Vadim Paretsky via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Thu May 18 09:40:30 PDT 2023


vadikp-intel created this revision.
vadikp-intel added reviewers: natgla, jkrishnavs.
vadikp-intel added a project: OpenMP.
Herald added subscribers: sunshaoce, guansong, yaxunl.
Herald added a project: All.
vadikp-intel requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: openmp-commits, jplehr, sstefan1.

on some platforms, std::abs may inadvertently pull in a math library. This patch replaces its use in the new loop collapse code with a no thrills in-situ implementation.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150882

Files:
  openmp/runtime/src/kmp_collapse.cpp


Index: openmp/runtime/src/kmp_collapse.cpp
===================================================================
--- openmp/runtime/src/kmp_collapse.cpp
+++ openmp/runtime/src/kmp_collapse.cpp
@@ -27,6 +27,13 @@
 // OMPTODO: different style of comments (see kmp_sched)
 // OMPTODO: OMPT/OMPD
 
+// avoid inadevertently using a library based abs
+template <typename T> T __abs(const T _X) {
+  return (_X < 0) ? -_X: _X;
+}
+kmp_uint32 __abs(const kmp_uint32 _X) { return _X; }
+kmp_uint64 __abs(const kmp_uint64 _X) { return _X; }
+
 //----------------------------------------------------------------------------
 // Common functions for working with rectangular and non-rectangular loops
 //----------------------------------------------------------------------------
@@ -131,7 +138,7 @@
       // kmp_loop_nest_iv_t anyway
       bounds->trip_count =
           static_cast<kmp_loop_nest_iv_t>(bounds->ub0 - bounds->lb0) /
-              std::abs(bounds->step) +
+              __abs(bounds->step) +
           1;
     }
   } else if (bounds->comparison == comparison_t::comp_greater_or_eq) {
@@ -144,7 +151,7 @@
       // kmp_loop_nest_iv_t anyway
       bounds->trip_count =
           static_cast<kmp_loop_nest_iv_t>(bounds->lb0 - bounds->ub0) /
-              std::abs(bounds->step) +
+              __abs(bounds->step) +
           1;
     }
   } else {
@@ -665,9 +672,9 @@
     } else {
       // get upper and lower bounds to be parallel
       // with values in the old range.
-      // Note: std::abs didn't work here.
-      if (((sign(old_lb1) == -1) && (old_lb1 < old_ub1)) ||
-          ((sign(old_lb1) == 1) && (old_lb1 > old_ub1))) {
+      // Note: abs didn't work here.
+      if (((old_lb1 < 0) && (old_lb1 < old_ub1)) ||
+          ((old_lb1 > 0) && (old_lb1 > old_ub1))) {
         bbounds.lb1 = old_ub1;
       } else {
         bbounds.ub1 = old_lb1;
@@ -804,13 +811,13 @@
     iterations =
         (static_cast<T>(original_ivs[ind]) - bounds->lb0 -
          bounds->lb1 * static_cast<T>(original_ivs[bounds->outer_iv])) /
-        std::abs(bounds->step);
+        __abs(bounds->step);
   } else {
     KMP_DEBUG_ASSERT(bounds->comparison == comparison_t::comp_greater_or_eq);
     iterations = (bounds->lb0 +
                   bounds->lb1 * static_cast<T>(original_ivs[bounds->outer_iv]) -
                   static_cast<T>(original_ivs[ind])) /
-                 std::abs(bounds->step);
+                 __abs(bounds->step);
   }
 
   return iterations;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150882.523411.patch
Type: text/x-patch
Size: 2482 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20230518/a28b238f/attachment.bin>


More information about the Openmp-commits mailing list