! Test lowering of IO statement involving INTEGER(8) and INTEGER(16) external ! unit number that may turn out at runtime to be too large to fit in a default ! integer. Unit numbers must fit on default integers. This file tests that the ! related generated runtime checks and error recovery code. ! RUN: %flang_fc1 -emit-hlfir %s -o - | FileCheck %s ! ----------------------------------------------------------------------------- ! Test that runtime checks are not emitted when not needed ! ----------------------------------------------------------------------------- ! CHECK-LABEL: func @_QPopen_4 subroutine open_4(n4) integer(4) :: n4 ! CHECK-NOT: CheckUnitNumberInRange ! CHECK-NOT: fir.if open(n4) end subroutine ! CHECK-LABEL: func @_QPwrite_4 subroutine write_4(n4) integer(4) :: n4 ! CHECK-NOT: CheckUnitNumberInRange ! CHECK-NOT: fir.if write(n4, *) "hello" end subroutine ! CHECK-LABEL: func @_QPwrite_4_recovery subroutine write_4_recovery(n4, ios) integer(4) :: n4, ios ! CHECK-NOT: CheckUnitNumberInRange write(n4, *, iostat=ios) "hello" end subroutine ! ----------------------------------------------------------------------------- ! Test that runtime checks are emitted for integer(8/16) when there is ! no user error recovery. ! ----------------------------------------------------------------------------- ! CHECK-LABEL: func @_QPopen_8( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref subroutine open_8(n) integer(8) :: n ! CHECK: %[[VAL_1:.*]] = fir.load %{{.*}} : !fir.ref ! CHECK: %[[VAL_2:.*]] = arith.constant false ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref, i64, !fir.ref, i32) -> i32 ! CHECK-NOT: fir.if ! CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_1]] : (i64) -> i32 ! CHECK: %[[VAL_13:.*]] = fir.call @_FortranAioBeginOpenUnit(%[[VAL_9]], %{{.*}}, %{{.*}}) {{.*}}: (i32, !fir.ref, i32) -> !fir.ref ! CHECK: %[[VAL_14:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_13]]) {{.*}}: (!fir.ref) -> i32 open(n) end subroutine ! CHECK-LABEL: func @_QPclose_8( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref subroutine close_8(n) integer(8) :: n ! CHECK: %[[VAL_1:.*]] = fir.load %{{.*}} : !fir.ref ! CHECK: %[[VAL_2:.*]] = arith.constant false ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref, i64, !fir.ref, i32) -> i32 ! CHECK-NOT: fir.if ! CHECK: BeginClose close(n) end subroutine ! CHECK-LABEL: func @_QPrewind_8( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref subroutine rewind_8(n) integer(8) :: n ! CHECK: %[[VAL_1:.*]] = fir.load %{{.*}} : !fir.ref ! CHECK: %[[VAL_2:.*]] = arith.constant false ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref, i64, !fir.ref, i32) -> i32 ! CHECK-NOT: fir.if ! CHECK: BeginRewind rewind(n) end subroutine ! CHECK-LABEL: func @_QPbackspace_8( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref subroutine backspace_8(n) integer(8) :: n backspace(n) ! CHECK: %[[VAL_1:.*]] = fir.load %{{.*}} : !fir.ref ! CHECK: %[[VAL_2:.*]] = arith.constant false ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref, i64, !fir.ref, i32) -> i32 ! CHECK-NOT: fir.if ! CHECK: BeginBackspace end subroutine ! CHECK-LABEL: func @_QPinquire_8( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref subroutine inquire_8(n, fm) integer(8) :: n character(*), fm ! CHECK: %[[VAL_1:.*]] = fir.load %{{.*}} : !fir.ref ! CHECK: %[[VAL_2:.*]] = arith.constant false ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref, i64, !fir.ref, i32) -> i32 ! CHECK-NOT: fir.if ! CHECK: BeginInquire inquire(n, formatted=fm) end subroutine ! CHECK-LABEL: func @_QPwrite_8( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref subroutine write_8(n) integer(8) :: n ! CHECK: %[[VAL_1:.*]] = fir.load %{{.*}} : !fir.ref ! CHECK: %[[VAL_2:.*]] = arith.constant false ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref, i64, !fir.ref, i32) -> i32 ! CHECK-NOT: fir.if ! CHECK: BeginExternalListOutput write(n, *) "hello" end subroutine ! CHECK-LABEL: func @_QPread_8( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref subroutine read_8(n, var) integer(8) :: n integer(4) :: var ! CHECK: %[[VAL_1:.*]] = fir.load %{{.*}} : !fir.ref ! CHECK: %[[VAL_2:.*]] = arith.constant false ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref, i64, !fir.ref, i32) -> i32 ! CHECK-NOT: fir.if ! CHECK: BeginExternalListInput read(n, *) var end subroutine ! CHECK-LABEL: func @_QPopen_16( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref subroutine open_16(n) integer(16) :: n ! CHECK: %[[VAL_1:.*]] = fir.load %{{.*}} : !fir.ref ! CHECK: %[[VAL_2:.*]] = arith.constant false ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange128(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i128, i1, !fir.ref, i64, !fir.ref, i32) -> i32 open(n) end subroutine ! ----------------------------------------------------------------------------- ! Test generation of user error recovery if-nests with INTEGER(8/16) ! unit numbers. ! ----------------------------------------------------------------------------- ! CHECK-LABEL: func @_QPopen_8_error_recovery_1( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref ! CHECK-SAME: , %[[VAL_1:.*]]: !fir.ref subroutine open_8_error_recovery_1(n, ios) integer(8) :: n integer(4) :: ios ! CHECK: %[[VAL_2:.*]] = fir.load %{{.*}} : !fir.ref ! CHECK: %[[VAL_3:.*]] = arith.constant true ! CHECK: %[[VAL_4:.*]] = fir.zero_bits !fir.ref ! CHECK: %[[VAL_5:.*]] = arith.constant 0 : i64 ! CHECK: %[[VAL_9:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %[[VAL_5]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref, i64, !fir.ref, i32) -> i32 ! CHECK: %[[VAL_10:.*]] = arith.constant 0 : i32 ! CHECK: %[[VAL_11:.*]] = arith.cmpi eq, %[[VAL_9]], %[[VAL_10]] : i32 ! CHECK: %[[VAL_12:.*]] = fir.if %[[VAL_11]] -> (i32) { ! CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_2]] : (i64) -> i32 ! CHECK: %[[VAL_17:.*]] = fir.call @_FortranAioBeginOpenUnit(%[[VAL_13]], %{{.*}}, {{.*}}) {{.*}}: (i32, !fir.ref, i32) -> !fir.ref ! CHECK: %[[VAL_18:.*]] = arith.constant true ! CHECK: %[[VAL_19:.*]] = arith.constant false ! CHECK: %[[VAL_20:.*]] = arith.constant false ! CHECK: %[[VAL_21:.*]] = arith.constant false ! CHECK: %[[VAL_22:.*]] = arith.constant false ! CHECK: fir.call @_FortranAioEnableHandlers(%[[VAL_17]], %[[VAL_18]], %[[VAL_19]], %[[VAL_20]], %[[VAL_21]], %[[VAL_22]]) {{.*}}: (!fir.ref, i1, i1, i1, i1, i1) -> () ! CHECK: %[[VAL_24:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_17]]) {{.*}}: (!fir.ref) -> i32 ! CHECK: fir.result %[[VAL_24]] : i32 ! CHECK: } else { ! CHECK: fir.result %[[VAL_9]] : i32 ! CHECK: } ! CHECK: fir.store %[[VAL_12]] to %{{.*}} : !fir.ref open(n, iostat=ios) end subroutine ! CHECK-LABEL: func @_QPopen_8_error_recovery_2( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref ! CHECK-SAME: , %[[VAL_1:.*]]: !fir.boxchar<1> subroutine open_8_error_recovery_2(n, msg) integer(8) :: n character(*) :: msg ! CHECK: %[[VAL_2:.*]]:2 = fir.unboxchar %[[VAL_1]] : (!fir.boxchar<1>) -> (!fir.ref>, index) ! CHECK: %[[VAL_3:.*]] = fir.load %{{.*}} : !fir.ref ! CHECK: %[[VAL_4:.*]] = arith.constant true ! CHECK: %[[VAL_5:.*]] = fir.convert %{{.*}} : (!fir.ref>) -> !fir.ref ! CHECK: %[[VAL_6:.*]] = fir.convert %{{.*}} : (index) -> i64 ! CHECK: %[[VAL_10:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_3]], %[[VAL_4]], %[[VAL_5]], %[[VAL_6]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref, i64, !fir.ref, i32) -> i32 ! CHECK: %[[VAL_11:.*]] = arith.constant 0 : i32 ! CHECK: %[[VAL_12:.*]] = arith.cmpi eq, %[[VAL_10]], %[[VAL_11]] : i32 ! CHECK: %[[VAL_13:.*]] = fir.if %[[VAL_12]] -> (i32) { ! CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_3]] : (i64) -> i32 ! CHECK: %[[VAL_18:.*]] = fir.call @_FortranAioBeginOpenUnit(%[[VAL_14]], %{{.*}}, %{{.*}}) {{.*}}: (i32, !fir.ref, i32) -> !fir.ref ! CHECK: %[[VAL_19:.*]] = arith.constant false ! CHECK: %[[VAL_20:.*]] = arith.constant true ! CHECK: %[[VAL_21:.*]] = arith.constant false ! CHECK: %[[VAL_22:.*]] = arith.constant false ! CHECK: %[[VAL_23:.*]] = arith.constant true ! CHECK: fir.call @_FortranAioEnableHandlers(%[[VAL_18]], %[[VAL_19]], %[[VAL_20]], %[[VAL_21]], %[[VAL_22]], %[[VAL_23]]) {{.*}}: (!fir.ref, i1, i1, i1, i1, i1) -> () ! CHECK: %[[VAL_25:.*]] = fir.convert %{{.*}} : (!fir.ref>) -> !fir.ref ! CHECK: %[[VAL_26:.*]] = fir.convert %{{.*}} : (index) -> i64 ! CHECK: fir.call @_FortranAioGetIoMsg(%[[VAL_18]], %[[VAL_25]], %[[VAL_26]]) {{.*}}: (!fir.ref, !fir.ref, i64) -> () ! CHECK: %[[VAL_28:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_18]]) {{.*}}: (!fir.ref) -> i32 ! CHECK: fir.result %[[VAL_28]] : i32 ! CHECK: } else { ! CHECK: fir.result %[[VAL_10]] : i32 ! CHECK: } ! CHECK: %[[VAL_29:.*]] = fir.convert %[[VAL_13]] : (i32) -> index ! CHECK: fir.select %[[VAL_29]] : index [0, ^bb1, unit, ^bb2] ! CHECK: ^bb1: ! CHECK: cf.br ^bb3 ! CHECK: ^bb2: ! CHECK: fir.call @_QPi_failed() {{.*}}: () -> () ! CHECK: cf.br ^bb3 ! CHECK: ^bb3: ! CHECK: return open(n, err=30, iomsg=msg) return 30 call i_failed() end subroutine ! Torture test for temp clean-ups when user recovery is enabled. ! Checks that temps are cleaned-up in the right nests. subroutine temp_cleanup(n, msg, ios) interface function make_temp0() integer, allocatable :: make_temp0 end function function make_temp1() integer, allocatable :: make_temp1 end function function make_temp2() integer, allocatable :: make_temp2 end function function make_temp3() integer, allocatable :: make_temp3 end function function make_temp4() integer, allocatable :: make_temp4 end function function make_temp5() integer, allocatable :: make_temp5 end function end interface integer(8) :: n(2) character(80) :: msg Integer ios(2) write(n(make_temp0()), iostat=ios(make_temp1()), iomsg=msg(make_temp2():make_temp3())) make_temp4(), make_temp5() ! CHECK-LABEL: func @_QPtemp_cleanup( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref> {fir.bindc_name = "n"}, ! CHECK-SAME: %[[VAL_1:.*]]: !fir.boxchar<1> {fir.bindc_name = "msg"}, ! CHECK-SAME: %[[VAL_2:.*]]: !fir.ref> {fir.bindc_name = "ios"}) { ! CHECK: fir.call @_QPmake_temp2() ! CHECK: fir.call @_QPmake_temp3() ! CHECK: fir.freemem ! CHECK: fir.freemem ! CHECK: fir.call @_QPmake_temp0() ! CHECK: fir.call @_FortranAioCheckUnitNumberInRange64( ! CHECK: %[[VAL_57:.*]] = arith.constant 0 : i32 ! CHECK: %[[VAL_58:.*]] = arith.cmpi eq, %{{.*}}, %[[VAL_57]] : i32 ! CHECK: %[[VAL_60:.*]] = fir.if %[[VAL_58]] -> (i32) { ! CHECK: fir.call @_FortranAioBeginUnformattedOutput( ! CHECK: fir.call @_FortranAioEnableHandlers( ! CHECK: fir.call @_QPmake_temp4() ! CHECK: fir.call @_FortranAioOutputDescriptor( ! CHECK: fir.freemem ! CHECK: fir.if %{{.*}} { ! CHECK: fir.call @_QPmake_temp5() ! CHECK: fir.call @_FortranAioOutputDescriptor( ! CHECK: fir.freemem ! CHECK: } ! CHECK-NOT: fir.call @_QPmake_temp3 ! CHECK: fir.call @_FortranAioGetIoMsg( ! CHECK: %[[VAL_97:.*]] = fir.call @_FortranAioEndIoStatement( ! CHECK: fir.result %[[VAL_97]] : i32 ! CHECK: } else { ! CHECK: fir.result %{{.*}} : i32 ! CHECK: } ! CHECK: fir.call @_QPmake_temp1() end