Skip to content

Commit c0abae3

Browse files
[MLIR][OPENMP] Relax requirement about branches as terminator of private alloc (llvm#128481)
Fixes llvm#126966
1 parent 8ffda96 commit c0abae3

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: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,20 +1433,19 @@ allocatePrivateVars(llvm::IRBuilderBase &builder,
14331433
const llvm::OpenMPIRBuilder::InsertPointTy &allocaIP,
14341434
llvm::DenseMap<Value, Value> *mappedPrivateVars = nullptr) {
14351435
// Allocate private vars
1436-
llvm::BranchInst *allocaTerminator =
1437-
llvm::cast<llvm::BranchInst>(allocaIP.getBlock()->getTerminator());
1436+
llvm::Instruction *allocaTerminator = allocaIP.getBlock()->getTerminator();
14381437
splitBB(llvm::OpenMPIRBuilder::InsertPointTy(allocaIP.getBlock(),
14391438
allocaTerminator->getIterator()),
14401439
true, allocaTerminator->getStableDebugLoc(),
14411440
"omp.region.after_alloca");
14421441

14431442
llvm::IRBuilderBase::InsertPointGuard guard(builder);
1444-
// Update the allocaTerminator in case the alloca block was split above.
1445-
allocaTerminator =
1446-
llvm::cast<llvm::BranchInst>(allocaIP.getBlock()->getTerminator());
1443+
// Update the allocaTerminator since the alloca block was split above.
1444+
allocaTerminator = allocaIP.getBlock()->getTerminator();
14471445
builder.SetInsertPoint(allocaTerminator);
1446+
// The new terminator is an uncondition branch created by the splitBB above.
14481447
assert(allocaTerminator->getNumSuccessors() == 1 &&
1449-
"This is an unconditional branch created by OpenMPIRBuilder");
1448+
"This is an unconditional branch created by splitBB");
14501449

14511450
llvm::BasicBlock *afterAllocas = allocaTerminator->getSuccessor(0);
14521451

mlir/test/Target/LLVMIR/openmp-private.mlir

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,43 @@ omp.private {type = firstprivate} @_QFequivalenceEx_firstprivate_ptr_f32 : f32 c
265265
// CHECK: store float %[[HOST_VAL]], ptr %[[PRIV_ALLOC]], align 4
266266
// Test that we inlined the body of the parallel region.
267267
// CHECK: store float 0x{{.*}}, ptr %[[PRIV_ALLOC]], align 4
268+
269+
// -----
270+
271+
omp.private {type = private} @_QFEi_private_i32 : i32
272+
llvm.func @_QPprivate_alloc_with_switch() {
273+
%0 = llvm.mlir.constant(1 : i32) : i32
274+
%1 = llvm.mlir.constant(30 : i32) : i32
275+
%2 = llvm.mlir.constant(1 : i64) : i64
276+
%3 = llvm.alloca %2 x i32 {bindc_name = "n"} : (i64) -> !llvm.ptr
277+
%4 = llvm.alloca %2 x i32 {bindc_name = "i"} : (i64) -> !llvm.ptr
278+
llvm.store %0, %3 : i32, !llvm.ptr
279+
%5 = llvm.load %3 : !llvm.ptr -> i32
280+
llvm.switch %5 : i32, ^bb1 [
281+
1: ^bb1,
282+
2: ^bb2
283+
]
284+
^bb1: // 2 preds: ^bb0, ^bb0
285+
omp.simd private(@_QFEi_private_i32 %4 -> %arg0 : !llvm.ptr) {
286+
omp.loop_nest (%arg1) : i32 = (%0) to (%1) inclusive step (%0) {
287+
llvm.store %arg1, %arg0 : i32, !llvm.ptr
288+
omp.yield
289+
}
290+
}
291+
llvm.br ^bb2
292+
^bb2: // 2 preds: ^bb0, ^bb1
293+
llvm.return
294+
}
295+
296+
// CHECK-LABEL: define void @_QPprivate_alloc_with_switch() {
297+
// CHECK: br label %[[AFTER_ALLOCA_BLOCK:.*]]
298+
// CHECK: [[AFTER_ALLOCA_BLOCK]]:
299+
// CHECK: switch i32 %{{.*}}, label %[[PRIVATE_INIT_BLOCK:.*]] [
300+
// CHECK: i32 1, label %[[PRIVATE_INIT_BLOCK]]
301+
// CHECK: i32 2, label %[[EXIT_BLOCK:.*]]
302+
// CHECK: ]
303+
// CHECK: [[PRIVATE_INIT_BLOCK]]:
304+
// CHECK: omp.private.init:
305+
// CHECK: omp.simd.region:
306+
// CHECK: [[EXIT_BLOCK]]:
307+
// CHECK: ret void

0 commit comments

Comments
 (0)