[Mlir-commits] [mlir] [mlir] Translating task_reduction clause for pass-by-value vars to LLVMIR (PR #125218)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Jan 1 23:16:34 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: None (NimishMishra)
<details>
<summary>Changes</summary>
This PR adds support for translating task_reduction clause for pass-by-value vars to LLVMIR. A `kmp_taskred_input_t` structure is populated by task reduction specific information: `reduce_shar (void*), reduce_orig (void*), reduce_size (size_t), reduce_init (void*), reduce_fini (void*), reduce_comb (void*), flags (i32)`. Here, `reduce_init` and `reduce_comb` are translated from the clause's `red_init` and `red_comb` regions. `reduce_fini` is optional for task reduction clause.
A todo is added for pass-by-ref variables in task reduction.
---
Patch is 24.64 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/125218.diff
4 Files Affected:
- (modified) mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td (+8)
- (modified) mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp (+254-4)
- (modified) mlir/test/Target/LLVMIR/openmp-llvm.mlir (+189)
- (modified) mlir/test/Target/LLVMIR/openmp-todo.mlir (+28-24)
``````````diff
diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td
index 05e2ee4e5632b..a1f987553e050 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td
@@ -1388,6 +1388,14 @@ class OpenMP_TaskReductionClauseSkip<
OptionalAttr<SymbolRefArrayAttr>:$task_reduction_syms
);
+ let extraClassDeclaration = [{
+ /// Returns the number of reduction variables.
+ unsigned getNumReductionVars() { return getTaskReductionVars().size(); }
+
+ /// Returns the reduction symbols
+ auto getReductionSyms() { return getTaskReductionSyms(); }
+ }];
+
let description = [{
The `task_reduction` clause specifies a reduction among tasks. For each list
item, the number of copies is unspecified. Any copies associated with the
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index 03d67a52853f6..c33d5bf3fa083 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -362,9 +362,13 @@ static LogicalResult checkImplementationStatus(Operation &op) {
result = todo("reduction with modifier");
};
auto checkTaskReduction = [&todo](auto op, LogicalResult &result) {
- if (!op.getTaskReductionVars().empty() || op.getTaskReductionByref() ||
- op.getTaskReductionSyms())
- result = todo("task_reduction");
+ if (op.getTaskReductionByref()) {
+ llvm::ArrayRef<bool> ByrefAttrs = op.getTaskReductionByref().value();
+ for (bool ByrefAttr : ByrefAttrs) {
+ if (ByrefAttr)
+ result = todo("task_reduction with pass by reference argument");
+ }
+ }
};
auto checkUntied = [&todo](auto op, LogicalResult &result) {
if (op.getUntied())
@@ -2469,6 +2473,228 @@ convertOmpTaskOp(omp::TaskOp taskOp, llvm::IRBuilderBase &builder,
return success();
}
+template <typename OP>
+static llvm::Value *createTaskReductionFunction(
+ llvm::IRBuilderBase &builder, const std::string &name, llvm::Type *redTy,
+ LLVM::ModuleTranslation &moduleTranslation,
+ SmallVectorImpl<omp::DeclareReductionOp> &reductionDecls, Region ®ion,
+ OP &op, unsigned cnt,
+ SmallVectorImpl<llvm::Value *> &privateReductionVariables,
+ DenseMap<Value, llvm::Value *> &reductionVariableMap) {
+
+ llvm::LLVMContext &Context = builder.getContext();
+ // TODO: by-ref reduction variables are yet to be handled.
+ llvm::Type *OpaquePtrTy = llvm::PointerType::get(Context, 0);
+ if (region.empty() && name == "red_fini")
+ // Finalization is optional for reductions.
+ return llvm::Constant::getNullValue(OpaquePtrTy);
+
+ // Prepare a general structure of the function to be emitted
+ llvm::FunctionType *funcType =
+ llvm::FunctionType::get(OpaquePtrTy, {OpaquePtrTy, OpaquePtrTy}, false);
+ llvm::Function *function =
+ llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, name,
+ builder.GetInsertBlock()->getModule());
+ function->setDoesNotRecurse();
+ llvm::BasicBlock *entry =
+ llvm::BasicBlock::Create(Context, "entry", function);
+ llvm::IRBuilder<> bbBuilder(entry);
+
+ // Prepare the function arguments
+ llvm::Value *arg0 = function->getArg(0);
+ llvm::Value *arg1 = function->getArg(1);
+
+ if (name == "red_init") {
+ // For the initialization, map the reduction variables
+ // to the arguments of the function
+ function->addParamAttr(0, llvm::Attribute::NoAlias);
+ function->addParamAttr(1, llvm::Attribute::NoAlias);
+ mlir::omp::DeclareReductionOp &reduction = reductionDecls[cnt];
+ Region &initializerRegion = reduction.getInitializerRegion();
+ Block &entry = initializerRegion.front();
+
+ mlir::Value mlirSource = op.getTaskReductionVars()[cnt];
+ llvm::Value *llvmSource = moduleTranslation.lookupValue(mlirSource);
+ llvm::Value *origVal = llvmSource;
+
+ moduleTranslation.mapValue(reduction.getInitializerMoldArg(), origVal);
+
+ if (entry.getNumArguments() > 1) {
+ llvm::Value *allocation =
+ reductionVariableMap.lookup(op.getReductionVars()[cnt]);
+ moduleTranslation.mapValue(reduction.getInitializerAllocArg(),
+ allocation);
+ }
+
+ } else if (name == "red_comb") {
+ // For the combiner, perform a load for each argument
+ // and map it to the combiner region.
+ llvm::Value *arg0L = bbBuilder.CreateLoad(redTy, arg0);
+ llvm::Value *arg1L = bbBuilder.CreateLoad(redTy, arg1);
+ moduleTranslation.mapValue(region.front().getArgument(0), arg0L);
+ moduleTranslation.mapValue(region.front().getArgument(1), arg1L);
+ }
+
+ // Emit an empty function body in case of empty region
+ if (region.empty())
+ bbBuilder.CreateRet(arg0); // Return from the function
+ return function;
+}
+
+SmallVector<llvm::Value *, 1> phis;
+if (failed(inlineConvertOmpRegions(region, "", bbBuilder, moduleTranslation,
+ &phis)))
+ return nullptr;
+assert(
+ phis.size() == 1 &&
+ "expected one value to be yielded from the reduction declaration region");
+bbBuilder.CreateStore(phis[0], arg0);
+bbBuilder.CreateRet(arg0); // Return from the function
+return function;
+}
+
+void emitTaskRedInitCall(
+ llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation,
+ const llvm::OpenMPIRBuilder::LocationDescription &ompLoc, int arraySize,
+ llvm::Value *ArrayAlloca) {
+
+ llvm::LLVMContext &Context = builder.getContext();
+ uint32_t SrcLocStrSize;
+ llvm::Constant *SrcLocStr =
+ moduleTranslation.getOpenMPBuilder()->getOrCreateSrcLocStr(ompLoc,
+ SrcLocStrSize);
+ llvm::Value *Ident = moduleTranslation.getOpenMPBuilder()->getOrCreateIdent(
+ SrcLocStr, SrcLocStrSize);
+ llvm::Value *ThreadID =
+ moduleTranslation.getOpenMPBuilder()->getOrCreateThreadID(Ident);
+ llvm::Constant *ConstInt =
+ llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), arraySize);
+
+ llvm::Function *TaskRedInitFn =
+ moduleTranslation.getOpenMPBuilder()->getOrCreateRuntimeFunctionPtr(
+ llvm::omp::OMPRTL___kmpc_taskred_init);
+ builder.CreateCall(TaskRedInitFn, {ThreadID, ConstInt, ArrayAlloca});
+}
+
+template <typename OP>
+static LogicalResult allocAndInitializeTaskReductionVars(
+ OP op, ArrayRef<BlockArgument> reductionArgs, llvm::IRBuilderBase &builder,
+ LLVM::ModuleTranslation &moduleTranslation,
+ llvm::OpenMPIRBuilder::InsertPointTy &allocaIP,
+ SmallVectorImpl<omp::DeclareReductionOp> &reductionDecls,
+ SmallVectorImpl<llvm::Value *> &privateReductionVariables,
+ DenseMap<Value, llvm::Value *> &reductionVariableMap) {
+
+ if (op.getNumReductionVars() == 0)
+ return success();
+
+ llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder);
+ llvm::LLVMContext &Context = builder.getContext();
+ SmallVector<DeferredStore> deferredStores;
+
+ // Save the current insertion point
+ auto oldIP = builder.saveIP();
+
+ // Set insertion point after the allocations
+ builder.SetInsertPoint(allocaIP.getBlock()->getTerminator());
+
+ // Define the kmp_taskred_input_t structure
+ llvm::StructType *kmp_taskred_input_t =
+ llvm::StructType::create(Context, "kmp_taskred_input_t");
+ llvm::Type *OpaquePtrTy = llvm::PointerType::get(Context, 0); // void*
+ llvm::Type *SizeTy = builder.getInt64Ty(); // size_t (assumed to be i64)
+ llvm::Type *FlagsTy = llvm::Type::getInt32Ty(Context); // flags (i32)
+
+ // Structure members
+ std::vector<llvm::Type *> structMembers = {
+ OpaquePtrTy, // reduce_shar (void*)
+ OpaquePtrTy, // reduce_orig (void*)
+ SizeTy, // reduce_size (size_t)
+ OpaquePtrTy, // reduce_init (void*)
+ OpaquePtrTy, // reduce_fini (void*)
+ OpaquePtrTy, // reduce_comb (void*)
+ FlagsTy // flags (i32)
+ };
+
+ kmp_taskred_input_t->setBody(structMembers);
+ int arraySize = op.getTaskReductionVars().size();
+ llvm::ArrayType *ArrayTy =
+ llvm::ArrayType::get(kmp_taskred_input_t, arraySize);
+
+ // Allocate the array for kmp_taskred_input_t
+ llvm::AllocaInst *ArrayAlloca =
+ builder.CreateAlloca(ArrayTy, nullptr, "kmp_taskred_array");
+
+ // Restore the insertion point
+ builder.restoreIP(oldIP);
+ llvm::DataLayout DL = builder.GetInsertBlock()->getModule()->getDataLayout();
+
+ for (int cnt = 0; cnt < arraySize; ++cnt) {
+ llvm::Value *shared =
+ moduleTranslation.lookupValue(op.getTaskReductionVars()[cnt]);
+
+ // Create a GEP to access the reduction element
+ llvm::Value *StructPtr = builder.CreateGEP(
+ ArrayTy, ArrayAlloca, {builder.getInt32(0), builder.getInt32(cnt)},
+ "red_element");
+
+ llvm::Value *FieldPtrReduceShar = builder.CreateStructGEP(
+ kmp_taskred_input_t, StructPtr, 0, "reduce_shar");
+ builder.CreateStore(shared, FieldPtrReduceShar);
+
+ llvm::Value *FieldPtrReduceOrig = builder.CreateStructGEP(
+ kmp_taskred_input_t, StructPtr, 1, "reduce_orig");
+ builder.CreateStore(shared, FieldPtrReduceOrig);
+
+ // Store size of the reduction variable
+ llvm::Value *FieldPtrReduceSize = builder.CreateStructGEP(
+ kmp_taskred_input_t, StructPtr, 2, "reduce_size");
+ llvm::Type *redTy =
+ moduleTranslation.convertType(reductionDecls[cnt].getType());
+ uint64_t sizeInBytes = DL.getTypeAllocSize(redTy);
+ llvm::ConstantInt *sizeConst =
+ llvm::ConstantInt::get(llvm::Type::getInt64Ty(Context), sizeInBytes);
+ builder.CreateStore(sizeConst, FieldPtrReduceSize);
+
+ // Initialize reduction variable
+ llvm::Value *FieldPtrReduceInit = builder.CreateStructGEP(
+ kmp_taskred_input_t, StructPtr, 3, "reduce_init");
+ llvm::Value *initFunction = createTaskReductionFunction(
+ builder, "red_init", redTy, moduleTranslation, reductionDecls,
+ reductionDecls[cnt].getInitializerRegion(), op, cnt,
+ privateReductionVariables, reductionVariableMap);
+ builder.CreateStore(initFunction, FieldPtrReduceInit);
+
+ // Create finish and combine functions
+ llvm::Value *FieldPtrReduceFini = builder.CreateStructGEP(
+ kmp_taskred_input_t, StructPtr, 4, "reduce_fini");
+ llvm::Value *finiFunction = createTaskReductionFunction(
+ builder, "red_fini", redTy, moduleTranslation, reductionDecls,
+ reductionDecls[cnt].getCleanupRegion(), op, cnt,
+ privateReductionVariables, reductionVariableMap);
+ builder.CreateStore(finiFunction, FieldPtrReduceFini);
+
+ llvm::Value *FieldPtrReduceComb = builder.CreateStructGEP(
+ kmp_taskred_input_t, StructPtr, 5, "reduce_comb");
+ llvm::Value *combFunction = createTaskReductionFunction(
+ builder, "red_comb", redTy, moduleTranslation, reductionDecls,
+ reductionDecls[cnt].getReductionRegion(), op, cnt,
+ privateReductionVariables, reductionVariableMap);
+ builder.CreateStore(combFunction, FieldPtrReduceComb);
+
+ llvm::Value *FieldPtrFlags =
+ builder.CreateStructGEP(kmp_taskred_input_t, StructPtr, 6, "flags");
+ llvm::ConstantInt *flagVal =
+ llvm::ConstantInt::get(llvm::Type::getInt64Ty(Context), 0);
+ builder.CreateStore(flagVal, FieldPtrFlags);
+ }
+
+ // Emit the runtime call
+ emitTaskRedInitCall(builder, moduleTranslation, ompLoc, arraySize,
+ ArrayAlloca);
+ return success();
+}
+
/// Converts an OpenMP taskgroup construct into LLVM IR using OpenMPIRBuilder.
static LogicalResult
convertOmpTaskgroupOp(omp::TaskgroupOp tgOp, llvm::IRBuilderBase &builder,
@@ -2477,8 +2703,32 @@ convertOmpTaskgroupOp(omp::TaskgroupOp tgOp, llvm::IRBuilderBase &builder,
if (failed(checkImplementationStatus(*tgOp)))
return failure();
+ SmallVector<omp::DeclareReductionOp> reductionDecls;
+ SmallVector<llvm::Value *> privateReductionVariables(
+ tgOp.getNumReductionVars());
+ DenseMap<Value, llvm::Value *> reductionVariableMap;
+ MutableArrayRef<BlockArgument> reductionArgs =
+ tgOp.getRegion().getArguments();
+ LogicalResult bodyGenStatus = success();
+
+ std::optional<ArrayAttr> attr = tgOp.getTaskReductionSyms();
+ if (attr) {
+ reductionDecls.reserve(reductionDecls.size() + tgOp.getNumReductionVars());
+ for (auto symbolRef : attr->getAsRange<SymbolRefAttr>()) {
+ reductionDecls.push_back(
+ SymbolTable::lookupNearestSymbolFrom<omp::DeclareReductionOp>(
+ tgOp, symbolRef));
+ }
+ assert(reductionDecls.size() == tgOp.getNumReductionVars() &&
+ "Missing reduction declaration");
+ }
auto bodyCB = [&](InsertPointTy allocaIP, InsertPointTy codegenIP) {
builder.restoreIP(codegenIP);
+
+ if (failed(allocAndInitializeTaskReductionVars(
+ tgOp, reductionArgs, builder, moduleTranslation, allocaIP,
+ reductionDecls, privateReductionVariables, reductionVariableMap)))
+ bodyGenStatus = failure();
return convertOmpOpRegions(tgOp.getRegion(), "omp.taskgroup.region",
builder, moduleTranslation)
.takeError();
@@ -2494,7 +2744,7 @@ convertOmpTaskgroupOp(omp::TaskgroupOp tgOp, llvm::IRBuilderBase &builder,
return failure();
builder.restoreIP(*afterIP);
- return success();
+ return bodyGenStatus;
}
static LogicalResult
diff --git a/mlir/test/Target/LLVMIR/openmp-llvm.mlir b/mlir/test/Target/LLVMIR/openmp-llvm.mlir
index 1eb501ca02703..a4165837d9df7 100644
--- a/mlir/test/Target/LLVMIR/openmp-llvm.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-llvm.mlir
@@ -3074,6 +3074,195 @@ llvm.func @omp_taskgroup_task(%x: i32, %y: i32, %zaddr: !llvm.ptr) {
// -----
+// CHECK-LABEL: define void @_QPtaskred_integer_arg() {
+// CHECK: %[[VAR:.*]] = alloca i32, i64 1, align 4
+// CHECK: %kmp_taskred_array = alloca [1 x %kmp_taskred_input_t], align 8
+// CHECK: %[[TID:.*]] = call i32 @__kmpc_global_thread_num(ptr @1)
+// CHECK: call void @__kmpc_taskgroup(ptr @1, i32 %[[TID]])
+// CHECK: %[[RED_ELEMENT:.*]] = getelementptr [1 x %kmp_taskred_input_t], ptr %kmp_taskred_array, i32 0, i32 0
+// CHECK: %[[REDUCE_SHAR:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 0
+// CHECK: store ptr %[[VAR]], ptr %[[REDUCE_SHAR]], align 8
+// CHECK: %[[REDUCE_ORIG:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 1
+// CHECK: store ptr %[[VAR]], ptr %[[REDUCE_ORIG]], align 8
+// CHECK: %[[REDUCE_SIZE:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 2
+// CHECK: store i64 4, ptr %[[REDUCE_SIZE]], align 4
+// CHECK: %[[REDUCE_INIT:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 3
+// CHECK: store ptr @red_init, ptr %[[REDUCE_INIT]], align 8
+// CHECK: %[[REDUCE_FINI:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 4
+// CHECK: store ptr null, ptr %reduce_fini, align 8
+// CHECK: %[[REDUCE_COMB:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 5
+// CHECK: store ptr @red_comb, ptr %[[REDUCE_COMB]], align 8
+// CHECK: %[[FLAGS:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 6
+// CHECK: store i64 0, ptr %[[FLAGS]], align 4
+// CHECK: %omp_global_thread_num1 = call i32 @__kmpc_global_thread_num(ptr @1)
+// CHECK: %[[INIT:.*]] = call ptr @__kmpc_taskred_init(i32 %omp_global_thread_num1, i32 1, ptr %kmp_taskred_array)
+
+// CHECK: define ptr @red_init(ptr noalias %0, ptr noalias %1) #2 {
+// CHECK: entry:
+// CHECK: store i32 0, ptr %0, align 4
+// CHECK: ret ptr %0
+// CHECK: }
+
+// CHECK: ; Function Attrs: norecurse
+// CHECK: define ptr @red_comb(ptr %0, ptr %1) #2 {
+// CHECK: entry:
+// CHECK: %2 = load i32, ptr %0, align 4
+// CHECK: %3 = load i32, ptr %1, align 4
+// CHECK: %4 = add i32 %2, %3
+// CHECK: store i32 %4, ptr %0, align 4
+// CHECK: ret ptr %0
+// CHECK: }
+
+omp.declare_reduction @add_reduction_i32 : i32 init {
+^bb0(%arg0: i32):
+ %0 = llvm.mlir.constant(0 : i32) : i32
+ omp.yield(%0 : i32)
+} combiner {
+^bb0(%arg0: i32, %arg1: i32):
+ %0 = llvm.add %arg0, %arg1 : i32
+ omp.yield(%0 : i32)
+}
+llvm.func @_QPtaskred_integer_arg() {
+ %0 = llvm.mlir.constant(1 : i64) : i64
+ %1 = llvm.alloca %0 x i32 {bindc_name = "s"} : (i64) -> !llvm.ptr
+ omp.taskgroup task_reduction(@add_reduction_i32 %1 -> %arg0 : !llvm.ptr) {
+ omp.terminator
+ }
+ llvm.return
+}
+
+// -----
+
+// CHECK-LABEL: define void @_QPfloat_arg() {
+// CHECK: %[[VAR:.*]] = alloca float, i64 1, align 4
+// CHECK: %kmp_taskred_array = alloca [1 x %kmp_taskred_input_t], align 8
+// CHECK: %[[TID:.*]] = call i32 @__kmpc_global_thread_num(ptr @1)
+// CHECK: call void @__kmpc_taskgroup(ptr @1, i32 %[[TID]])
+// CHECK: %[[RED_ELEMENT:.*]] = getelementptr [1 x %kmp_taskred_input_t], ptr %kmp_taskred_array, i32 0, i32 0
+// CHECK: %[[REDUCE_SHAR:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 0
+// CHECK: store ptr %[[VAR]], ptr %[[REDUCE_SHAR]], align 8
+// CHECK: %[[REDUCE_ORIG:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 1
+// CHECK: store ptr %[[VAR]], ptr %[[REDUCE_ORIG]], align 8
+// CHECK: %[[REDUCE_SIZE:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 2
+// CHECK: store i64 4, ptr %[[REDUCE_SIZE]], align 4
+// CHECK: %[[REDUCE_INIT:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 3
+// CHECK: store ptr @red_init, ptr %[[REDUCE_INIT]], align 8
+// CHECK: %[[REDUCE_FINI:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 4
+// CHECK: store ptr null, ptr %reduce_fini, align 8
+// CHECK: %[[REDUCE_COMB:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 5
+// CHECK: store ptr @red_comb, ptr %[[REDUCE_COMB]], align 8
+// CHECK: %[[FLAGS:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 6
+// CHECK: store i64 0, ptr %[[FLAGS]], align 4
+// CHECK: %omp_global_thread_num1 = call i32 @__kmpc_global_thread_num(ptr @1)
+// CHECK: %[[INIT:.*]] = call ptr @__kmpc_taskred_init(i32 %omp_global_thread_num1, i32 1, ptr %kmp_taskred_array)
+
+
+// CHECK: ; Function Attrs: norecurse
+// CHECK: define ptr @red_init(ptr noalias %0, ptr noalias %1) #2 {
+// CHECK: entry:
+// CHECK: store float 1.000000e+00, ptr %0, align 4
+// CHECK: ret ptr %0
+// CHECK: }
+
+// CHECK: ; Function Attrs: norecurse
+// CHECK: define ptr @red_comb(ptr %0, ptr %1) #2 {
+// CHECK: entry:
+// CHECK: %2 = load float, ptr %0, align 4
+// CHECK: %3 = load float, ptr %1, align 4
+// CHECK: %4 = fmul contract float %2, %3
+// CHECK: store float %4, ptr %0, align 4
+// CHECK: ret ptr %0
+// CHECK: }
+
+
+omp.declare_reduction @multiply_reduction_f32 : f32 init {
+^bb0(%arg0: f32):
+ %0 = llvm.mlir.constant(1.000000e+00 : f32) : f32
+ omp.yield(%0 : f32)
+} combiner {
+^bb0(%arg0: f32, %arg1: f32):
+ %0 = llvm.fmul %arg0, %arg1 {fastmathFlags = #llvm.fastmath<contract>} : f32
+ omp.yield(%0 : f32)
+}
+llvm.func @_QPfloat_arg() {
+ %0 = llvm.mlir.constant(1 : i64) : i64
+ %1 = llvm.alloca %0 x f32 {bindc_name = "s"} : (i64) -> !llvm.ptr
+ omp.taskgroup task_reduction(@multiply_reduction_f32 %1 -> %arg0 : !llvm.ptr) {
+ omp.terminator
+ }
+ llvm.return
+}
+
+// -----
+
+// CHECK: define void @_QPlogical_reduction() {
+// CHECK: %[[VAR:.*]] = alloca i32, i64 1, align 4
+// CHECK: %kmp_taskred_array = alloca [1 x %kmp_taskred_input_t], align 8
+// CHECK: %[[TID:.*]] = call i32 @__kmpc_global_thread_num(ptr @1)
+// CHECK: call void @__kmpc_taskgroup(ptr @1, i32 %[[TID]])
+// CHECK: %[[RED_ELEMENT:.*]] = getelementptr [1 x %kmp_taskred_input_t], ptr %kmp_taskred_array, i32 0, i32 0
+// CHECK: %[[REDUCE_SHAR:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 0
+// CHECK: store ptr %[[VAR]], ptr %[[REDUCE_SHAR]], align 8
+// CHECK: %[[REDUCE_ORIG:.*]] = getelementptr inbounds nuw %kmp_taskred_input_t, ptr %[[RED_ELEMENT]], i32 0, i32 1
+// CHECK: store ptr %[[VAR]], ptr %[[REDUCE_ORIG]], align 8
+// CHECK: %[[REDUCE_SIZE:.*]] = getelement...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/125218
More information about the Mlir-commits
mailing list