[llvm-branch-commits] [flang] [Flang] Split runtime headers in preparation for cross-compilation. NFC. (PR #112188)

Michael Kruse via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Oct 14 07:54:19 PDT 2024


https://github.com/Meinersbur updated https://github.com/llvm/llvm-project/pull/112188

>From 42e5abb5b291e78aeb152d7c636c75fe4d90492a Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Mon, 14 Oct 2024 13:55:16 +0200
Subject: [PATCH 1/2] Split headers in preparation for cross-compilation. NFC.

---
 flang/include/flang/Lower/Allocatable.h       |  2 +-
 .../flang/Optimizer/Builder/MutableBox.h      |  2 +-
 .../flang/Optimizer/CodeGen/DescriptorModel.h |  2 +-
 flang/include/flang/Runtime/CUDA/allocator.h  |  2 +-
 flang/include/flang/Runtime/CUDA/descriptor.h |  2 +-
 .../flang/Runtime/allocator-registry-consts.h | 20 +++++
 .../flang/Runtime/allocator-registry.h        |  9 +--
 .../flang/Runtime/array-constructor-consts.h  | 54 +++++++++++++
 .../include/flang/Runtime/array-constructor.h | 80 +++----------------
 .../include/flang/Runtime/descriptor-consts.h | 72 +++++++++++++++++
 flang/include/flang/Runtime/descriptor.h      | 15 +---
 flang/include/flang/Runtime/io-api-funcs.h    | 39 +++++++++
 flang/include/flang/Runtime/io-api.h          |  3 -
 flang/include/flang/Runtime/iostat-funcs.h    | 23 ++++++
 flang/include/flang/Runtime/iostat.h          |  2 -
 flang/lib/Lower/ConvertVariable.cpp           |  2 +-
 .../Builder/Runtime/ArrayConstructor.cpp      |  6 +-
 flang/lib/Optimizer/CodeGen/CodeGen.cpp       | 18 ++---
 flang/lib/Semantics/compute-offsets.cpp       |  8 +-
 flang/runtime/environment-default-list.h      |  0
 flang/runtime/extensions.cpp                  |  2 +-
 flang/runtime/internal-unit.cpp               |  2 +
 flang/runtime/io-api-common.h                 |  2 +-
 flang/runtime/io-api-minimal.cpp              |  2 +-
 flang/runtime/io-api.cpp                      |  2 +-
 flang/runtime/io-error.h                      |  2 +-
 flang/runtime/io-stmt.h                       |  2 +-
 flang/runtime/iostat.cpp                      |  2 +-
 flang/runtime/namelist.cpp                    |  2 +-
 .../Builder/Runtime/AllocatableTest.cpp       |  2 +-
 30 files changed, 254 insertions(+), 127 deletions(-)
 create mode 100644 flang/include/flang/Runtime/allocator-registry-consts.h
 create mode 100644 flang/include/flang/Runtime/array-constructor-consts.h
 create mode 100644 flang/include/flang/Runtime/descriptor-consts.h
 create mode 100644 flang/include/flang/Runtime/io-api-funcs.h
 create mode 100644 flang/include/flang/Runtime/iostat-funcs.h
 mode change 100755 => 100644 flang/runtime/environment-default-list.h

diff --git a/flang/include/flang/Lower/Allocatable.h b/flang/include/flang/Lower/Allocatable.h
index 1209b157ed1f41..0e89af94af40f6 100644
--- a/flang/include/flang/Lower/Allocatable.h
+++ b/flang/include/flang/Lower/Allocatable.h
@@ -15,7 +15,7 @@
 
 #include "flang/Lower/AbstractConverter.h"
 #include "flang/Optimizer/Builder/MutableBox.h"
-#include "flang/Runtime/allocator-registry.h"
+#include "flang/Runtime/allocator-registry-consts.h"
 #include "llvm/ADT/StringRef.h"
 
 namespace mlir {
diff --git a/flang/include/flang/Optimizer/Builder/MutableBox.h b/flang/include/flang/Optimizer/Builder/MutableBox.h
index fea7c7204837b4..39657ddaf6e03a 100644
--- a/flang/include/flang/Optimizer/Builder/MutableBox.h
+++ b/flang/include/flang/Optimizer/Builder/MutableBox.h
@@ -14,7 +14,7 @@
 #define FORTRAN_OPTIMIZER_BUILDER_MUTABLEBOX_H
 
 #include "flang/Optimizer/Builder/BoxValue.h"
-#include "flang/Runtime/allocator-registry.h"
+#include "flang/Runtime/allocator-registry-consts.h"
 #include "llvm/ADT/StringRef.h"
 
 namespace mlir {
diff --git a/flang/include/flang/Optimizer/CodeGen/DescriptorModel.h b/flang/include/flang/Optimizer/CodeGen/DescriptorModel.h
index ff0cf29e8073e6..9cccf8db87270e 100644
--- a/flang/include/flang/Optimizer/CodeGen/DescriptorModel.h
+++ b/flang/include/flang/Optimizer/CodeGen/DescriptorModel.h
@@ -23,7 +23,7 @@
 #define OPTIMIZER_DESCRIPTOR_MODEL_H
 
 #include "flang/ISO_Fortran_binding_wrapper.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang/Runtime/descriptor-consts.h"
 #include "mlir/Dialect/LLVMIR/LLVMTypes.h"
 #include "mlir/IR/BuiltinTypes.h"
 #include "llvm/Support/ErrorHandling.h"
diff --git a/flang/include/flang/Runtime/CUDA/allocator.h b/flang/include/flang/Runtime/CUDA/allocator.h
index 4527c9f18fa054..cc88896b1f0bd6 100644
--- a/flang/include/flang/Runtime/CUDA/allocator.h
+++ b/flang/include/flang/Runtime/CUDA/allocator.h
@@ -9,7 +9,7 @@
 #ifndef FORTRAN_RUNTIME_CUDA_ALLOCATOR_H_
 #define FORTRAN_RUNTIME_CUDA_ALLOCATOR_H_
 
-#include "flang/Runtime/descriptor.h"
+#include "flang/Runtime/descriptor-consts.h"
 #include "flang/Runtime/entry-names.h"
 
 #define CUDA_REPORT_IF_ERROR(expr) \
diff --git a/flang/include/flang/Runtime/CUDA/descriptor.h b/flang/include/flang/Runtime/CUDA/descriptor.h
index d593989420420f..501a834f43bd42 100644
--- a/flang/include/flang/Runtime/CUDA/descriptor.h
+++ b/flang/include/flang/Runtime/CUDA/descriptor.h
@@ -9,7 +9,7 @@
 #ifndef FORTRAN_RUNTIME_CUDA_DESCRIPTOR_H_
 #define FORTRAN_RUNTIME_CUDA_DESCRIPTOR_H_
 
-#include "flang/Runtime/descriptor.h"
+#include "flang/Runtime/descriptor-consts.h"
 #include "flang/Runtime/entry-names.h"
 #include <cstddef>
 
diff --git a/flang/include/flang/Runtime/allocator-registry-consts.h b/flang/include/flang/Runtime/allocator-registry-consts.h
new file mode 100644
index 00000000000000..70735c2fc7a71c
--- /dev/null
+++ b/flang/include/flang/Runtime/allocator-registry-consts.h
@@ -0,0 +1,20 @@
+//===-- include/flang/Runtime/allocator-registry-consts.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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef FORTRAN_RUNTIME_ALLOCATOR_REGISTRY_CONSTS_H_
+#define FORTRAN_RUNTIME_ALLOCATOR_REGISTRY_CONSTS_H_
+
+static constexpr unsigned kDefaultAllocator = 0;
+
+// Allocator used for CUF
+static constexpr unsigned kPinnedAllocatorPos = 1;
+static constexpr unsigned kDeviceAllocatorPos = 2;
+static constexpr unsigned kManagedAllocatorPos = 3;
+static constexpr unsigned kUnifiedAllocatorPos = 4;
+
+#endif /* FORTRAN_RUNTIME_ALLOCATOR_REGISTRY_CONSTS_H_ */
diff --git a/flang/include/flang/Runtime/allocator-registry.h b/flang/include/flang/Runtime/allocator-registry.h
index acfada506fafc6..c5db7b2b1f2014 100644
--- a/flang/include/flang/Runtime/allocator-registry.h
+++ b/flang/include/flang/Runtime/allocator-registry.h
@@ -10,17 +10,10 @@
 #define FORTRAN_RUNTIME_ALLOCATOR_H_
 
 #include "flang/Common/api-attrs.h"
+#include "flang/Runtime/allocator-registry-consts.h"
 #include <cstdlib>
 #include <vector>
 
-static constexpr unsigned kDefaultAllocator = 0;
-
-// Allocator used for CUF
-static constexpr unsigned kPinnedAllocatorPos = 1;
-static constexpr unsigned kDeviceAllocatorPos = 2;
-static constexpr unsigned kManagedAllocatorPos = 3;
-static constexpr unsigned kUnifiedAllocatorPos = 4;
-
 #define MAX_ALLOCATOR 7 // 3 bits are reserved in the descriptor.
 
 namespace Fortran::runtime {
diff --git a/flang/include/flang/Runtime/array-constructor-consts.h b/flang/include/flang/Runtime/array-constructor-consts.h
new file mode 100644
index 00000000000000..da058874e50487
--- /dev/null
+++ b/flang/include/flang/Runtime/array-constructor-consts.h
@@ -0,0 +1,54 @@
+//===-- include/flang/Runtime/array-constructor-consts.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
+//
+//===----------------------------------------------------------------------===//
+
+// External APIs to create temporary storage for array constructors when their
+// final extents or length parameters cannot be pre-computed.
+
+#ifndef FORTRAN_RUNTIME_ARRAY_CONSTRUCTOR_CONSTS_H_
+#define FORTRAN_RUNTIME_ARRAY_CONSTRUCTOR_CONSTS_H_
+
+#include "flang/Runtime/descriptor-consts.h"
+#include "flang/Runtime/entry-names.h"
+#include <cstdint>
+
+namespace Fortran::runtime {
+struct ArrayConstructorVector;
+
+// Max sizeof(ArrayConstructorVector) and sizeof(ArrayConstructorVector) for any
+// target.
+// TODO: Use target-specific size/alignment instead of overapproximation.
+constexpr std::size_t MaxArrayConstructorVectorSizeInBytes = 2 * 40;
+constexpr std::size_t MaxArrayConstructorVectorAlignInBytes = 8;
+
+extern "C" {
+// API to initialize an ArrayConstructorVector before any values are pushed to
+// it. Inlined code is only expected to allocate the "ArrayConstructorVector"
+// class instance storage with sufficient size (using
+// "2*sizeof(ArrayConstructorVector)" on the host should be safe regardless of
+// the target the runtime is compiled for). This avoids the need for the runtime
+// to maintain a state, or to use dynamic allocation for it. "vectorClassSize"
+// is used to validate that lowering allocated enough space for it.
+void RTDECL(InitArrayConstructorVector)(ArrayConstructorVector &vector,
+    Descriptor &to, bool useValueLengthParameters, int vectorClassSize,
+    const char *sourceFile = nullptr, int sourceLine = 0);
+
+// Generic API to push any kind of entity into the array constructor (any
+// Fortran type and any rank).
+void RTDECL(PushArrayConstructorValue)(
+    ArrayConstructorVector &vector, const Descriptor &from);
+
+// API to push scalar array constructor value of:
+//   - a numerical or logical type,
+//   - or a derived type that has no length parameters, and no allocatable
+//   component (that would require deep copies).
+// It requires no descriptor for the value that is passed via its base address.
+void RTDECL(PushArrayConstructorSimpleScalar)(
+    ArrayConstructorVector &vector, void *from);
+} // extern "C"
+} // namespace Fortran::runtime
+#endif /* FORTRAN_RUNTIME_ARRAY_CONSTRUCTOR_CONSTS_H_ */
diff --git a/flang/include/flang/Runtime/array-constructor.h b/flang/include/flang/Runtime/array-constructor.h
index 46fc0418c7991e..c068af9ba120c0 100644
--- a/flang/include/flang/Runtime/array-constructor.h
+++ b/flang/include/flang/Runtime/array-constructor.h
@@ -12,6 +12,7 @@
 #ifndef FORTRAN_RUNTIME_ARRAYCONSTRUCTOR_H_
 #define FORTRAN_RUNTIME_ARRAYCONSTRUCTOR_H_
 
+#include "flang/Runtime/array-constructor-consts.h"
 #include "flang/Runtime/descriptor.h"
 #include "flang/Runtime/entry-names.h"
 #include <cstdint>
@@ -42,77 +43,14 @@ struct ArrayConstructorVector {
 private:
   unsigned char useValueLengthParameters_ : 1;
 };
+static_assert(sizeof(Fortran::runtime::ArrayConstructorVector) <=
+        MaxArrayConstructorVectorSizeInBytes,
+    "ABI requires sizeof(ArrayConstructorVector) to be smaller than "
+    "MaxArrayConstructorVectorSizeInBytes");
+static_assert(alignof(Fortran::runtime::ArrayConstructorVector) <=
+        MaxArrayConstructorVectorAlignInBytes,
+    "ABI requires alignof(ArrayConstructorVector) to be smaller than "
+    "MaxArrayConstructorVectorAlignInBytes");
 
-// This file defines an API to "push" an evaluated array constructor value
-// "from" into some storage "to" of an array constructor. It can be seen as a
-// form of std::vector::push_back() implementation for Fortran array
-// constructors. In the APIs and ArrayConstructorVector struct above:
-//
-// - "to" is a ranked-1 descriptor whose declared type is already set to the
-// array constructor derived type. It may be already allocated, even before the
-// first call to this API, or it may be unallocated. "to" extent is increased
-// every time a "from" is pushed past its current extent. At this end of the
-// API calls, its extent is the extent of the array constructor. If "to" is
-// unallocated and its extent is not null, it is assumed this is the final array
-// constructor extent value, and the first allocation already "reserves" storage
-// space accordingly to avoid reallocations.
-//  - "from" is a scalar or array descriptor for the evaluated array
-//  constructor value that must be copied into the storage of "to" at
-//  "nextValuePosition".
-//  - "useValueLengthParameters" must be set to true if the array constructor
-//  has length parameters and no type spec. If it is true and "to" is
-//  unallocated, "to" will take the length parameters of "from". If it is true
-//  and "to" is an allocated character array constructor, it will be checked
-//  that "from" length matches the one from "to". When it is false, the
-//  character length must already be set in "to" before the first call to this
-//  API and "from" character lengths are allowed to mismatch from "to".
-// - "nextValuePosition" is the zero based sequence position of "from" in the
-// array constructor. It is updated after this call by the number of "from"
-// elements. It should be set to zero by the caller of this API before the first
-// call.
-// - "actualAllocationSize" is the current allocation size of "to" storage. It
-// may be bigger than "to" extent for reallocation optimization purposes, but
-// should never be smaller, unless this is the first call and "to" is
-// unallocated. It is updated by the runtime after each successful allocation or
-// reallocation. It should be set to "to" extent if "to" is allocated before the
-// first call of this API, and can be left undefined otherwise.
-//
-// Note that this API can be used with "to" being a variable (that can be
-// discontiguous). This can be done when the variable is the left hand side of
-// an assignment from an array constructor as long as:
-//  - none of the ac-value overlaps with the variable,
-//  - this is an intrinsic assignment that is not a whole allocatable
-//  assignment, *and* for a type that has no components requiring user defined
-//  assignments,
-//  - the variable is properly finalized before using this API if its need to,
-//  - "useValueLengthParameters" should be set to false in this case, even if
-//  the array constructor has no type-spec, since the variable may have a
-//  different character length than the array constructor values.
-
-extern "C" {
-// API to initialize an ArrayConstructorVector before any values are pushed to
-// it. Inlined code is only expected to allocate the "ArrayConstructorVector"
-// class instance storage with sufficient size (using
-// "2*sizeof(ArrayConstructorVector)" on the host should be safe regardless of
-// the target the runtime is compiled for). This avoids the need for the runtime
-// to maintain a state, or to use dynamic allocation for it. "vectorClassSize"
-// is used to validate that lowering allocated enough space for it.
-void RTDECL(InitArrayConstructorVector)(ArrayConstructorVector &vector,
-    Descriptor &to, bool useValueLengthParameters, int vectorClassSize,
-    const char *sourceFile = nullptr, int sourceLine = 0);
-
-// Generic API to push any kind of entity into the array constructor (any
-// Fortran type and any rank).
-void RTDECL(PushArrayConstructorValue)(
-    ArrayConstructorVector &vector, const Descriptor &from);
-
-// API to push scalar array constructor value of:
-//   - a numerical or logical type,
-//   - or a derived type that has no length parameters, and no allocatable
-//   component (that would require deep copies).
-// It requires no descriptor for the value that is passed via its base address.
-void RTDECL(PushArrayConstructorSimpleScalar)(
-    ArrayConstructorVector &vector, void *from);
-} // extern "C"
 } // namespace Fortran::runtime
 #endif // FORTRAN_RUNTIME_ARRAYCONSTRUCTOR_H_
diff --git a/flang/include/flang/Runtime/descriptor-consts.h b/flang/include/flang/Runtime/descriptor-consts.h
new file mode 100644
index 00000000000000..098eb59ca01b2e
--- /dev/null
+++ b/flang/include/flang/Runtime/descriptor-consts.h
@@ -0,0 +1,72 @@
+//===-- include/flang/Runtime/descriptor-consts.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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef FORTRAN_RUNTIME_DESCRIPTOR_CONSTS_H_
+#define FORTRAN_RUNTIME_DESCRIPTOR_CONSTS_H_
+
+#include "flang/Common/api-attrs.h"
+#include "flang/ISO_Fortran_binding_wrapper.h"
+#include <cstddef>
+#include <cstdint>
+
+// Value of the addendum presence flag.
+#define _CFI_ADDENDUM_FLAG 1
+// Number of bits needed to be shifted when manipulating the allocator index.
+#define _CFI_ALLOCATOR_IDX_SHIFT 1
+// Allocator index mask.
+#define _CFI_ALLOCATOR_IDX_MASK 0b00001110
+
+namespace Fortran::runtime::typeInfo {
+using TypeParameterValue = std::int64_t;
+class DerivedType;
+} // namespace Fortran::runtime::typeInfo
+
+namespace Fortran::runtime {
+class Descriptor;
+
+/// Returns size in bytes of the descriptor (not the data)
+/// This must be at least as large as the largest descriptor of any target
+/// triple.
+static constexpr RT_API_ATTRS std::size_t MaxDescriptorSizeInBytes(
+    int rank, bool addendum = false, int lengthTypeParameters = 0) {
+  // Layout:
+  //
+  // fortran::runtime::Descriptor {
+  //   ISO::CFI_cdesc_t {
+  //     void *base_addr;           (pointer -> up to 8 bytes)
+  //     size_t elem_len;           (up to 8 bytes)
+  //     int version;               (up to 4 bytes)
+  //     CFI_rank_t rank;           (unsigned char -> 1 byte)
+  //     CFI_type_t type;           (signed char -> 1 byte)
+  //     CFI_attribute_t attribute; (unsigned char -> 1 byte)
+  //     unsigned char extra;       (1 byte)
+  //   }
+  // }
+  // fortran::runtime::Dimension[rank] {
+  //   ISO::CFI_dim_t {
+  //     CFI_index_t lower_bound; (ptrdiff_t -> up to 8 bytes)
+  //     CFI_index_t extent;      (ptrdiff_t -> up to 8 bytes)
+  //     CFI_index_t sm;          (ptrdiff_t -> up to 8 bytes)
+  //   }
+  // }
+  // fortran::runtime::DescriptorAddendum {
+  //   const typeInfo::DerivedType *derivedType_;        (pointer -> up to 8
+  //   bytes) typeInfo::TypeParameterValue len_[lenParameters]; (int64_t -> 8
+  //   bytes)
+  // }
+  std::size_t bytes{24u + rank * 24u};
+  if (addendum || lengthTypeParameters > 0) {
+    if (lengthTypeParameters < 1)
+      lengthTypeParameters = 1;
+    bytes += 8u + static_cast<std::size_t>(lengthTypeParameters) * 8u;
+  }
+  return bytes;
+}
+
+} // namespace Fortran::runtime
+#endif /* FORTRAN_RUNTIME_DESCRIPTOR_CONSTS_H_ */
diff --git a/flang/include/flang/Runtime/descriptor.h b/flang/include/flang/Runtime/descriptor.h
index 030d0c1031fbaa..6d0f09c79508cb 100644
--- a/flang/include/flang/Runtime/descriptor.h
+++ b/flang/include/flang/Runtime/descriptor.h
@@ -19,6 +19,7 @@
 // but should never reference this internal header.
 
 #include "flang/ISO_Fortran_binding_wrapper.h"
+#include "flang/Runtime/descriptor-consts.h"
 #include "flang/Runtime/memory.h"
 #include "flang/Runtime/type-code.h"
 #include <algorithm>
@@ -28,11 +29,6 @@
 #include <cstdio>
 #include <cstring>
 
-namespace Fortran::runtime::typeInfo {
-using TypeParameterValue = std::int64_t;
-class DerivedType;
-} // namespace Fortran::runtime::typeInfo
-
 namespace Fortran::runtime {
 
 using SubscriptValue = ISO::CFI_index_t;
@@ -420,13 +416,6 @@ class Descriptor {
 
   void Dump(FILE * = stdout) const;
 
-// Value of the addendum presence flag.
-#define _CFI_ADDENDUM_FLAG 1
-// Number of bits needed to be shifted when manipulating the allocator index.
-#define _CFI_ALLOCATOR_IDX_SHIFT 1
-// Allocator index mask.
-#define _CFI_ALLOCATOR_IDX_MASK 0b00001110
-
   RT_API_ATTRS inline bool HasAddendum() const {
     return raw_.extra & _CFI_ADDENDUM_FLAG;
   }
@@ -464,6 +453,8 @@ class alignas(Descriptor) StaticDescriptor {
   static constexpr bool hasAddendum{ADDENDUM || MAX_LEN_PARMS > 0};
   static constexpr std::size_t byteSize{
       Descriptor::SizeInBytes(maxRank, hasAddendum, maxLengthTypeParameters)};
+  static_assert(byteSize <=
+      MaxDescriptorSizeInBytes(maxRank, hasAddendum, maxLengthTypeParameters));
   RT_OFFLOAD_VAR_GROUP_END
 
   RT_API_ATTRS Descriptor &descriptor() {
diff --git a/flang/include/flang/Runtime/io-api-funcs.h b/flang/include/flang/Runtime/io-api-funcs.h
new file mode 100644
index 00000000000000..e81c3cb8156c4e
--- /dev/null
+++ b/flang/include/flang/Runtime/io-api-funcs.h
@@ -0,0 +1,39 @@
+//===-- include/flang/Runtime/io-api-funcs.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
+//
+//===----------------------------------------------------------------------===//
+
+// Defines API between compiled code and I/O runtime library.
+
+#ifndef FORTRAN_RUNTIME_IO_API_FUNCS_H_
+#define FORTRAN_RUNTIME_IO_API_FUNCS_H_
+
+#include "flang/Common/uint128.h"
+#include "flang/Runtime/entry-names.h"
+#include "flang/Runtime/io-api.h"
+#include "flang/Runtime/iostat.h"
+#include "flang/Runtime/magic-numbers.h"
+#include <cinttypes>
+#include <cstddef>
+
+namespace Fortran::runtime {
+class Descriptor;
+} // namespace Fortran::runtime
+
+namespace Fortran::runtime::io {
+
+struct NonTbpDefinedIoTable;
+class NamelistGroup;
+class IoStatementState;
+using Cookie = IoStatementState *;
+using ExternalUnit = int;
+using AsynchronousId = int;
+
+RT_API_ATTRS const char *InquiryKeywordHashDecode(
+    char *buffer, std::size_t, InquiryKeywordHash);
+
+} // namespace Fortran::runtime::io
+#endif /* FORTRAN_RUNTIME_IO_API_FUNCS_H_ */
diff --git a/flang/include/flang/Runtime/io-api.h b/flang/include/flang/Runtime/io-api.h
index 328afc715a3f1e..e588b46dc4b3a1 100644
--- a/flang/include/flang/Runtime/io-api.h
+++ b/flang/include/flang/Runtime/io-api.h
@@ -51,9 +51,6 @@ constexpr InquiryKeywordHash HashInquiryKeyword(const char *p) {
   return hash;
 }
 
-RT_API_ATTRS const char *InquiryKeywordHashDecode(
-    char *buffer, std::size_t, InquiryKeywordHash);
-
 extern "C" {
 
 #define IONAME(name) RTNAME(io##name)
diff --git a/flang/include/flang/Runtime/iostat-funcs.h b/flang/include/flang/Runtime/iostat-funcs.h
new file mode 100644
index 00000000000000..3ab4527ea829ae
--- /dev/null
+++ b/flang/include/flang/Runtime/iostat-funcs.h
@@ -0,0 +1,23 @@
+//===-- include/flang/Runtime/iostat-funcs.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
+//
+//===----------------------------------------------------------------------===//
+
+// Defines the values returned by the runtime for IOSTAT= specifiers
+// on I/O statements.
+
+#ifndef FORTRAN_RUNTIME_IOSTAT_FUNCS_H_
+#define FORTRAN_RUNTIME_IOSTAT_FUNCS_H_
+
+#include "flang/Common/api-attrs.h"
+#include "flang/Runtime/iostat.h"
+
+namespace Fortran::runtime::io {
+
+RT_API_ATTRS const char *IostatErrorString(int);
+
+} // namespace Fortran::runtime::io
+#endif /* FORTRAN_RUNTIME_IOSTAT_FUNCS_H_ */
diff --git a/flang/include/flang/Runtime/iostat.h b/flang/include/flang/Runtime/iostat.h
index 6ce7c82b424eb7..0565718f6c103d 100644
--- a/flang/include/flang/Runtime/iostat.h
+++ b/flang/include/flang/Runtime/iostat.h
@@ -89,7 +89,5 @@ enum Iostat {
   IostatNonExternalDefinedUnformattedIo,
 };
 
-RT_API_ATTRS const char *IostatErrorString(int);
-
 } // namespace Fortran::runtime::io
 #endif // FORTRAN_RUNTIME_IOSTAT_H_
diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index f76d44f5479d32..82bedca7bd41dd 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -39,7 +39,7 @@
 #include "flang/Optimizer/Support/FatalError.h"
 #include "flang/Optimizer/Support/InternalNames.h"
 #include "flang/Optimizer/Support/Utils.h"
-#include "flang/Runtime/allocator-registry.h"
+#include "flang/Runtime/allocator-registry-consts.h"
 #include "flang/Semantics/runtime-type-info.h"
 #include "flang/Semantics/tools.h"
 #include "llvm/Support/CommandLine.h"
diff --git a/flang/lib/Optimizer/Builder/Runtime/ArrayConstructor.cpp b/flang/lib/Optimizer/Builder/Runtime/ArrayConstructor.cpp
index c786bef5cb1c4c..b7c6dc5fb2bf6c 100644
--- a/flang/lib/Optimizer/Builder/Runtime/ArrayConstructor.cpp
+++ b/flang/lib/Optimizer/Builder/Runtime/ArrayConstructor.cpp
@@ -9,7 +9,7 @@
 #include "flang/Optimizer/Builder/Runtime/ArrayConstructor.h"
 #include "flang/Optimizer/Builder/FIRBuilder.h"
 #include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
-#include "flang/Runtime/array-constructor.h"
+#include "flang/Runtime/array-constructor-consts.h"
 
 using namespace Fortran::runtime;
 
@@ -29,8 +29,8 @@ mlir::Value fir::runtime::genInitArrayConstructorVector(
   // the target. The "cookieSize" argument is used to validate this wild
   // assumption until runtime interfaces are improved.
   std::size_t arrayVectorStructBitSize =
-      2 * sizeof(Fortran::runtime::ArrayConstructorVector) * 8;
-  std::size_t alignLike = alignof(Fortran::runtime::ArrayConstructorVector) * 8;
+      MaxArrayConstructorVectorSizeInBytes * 8;
+  std::size_t alignLike = MaxArrayConstructorVectorAlignInBytes * 8;
   fir::SequenceType::Extent numElem =
       (arrayVectorStructBitSize + alignLike - 1) / alignLike;
   mlir::Type intType = builder.getIntegerType(alignLike);
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index 88293bcf36a780..eed389380f7cca 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -23,8 +23,8 @@
 #include "flang/Optimizer/Support/InternalNames.h"
 #include "flang/Optimizer/Support/TypeCode.h"
 #include "flang/Optimizer/Support/Utils.h"
-#include "flang/Runtime/allocator-registry.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang/Runtime/allocator-registry-consts.h"
+#include "flang/Runtime/descriptor-consts.h"
 #include "flang/Semantics/runtime-type-info.h"
 #include "mlir/Conversion/ArithCommon/AttrToLLVMConverter.h"
 #include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
@@ -1273,16 +1273,12 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> {
           insertField(rewriter, loc, descriptor, {kExtraPosInBox}, extraField);
     } else {
       // Compute the value of the extra field based on allocator_idx and
-      // addendum present using a Descriptor object.
-      Fortran::runtime::StaticDescriptor staticDescriptor;
-      Fortran::runtime::Descriptor &desc{staticDescriptor.descriptor()};
-      desc.raw().extra = 0;
-      desc.SetAllocIdx(allocatorIdx);
+      // addendum present.
+      unsigned extra = allocatorIdx << _CFI_ALLOCATOR_IDX_SHIFT;
       if (hasAddendum)
-        desc.SetHasAddendum();
-      descriptor =
-          insertField(rewriter, loc, descriptor, {kExtraPosInBox},
-                      this->genI32Constant(loc, rewriter, desc.raw().extra));
+        extra |= _CFI_ADDENDUM_FLAG;
+      descriptor = insertField(rewriter, loc, descriptor, {kExtraPosInBox},
+                               this->genI32Constant(loc, rewriter, extra));
     }
 
     if (hasAddendum) {
diff --git a/flang/lib/Semantics/compute-offsets.cpp b/flang/lib/Semantics/compute-offsets.cpp
index b5a58ddca0ecdd..c5774ed30de748 100644
--- a/flang/lib/Semantics/compute-offsets.cpp
+++ b/flang/lib/Semantics/compute-offsets.cpp
@@ -11,7 +11,7 @@
 #include "flang/Evaluate/fold.h"
 #include "flang/Evaluate/shape.h"
 #include "flang/Evaluate/type.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang/Runtime/descriptor-consts.h"
 #include "flang/Semantics/scope.h"
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/symbol.h"
@@ -341,8 +341,12 @@ auto ComputeOffsetsHelper::GetSizeAndAlignment(
     const auto *derived{evaluate::GetDerivedTypeSpec(dyType)};
     int lenParams{derived ? CountLenParameters(*derived) : 0};
     bool needAddendum{derived || (dyType && dyType->IsUnlimitedPolymorphic())};
-    std::size_t size{runtime::Descriptor::SizeInBytes(
+
+    // FIXME: Get descriptor size from targetCharacteristics instead
+    // overapproximation
+    std::size_t size{runtime::MaxDescriptorSizeInBytes(
         symbol.Rank(), needAddendum, lenParams)};
+
     return {size, targetCharacteristics.descriptorAlignment()};
   }
   if (IsProcedurePointer(symbol)) {
diff --git a/flang/runtime/environment-default-list.h b/flang/runtime/environment-default-list.h
old mode 100755
new mode 100644
diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index be3833db88b07a..8cd5f3ce696b49 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -15,7 +15,7 @@
 #include "flang/Runtime/command.h"
 #include "flang/Runtime/descriptor.h"
 #include "flang/Runtime/entry-names.h"
-#include "flang/Runtime/io-api.h"
+#include "flang/Runtime/io-api-funcs.h"
 #include <chrono>
 #include <cstring>
 #include <ctime>
diff --git a/flang/runtime/internal-unit.cpp b/flang/runtime/internal-unit.cpp
index f28700ee015815..f8f3877efb20ea 100644
--- a/flang/runtime/internal-unit.cpp
+++ b/flang/runtime/internal-unit.cpp
@@ -36,6 +36,8 @@ RT_API_ATTRS InternalDescriptorUnit<DIR>::InternalDescriptorUnit(
   Descriptor &d{descriptor()};
   RUNTIME_CHECK(
       terminator, that.SizeInBytes() <= d.SizeInBytes(maxRank, true, 0));
+  RUNTIME_CHECK(terminator,
+      that.SizeInBytes() <= MaxDescriptorSizeInBytes(maxRank, true, 0));
   new (&d) Descriptor{that};
   d.Check();
   internalIoCharKind = thatType->second;
diff --git a/flang/runtime/io-api-common.h b/flang/runtime/io-api-common.h
index c7b86cab73a525..b4ac9a61c09ec3 100644
--- a/flang/runtime/io-api-common.h
+++ b/flang/runtime/io-api-common.h
@@ -14,7 +14,7 @@
 #include "unit.h"
 #include "flang/Common/api-attrs.h"
 #include "flang/Common/optional.h"
-#include "flang/Runtime/io-api.h"
+#include "flang/Runtime/io-api-funcs.h"
 
 namespace Fortran::runtime::io {
 
diff --git a/flang/runtime/io-api-minimal.cpp b/flang/runtime/io-api-minimal.cpp
index ad76fe3de0324c..82376cf9641664 100644
--- a/flang/runtime/io-api-minimal.cpp
+++ b/flang/runtime/io-api-minimal.cpp
@@ -16,7 +16,7 @@
 #include "terminator.h"
 #include "tools.h"
 #include "unit.h"
-#include "flang/Runtime/io-api.h"
+#include "flang/Runtime/io-api-funcs.h"
 
 namespace Fortran::runtime::io {
 RT_EXT_API_GROUP_BEGIN
diff --git a/flang/runtime/io-api.cpp b/flang/runtime/io-api.cpp
index e3c6b9e5ca8959..0fe69c981d17c7 100644
--- a/flang/runtime/io-api.cpp
+++ b/flang/runtime/io-api.cpp
@@ -13,7 +13,6 @@
 // OutputReal{32,64}, OutputComplex{32,64}, OutputAscii, & EndIoStatement()
 // are in runtime/io-api-minimal.cpp.
 
-#include "flang/Runtime/io-api.h"
 #include "descriptor-io.h"
 #include "edit-input.h"
 #include "edit-output.h"
@@ -26,6 +25,7 @@
 #include "unit.h"
 #include "flang/Common/optional.h"
 #include "flang/Runtime/descriptor.h"
+#include "flang/Runtime/io-api-funcs.h"
 #include "flang/Runtime/memory.h"
 #include <cstdlib>
 #include <memory>
diff --git a/flang/runtime/io-error.h b/flang/runtime/io-error.h
index 426573e2faf00c..22e43db7102882 100644
--- a/flang/runtime/io-error.h
+++ b/flang/runtime/io-error.h
@@ -16,7 +16,7 @@
 #define FORTRAN_RUNTIME_IO_ERROR_H_
 
 #include "terminator.h"
-#include "flang/Runtime/iostat.h"
+#include "flang/Runtime/iostat-funcs.h"
 #include "flang/Runtime/memory.h"
 #include <cinttypes>
 
diff --git a/flang/runtime/io-stmt.h b/flang/runtime/io-stmt.h
index 2e0ca46078ecdc..30d7bd86c54b37 100644
--- a/flang/runtime/io-stmt.h
+++ b/flang/runtime/io-stmt.h
@@ -20,7 +20,7 @@
 #include "flang/Common/reference-wrapper.h"
 #include "flang/Common/visit.h"
 #include "flang/Runtime/descriptor.h"
-#include "flang/Runtime/io-api.h"
+#include "flang/Runtime/io-api-funcs.h"
 #include <flang/Common/variant.h>
 #include <functional>
 #include <type_traits>
diff --git a/flang/runtime/iostat.cpp b/flang/runtime/iostat.cpp
index 39e224cb01286b..0d71eb09e0a504 100644
--- a/flang/runtime/iostat.cpp
+++ b/flang/runtime/iostat.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang/Runtime/iostat.h"
+#include "flang/Runtime/iostat-funcs.h"
 
 namespace Fortran::runtime::io {
 RT_OFFLOAD_API_GROUP_BEGIN
diff --git a/flang/runtime/namelist.cpp b/flang/runtime/namelist.cpp
index af092de70f7819..cfed7f2c6bf97d 100644
--- a/flang/runtime/namelist.cpp
+++ b/flang/runtime/namelist.cpp
@@ -10,7 +10,7 @@
 #include "descriptor-io.h"
 #include "emit-encoded.h"
 #include "io-stmt.h"
-#include "flang/Runtime/io-api.h"
+#include "flang/Runtime/io-api-funcs.h"
 #include <algorithm>
 #include <cstring>
 #include <limits>
diff --git a/flang/unittests/Optimizer/Builder/Runtime/AllocatableTest.cpp b/flang/unittests/Optimizer/Builder/Runtime/AllocatableTest.cpp
index 1db43cacc90f07..f618e72d7b7f3c 100644
--- a/flang/unittests/Optimizer/Builder/Runtime/AllocatableTest.cpp
+++ b/flang/unittests/Optimizer/Builder/Runtime/AllocatableTest.cpp
@@ -9,7 +9,7 @@
 #include "flang/Optimizer/Builder/Runtime/Allocatable.h"
 #include "RuntimeCallTestBase.h"
 #include "gtest/gtest.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang/Runtime/descriptor-consts.h"
 
 using namespace Fortran::runtime;
 

>From 8757eb974fcc57630e85ed1ad838bb2dbbb5a162 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Mon, 14 Oct 2024 16:51:07 +0200
Subject: [PATCH 2/2] Move SubscriptValue to public header

---
 flang/include/flang/Runtime/descriptor-consts.h | 1 +
 flang/include/flang/Runtime/descriptor.h        | 1 -
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/include/flang/Runtime/descriptor-consts.h b/flang/include/flang/Runtime/descriptor-consts.h
index 098eb59ca01b2e..c08e5018d0af53 100644
--- a/flang/include/flang/Runtime/descriptor-consts.h
+++ b/flang/include/flang/Runtime/descriptor-consts.h
@@ -28,6 +28,7 @@ class DerivedType;
 
 namespace Fortran::runtime {
 class Descriptor;
+using SubscriptValue = ISO::CFI_index_t;
 
 /// Returns size in bytes of the descriptor (not the data)
 /// This must be at least as large as the largest descriptor of any target
diff --git a/flang/include/flang/Runtime/descriptor.h b/flang/include/flang/Runtime/descriptor.h
index 6d0f09c79508cb..dd36fba157ca92 100644
--- a/flang/include/flang/Runtime/descriptor.h
+++ b/flang/include/flang/Runtime/descriptor.h
@@ -31,7 +31,6 @@
 
 namespace Fortran::runtime {
 
-using SubscriptValue = ISO::CFI_index_t;
 class Terminator;
 
 RT_VAR_GROUP_BEGIN



More information about the llvm-branch-commits mailing list