Skip to content

Commit eef63d3

Browse files
authored
[mlir][OpenMP] add missing load for reduction cleanup region (#88289)
I missed this before. For by-ref reductions, the private reduction variable is a pointer to the pointer to the variable. So an extra load is required to get the right value. See the "red.private.value.n" loads in the reduction combiner region for reference.
1 parent e7bc537 commit eef63d3

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,17 @@ static LogicalResult inlineReductionCleanup(
889889

890890
// map the argument to the cleanup region
891891
Block &entry = cleanupRegion.front();
892-
moduleTranslation.mapValue(entry.getArgument(0),
893-
privateReductionVariables[i]);
892+
893+
llvm::Instruction *potentialTerminator =
894+
builder.GetInsertBlock()->empty() ? nullptr
895+
: &builder.GetInsertBlock()->back();
896+
if (potentialTerminator && potentialTerminator->isTerminator())
897+
builder.SetInsertPoint(potentialTerminator);
898+
llvm::Value *reductionVar = builder.CreateLoad(
899+
moduleTranslation.convertType(entry.getArgument(0).getType()),
900+
privateReductionVariables[i]);
901+
902+
moduleTranslation.mapValue(entry.getArgument(0), reductionVar);
894903

895904
if (failed(inlineConvertOmpRegions(cleanupRegion, "omp.reduction.cleanup",
896905
builder, moduleTranslation)))

mlir/test/Target/LLVMIR/openmp-parallel-reduction-cleanup.mlir

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@
8686

8787
// Cleanup region:
8888
// CHECK: [[OMP_FINALIZE]]:
89-
// CHECK: call void @free(ptr %[[PRIV_PTR_I]])
90-
// CHECK: call void @free(ptr %[[PRIV_PTR_J]])
89+
// CHECK: %[[PRIV_I:.+]] = load ptr, ptr %[[PRIV_PTR_I]], align 8
90+
// CHECK: call void @free(ptr %[[PRIV_I]])
91+
// CHECK: %[[PRIV_J:.+]] = load ptr, ptr %[[PRIV_PTR_J]], align 8
92+
// CHECK: call void @free(ptr %[[PRIV_J]])
9193

9294
// Reduction function.
9395
// CHECK: define internal void @[[REDFUNC]]

mlir/test/Target/LLVMIR/openmp-wsloop-reduction-cleanup.mlir

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@
6363
// Weirdly the finalization block is generated before the reduction blocks:
6464
// CHECK: [[FINALIZE:.+]]:
6565
// CHECK: call void @__kmpc_barrier
66-
// CHECK: call void @free(ptr %[[PRIV_PTR_I]])
67-
// CHECK: call void @free(ptr %[[PRIV_PTR_J]])
66+
// CHECK: %[[PRIV_I:.+]] = load ptr, ptr %[[PRIV_PTR_I]], align 8
67+
// CHECK: call void @free(ptr %[[PRIV_I]])
68+
// CHECK: %[[PRIV_J:.+]] = load ptr, ptr %[[PRIV_PTR_J]], align 8
69+
// CHECK: call void @free(ptr %[[PRIV_J]])
6870
// CHECK: ret void
6971

7072
// Non-atomic reduction:

0 commit comments

Comments
 (0)