[flang-commits] [PATCH] D106820: [flang] Fix runtime ICE with maxloc and scalar result

Mark LeAir via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Jul 26 12:31:29 PDT 2021


mleair created this revision.
mleair added reviewers: klausler, PeteSteinfeld, jeanPerier.
mleair added a project: Flang.
Herald added a subscriber: jdoerfert.
mleair requested review of this revision.

The following legal Fortran causes a runtime ICE in certain cases. Also, valgrind reports use of an unitialized memory reference (UMR). Steve's list of F95 <https://reviews.llvm.org/F95> compliance tests exposed this problem.

    real :: a(5)
    a = 1.1
    a(3) = 2.2
    i = maxloc(a,dim=1)
    print*, i
  end

The resulting ICE:

fatal Fortran runtime error(/local/home/mleair/fir-dev/f18-llvm-project/flang/runtime/reduction-templates.h:203): Internal error: RUNTIME_CHECK(at[0] == 1) failed at /local/home/mleair/fir-dev/f18-llvm-project/flang/runtime/reduction-templates.h(203)

The ICE occurs because a scalar result descriptor has 1 element and a rank == 0. The runtime calls GetLowerBounds() which does nothing with rank == 0. This can lead to an ICE and/or UMR.

Since this occurs with only a handful of intrinsics, I propose adding a check for this case where INTERNAL_CHECK is used instead of changing GetLowerBounds(). We may not want to make an assumption with rank == 0 in other cases. But I'll let the code reviewers weigh in on whether we should just modify GetLowerBounds() to work with rank == 0 and number of elements == 1.

This change is done for the maxloc, maxval, minloc, minval, findloc, count, parity, all, any macros. I also added scalar cases to the unit tests for each of these intrinsics as well. Let me know if there are other intrinsics to consider with this change.


https://reviews.llvm.org/D106820

Files:
  flang/runtime/reduction-templates.h
  flang/runtime/reduction.cpp
  flang/unittests/RuntimeGTest/Reduction.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106820.361750.patch
Type: text/x-patch
Size: 8022 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20210726/cd652663/attachment.bin>


More information about the flang-commits mailing list