[flang-commits] [flang] [flang] Avoid recursion in runtime derived type initialization (PR #102394)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Wed Aug 7 16:56:21 PDT 2024


================
@@ -0,0 +1,112 @@
+//===-- runtime/engine.h --------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef FORTRAN_RUNTIME_ENGINE_H_
+#define FORTRAN_RUNTIME_ENGINE_H_
+
+#include "derived.h"
+#include "stat.h"
+#include "terminator.h"
+#include "type-info.h"
+#include "flang/Runtime/descriptor.h"
+
+namespace Fortran::runtime::engine {
+
+class Engine;
+
+enum class Job { Initialize };
+
+struct CommonState {
+  struct Iteration {
+    bool Iterating(std::size_t iters, const Descriptor *dtor = nullptr) {
+      if (!active) {
+        if (iters > 0) {
+          active = true;
+          at = 0;
+          n = iters;
+          descriptor = dtor;
+          if (descriptor) {
+            descriptor->GetLowerBounds(subscripts);
+          }
+        }
+      } else if (resuming) {
+        resuming = false;
+      } else if (++at < n) {
+        if (descriptor) {
+          descriptor->IncrementSubscripts(subscripts);
+        }
+      } else {
+        active = false;
+      }
+      return active;
+    }
+    void ResumeAtSameIteration() { resuming = true; }
+
+    bool active{false}, resuming{false};
+    std::size_t at, n;
+    const Descriptor *descriptor;
+    SubscriptValue subscripts[maxRank];
+  };
+
+  const Descriptor &instance_;
+  const typeInfo::DerivedType *derived_;
+  const Descriptor *componentDesc_;
+  std::size_t elements_, components_;
+  Iteration element_, component_;
+  StaticDescriptor<maxRank, true, 8> staticDescriptor_;
----------------
vzakhari wrote:

Do we really need to reserve for 8 length parameters?
We can also probably move this to `Initialization` class.  I see that at least one static descriptor is used in all recursive derived types processing functions currently, but it still looks like the job-specific property.  E.g. if I use this for `CopyElement` I will need two static descriptors in the state.

https://github.com/llvm/llvm-project/pull/102394


More information about the flang-commits mailing list