Skip to content

Commit 3fac959

Browse files
ftynsekiranchandramohan
authored andcommitted
[mlir] support collapsed loops in OpenMP-to-LLVM translation
Reviewed By: Meinersbur Differential Revision: https://reviews.llvm.org/D105706
1 parent 7549dcf commit 3fac959

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-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
@@ -389,8 +389,8 @@ convertOmpWsLoop(Operation &opInst, llvm::IRBuilderBase &builder,
389389
break;
390390
}
391391
}
392-
ompBuilder->createDynamicWorkshareLoop(
393-
ompLoc, loopInfo, allocaIP, schedType, !loop.nowait(), chunk);
392+
ompBuilder->createDynamicWorkshareLoop(ompLoc, loopInfo, allocaIP,
393+
schedType, !loop.nowait(), chunk);
394394
}
395395

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

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,65 @@ llvm.func @omp_critical(%x : !llvm.ptr<i32>, %xval : i32) -> () {
562562
omp.terminator
563563
}
564564
// CHECK: call void @__kmpc_end_critical({{.*}}critical_user_mutex.var{{.*}})
565+
llvm.return
566+
}
567+
568+
// -----
569+
570+
// Check that the loop bounds are emitted in the correct location in case of
571+
// collapse. This only checks the overall shape of the IR, detailed checking
572+
// is done by the OpenMPIRBuilder.
565573

574+
// CHECK-LABEL: @collapse_wsloop
575+
// CHECK: i32* noalias %[[TIDADDR:[0-9A-Za-z.]*]]
576+
// CHECK: load i32, i32* %[[TIDADDR]]
577+
// CHECK: store
578+
// CHECK: load
579+
// CHECK: %[[LB0:.*]] = load i32
580+
// CHECK: %[[UB0:.*]] = load i32
581+
// CHECK: %[[STEP0:.*]] = load i32
582+
// CHECK: %[[LB1:.*]] = load i32
583+
// CHECK: %[[UB1:.*]] = load i32
584+
// CHECK: %[[STEP1:.*]] = load i32
585+
// CHECK: %[[LB2:.*]] = load i32
586+
// CHECK: %[[UB2:.*]] = load i32
587+
// CHECK: %[[STEP2:.*]] = load i32
588+
llvm.func @collapse_wsloop(
589+
%0: i32, %1: i32, %2: i32,
590+
%3: i32, %4: i32, %5: i32,
591+
%6: i32, %7: i32, %8: i32,
592+
%20: !llvm.ptr<i32>) {
593+
omp.parallel {
594+
// CHECK: icmp slt i32 %[[LB0]], 0
595+
// CHECK-COUNT-4: select
596+
// CHECK: %[[TRIPCOUNT0:.*]] = select
597+
// CHECK: br label %[[PREHEADER:.*]]
598+
//
599+
// CHECK: [[PREHEADER]]:
600+
// CHECK: icmp slt i32 %[[LB1]], 0
601+
// CHECK-COUNT-4: select
602+
// CHECK: %[[TRIPCOUNT1:.*]] = select
603+
// CHECK: icmp slt i32 %[[LB2]], 0
604+
// CHECK-COUNT-4: select
605+
// CHECK: %[[TRIPCOUNT2:.*]] = select
606+
// CHECK: %[[PROD:.*]] = mul nuw i32 %[[TRIPCOUNT0]], %[[TRIPCOUNT1]]
607+
// CHECK: %[[TOTAL:.*]] = mul nuw i32 %[[PROD]], %[[TRIPCOUNT2]]
608+
// CHECK: br label %[[COLLAPSED_PREHEADER:.*]]
609+
//
610+
// CHECK: [[COLLAPSED_PREHEADER]]:
611+
// CHECK: store i32 0, i32*
612+
// CHECK: %[[TOTAL_SUB_1:.*]] = sub i32 %[[TOTAL]], 1
613+
// CHECK: store i32 %[[TOTAL_SUB_1]], i32*
614+
// CHECK: call void @__kmpc_for_static_init_4u
615+
omp.wsloop (%arg0, %arg1, %arg2) : i32 = (%0, %1, %2) to (%3, %4, %5) step (%6, %7, %8) collapse(3) {
616+
%31 = llvm.load %20 : !llvm.ptr<i32>
617+
%32 = llvm.add %31, %arg0 : i32
618+
%33 = llvm.add %32, %arg1 : i32
619+
%34 = llvm.add %33, %arg2 : i32
620+
llvm.store %34, %20 : !llvm.ptr<i32>
621+
omp.yield
622+
}
623+
omp.terminator
624+
}
566625
llvm.return
567626
}

0 commit comments

Comments
 (0)