[flang-commits] [PATCH] D145595: [flang] is_iostat_end and is_iostat_eor intrinsics

vdonaldson via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Mar 8 09:50:47 PST 2023


vdonaldson created this revision.
vdonaldson added a project: Flang.
Herald added subscribers: sunshaoce, mehdi_amini, jdoerfert.
Herald added a reviewer: sscalpone.
Herald added a project: All.
vdonaldson requested review of this revision.

https://reviews.llvm.org/D145595

Files:
  flang/lib/Optimizer/Builder/IntrinsicCall.cpp
  flang/test/Lower/Intrinsics/is_iostat_value.f90


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<i32>
+    ! 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<i32>
+    ! CHECK:     %[[V_21:[0-9]+]] = fir.call @_FortranAioOutputInteger32(%{{.*}} %[[V_20]]) fastmath<contract> : (!fir.ref<i8>, i32) -> i1
+    ! CHECK:   }
+    if (is_iostat_end(i)) print*, "iostat_end =", i
+    ! CHECK:   %[[V_7:[0-9]+]] = fir.load %[[V_0]] : !fir.ref<i32>
+    ! 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<i32>
+    ! CHECK:     %[[V_21]] = fir.call @_FortranAioOutputInteger32(%{{.*}} %[[V_20]]) fastmath<contract> : (!fir.ref<i8>, i32) -> i1
+    ! CHECK:   }
+    if (is_iostat_eor(i)) print*, "iostat_eor =", i
+  ! CHECK: }
+  enddo
+end
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>);
   fir::ExtendedValue genIsContiguous(mlir::Type,
                                      llvm::ArrayRef<fir::ExtendedValue>);
+  template <Fortran::runtime::io::Iostat value>
+  mlir::Value genIsIostatValue(mlir::Type, llvm::ArrayRef<mlir::Value>);
   mlir::Value genIsNan(mlir::Type, llvm::ArrayRef<mlir::Value>);
   mlir::Value genIsFPClass(mlir::Type, llvm::ArrayRef<mlir::Value>,
                            int fpclass);
@@ -647,6 +650,8 @@
      &I::genIsContiguous,
      {{{"array", asBox}}},
      /*isElemental=*/false},
+    {"is_iostat_end", &I::genIsIostatValue<Fortran::runtime::io::IostatEnd>},
+    {"is_iostat_eor", &I::genIsIostatValue<Fortran::runtime::io::IostatEor>},
     {"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 <Fortran::runtime::io::Iostat value>
+mlir::Value
+IntrinsicLibrary::genIsIostatValue(mlir::Type resultType,
+                                   llvm::ArrayRef<mlir::Value> args) {
+  assert(args.size() == 1);
+  return builder.create<mlir::arith::CmpIOp>(
+      loc, mlir::arith::CmpIPredicate::eq, args[0],
+      builder.createIntegerConstant(loc, args[0].getType(), value));
+}
+
 mlir::Value IntrinsicLibrary::genIsFPClass(mlir::Type resultType,
                                            llvm::ArrayRef<mlir::Value> 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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145595.503419.patch
Type: text/x-patch
Size: 4596 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230308/0962c5a7/attachment.bin>


More information about the flang-commits mailing list