[flang-commits] [flang] 4d323f4 - [flang] Lower exit intrinsic

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Thu Mar 17 05:18:40 PDT 2022


Author: Valentin Clement
Date: 2022-03-17T13:18:32+01:00
New Revision: 4d323f4837846c63aef1421de0cd4bda3d1a197b

URL: https://github.com/llvm/llvm-project/commit/4d323f4837846c63aef1421de0cd4bda3d1a197b
DIFF: https://github.com/llvm/llvm-project/commit/4d323f4837846c63aef1421de0cd4bda3d1a197b.diff

LOG: [flang] Lower exit intrinsic

This patch adds lowering for the `exit`
intrinsic.

This patch is part of the upstreaming effort from fir-dev branch.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D121882

Co-authored-by: Jean Perier <jperier at nvidia.com>
Co-authored-by: Josh Mottley <Josh.Mottley at arm.com>

Added: 
    flang/test/Lower/Intrinsics/exit.f90

Modified: 
    flang/lib/Lower/IntrinsicCall.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp
index 4575db5fd3cc6..1b64197aaf368 100644
--- a/flang/lib/Lower/IntrinsicCall.cpp
+++ b/flang/lib/Lower/IntrinsicCall.cpp
@@ -29,6 +29,7 @@
 #include "flang/Optimizer/Builder/Runtime/Numeric.h"
 #include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
 #include "flang/Optimizer/Builder/Runtime/Reduction.h"
+#include "flang/Optimizer/Builder/Runtime/Stop.h"
 #include "flang/Optimizer/Builder/Runtime/Transformational.h"
 #include "flang/Optimizer/Dialect/FIROpsSupport.h"
 #include "flang/Optimizer/Support/FatalError.h"
@@ -453,6 +454,7 @@ struct IntrinsicLibrary {
   fir::ExtendedValue genDotProduct(mlir::Type,
                                    llvm::ArrayRef<fir::ExtendedValue>);
   fir::ExtendedValue genEoshift(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
+  void genExit(llvm::ArrayRef<fir::ExtendedValue>);
   mlir::Value genExponent(mlir::Type, llvm::ArrayRef<mlir::Value>);
   template <Extremum, ExtremumBehavior>
   mlir::Value genExtremum(mlir::Type, llvm::ArrayRef<mlir::Value>);
@@ -652,6 +654,10 @@ static constexpr IntrinsicHandler handlers[]{
        {"boundary", asBox, handleDynamicOptional},
        {"dim", asValue}}},
      /*isElemental=*/false},
+    {"exit",
+     &I::genExit,
+     {{{"status", asValue}}},
+     /*isElemental=*/false},
     {"exponent", &I::genExponent},
     {"floor", &I::genFloor},
     {"fraction", &I::genFraction},
@@ -1981,6 +1987,22 @@ IntrinsicLibrary::genEoshift(mlir::Type resultType,
                            "unexpected result for EOSHIFT");
 }
 
+// EXIT
+void IntrinsicLibrary::genExit(llvm::ArrayRef<fir::ExtendedValue> args) {
+  assert(args.size() == 1);
+
+  mlir::Value status =
+      isAbsent(args[0])
+          ? builder.createIntegerConstant(loc, builder.getDefaultIntegerType(),
+                                          EXIT_SUCCESS)
+          : fir::getBase(args[0]);
+
+  assert(status.getType() == builder.getDefaultIntegerType() &&
+         "STATUS parameter must be an INTEGER of default kind");
+
+  fir::runtime::genExit(builder, loc, status);
+}
+
 // EXPONENT
 mlir::Value IntrinsicLibrary::genExponent(mlir::Type resultType,
                                           llvm::ArrayRef<mlir::Value> args) {

diff  --git a/flang/test/Lower/Intrinsics/exit.f90 b/flang/test/Lower/Intrinsics/exit.f90
new file mode 100644
index 0000000000000..2963c58907303
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/exit.f90
@@ -0,0 +1,23 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck --check-prefixes=CHECK,CHECK-32 -DDEFAULT_INTEGER_SIZE=32 %s
+! bbc doesn't have a way to set the default kinds so we use flang-new driver
+! RUN: flang-new -fc1 -fdefault-integer-8 -emit-fir %s -o - | FileCheck --check-prefixes=CHECK,CHECK-64 -DDEFAULT_INTEGER_SIZE=64 %s
+
+! CHECK-LABEL: func @_QPexit_test1() {
+subroutine exit_test1
+    call exit()
+  ! CHECK: %[[status:.*]] = arith.constant 0 : i[[DEFAULT_INTEGER_SIZE]]
+  ! CHECK-64: %[[statusConvert:.*]] = fir.convert %[[status]] : (i64) -> i32
+  ! CHECK-32: %{{[0-9]+}} = fir.call @_FortranAExit(%[[status]]) : (i32) -> none
+  ! CHECK-64: %{{[0-9]+}} = fir.call @_FortranAExit(%[[statusConvert]]) : (i32) -> none
+  end subroutine exit_test1
+  
+  ! CHECK-LABEL: func @_QPexit_test2(
+  ! CHECK-SAME: %[[statusArg:.*]]: !fir.ref<i[[DEFAULT_INTEGER_SIZE]]>{{.*}}) {
+  subroutine exit_test2(status)
+    integer :: status
+    call exit(status)
+  ! CHECK: %[[status:.*]] = fir.load %[[statusArg]] : !fir.ref<i[[DEFAULT_INTEGER_SIZE]]>
+  ! CHECK-64: %[[statusConv:.*]] = fir.convert %[[status]] : (i64) -> i32
+  ! CHECK-32: %{{[0-9]+}} = fir.call @_FortranAExit(%[[status]]) : (i32) -> none
+  ! CHECK-64: %{{[0-9]+}} = fir.call @_FortranAExit(%[[statusConv]]) : (i32) -> none
+  end subroutine exit_test2


        


More information about the flang-commits mailing list