[Openmp-commits] [openmp] f5334f5 - [OpenMP] Add debug checks for divide by zero (#83300)
via Openmp-commits
openmp-commits at lists.llvm.org
Tue Mar 12 11:36:24 PDT 2024
Author: Jonathan Peyton
Date: 2024-03-12T11:36:19-07:00
New Revision: f5334f5da5166ac26fb24ca44669b3d425f0d131
URL: https://github.com/llvm/llvm-project/commit/f5334f5da5166ac26fb24ca44669b3d425f0d131
DIFF: https://github.com/llvm/llvm-project/commit/f5334f5da5166ac26fb24ca44669b3d425f0d131.diff
LOG: [OpenMP] Add debug checks for divide by zero (#83300)
Added:
Modified:
openmp/runtime/src/kmp_affinity.cpp
openmp/runtime/src/kmp_sched.cpp
Removed:
################################################################################
diff --git a/openmp/runtime/src/kmp_affinity.cpp b/openmp/runtime/src/kmp_affinity.cpp
index b79b57eafd6a81..c3ee4de75a237a 100644
--- a/openmp/runtime/src/kmp_affinity.cpp
+++ b/openmp/runtime/src/kmp_affinity.cpp
@@ -1777,6 +1777,8 @@ static bool __kmp_affinity_create_hwloc_map(kmp_i18n_id_t *const msg_id) {
__kmp_nThreadsPerCore = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_PU);
else
__kmp_nThreadsPerCore = 1; // no CORE found
+ if (__kmp_nThreadsPerCore == 0)
+ __kmp_nThreadsPerCore = 1;
__kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
if (nCoresPerPkg == 0)
nCoresPerPkg = 1; // to prevent possible division by 0
diff --git a/openmp/runtime/src/kmp_sched.cpp b/openmp/runtime/src/kmp_sched.cpp
index 53182bef58732e..4d764e441f2835 100644
--- a/openmp/runtime/src/kmp_sched.cpp
+++ b/openmp/runtime/src/kmp_sched.cpp
@@ -52,6 +52,7 @@ char const *traits_t<long>::spec = "ld";
} else if (i > 0) { \
t = (u - l) / i + 1; \
} else { \
+ KMP_DEBUG_ASSERT(i != 0); \
t = (l - u) / (-i) + 1; \
} \
KMP_COUNT_VALUE(stat, t); \
@@ -284,6 +285,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
// upper-lower can exceed the limit of signed type
trip_count = (UT)(*pupper - *plower) / incr + 1;
} else {
+ KMP_DEBUG_ASSERT(incr != 0);
trip_count = (UT)(*plower - *pupper) / (-incr) + 1;
}
@@ -318,6 +320,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
if (plastiter != NULL)
*plastiter = (tid == trip_count - 1);
} else {
+ KMP_DEBUG_ASSERT(nth != 0);
if (__kmp_static == kmp_sch_static_balanced) {
UT small_chunk = trip_count / nth;
UT extras = trip_count % nth;
@@ -358,6 +361,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
case kmp_sch_static_chunked: {
ST span;
UT nchunks;
+ KMP_DEBUG_ASSERT(chunk != 0);
if (chunk < 1)
chunk = 1;
else if ((UT)chunk > trip_count)
@@ -383,6 +387,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
}
case kmp_sch_static_balanced_chunked: {
T old_upper = *pupper;
+ KMP_DEBUG_ASSERT(nth != 0);
// round up to make sure the chunk is enough to cover all iterations
UT span = (trip_count + nth - 1) / nth;
@@ -398,8 +403,10 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
} else if (*pupper < old_upper)
*pupper = old_upper;
- if (plastiter != NULL)
+ if (plastiter != NULL) {
+ KMP_DEBUG_ASSERT(chunk != 0);
*plastiter = (tid == ((trip_count - 1) / (UT)chunk));
+ }
break;
}
default:
@@ -417,6 +424,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
// Calculate chunk in case it was not specified; it is specified for
// kmp_sch_static_chunked
if (schedtype == kmp_sch_static) {
+ KMP_DEBUG_ASSERT(nth != 0);
cur_chunk = trip_count / nth + ((trip_count % nth) ? 1 : 0);
}
// 0 - "static" schedule
@@ -547,6 +555,7 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
// upper-lower can exceed the limit of signed type
trip_count = (UT)(*pupper - *plower) / incr + 1;
} else {
+ KMP_DEBUG_ASSERT(incr != 0);
trip_count = (UT)(*plower - *pupper) / (-incr) + 1;
}
@@ -568,6 +577,7 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
*plastiter = (tid == 0 && team_id == trip_count - 1);
} else {
// Get the team's chunk first (each team gets at most one chunk)
+ KMP_DEBUG_ASSERT(nteams != 0);
if (__kmp_static == kmp_sch_static_balanced) {
UT chunkD = trip_count / nteams;
UT extras = trip_count % nteams;
@@ -619,6 +629,7 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
// upper-lower can exceed the limit of signed type
trip_count = (UT)(*pupperDist - *plower) / incr + 1;
} else {
+ KMP_DEBUG_ASSERT(incr != 0);
trip_count = (UT)(*plower - *pupperDist) / (-incr) + 1;
}
KMP_DEBUG_ASSERT(trip_count);
@@ -637,6 +648,7 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
if (*plastiter != 0 && !(tid == trip_count - 1))
*plastiter = 0;
} else {
+ KMP_DEBUG_ASSERT(nth != 0);
if (__kmp_static == kmp_sch_static_balanced) {
UT chunkL = trip_count / nth;
UT extras = trip_count % nth;
@@ -684,9 +696,11 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
*pstride = span * nth;
*plower = *plower + (span * tid);
*pupper = *plower + span - incr;
- if (plastiter != NULL)
+ if (plastiter != NULL) {
+ KMP_DEBUG_ASSERT(chunk != 0);
if (*plastiter != 0 && !(tid == ((trip_count - 1) / (UT)chunk) % nth))
*plastiter = 0;
+ }
break;
}
default:
@@ -809,6 +823,7 @@ static void __kmp_team_static_init(ident_t *loc, kmp_int32 gtid,
// upper-lower can exceed the limit of signed type
trip_count = (UT)(upper - lower) / incr + 1;
} else {
+ KMP_DEBUG_ASSERT(incr != 0);
trip_count = (UT)(lower - upper) / (-incr) + 1;
}
if (chunk < 1)
@@ -817,8 +832,10 @@ static void __kmp_team_static_init(ident_t *loc, kmp_int32 gtid,
*p_st = span * nteams;
*p_lb = lower + (span * team_id);
*p_ub = *p_lb + span - incr;
- if (p_last != NULL)
+ if (p_last != NULL) {
+ KMP_DEBUG_ASSERT(chunk != 0);
*p_last = (team_id == ((trip_count - 1) / (UT)chunk) % nteams);
+ }
// Correct upper bound if needed
if (incr > 0) {
if (*p_ub < *p_lb) // overflow?
More information about the Openmp-commits
mailing list