Skip to content

Commit 05fd4c5

Browse files
tblahJaddyen
authored andcommitted
[mlir][OpenMP] Fix broken insertion point for charbox with omp task (llvm#143112)
Fixes llvm#142365
1 parent 2198eaa commit 05fd4c5

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,8 +2294,7 @@ convertOmpTaskOp(omp::TaskOp taskOp, llvm::IRBuilderBase &builder,
22942294
if (!privateVarOrErr)
22952295
return handleError(privateVarOrErr, *taskOp.getOperation());
22962296

2297-
llvm::IRBuilderBase::InsertPointGuard guard(builder);
2298-
builder.SetInsertPoint(builder.GetInsertBlock()->getTerminator());
2297+
setInsertPointForPossiblyEmptyBlock(builder);
22992298

23002299
// TODO: this is a bit of a hack for Fortran character boxes.
23012300
// Character boxes are passed by value into the init region and then the
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// RUN: mlir-translate --mlir-to-llvmir %s | FileCheck %s
2+
3+
// Regression test for a compiler crash. Ensure that the insertion point is set
4+
// correctly when triggering the charbox hack multiple times.
5+
// Nonsense test code to minimally reproduce the issue.
6+
7+
module {
8+
llvm.func @free(!llvm.ptr)
9+
llvm.func @malloc(i64) -> !llvm.ptr
10+
omp.private {type = private} @_QFEc2_private_box_heap_c8xU : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> init {
11+
^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr):
12+
%0 = llvm.mlir.constant(24 : i32) : i32
13+
%1 = llvm.mlir.constant(0 : i64) : i64
14+
%2 = llvm.mlir.constant(1 : i32) : i32
15+
%3 = llvm.alloca %2 x !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
16+
"llvm.intr.memcpy"(%3, %arg0, %0) <{isVolatile = false}> : (!llvm.ptr, !llvm.ptr, i32) -> ()
17+
%6 = llvm.ptrtoint %arg0 : !llvm.ptr to i64
18+
%7 = llvm.icmp "eq" %6, %1 : i64
19+
llvm.cond_br %7, ^bb1, ^bb2
20+
^bb1: // pred: ^bb0
21+
llvm.br ^bb3
22+
^bb2: // pred: ^bb0
23+
llvm.br ^bb3
24+
^bb3: // 2 preds: ^bb1, ^bb2
25+
omp.yield(%arg1 : !llvm.ptr)
26+
} dealloc {
27+
^bb0(%arg0: !llvm.ptr):
28+
omp.yield
29+
}
30+
omp.private {type = private} @_QFEc1_private_box_ptr_c8xU : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> init {
31+
^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr):
32+
%0 = llvm.mlir.constant(24 : i32) : i32
33+
%1 = llvm.mlir.constant(1 : i32) : i32
34+
%2 = llvm.alloca %1 x !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
35+
"llvm.intr.memcpy"(%2, %arg0, %0) <{isVolatile = false}> : (!llvm.ptr, !llvm.ptr, i32) -> ()
36+
omp.yield(%arg1 : !llvm.ptr)
37+
}
38+
llvm.func @_QQmain() {
39+
%0 = llvm.mlir.constant(1 : i64) : i64
40+
%1 = llvm.alloca %0 x !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> {bindc_name = "c2"} : (i64) -> !llvm.ptr
41+
%2 = llvm.alloca %0 x !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> {bindc_name = "c1"} : (i64) -> !llvm.ptr
42+
omp.task private(@_QFEc1_private_box_ptr_c8xU %2 -> %arg0, @_QFEc2_private_box_heap_c8xU %1 -> %arg1 : !llvm.ptr, !llvm.ptr) {
43+
omp.terminator
44+
}
45+
llvm.return
46+
}
47+
}
48+
49+
// CHECK-LABEL: @_QQmain() {
50+
// CHECK: %[[STRUCTARG:.*]] = alloca { ptr }, align 8
51+
// CHECK: %[[VAL_0:.*]] = alloca { ptr, i64, i32, i8, i8, i8, i8 }, i64 1, align 8
52+
// CHECK: br label %[[VAL_2:.*]]
53+
// CHECK: entry: ; preds = %[[VAL_3:.*]]
54+
// CHECK: br label %[[VAL_4:.*]]
55+
// CHECK: omp.private.init: ; preds = %[[VAL_2]]
56+
// CHECK: %[[VAL_5:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ { ptr, i64, i32, i8, i8, i8, i8 }, { ptr, i64, i32, i8, i8, i8, i8 } }, ptr null, i32 1) to i64))
57+
// CHECK: %[[VAL_6:.*]] = getelementptr { { ptr, i64, i32, i8, i8, i8, i8 }, { ptr, i64, i32, i8, i8, i8, i8 } }, ptr %[[VAL_5]], i32 0, i32 0
58+
// CHECK: %[[VAL_7:.*]] = getelementptr { { ptr, i64, i32, i8, i8, i8, i8 }, { ptr, i64, i32, i8, i8, i8, i8 } }, ptr %[[VAL_5]], i32 0, i32 1
59+
// ...
60+
// CHECK: br label %[[VAL_9:.*]]
61+
// CHECK: omp.private.init4: ; preds = %[[VAL_10:.*]], %[[VAL_11:.*]]
62+
// CHECK: br label %[[VAL_12:.*]]
63+
// CHECK: omp.private.init3: ; preds = %[[VAL_9]]
64+
// CHECK: br label %[[VAL_13:.*]]
65+
// CHECK: omp.private.init2: ; preds = %[[VAL_9]]
66+
// CHECK: br label %[[VAL_13]]
67+
// CHECK: omp.private.init1: ; preds = %[[VAL_4]]
68+
// CHECK: %[[VAL_14:.*]] = alloca { ptr, i64, i32, i8, i8, i8, i8 }, align 8
69+
// CHECK: call void @llvm.memcpy.p0.p0.i32(ptr %[[VAL_14]], ptr %[[VAL_0]], i32 24, i1 false)
70+
// CHECK: %[[VAL_15:.*]] = ptrtoint ptr %[[VAL_0]] to i64
71+
// CHECK: %[[VAL_16:.*]] = icmp eq i64 %[[VAL_15]], 0
72+
// CHECK: br i1 %[[VAL_16]], label %[[VAL_10]], label %[[VAL_11]]
73+
// CHECK: omp.region.cont: ; preds = %[[VAL_13]]
74+
// CHECK: %[[VAL_17:.*]] = phi ptr [ %[[VAL_7]], %[[VAL_13]] ]
75+
// CHECK: br label %[[VAL_18:.*]]
76+
// CHECK: omp.private.copy: ; preds = %[[VAL_12]]
77+
// CHECK: br label %[[VAL_19:.*]]
78+
// CHECK: omp.task.start: ; preds = %[[VAL_18]]
79+
// CHECK: br label %[[VAL_20:.*]]
80+
// CHECK: codeRepl: ; preds = %[[VAL_19]]
81+
// CHECK: %[[VAL_21:.*]] = getelementptr { ptr }, ptr %[[STRUCTARG]], i32 0, i32 0
82+
// CHECK: store ptr %[[VAL_5]], ptr %[[VAL_21]], align 8
83+
// CHECK: %[[VAL_22:.*]] = call i32 @__kmpc_global_thread_num(ptr @1)
84+
// CHECK: %[[VAL_23:.*]] = call ptr @__kmpc_omp_task_alloc(ptr @1, i32 %[[VAL_22]], i32 1, i64 40, i64 8, ptr @_QQmain..omp_par)
85+
// CHECK: %[[VAL_24:.*]] = load ptr, ptr %[[VAL_23]], align 8
86+
// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %[[VAL_24]], ptr align 1 %[[STRUCTARG]], i64 8, i1 false)
87+
// CHECK: %[[VAL_25:.*]] = call i32 @__kmpc_omp_task(ptr @1, i32 %[[VAL_22]], ptr %[[VAL_23]])

0 commit comments

Comments
 (0)