Index: flang/lib/Optimizer/Builder/IntrinsicCall.cpp =================================================================== --- flang/lib/Optimizer/Builder/IntrinsicCall.cpp +++ flang/lib/Optimizer/Builder/IntrinsicCall.cpp @@ -37,6 +37,7 @@ #include "flang/Optimizer/Support/FatalError.h" #include "flang/Optimizer/Support/Utils.h" #include "flang/Runtime/entry-names.h" +#include "flang/Runtime/iostat.h" #include "mlir/Dialect/Complex/IR/Complex.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/Dialect/Math/IR/Math.h" @@ -251,6 +252,8 @@ fir::ExtendedValue genIparity(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genIsContiguous(mlir::Type, llvm::ArrayRef); + template + mlir::Value genIsIostatValue(mlir::Type, llvm::ArrayRef); mlir::Value genIsNan(mlir::Type, llvm::ArrayRef); mlir::Value genIsFPClass(mlir::Type, llvm::ArrayRef, int fpclass); @@ -647,6 +650,8 @@ &I::genIsContiguous, {{{"array", asBox}}}, /*isElemental=*/false}, + {"is_iostat_end", &I::genIsIostatValue}, + {"is_iostat_eor", &I::genIsIostatValue}, {"ishft", &I::genIshft}, {"ishftc", &I::genIshftc}, {"isnan", &I::genIsNan}, @@ -3051,8 +3056,8 @@ // Create mutable fir.box to be passed to the runtime for the result. mlir::Type resultArrayType = builder.getVarLenSeqTy(resultType, arrayRank); - fir::MutableBoxValue resultMutableBox = - fir::factory::createTempMutableBox(builder, loc, resultArrayType, {}, + fir::MutableBoxValue resultMutableBox = fir::factory::createTempMutableBox( + builder, loc, resultArrayType, {}, fir::isPolymorphicType(array.getType()) ? array : mlir::Value{}); mlir::Value resultIrBox = fir::factory::getMutableIRBox(builder, loc, resultMutableBox); @@ -3714,6 +3719,17 @@ fir::runtime::genIsContiguous(builder, loc, fir::getBase(args[0]))); } +// IS_IOSTAT_END, IS_IOSTAT_EOR +template +mlir::Value +IntrinsicLibrary::genIsIostatValue(mlir::Type resultType, + llvm::ArrayRef args) { + assert(args.size() == 1); + return builder.create( + loc, mlir::arith::CmpIPredicate::eq, args[0], + builder.createIntegerConstant(loc, args[0].getType(), value)); +} + mlir::Value IntrinsicLibrary::genIsFPClass(mlir::Type resultType, llvm::ArrayRef args, int fpclass) { @@ -5027,8 +5043,8 @@ mlir::Type type = (moldRank == 0 && absentSize) ? resultType : builder.getVarLenSeqTy(resultType, 1); - fir::MutableBoxValue resultMutableBox = - fir::factory::createTempMutableBox(builder, loc, type, {}, + fir::MutableBoxValue resultMutableBox = fir::factory::createTempMutableBox( + builder, loc, type, {}, fir::isPolymorphicType(mold.getType()) ? mold : mlir::Value{}); if (moldRank == 0 && absentSize) { Index: flang/test/Lower/Intrinsics/is_iostat_value.f90 =================================================================== --- /dev/null +++ flang/test/Lower/Intrinsics/is_iostat_value.f90 @@ -0,0 +1,23 @@ +! RUN: bbc -emit-fir -o - %s | FileCheck %s + + ! CHECK-LABEL: func @_QQmain + ! CHECK: %[[V_0:[0-9]+]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFEi"} + ! CHECK: fir.do_loop + do i=-20,20 + ! CHECK: %[[V_5:[0-9]+]] = fir.load %[[V_0]] : !fir.ref + ! CHECK: %[[V_6:[0-9]+]] = arith.cmpi eq, %[[V_5]], %c-1{{.*}} : i32 + ! CHECK: fir.if %[[V_6]] { + ! CHECK: %[[V_20:[0-9]+]] = fir.load %[[V_0]] : !fir.ref + ! CHECK: %[[V_21:[0-9]+]] = fir.call @_FortranAioOutputInteger32(%{{.*}} %[[V_20]]) fastmath : (!fir.ref, i32) -> i1 + ! CHECK: } + if (is_iostat_end(i)) print*, "iostat_end =", i + ! CHECK: %[[V_7:[0-9]+]] = fir.load %[[V_0]] : !fir.ref + ! CHECK: %[[V_8:[0-9]+]] = arith.cmpi eq, %[[V_7]], %c-2{{.*}} : i32 + ! CHECK: fir.if %[[V_8]] { + ! CHECK: %[[V_20]] = fir.load %[[V_0]] : !fir.ref + ! CHECK: %[[V_21]] = fir.call @_FortranAioOutputInteger32(%{{.*}} %[[V_20]]) fastmath : (!fir.ref, i32) -> i1 + ! CHECK: } + if (is_iostat_eor(i)) print*, "iostat_eor =", i + ! CHECK: } + enddo +end