Skip to content

Commit a32c456

Browse files
authored
[flang][OpenMP] Generalize fixing alloca IP pre-condition for private ops (#122866)
This PR generalizes a fix that we implemented previously for `omp.wsloop`s. The fix makes sure the pre-condtion that the `alloca` block has a single successor whenever we inline delayed privatizers is respected. I simply moved the fix to `allocatePrivateVars` so that it kicks in for any op not just `omp.wsloop`. This handles a bug uncovered by [a test](https://github.com/OpenMP-Validation-and-Verification/OpenMP_VV/blob/master/tests/4.5/target_simd/test_target_simd_safelen.F90) in the OpenMP_VV test suite.
1 parent d1314d0 commit a32c456

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,13 +1344,23 @@ allocatePrivateVars(llvm::IRBuilderBase &builder,
13441344
llvm::SmallVectorImpl<llvm::Value *> &llvmPrivateVars,
13451345
const llvm::OpenMPIRBuilder::InsertPointTy &allocaIP,
13461346
llvm::DenseMap<Value, Value> *mappedPrivateVars = nullptr) {
1347-
llvm::IRBuilderBase::InsertPointGuard guard(builder);
13481347
// Allocate private vars
13491348
llvm::BranchInst *allocaTerminator =
13501349
llvm::cast<llvm::BranchInst>(allocaIP.getBlock()->getTerminator());
1350+
if (allocaTerminator->getNumSuccessors() != 1) {
1351+
splitBB(llvm::OpenMPIRBuilder::InsertPointTy(
1352+
allocaIP.getBlock(), allocaTerminator->getIterator()),
1353+
true, "omp.region.after_alloca");
1354+
}
1355+
1356+
llvm::IRBuilderBase::InsertPointGuard guard(builder);
1357+
// Update the allocaTerminator in case the alloca block was split above.
1358+
allocaTerminator =
1359+
llvm::cast<llvm::BranchInst>(allocaIP.getBlock()->getTerminator());
13511360
builder.SetInsertPoint(allocaTerminator);
13521361
assert(allocaTerminator->getNumSuccessors() == 1 &&
13531362
"This is an unconditional branch created by OpenMPIRBuilder");
1363+
13541364
llvm::BasicBlock *afterAllocas = allocaTerminator->getSuccessor(0);
13551365

13561366
// FIXME: Some of the allocation regions do more than just allocating.
@@ -1880,11 +1890,6 @@ convertOmpWsloop(Operation &opInst, llvm::IRBuilderBase &builder,
18801890
SmallVector<llvm::Value *> privateReductionVariables(
18811891
wsloopOp.getNumReductionVars());
18821892

1883-
splitBB(llvm::OpenMPIRBuilder::InsertPointTy(
1884-
allocaIP.getBlock(),
1885-
allocaIP.getBlock()->getTerminator()->getIterator()),
1886-
true, "omp.region.after_alloca");
1887-
18881893
llvm::Expected<llvm::BasicBlock *> afterAllocas = allocatePrivateVars(
18891894
builder, moduleTranslation, privateBlockArgs, privateDecls,
18901895
mlirPrivateVars, llvmPrivateVars, allocaIP);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
module attributes {omp.is_target_device = true} {
4+
omp.private {type = private} @simd_privatizer : !llvm.ptr alloc {
5+
^bb0(%arg0: !llvm.ptr):
6+
omp.yield(%arg0 : !llvm.ptr)
7+
}
8+
9+
llvm.func @test_target_simd() {
10+
omp.target {
11+
%5 = llvm.mlir.constant(1 : i32) : i32
12+
%x = llvm.alloca %5 x i32 {bindc_name = "x"} : (i32) -> !llvm.ptr
13+
omp.simd private(@simd_privatizer %x -> %arg1 : !llvm.ptr) {
14+
omp.loop_nest (%arg2) : i32 = (%5) to (%5) inclusive step (%5) {
15+
omp.yield
16+
}
17+
}
18+
omp.terminator
19+
}
20+
llvm.return
21+
}
22+
23+
}
24+
25+
// CHECK-LABEL: define {{.*}} @__omp_offloading_{{.*}}_test_target_simd_{{.*}}
26+
27+
// CHECK: %[[INT:.*]] = alloca i32, align 4
28+
// CHECK: br label %[[LATE_ALLOC_BB:.*]]
29+
30+
// CHECK: [[LATE_ALLOC_BB]]:
31+
// CHECK: br label %[[AFTER_ALLOC_BB:.*]]
32+
33+
// CHECK: [[AFTER_ALLOC_BB]]:
34+
// CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}

0 commit comments

Comments
 (0)