[Mlir-commits] [mlir] [flang][mlir] Add support for translating task_reduction to LLVMIR (PR #120957)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Jan 12 17:50:26 PST 2025


================
@@ -0,0 +1,93 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+
+omp.declare_reduction @add_reduction_byref_i32 : !llvm.ptr alloc {
+   %0 = llvm.mlir.constant(1 : i64) : i64
+   %1 = llvm.alloca %0 x i32 : (i64) -> !llvm.ptr
+   %2 = llvm.mlir.constant(1 : i64) : i64
+   omp.yield(%1 : !llvm.ptr)
+} init {
+ ^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr):
+   %0 = llvm.mlir.constant(0 : i32) : i32
+   llvm.store %0, %arg1 : i32, !llvm.ptr
+   omp.yield(%arg1 : !llvm.ptr)
+} combiner {
+ ^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr):
+   %0 = llvm.load %arg0 : !llvm.ptr -> i32
+   %1 = llvm.load %arg1 : !llvm.ptr -> i32
+   %2 = llvm.add %0, %1 : i32
+   llvm.store %2, %arg0 : i32, !llvm.ptr
+   omp.yield(%arg0 : !llvm.ptr)
+}
+llvm.func @_QPtest_task_reduction() {
+   %0 = llvm.mlir.constant(1 : i64) : i64
+   %1 = llvm.alloca %0 x i32 {bindc_name = "x"} : (i64) -> !llvm.ptr
+   %2 = llvm.mlir.constant(1 : i64) : i64
+   omp.taskgroup task_reduction(byref @add_reduction_byref_i32 %1 -> %arg0 : !llvm.ptr) {
+     omp.terminator
+   }
+  llvm.return
+} 
+
+//CHECK-LABEL: define void @_QPtest_task_reduction() {
+//CHECK:   %[[VAL1:.*]] = alloca i32, i64 1, align 4
+//CHECK:   %[[RED_ARRY:.*]] = alloca [1 x %kmp_taskred_input_t], align 8
+//CHECK:   br label %entry
+
+//CHECK: entry:
+//CHECK:   %[[TID:.*]] = call i32 @__kmpc_global_thread_num(ptr @{{.*}})
+//CHECK:   call void @__kmpc_taskgroup(ptr @1, i32 %[[TID]])
+//CHECK:   %[[RED_ELEMENT:.*]] = getelementptr [1 x %kmp_taskred_input_t], ptr %[[RED_ARRY]], i32 0, i32 0
+//CHECK:   %[[RED_SHARED:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 0
+//CHECK:   store ptr %[[VAL1]], ptr %[[RED_SHARED]], align 8
----------------
NimishMishra wrote:

I think you are right about pointer lifetimes, and we will have to deal with it (which this PR does not, at the point). Is the PR submitted as a result of this RFC a good point to begin looking into it?

I think we could do the following:

(1) go ahead with the by-value reduction for the time-being
(2) I go over your solution and try to incorporate that for by-reference. That could be another PR.

Does it work?

https://github.com/llvm/llvm-project/pull/120957


More information about the Mlir-commits mailing list