-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[flang] Select proper library APIs for derived type io. #66327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This patch syncs the logic inside `getInputFunc` that selects the library API and the logic in `createIoRuntimeCallForItem` that creates the input arguments for the library call. There were cases where we selected `InputDerivedType` API and passed only two arguments, and also we selected `InputDescriptor` and passed three arguments. It turns out we also were incorrectly selecting `OutputDescriptor` in `getOutputFunc` (`test4` case in the new LIT test), which caused runtime issues for output of a derived type with descriptor components (due to the missing non-type-bound table).
@llvm/pr-subscribers-flang-fir-hlfir ChangesThis patch syncs the logic inside `getInputFunc` that selects the library API and the logic in `createIoRuntimeCallForItem` that creates the input arguments for the library call. There were cases where we selected `InputDerivedType` API and passed only two arguments, and also we selected `InputDescriptor` and passed three arguments. It turns out we also were incorrectly selecting `OutputDescriptor` in `getOutputFunc` (`test4` case in the new LIT test), which caused runtime issues for output of a derived type with descriptor components (due to the missing non-type-bound table).-- 3 Files Affected:
diff --git a/flang/lib/Lower/IO.cpp b/flang/lib/Lower/IO.cpp index ac1fe7f68a9a665..48f2baa2e4f4ed2 100644 --- a/flang/lib/Lower/IO.cpp +++ b/flang/lib/Lower/IO.cpp @@ -655,7 +655,7 @@ static void genNamelistIO(Fortran::lower::AbstractConverter &converter, static mlir::func::FuncOp getOutputFunc(mlir::Location loc, fir::FirOpBuilder &builder, mlir::Type type, bool isFormatted) { - if (type.isa<fir::RecordType>()) + if (fir::unwrapPassByRefType(type).isa<fir::RecordType>()) return getIORuntimeFunc<mkIOKey(OutputDerivedType)>(loc, builder); if (!isFormatted) return getIORuntimeFunc<mkIOKey(OutputDescriptor)>(loc, builder); @@ -737,7 +737,7 @@ static void genOutputItemList( if (argType.isa<fir::BoxType>()) { mlir::Value box = fir::getBase(converter.genExprBox(loc, *expr, stmtCtx)); outputFuncArgs.push_back(builder.createConvert(loc, argType, box)); - if (itemTy.isa<fir::RecordType>()) + if (fir::unwrapPassByRefType(itemTy).isa<fir::RecordType>()) outputFuncArgs.push_back(getNonTbpDefinedIoTableAddr(converter)); } else if (helper.isCharacterScalar(itemTy)) { fir::ExtendedValue exv = converter.genExprAddr(loc, expr, stmtCtx); @@ -772,7 +772,7 @@ static void genOutputItemList( static mlir::func::FuncOp getInputFunc(mlir::Location loc, fir::FirOpBuilder &builder, mlir::Type type, bool isFormatted) { - if (type.isa<fir::RecordType>()) + if (fir::unwrapPassByRefType(type).isa<fir::RecordType>()) return getIORuntimeFunc<mkIOKey(InputDerivedType)>(loc, builder); if (!isFormatted) return getIORuntimeFunc<mkIOKey(InputDescriptor)>(loc, builder); @@ -834,7 +834,7 @@ createIoRuntimeCallForItem(Fortran::lower::AbstractConverter &converter, auto boxTy = box.getType().dyn_cast<fir::BaseBoxType>(); assert(boxTy && "must be previously emboxed"); inputFuncArgs.push_back(builder.createConvert(loc, argType, box)); - if (boxTy.getEleTy().isa<fir::RecordType>()) + if (fir::unwrapPassByRefType(boxTy).isa<fir::RecordType>()) inputFuncArgs.push_back(getNonTbpDefinedIoTableAddr(converter)); } else { mlir::Value itemAddr = fir::getBase(item); diff --git a/flang/test/Lower/io-derived-type-2.f90 b/flang/test/Lower/io-derived-type-2.f90 new file mode 100644 index 000000000000000..c2f1ff1850725da --- /dev/null +++ b/flang/test/Lower/io-derived-type-2.f90 @@ -0,0 +1,70 @@ +! Check that InputDerivedType/OutputDeriverType APIs are used +! for io of derived types. +! RUN: bbc -polymorphic-type -emit-fir -o - %s | FileCheck %s + +module p + type :: person + type(person), pointer :: next => null() + end type person + type :: club + class(person), allocatable :: membership(:) + end type club +contains + subroutine pwf (dtv,unit,iotype,vlist,iostat,iomsg) + class(person), intent(in) :: dtv + integer, intent(in) :: unit + character (len=*), intent(in) :: iotype + integer, intent(in) :: vlist(:) + integer, intent(out) :: iostat + character (len=*), intent(inout) :: iomsg + print *, 'write' + end subroutine pwf + subroutine prf (dtv,unit,iotype,vlist,iostat,iomsg) + class(person), intent(inout) :: dtv + integer, intent(in) :: unit + character (len=*), intent(in) :: iotype + integer, intent(in) :: vlist(:) + integer, intent(out) :: iostat + character (len=*), intent(inout) :: iomsg + end subroutine prf + subroutine test1(dtv) + interface read(formatted) + module procedure prf + end interface read(formatted) + class(person), intent(inout) :: dtv + read(7, fmt='(DT)') dtv%next + end subroutine test1 +! CHECK-LABEL: func.func @_QMpPtest1( +! CHECK: %{{.*}} = fir.call @_FortranAioInputDerivedType(%{{.*}}, %{{.*}}, %{{.*}}) fastmath<contract> : (!fir.ref<i8>, !fir.box<none>, !fir.ref<none>) -> i1 + + subroutine test2(social_club) + interface read(formatted) + module procedure prf + end interface read(formatted) + class(club) :: social_club + read(7, fmt='(DT)') social_club%membership(0) + end subroutine test2 +! CHECK-LABEL: func.func @_QMpPtest2( +! CHECK: %{{.*}} = fir.call @_FortranAioInputDerivedType(%{{.*}}, %{{.*}}, %{{.*}}) fastmath<contract> : (!fir.ref<i8>, !fir.box<none>, !fir.ref<none>) -> i1 + + subroutine test3(dtv) + interface write(formatted) + module procedure pwf + end interface write(formatted) + class(person), intent(inout) :: dtv + write(7, fmt='(DT)') dtv%next + end subroutine test3 +! CHECK-LABEL: func.func @_QMpPtest3( +! CHECK: %{{.*}} = fir.call @_FortranAioOutputDerivedType(%{{.*}}, %{{.*}}, %{{.*}}) fastmath<contract> : (!fir.ref<i8>, !fir.box<none>, !fir.ref<none>) -> i1 + + subroutine test4(social_club) + interface write(formatted) + module procedure pwf + end interface write(formatted) + class(club) :: social_club + write(7, fmt='(DT)') social_club%membership(0) + end subroutine test4 +! CHECK-LABEL: func.func @_QMpPtest4( +! CHECK: %{{.*}} = fir.call @_FortranAioOutputDerivedType(%{{.*}}, %{{.*}}, %{{.*}}) fastmath<contract> : (!fir.ref<i8>, !fir.box<none>, !fir.ref<none>) -> i1 +end module p + diff --git a/flang/test/Lower/polymorphic.f90 b/flang/test/Lower/polymorphic.f90 index ba605476638e395..1dc945c1c3c422d 100644 --- a/flang/test/Lower/polymorphic.f90 +++ b/flang/test/Lower/polymorphic.f90 @@ -766,7 +766,7 @@ subroutine test_polymorphic_io() ! CHECK: %[[P:.*]] = fir.alloca !fir.class<!fir.ptr<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>> {bindc_name = "p", uniq_name = "_QMpolymorphic_testFtest_polymorphic_ioEp"} ! CHECK: %[[LOAD_P:.*]] = fir.load %[[P]] : !fir.ref<!fir.class<!fir.ptr<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>> ! CHECK: %[[BOX_NONE:.*]] = fir.convert %[[LOAD_P]] : (!fir.class<!fir.ptr<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>) -> !fir.box<none> -! CHECK: %{{.*}} = fir.call @_FortranAioInputDescriptor(%{{.*}}, %[[BOX_NONE]]) {{.*}} : (!fir.ref<i8>, !fir.box<none>) -> i1 +! CHECK: %{{.*}} = fir.call @_FortranAioInputDerivedType(%{{.*}}, %[[BOX_NONE]], %{{.*}}) {{.*}} : (!fir.ref<i8>, !fir.box<none>, !fir.ref<none>) -> i1 function unlimited_polymorphic_alloc_array_ret() class(*), allocatable :: unlimited_polymorphic_alloc_array_ret(:) |
clementval
approved these changes
Sep 14, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
kstoimenov
pushed a commit
to kstoimenov/llvm-project
that referenced
this pull request
Sep 14, 2023
This patch syncs the logic inside `getInputFunc` that selects the library API and the logic in `createIoRuntimeCallForItem` that creates the input arguments for the library call. There were cases where we selected `InputDerivedType` API and passed only two arguments, and also we selected `InputDescriptor` and passed three arguments. It turns out we also were incorrectly selecting `OutputDescriptor` in `getOutputFunc` (`test4` case in the new LIT test), which caused runtime issues for output of a derived type with descriptor components (due to the missing non-type-bound table).
This was referenced Sep 14, 2023
ZijunZhaoCCK
pushed a commit
to ZijunZhaoCCK/llvm-project
that referenced
this pull request
Sep 19, 2023
This patch syncs the logic inside `getInputFunc` that selects the library API and the logic in `createIoRuntimeCallForItem` that creates the input arguments for the library call. There were cases where we selected `InputDerivedType` API and passed only two arguments, and also we selected `InputDescriptor` and passed three arguments. It turns out we also were incorrectly selecting `OutputDescriptor` in `getOutputFunc` (`test4` case in the new LIT test), which caused runtime issues for output of a derived type with descriptor components (due to the missing non-type-bound table).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch syncs the logic inside
getInputFunc
that selectsthe library API and the logic in
createIoRuntimeCallForItem
that creates the input arguments for the library call.
There were cases where we selected
InputDerivedType
APIand passed only two arguments, and also we selected
InputDescriptor
and passed three arguments.
It turns out we also were incorrectly selecting
OutputDescriptor
in
getOutputFunc
(test4
case in the new LIT test),which caused runtime issues for output of a derived type
with descriptor components (due to the missing non-type-bound table).