Skip to content

Commit 8c8074f

Browse files
authored
[Flang][OpenMP] Fix OpenMP static scheduling when trip count is zero (#170863)
Code-gen produced incorrect code for cases when the trip count an associated DO loop was zero. The generated code evaluated the trip count of the loop and substracted 1 from it. When this was passed to __kmpc_for_static_init_4u, the value was interpreted as unsigned, which made the upper bound of the worksharing loop 2^32-1 and caused a division by zero in the calculation of the loop bounds for the threads.
1 parent fdd0d53 commit 8c8074f

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

clang/test/OpenMP/irbuilder_for_unsigned_static_chunked.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ extern "C" void workshareloop_unsigned_static_chunked(float *a, float *b, float
5050
// CHECK: omp_loop.preheader:
5151
// CHECK-NEXT: store i32 0, ptr [[P_LOWERBOUND]], align 4
5252
// CHECK-NEXT: [[TMP3:%.*]] = sub i32 [[DOTCOUNT]], 1
53-
// CHECK-NEXT: store i32 [[TMP3]], ptr [[P_UPPERBOUND]], align 4
53+
// CHECK-NEXT: [[COUNTCOND:%.*]] = icmp eq i32 [[DOTCOUNT]], 0
54+
// CHECK-NEXT: [[UPPERSEL:%.*]] = select i1 [[COUNTCOND]], i32 0, i32 [[TMP3]]
55+
// CHECK-NEXT: store i32 [[UPPERSEL]], ptr [[P_UPPERBOUND]], align 4
5456
// CHECK-NEXT: store i32 1, ptr [[P_STRIDE]], align 4
5557
// CHECK-NEXT: [[OMP_GLOBAL_THREAD_NUM:%.*]] = call i32 @__kmpc_global_thread_num(ptr @[[GLOB1:[0-9]+]])
5658
// CHECK-NEXT: call void @__kmpc_for_static_init_4u(ptr @[[GLOB1]], i32 [[OMP_GLOBAL_THREAD_NUM]], i32 33, ptr [[P_LASTITER]], ptr [[P_LOWERBOUND]], ptr [[P_UPPERBOUND]], ptr [[P_STRIDE]], i32 1, i32 5)

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5140,7 +5140,10 @@ OpenMPIRBuilder::applyStaticChunkedWorkshareLoop(
51405140
ConstantInt::get(I32Type, static_cast<int>(DistScheduleSchedType));
51415141
Builder.CreateStore(Zero, PLowerBound);
51425142
Value *OrigUpperBound = Builder.CreateSub(CastedTripCount, One);
5143-
Builder.CreateStore(OrigUpperBound, PUpperBound);
5143+
Value *IsTripCountZero = Builder.CreateICmpEQ(CastedTripCount, Zero);
5144+
Value *UpperBound =
5145+
Builder.CreateSelect(IsTripCountZero, Zero, OrigUpperBound);
5146+
Builder.CreateStore(UpperBound, PUpperBound);
51445147
Builder.CreateStore(One, PStride);
51455148

51465149
// Call the "init" function and update the trip count of the loop with the

0 commit comments

Comments
 (0)