Skip to content

Commit

Permalink
[mlir][OpenMP] - Honor dependencies in code-generation of the if clau…
Browse files Browse the repository at this point in the history
…se in `omp.task` correctly (#90891)

This patch fixes the code generation of the if clause, specifically when the
condition evaluates to false and when the task directive has the depend
clause on it. When the if clause of a task construct evaluates to false,
then the task is an undeferred task. This undeferred task still has to
honor dependencies. Previously, the OpenMPIRbuilder didn't honor
dependencies. This patch fixes that.

Fixes #90869
  • Loading branch information
bhandarkar-pranav committed May 13, 2024
1 parent 1a25b72 commit 13cd881
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,9 @@ OpenMPIRBuilder::createTask(const LocationDescription &Loc,
// call @__kmpc_omp_task(...)
// br label %exit
// else:
// ;; Wait for resolution of dependencies, if any, before
// ;; beginning the task
// call @__kmpc_omp_wait_deps(...)
// call @__kmpc_omp_task_begin_if0(...)
// call @outlined_fn(...)
// call @__kmpc_omp_task_complete_if0(...)
Expand All @@ -1887,6 +1890,16 @@ OpenMPIRBuilder::createTask(const LocationDescription &Loc,
SplitBlockAndInsertIfThenElse(IfCondition, IfTerminator, &ThenTI,
&ElseTI);
Builder.SetInsertPoint(ElseTI);

if (Dependencies.size()) {
Function *TaskWaitFn =
getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_omp_wait_deps);
Builder.CreateCall(
TaskWaitFn,
{Ident, ThreadID, Builder.getInt32(Dependencies.size()), DepArray,
ConstantInt::get(Builder.getInt32Ty(), 0),
ConstantPointerNull::get(PointerType::getUnqual(M.getContext()))});
}
Function *TaskBeginFn =
getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_omp_task_begin_if0);
Function *TaskCompleteFn =
Expand Down
17 changes: 17 additions & 0 deletions mlir/test/Target/LLVMIR/omptask_if_false.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s

llvm.func @foo_(%arg0: !llvm.ptr {fir.bindc_name = "n"}, %arg1: !llvm.ptr {fir.bindc_name = "r"}) attributes {fir.internal_name = "_QPfoo"} {
%0 = llvm.mlir.constant(false) : i1
omp.task if(%0) depend(taskdependin -> %arg0 : !llvm.ptr) {
%1 = llvm.load %arg0 : !llvm.ptr -> i32
llvm.store %1, %arg1 : i32, !llvm.ptr
omp.terminator
}
llvm.return
}

// CHECK: call void @__kmpc_omp_wait_deps
// CHECK-NEXT: call void @__kmpc_omp_task_begin_if0
// CHECK-NEXT: call void @foo_..omp_par
// CHECK-NEXT: call void @__kmpc_omp_task_complete_if0

0 comments on commit 13cd881

Please sign in to comment.