[flang-commits] [PATCH] D134395: [flang] Improve handling of NULL pointers when folding components of named constants

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Sep 23 09:23:03 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG47c998cc2e3f: [flang] Improve handling of NULL pointers when folding components of named… (authored by klausler).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D134395/new/

https://reviews.llvm.org/D134395

Files:
  flang/include/flang/Evaluate/tools.h
  flang/lib/Evaluate/fold.cpp
  flang/lib/Evaluate/tools.cpp


Index: flang/lib/Evaluate/tools.cpp
===================================================================
--- flang/lib/Evaluate/tools.cpp
+++ flang/lib/Evaluate/tools.cpp
@@ -784,11 +784,8 @@
   }
 }
 
-bool IsBareNullPointer(const Expr<SomeType> *expr) {
-  return expr && std::holds_alternative<NullPointer>(expr->u);
-}
+// IsNullPointer() & variations
 
-// IsNullObjectPointetr, IsNullProcedurePointer(), IsNullPointer()
 template <bool IS_PROC_PTR> struct IsNullPointerHelper {
   template <typename A> bool operator()(const A &) const { return false; }
   bool operator()(const ProcedureRef &call) const {
@@ -811,6 +808,27 @@
               characteristics::Procedure::Attr::NullPointer);
     }
   }
+  template <typename T> bool operator()(const Designator<T> &x) const {
+    if (const auto *component{std::get_if<Component>(&x.u)}) {
+      if (const auto *baseSym{std::get_if<SymbolRef>(&component->base().u)}) {
+        const Symbol &base{**baseSym};
+        if (const auto *object{
+                base.detailsIf<semantics::ObjectEntityDetails>()}) {
+          // TODO: nested component and array references
+          if (IsNamedConstant(base) && object->init()) {
+            if (auto structCons{
+                    GetScalarConstantValue<SomeDerived>(*object->init())}) {
+              auto iter{structCons->values().find(component->GetLastSymbol())};
+              if (iter != structCons->values().end()) {
+                return (*this)(iter->second.value());
+              }
+            }
+          }
+        }
+      }
+    }
+    return false;
+  }
   bool operator()(const NullPointer &) const { return true; }
   template <typename T> bool operator()(const Parentheses<T> &x) const {
     return (*this)(x.left());
@@ -823,13 +841,19 @@
 bool IsNullObjectPointer(const Expr<SomeType> &expr) {
   return IsNullPointerHelper<false>{}(expr);
 }
+
 bool IsNullProcedurePointer(const Expr<SomeType> &expr) {
   return IsNullPointerHelper<true>{}(expr);
 }
+
 bool IsNullPointer(const Expr<SomeType> &expr) {
   return IsNullObjectPointer(expr) || IsNullProcedurePointer(expr);
 }
 
+bool IsBareNullPointer(const Expr<SomeType> *expr) {
+  return expr && std::holds_alternative<NullPointer>(expr->u);
+}
+
 // GetSymbolVector()
 auto GetSymbolVectorHelper::operator()(const Symbol &x) const -> Result {
   if (const auto *details{x.detailsIf<semantics::AssocEntityDetails>()}) {
Index: flang/lib/Evaluate/fold.cpp
===================================================================
--- flang/lib/Evaluate/fold.cpp
+++ flang/lib/Evaluate/fold.cpp
@@ -72,7 +72,11 @@
   for (auto &&[symbol, value] : std::move(structure)) {
     auto expr{Fold(context, std::move(value.value()))};
     if (IsPointer(symbol)) {
-      if (IsProcedure(symbol)) {
+      if (IsNullPointer(expr)) {
+        // Handle x%c when x designates a named constant of derived
+        // type and %c is NULL() in that constant.
+        expr = Expr<SomeType>{NullPointer{}};
+      } else if (IsProcedure(symbol)) {
         isConstant &= IsInitialProcedureTarget(expr);
       } else {
         isConstant &= IsInitialDataTarget(expr);
Index: flang/include/flang/Evaluate/tools.h
===================================================================
--- flang/include/flang/Evaluate/tools.h
+++ flang/include/flang/Evaluate/tools.h
@@ -938,7 +938,7 @@
 bool IsProcedure(const Expr<SomeType> &);
 bool IsFunction(const Expr<SomeType> &);
 bool IsProcedurePointerTarget(const Expr<SomeType> &);
-bool IsBareNullPointer(const Expr<SomeType> *); // NULL() w/o MOLD=
+bool IsBareNullPointer(const Expr<SomeType> *); // NULL() w/o MOLD= or type
 bool IsNullObjectPointer(const Expr<SomeType> &);
 bool IsNullProcedurePointer(const Expr<SomeType> &);
 bool IsNullPointer(const Expr<SomeType> &);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134395.462516.patch
Type: text/x-patch
Size: 3790 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220923/29fc1e92/attachment.bin>


More information about the flang-commits mailing list