Skip to content

Commit cccf4d6

Browse files
committed
[flang] Skip OPTIONAL arguments in LoopVersioning.
This patch fixes multiple tests failing with segfault due to accessing absent argument box before the loop versioning check. The absent arguments might be treated as contiguous for the purpose of loop versioning, but this is not done in this patch. Reviewed By: PeteSteinfeld Differential Revision: https://reviews.llvm.org/D158800
1 parent 291101a commit cccf4d6

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

flang/lib/Optimizer/Transforms/LoopVersioning.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ void LoopVersioningPass::runOnOperation() {
141141
fir::KindMapping kindMap = fir::getKindMapping(module);
142142
mlir::SmallVector<ArgInfo, 4> argsOfInterest;
143143
for (auto &arg : args) {
144+
// Optional arguments must be checked for IsPresent before
145+
// looking for the bounds. They are unsupported for the time being.
146+
if (func.getArgAttrOfType<mlir::UnitAttr>(arg.getArgNumber(),
147+
fir::getOptionalAttrName())) {
148+
LLVM_DEBUG(llvm::dbgs() << "OPTIONAL is not supported\n");
149+
continue;
150+
}
151+
144152
if (auto seqTy = getAsSequenceType(&arg)) {
145153
unsigned rank = seqTy.getDimension();
146154
if (rank > 0 &&

flang/test/Transforms/loop-versioning.fir

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,4 +522,52 @@ func.func @sum1dfixed(%arg0: !fir.ref<!fir.array<?xf64>> {fir.bindc_name = "a"},
522522
// CHECK: fir.store %[[IF_RES]]#1 to %{{.*}}
523523
// CHECK: return
524524

525+
// Check that OPTIONAL argument's box is not accessed illegally
526+
// before the loop.
527+
func.func @test_optional_arg(%arg0: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "x", fir.optional}) {
528+
%c1 = arith.constant 1 : index
529+
%c20 = arith.constant 20 : index
530+
%c0_i64 = arith.constant 0 : i64
531+
%0 = fir.alloca i32 {bindc_name = "i", uniq_name = "_QMcheck_modFtestEi"}
532+
%1 = fir.convert %c1 : (index) -> i32
533+
%2:2 = fir.do_loop %arg1 = %c1 to %c20 step %c1 iter_args(%arg2 = %1) -> (index, i32) {
534+
fir.store %arg2 to %0 : !fir.ref<i32>
535+
%3 = fir.is_present %arg0 : (!fir.box<!fir.array<?xf32>>) -> i1
536+
fir.if %3 {
537+
%8 = fir.coordinate_of %arg0, %c0_i64 : (!fir.box<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
538+
} else {
539+
}
540+
%4 = arith.addi %arg1, %c1 : index
541+
%5 = fir.convert %c1 : (index) -> i32
542+
%6 = fir.load %0 : !fir.ref<i32>
543+
%7 = arith.addi %6, %5 : i32
544+
fir.result %4, %7 : index, i32
545+
}
546+
fir.store %2#1 to %0 : !fir.ref<i32>
547+
return
548+
}
549+
// CHECK-LABEL: func.func @test_optional_arg(
550+
// CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "x", fir.optional}) {
551+
// CHECK-NEXT: %[[VAL_1:.*]] = arith.constant 1 : index
552+
// CHECK-NEXT: %[[VAL_2:.*]] = arith.constant 20 : index
553+
// CHECK-NEXT: %[[VAL_3:.*]] = arith.constant 0 : i64
554+
// CHECK-NEXT: %[[VAL_4:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QMcheck_modFtestEi"}
555+
// CHECK-NEXT: %[[VAL_5:.*]] = fir.convert %[[VAL_1]] : (index) -> i32
556+
// CHECK-NEXT: %[[VAL_6:.*]]:2 = fir.do_loop %[[VAL_7:.*]] = %[[VAL_1]] to %[[VAL_2]] step %[[VAL_1]] iter_args(%[[VAL_8:.*]] = %[[VAL_5]]) -> (index, i32) {
557+
// CHECK-NEXT: fir.store %[[VAL_8]] to %[[VAL_4]] : !fir.ref<i32>
558+
// CHECK-NEXT: %[[VAL_9:.*]] = fir.is_present %[[VAL_0]] : (!fir.box<!fir.array<?xf32>>) -> i1
559+
// CHECK-NEXT: fir.if %[[VAL_9]] {
560+
// CHECK-NEXT: %[[VAL_10:.*]] = fir.coordinate_of %[[VAL_0]], %[[VAL_3]] : (!fir.box<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
561+
// CHECK-NEXT: } else {
562+
// CHECK-NEXT: }
563+
// CHECK-NEXT: %[[VAL_11:.*]] = arith.addi %[[VAL_7]], %[[VAL_1]] : index
564+
// CHECK-NEXT: %[[VAL_12:.*]] = fir.convert %[[VAL_1]] : (index) -> i32
565+
// CHECK-NEXT: %[[VAL_13:.*]] = fir.load %[[VAL_4]] : !fir.ref<i32>
566+
// CHECK-NEXT: %[[VAL_14:.*]] = arith.addi %[[VAL_13]], %[[VAL_12]] : i32
567+
// CHECK-NEXT: fir.result %[[VAL_11]], %[[VAL_14]] : index, i32
568+
// CHECK-NEXT: }
569+
// CHECK-NEXT: fir.store %[[VAL_15:.*]]#1 to %[[VAL_4]] : !fir.ref<i32>
570+
// CHECK-NEXT: return
571+
// CHECK-NEXT: }
572+
525573
} // End module

0 commit comments

Comments
 (0)