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

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Mon Aug 19 13:45:40 PDT 2024


================
@@ -0,0 +1,169 @@
+//===-- runtime/engine.h ---------------------------------------*- C++ -*- ===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// Implements a work engine for restartable tasks iterating over elements,
+// components, &c. of arrays and derived types.  Avoids recursion and
+// function pointers.
+
+#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;
+
+// Every task object derives from Task.
+struct Task {
+
+  enum class ResultType { ResultValue /*doesn't matter*/ };
+
+  Task(const Descriptor &instance, const typeInfo::DerivedType *derived)
+      : instance_{instance}, derived_{derived} {}
+
+  struct Iteration {
+    RT_API_ATTRS 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;
+    }
+    // Call on all Iteration instances before calling Engine::Begin()
+    // when they should not advance when the job is resumed.
+    RT_API_ATTRS 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_;
+  int phase_{0};
----------------
vzakhari wrote:

nit: move this to `Finalization` class?

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


More information about the flang-commits mailing list