Skip to content

Commit a5605c9

Browse files
committed
[MLIR] Fix afterIP for dynamic worksharing-loop after collaping loops
The loopInfos gets invalidated after collapsing nested loops. Use the saved afterIP since the returned afterIP by applyDynamicWorkshareLoop may be not valid. Reviewed By: shraiysh Differential Revision: https://reviews.llvm.org/D120294
1 parent 5963091 commit a5605c9

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,8 @@ convertOmpWsLoop(Operation &opInst, llvm::IRBuilderBase &builder,
823823
break;
824824
}
825825
}
826-
afterIP = ompBuilder->applyDynamicWorkshareLoop(
827-
ompLoc.DL, loopInfo, allocaIP, schedType, !loop.nowait(), chunk);
826+
ompBuilder->applyDynamicWorkshareLoop(ompLoc.DL, loopInfo, allocaIP,
827+
schedType, !loop.nowait(), chunk);
828828
}
829829

830830
// Continue building IR after the loop. Note that the LoopInfo returned by

mlir/test/Target/LLVMIR/openmp-llvm.mlir

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,66 @@ llvm.func @collapse_wsloop(
751751

752752
// -----
753753

754+
// Check that the loop bounds are emitted in the correct location in case of
755+
// collapse for dynamic schedule. This only checks the overall shape of the IR,
756+
// detailed checking is done by the OpenMPIRBuilder.
757+
758+
// CHECK-LABEL: @collapse_wsloop_dynamic
759+
// CHECK: i32* noalias %[[TIDADDR:[0-9A-Za-z.]*]]
760+
// CHECK: load i32, i32* %[[TIDADDR]]
761+
// CHECK: store
762+
// CHECK: load
763+
// CHECK: %[[LB0:.*]] = load i32
764+
// CHECK: %[[UB0:.*]] = load i32
765+
// CHECK: %[[STEP0:.*]] = load i32
766+
// CHECK: %[[LB1:.*]] = load i32
767+
// CHECK: %[[UB1:.*]] = load i32
768+
// CHECK: %[[STEP1:.*]] = load i32
769+
// CHECK: %[[LB2:.*]] = load i32
770+
// CHECK: %[[UB2:.*]] = load i32
771+
// CHECK: %[[STEP2:.*]] = load i32
772+
773+
llvm.func @collapse_wsloop_dynamic(
774+
%0: i32, %1: i32, %2: i32,
775+
%3: i32, %4: i32, %5: i32,
776+
%6: i32, %7: i32, %8: i32,
777+
%20: !llvm.ptr<i32>) {
778+
omp.parallel {
779+
// CHECK: icmp slt i32 %[[LB0]], 0
780+
// CHECK-COUNT-4: select
781+
// CHECK: %[[TRIPCOUNT0:.*]] = select
782+
// CHECK: br label %[[PREHEADER:.*]]
783+
//
784+
// CHECK: [[PREHEADER]]:
785+
// CHECK: icmp slt i32 %[[LB1]], 0
786+
// CHECK-COUNT-4: select
787+
// CHECK: %[[TRIPCOUNT1:.*]] = select
788+
// CHECK: icmp slt i32 %[[LB2]], 0
789+
// CHECK-COUNT-4: select
790+
// CHECK: %[[TRIPCOUNT2:.*]] = select
791+
// CHECK: %[[PROD:.*]] = mul nuw i32 %[[TRIPCOUNT0]], %[[TRIPCOUNT1]]
792+
// CHECK: %[[TOTAL:.*]] = mul nuw i32 %[[PROD]], %[[TRIPCOUNT2]]
793+
// CHECK: br label %[[COLLAPSED_PREHEADER:.*]]
794+
//
795+
// CHECK: [[COLLAPSED_PREHEADER]]:
796+
// CHECK: store i32 1, i32*
797+
// CHECK: store i32 %[[TOTAL]], i32*
798+
// CHECK: call void @__kmpc_dispatch_init_4u
799+
omp.wsloop (%arg0, %arg1, %arg2) : i32 = (%0, %1, %2) to (%3, %4, %5) step (%6, %7, %8) collapse(3) schedule(dynamic) {
800+
%31 = llvm.load %20 : !llvm.ptr<i32>
801+
%32 = llvm.add %31, %arg0 : i32
802+
%33 = llvm.add %32, %arg1 : i32
803+
%34 = llvm.add %33, %arg2 : i32
804+
llvm.store %34, %20 : !llvm.ptr<i32>
805+
omp.yield
806+
}
807+
omp.terminator
808+
}
809+
llvm.return
810+
}
811+
812+
// -----
813+
754814
// CHECK-LABEL: @omp_ordered
755815
llvm.func @omp_ordered(%arg0 : i32, %arg1 : i32, %arg2 : i32, %arg3 : i64,
756816
%arg4: i64, %arg5: i64, %arg6: i64) -> () {

0 commit comments

Comments
 (0)