[flang-commits] [PATCH] D143251: [flang][hlfir] Handle intrinsic subroutines

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Feb 3 02:12:03 PST 2023


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

The code did not propagate the result optionality for subroutine.
Make the result of genIntrinsicRef optional.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143251

Files:
  flang/lib/Lower/ConvertCall.cpp
  flang/test/Lower/HLFIR/intrinsic-subroutines.f90


Index: flang/test/Lower/HLFIR/intrinsic-subroutines.f90
===================================================================
--- /dev/null
+++ flang/test/Lower/HLFIR/intrinsic-subroutines.f90
@@ -0,0 +1,14 @@
+! Test lowering of intrinsic subroutines to HLFIR what matters here
+! is not to test each subroutine, but to check how their
+! lowering interfaces with the rest of lowering.
+! RUN: bbc -emit-fir -hlfir -o - %s | FileCheck %s
+
+subroutine test_subroutine(x)
+ real :: x
+ call cpu_time(x)
+end subroutine
+! CHECK-LABEL: func.func @_QPtest_subroutine(
+! CHECK:  %[[VAL_1:.*]]:2 = hlfir.declare %{{.*}}
+! CHECK:  %[[VAL_2:.*]] = fir.call @_FortranACpuTime() fastmath<contract> : () -> f64
+! CHECK:  %[[VAL_3:.*]] = fir.convert %[[VAL_2]] : (f64) -> f32
+! CHECK:  fir.store %[[VAL_3]] to %[[VAL_1]]#1 : !fir.ref<f32>
Index: flang/lib/Lower/ConvertCall.cpp
===================================================================
--- flang/lib/Lower/ConvertCall.cpp
+++ flang/lib/Lower/ConvertCall.cpp
@@ -1057,7 +1057,7 @@
 
 /// Lower calls to intrinsic procedures with actual arguments that have been
 /// pre-lowered but have not yet been prepared according to the interface.
-static hlfir::EntityWithAttributes genIntrinsicRefCore(
+static std::optional<hlfir::EntityWithAttributes> genIntrinsicRefCore(
     PreparedActualArguments &loweredActuals,
     const Fortran::evaluate::SpecificIntrinsic &intrinsic,
     const Fortran::lower::IntrinsicArgumentLoweringRules *argLowering,
@@ -1112,6 +1112,8 @@
   auto [resultExv, mustBeFreed] = Fortran::lower::genIntrinsicCall(
       callContext.getBuilder(), loc, intrinsic.name, scalarResultType,
       operands);
+  if (!fir::getBase(resultExv))
+    return std::nullopt;
   hlfir::EntityWithAttributes resultEntity = extendedValueToHlfirEntity(
       loc, builder, resultExv, ".tmp.intrinsic_result");
   // Move result into memory into an hlfir.expr since they are immutable from
@@ -1371,7 +1373,7 @@
 }
 
 /// Lower an intrinsic procedure reference.
-static hlfir::EntityWithAttributes
+static std::optional<hlfir::EntityWithAttributes>
 genIntrinsicRef(const Fortran::evaluate::SpecificIntrinsic &intrinsic,
                 CallContext &callContext) {
   mlir::Location loc = callContext.loc;
@@ -1413,12 +1415,12 @@
         .genElementalCall(loweredActuals, /*isImpure=*/!isFunction, callContext)
         .value();
   }
-  hlfir::EntityWithAttributes result =
+  std::optional<hlfir::EntityWithAttributes> result =
       genIntrinsicRefCore(loweredActuals, intrinsic, argLowering, callContext);
-  if (result.getType().isa<hlfir::ExprType>()) {
+  if (result && result->getType().isa<hlfir::ExprType>()) {
     fir::FirOpBuilder *bldr = &callContext.getBuilder();
     callContext.stmtCtx.attachCleanup(
-        [=]() { bldr->create<hlfir::DestroyOp>(loc, result); });
+        [=]() { bldr->create<hlfir::DestroyOp>(loc, *result); });
   }
   return result;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143251.494556.patch
Type: text/x-patch
Size: 2937 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230203/47dbc2e4/attachment-0001.bin>


More information about the flang-commits mailing list