[flang-commits] [flang] [llvm] [flang-rt] Prototype: skip copy-out into read-only memory via a process memory-map snapshot (PR #223002)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Fri Sep 11 11:46:46 PDT 2026


================
@@ -0,0 +1,95 @@
+//===-- include/flang-rt/runtime/memory-map.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
+//
+//===----------------------------------------------------------------------===//
+
+// Optional copy-out compatibility feature: consult the process memory map to
+// recognize copy-out destinations that live in read-only memory and skip the
+// write-back (see RTDEF(CopyOutAssign)). A compiler-generated copy-out into
+// genuinely read-only storage could only ever rewrite identical bytes or
+// fault, so skipping converts the fault into a no-op for programs that
+// (invalidly) modified a temporary whose original is not definable.
+//
+// Modes (FLANG_RT_COPYOUT_READONLY_MODE): 0 = off (default), 1 = trust the
+// one-time lazy snapshot (no system calls on the copy-out path), 2 = re-confirm
+// each snapshot hit against the current OS state before skipping. Every
+// uncertainty - unsupported platform, parse anomaly, allocation failure,
+// degenerate descriptor, partial containment - answers "not read-only", i.e.
+// the regular copy-out runs. Host-only: all of this is compiled out of device
+// paths.
+
+#ifndef FLANG_RT_RUNTIME_MEMORY_MAP_H_
+#define FLANG_RT_RUNTIME_MEMORY_MAP_H_
+
+#include "flang/Common/api-attrs.h"
+#include <cstddef>
+#include <cstdint>
+
+namespace Fortran::runtime {
+class Descriptor;
+
+#if !defined(RT_DEVICE_COMPILATION) && !defined(RT_GPU_TARGET)
+
+enum class CopyOutReadOnlyMode : int {
+  Off = 0, // feature disabled
+  Trust = 1, // snapshot table only; no system calls at copy-out
+  Confirm = 2, // snapshot hit re-confirmed against current OS state
+};
+
+// Parsed lazily from FLANG_RT_COPYOUT_READONLY_MODE; invalid values are Off.
+CopyOutReadOnlyMode GetCopyOutReadOnlyMode();
+
+// True iff the whole data span of 'var' lies within the snapshot's read-only
+// regions. Performs no system calls after the one-time lazy snapshot.
+bool CopyOutReadOnlyCandidate(const Descriptor &var);
+
+// True iff the whole data span of 'var' is mapped read-only in the *current*
+// OS memory map. Performs system calls; used by mode 2 on candidate hits.
+bool CopyOutReadOnlyConfirm(const Descriptor &var);
----------------
vzakhari wrote:

Great idea!

Can we have these APIs exposed to the compiler so that it can use them in the inlined copy-out code?

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


More information about the flang-commits mailing list