[llvm-branch-commits] [clang] [flang] [lld] [llvm] [Flang] LLVM_ENABLE_RUNTIMES=flang-rt (PR #110217)

Michael Kruse via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Feb 7 04:11:28 PST 2025


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

>From f67081627ea71065635f5dd84172bcd68904f4f6 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Fri, 24 Jan 2025 16:50:07 +0100
Subject: [PATCH 1/9] [Flang] LLVM_ENABLE_RUNTIMES=flang-rt

---
 clang/lib/Driver/ToolChains/Flang.cpp         |  14 +-
 flang-rt/.clang-tidy                          |   2 +
 flang-rt/CMakeLists.txt                       | 248 ++++++++++++++++++
 flang-rt/CODE_OWNERS.TXT                      |  14 +
 flang-rt/LICENSE.TXT                          | 234 +++++++++++++++++
 flang-rt/README.md                            | 188 +++++++++++++
 flang-rt/cmake/modules/AddFlangRT.cmake       | 186 +++++++++++++
 .../cmake/modules/AddFlangRTOffload.cmake     | 101 +++++++
 flang-rt/cmake/modules/GetToolchainDirs.cmake | 125 +++++++++
 flang-rt/lib/CMakeLists.txt                   |  18 ++
 .../lib/FortranFloat128Math/CMakeLists.txt    | 136 ++++++++++
 flang-rt/lib/Testing/CMakeLists.txt           |  20 ++
 flang-rt/lib/flang_rt/CMakeLists.txt          | 213 +++++++++++++++
 flang-rt/lib/flang_rt/CUDA/CMakeLists.txt     |  33 +++
 flang-rt/lib/flang_rt/io-api-minimal.cpp      |   2 +-
 flang-rt/test/CMakeLists.txt                  |  59 +++++
 flang-rt/test/Driver/ctofortran.f90           |  29 +-
 flang-rt/test/Driver/exec.f90                 |   8 +-
 flang-rt/test/NonGtestUnit/lit.cfg.py         |  22 ++
 flang-rt/test/NonGtestUnit/lit.site.cfg.py.in |  14 +
 flang-rt/test/Runtime/no-cpp-dep.c            |   5 +-
 flang-rt/test/Unit/lit.cfg.py                 |  21 ++
 flang-rt/test/Unit/lit.site.cfg.py.in         |  15 ++
 flang-rt/test/lit.cfg.py                      | 102 +++++++
 flang-rt/test/lit.site.cfg.py.in              |  19 ++
 flang-rt/unittests/CMakeLists.txt             | 111 ++++++++
 flang-rt/unittests/Evaluate/CMakeLists.txt    |  21 ++
 flang-rt/unittests/Runtime/CMakeLists.txt     |  48 ++++
 .../unittests/Runtime/CUDA/CMakeLists.txt     |  18 ++
 flang/CMakeLists.txt                          |  53 ++--
 flang/cmake/modules/FlangCommon.cmake         |  43 +++
 flang/docs/GettingStarted.md                  | 108 ++++----
 flang/docs/ReleaseNotes.md                    |   8 +-
 flang/module/iso_fortran_env_impl.f90         |   2 +-
 flang/test/lit.cfg.py                         |  20 --
 flang/test/lit.site.cfg.py.in                 |   3 -
 llvm/CMakeLists.txt                           |   8 +-
 .../modules/LLVMExternalProjectUtils.cmake    |  16 +-
 llvm/projects/CMakeLists.txt                  |   4 +-
 llvm/runtimes/CMakeLists.txt                  |  25 +-
 runtimes/CMakeLists.txt                       |   2 +-
 41 files changed, 2169 insertions(+), 149 deletions(-)
 create mode 100644 flang-rt/.clang-tidy
 create mode 100644 flang-rt/CMakeLists.txt
 create mode 100644 flang-rt/CODE_OWNERS.TXT
 create mode 100644 flang-rt/LICENSE.TXT
 create mode 100644 flang-rt/README.md
 create mode 100644 flang-rt/cmake/modules/AddFlangRT.cmake
 create mode 100644 flang-rt/cmake/modules/AddFlangRTOffload.cmake
 create mode 100644 flang-rt/cmake/modules/GetToolchainDirs.cmake
 create mode 100644 flang-rt/lib/CMakeLists.txt
 create mode 100644 flang-rt/lib/FortranFloat128Math/CMakeLists.txt
 create mode 100644 flang-rt/lib/Testing/CMakeLists.txt
 create mode 100644 flang-rt/lib/flang_rt/CMakeLists.txt
 create mode 100644 flang-rt/lib/flang_rt/CUDA/CMakeLists.txt
 create mode 100644 flang-rt/test/CMakeLists.txt
 create mode 100644 flang-rt/test/NonGtestUnit/lit.cfg.py
 create mode 100644 flang-rt/test/NonGtestUnit/lit.site.cfg.py.in
 create mode 100644 flang-rt/test/Unit/lit.cfg.py
 create mode 100644 flang-rt/test/Unit/lit.site.cfg.py.in
 create mode 100644 flang-rt/test/lit.cfg.py
 create mode 100644 flang-rt/test/lit.site.cfg.py.in
 create mode 100644 flang-rt/unittests/CMakeLists.txt
 create mode 100644 flang-rt/unittests/Evaluate/CMakeLists.txt
 create mode 100644 flang-rt/unittests/Runtime/CMakeLists.txt
 create mode 100644 flang-rt/unittests/Runtime/CUDA/CMakeLists.txt
 create mode 100644 flang/cmake/modules/FlangCommon.cmake

diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 68a17edf8ca341f..17a8a4dd8d0a874 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -342,11 +342,15 @@ static void processVSRuntimeLibrary(const ToolChain &TC, const ArgList &Args,
                                     ArgStringList &CmdArgs) {
   assert(TC.getTriple().isKnownWindowsMSVCEnvironment() &&
          "can only add VS runtime library on Windows!");
-  // if -fno-fortran-main has been passed, skip linking Fortran_main.a
-  if (TC.getTriple().isKnownWindowsMSVCEnvironment()) {
-    CmdArgs.push_back(Args.MakeArgString(
-        "--dependent-lib=" + TC.getCompilerRTBasename(Args, "builtins")));
-  }
+
+  // Flang/Clang (including clang-cl) -compiled programs targeting the MSVC ABI
+  // should only depend on msv(u)crt. LLVM still emits libgcc/compiler-rt
+  // functions in some cases like 128-bit integer math (__udivti3, __modti3,
+  // __fixsfti, __floattidf, ...) that msvc does not support. We are injecting a
+  // dependency to Compiler-RT's builtin library where these are implemented.
+  CmdArgs.push_back(Args.MakeArgString(
+      "--dependent-lib=" + TC.getCompilerRTBasename(Args, "builtins")));
+
   unsigned RTOptionID = options::OPT__SLASH_MT;
   if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) {
     RTOptionID = llvm::StringSwitch<unsigned>(rtl->getValue())
diff --git a/flang-rt/.clang-tidy b/flang-rt/.clang-tidy
new file mode 100644
index 000000000000000..ee3a0ab2201bf38
--- /dev/null
+++ b/flang-rt/.clang-tidy
@@ -0,0 +1,2 @@
+Checks: '-llvm-include-order,readability-braces-around-statements,-readability-identifier-naming,-clang-diagnostic-*'
+InheritParentConfig: true
diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt
new file mode 100644
index 000000000000000..655d0a55b400446
--- /dev/null
+++ b/flang-rt/CMakeLists.txt
@@ -0,0 +1,248 @@
+#===-- CMakeLists.txt ------------------------------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+#
+# Build instructions for the flang-rt library. This is file is intended to be
+# included using the LLVM_ENABLE_RUNTIMES mechanism.
+#
+#===------------------------------------------------------------------------===#
+
+if (NOT LLVM_RUNTIMES_BUILD)
+  message(FATAL_ERROR "Use this CMakeLists.txt from LLVM's runtimes build system.
+      Example:
+        cmake <llvm-project>/runtimes -DLLVM_ENABLE_RUNTIMES=flang-rt
+    ")
+endif ()
+
+set(LLVM_SUBPROJECT_TITLE "Flang-RT")
+set(FLANG_RT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
+set(FLANG_RT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
+set(FLANG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../flang")
+
+# CMake 3.24 is the first version of CMake that directly recognizes Flang.
+# LLVM's requirement is only CMake 3.20, teach CMake 3.20-3.23 how to use Flang.
+if (CMAKE_VERSION VERSION_LESS "3.24")
+  cmake_path(GET CMAKE_Fortran_COMPILER STEM _Fortran_COMPILER_STEM)
+  if (_Fortran_COMPILER_STEM STREQUAL "flang-new" OR _Fortran_COMPILER_STEM STREQUAL "flang")
+    include(CMakeForceCompiler)
+    CMAKE_FORCE_Fortran_COMPILER("${CMAKE_Fortran_COMPILER}" "LLVMFlang")
+
+    set(CMAKE_Fortran_COMPILER_ID "LLVMFlang")
+    set(CMAKE_Fortran_COMPILER_VERSION "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")
+
+    set(CMAKE_Fortran_SUBMODULE_SEP "-")
+    set(CMAKE_Fortran_SUBMODULE_EXT ".mod")
+
+    set(CMAKE_Fortran_PREPROCESS_SOURCE
+      "<CMAKE_Fortran_COMPILER> -cpp <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE> > <PREPROCESSED_SOURCE>")
+
+    set(CMAKE_Fortran_FORMAT_FIXED_FLAG "-ffixed-form")
+    set(CMAKE_Fortran_FORMAT_FREE_FLAG "-ffree-form")
+
+    set(CMAKE_Fortran_MODDIR_FLAG "-module-dir")
+
+    set(CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_ON "-cpp")
+    set(CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_OFF "-nocpp")
+    set(CMAKE_Fortran_POSTPROCESS_FLAG "-ffixed-line-length-72")
+
+    set(CMAKE_Fortran_COMPILE_OPTIONS_TARGET "--target=")
+
+    set(CMAKE_Fortran_LINKER_WRAPPER_FLAG "-Wl,")
+    set(CMAKE_Fortran_LINKER_WRAPPER_FLAG_SEP ",")
+  endif ()
+endif ()
+enable_language(Fortran)
+
+
+list(APPEND CMAKE_MODULE_PATH
+    "${FLANG_RT_SOURCE_DIR}/cmake/modules"
+    "${FLANG_SOURCE_DIR}/cmake/modules"
+  )
+include(AddFlangRT)
+include(GetToolchainDirs)
+include(FlangCommon)
+include(HandleCompilerRT)
+include(ExtendPath)
+
+
+############################
+# Build Mode Introspection #
+############################
+
+# Determine whether we are in the runtimes/runtimes-bins directory of a
+# bootstrap build.
+set(LLVM_TREE_AVAILABLE OFF)
+if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION)
+  set(LLVM_TREE_AVAILABLE ON)
+endif()
+
+# Path to LLVM development tools (FileCheck, llvm-lit, not, ...)
+set(LLVM_TOOLS_DIR "${LLVM_BINARY_DIR}/bin")
+
+# Determine build and install paths.
+# The build path is absolute, but the install dir is relative, CMake's install
+# command has to apply CMAKE_INSTALL_PREFIX itself.
+get_toolchain_library_subdir(toolchain_lib_subdir)
+if (LLVM_TREE_AVAILABLE)
+  # In a bootstrap build emit the libraries into a default search path in the
+  # build directory of the just-built compiler. This allows using the
+  # just-built compiler without specifying paths to runtime libraries.
+  #
+  # Despite Clang in the name, get_clang_resource_dir does not depend on Clang
+  # being added to the build. Flang uses the same resource dir as clang.
+  include(GetClangResourceDir)
+  get_clang_resource_dir(FLANG_RT_OUTPUT_RESOURCE_DIR PREFIX "${LLVM_LIBRARY_OUTPUT_INTDIR}/..")
+  get_clang_resource_dir(FLANG_RT_INSTALL_RESOURCE_PATH)
+
+  extend_path(FLANG_RT_OUTPUT_RESOURCE_LIB_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR}" "${toolchain_lib_subdir}")
+else ()
+  # In a standalone runtimes build, do not write into LLVM_BINARY_DIR. It may be
+  # read-only and/or shared by multiple runtimes with different build
+  # configurations (e.g. Debug/Release). Use the runtime's own lib dir like any
+  # non-toolchain library.
+  # For the install prefix, still use the resource dir assuming that Flang will
+  # be installed there using the same prefix. This is to not have a difference
+  # between bootstrap and standalone runtimes builds.
+  set(FLANG_RT_OUTPUT_RESOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
+  set(FLANG_RT_INSTALL_RESOURCE_PATH "lib${LLVM_LIBDIR_SUFFIX}/clang/${LLVM_VERSION_MAJOR}")
+
+  extend_path(FLANG_RT_OUTPUT_RESOURCE_LIB_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR}" "lib${LLVM_LIBDIR_SUFFIX}")
+endif ()
+extend_path(FLANG_RT_INSTALL_RESOURCE_LIB_PATH "${FLANG_RT_INSTALL_RESOURCE_PATH}" "${toolchain_lib_subdir}")
+cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_DIR)
+cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_PATH)
+cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_LIB_DIR)
+cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_LIB_PATH)
+
+
+#################
+# Build Options #
+#################
+
+# Important: flang-rt user options must be prefixed with "FLANG_RT_". Variables
+# with this prefix will be forwarded in bootstrap builds.
+
+option(FLANG_RT_INCLUDE_TESTS "Generate build targets for the flang-rt unit and regression-tests." "${LLVM_INCLUDE_TESTS}")
+
+
+set(FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT "" CACHE STRING "Compile Flang-RT with GPU support (CUDA or OpenMP)")
+set_property(CACHE FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT PROPERTY STRINGS
+    ""
+    CUDA
+    OpenMP
+  )
+if (NOT FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT)
+  # Support for GPUs disabled
+elseif (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "CUDA")
+  # Support for CUDA
+  set(FLANG_RT_LIBCUDACXX_PATH "" CACHE PATH "Path to libcu++ package installation")
+  option(FLANG_RT_CUDA_RUNTIME_PTX_WITHOUT_GLOBAL_VARS "Do not compile global variables' definitions when producing PTX library" OFF)
+elseif (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "OpenMP")
+  # Support for OpenMP offloading
+  set(FLANG_RT_DEVICE_ARCHITECTURES "all" CACHE STRING
+      "List of OpenMP device architectures to be used to compile the Fortran runtime (e.g. 'gfx1103;sm_90')"
+    )
+
+  if (FLANG_RT_DEVICE_ARCHITECTURES STREQUAL "all")
+    # TODO: support auto detection on the build system.
+    set(all_amdgpu_architectures
+      "gfx700;gfx701;gfx801;gfx803;gfx900;gfx902;gfx906"
+      "gfx908;gfx90a;gfx90c;gfx940;gfx1010;gfx1030"
+      "gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036"
+      "gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151"
+      "gfx1152;gfx1153")
+    set(all_nvptx_architectures
+      "sm_35;sm_37;sm_50;sm_52;sm_53;sm_60;sm_61;sm_62"
+      "sm_70;sm_72;sm_75;sm_80;sm_86;sm_89;sm_90")
+    set(all_gpu_architectures
+      "${all_amdgpu_architectures};${all_nvptx_architectures}")
+      set(FLANG_RT_DEVICE_ARCHITECTURES ${all_gpu_architectures})
+  endif()
+  list(REMOVE_DUPLICATES FLANG_RT_DEVICE_ARCHITECTURES)
+else ()
+  message(FATAL_ERROR "Invalid value '${FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT}' for FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT; must be empty, 'CUDA', or 'OpenMP'")
+endif ()
+
+
+option(FLANG_RT_ENABLE_CUF "Compile CUDA Fortran runtime sources" OFF)
+if (FLANG_RT_ENABLE_CUF)
+  find_package(CUDAToolkit REQUIRED)
+endif()
+
+
+########################
+# System Introspection #
+########################
+
+include(CheckCXXSymbolExists)
+include(CheckCXXSourceCompiles)
+check_cxx_symbol_exists(strerror_r string.h HAVE_STRERROR_R)
+# Can't use symbol exists here as the function is overloaded in C++
+check_cxx_source_compiles(
+  "#include <string.h>
+   int main() {
+     char buf[4096];
+     return strerror_s(buf, 4096, 0);
+   }
+  "
+  HAVE_DECL_STRERROR_S)
+
+
+# Search for clang_rt.builtins library. Need in addition to msvcrt.
+if (WIN32)
+  find_compiler_rt_library(builtins FLANG_RT_BUILTINS_LIBRARY)
+endif ()
+
+
+# Check whether the compiler can undefine a macro using the "-U" flag.
+# Aternatively, we could use
+#   CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU"
+# but some older versions of CMake don't define it for GCC itself.
+check_cxx_compiler_flag("-UTESTFLAG" FLANG_RT_SUPPORTS_UNDEFINE_FLAG)
+
+
+#####################
+# Build Preparation #
+#####################
+
+if (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT AND FLANG_RT_INCLUDE_TESTS)
+  # If Fortran runtime is built as CUDA library, the linking
+  # of targets that link flang-rt must be done
+  # with CUDA_RESOLVE_DEVICE_SYMBOLS.
+  # CUDA language must be enabled for CUDA_RESOLVE_DEVICE_SYMBOLS
+  # to take effect.
+  enable_language(CUDA)
+endif()
+
+
+# C++17 is required for flang-rt; user or other runtimes may override this.
+# GTest included later also requires C++17.
+set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
+set(CMAKE_CXX_STANDARD_REQUIRED YES)
+
+
+configure_file(cmake/config.h.cmake.in config.h)
+
+
+# The bootstrap build will create a phony target with the same as the top-level
+# directory ("flang-rt") and delegate it to the runtimes build dir.
+# AddFlangRT will add all non-EXCLUDE_FROM_ALL targets to it.
+add_custom_target(flang-rt)
+
+
+###################
+# Build Artifacts #
+###################
+
+add_subdirectory(lib)
+
+if (FLANG_RT_INCLUDE_TESTS)
+  add_subdirectory(unittests)
+  add_subdirectory(test)
+else ()
+  add_custom_target(check-flang-rt)
+endif()
diff --git a/flang-rt/CODE_OWNERS.TXT b/flang-rt/CODE_OWNERS.TXT
new file mode 100644
index 000000000000000..649243aa1e8fec3
--- /dev/null
+++ b/flang-rt/CODE_OWNERS.TXT
@@ -0,0 +1,14 @@
+This file is a list of the people responsible for ensuring that patches for a
+particular part of Flang are reviewed, either by themself or by someone else.
+They are also the gatekeepers for their part of Flang, with the final word on
+what goes in or not.
+
+The list is sorted by surname and formatted to allow easy grepping and
+beautification by scripts. The fields are: name (N), email (E), web-address
+(W), PGP key ID and fingerprint (P), description (D), snail-mail address
+(S) and (I) IRC handle. Each entry should contain at least the (N), (E) and
+(D) fields.
+
+N: Steve Scalpone
+E: sscalpone at nvidia.com
+D: Anything not covered by others
diff --git a/flang-rt/LICENSE.TXT b/flang-rt/LICENSE.TXT
new file mode 100644
index 000000000000000..53bb2e7fbc76434
--- /dev/null
+++ b/flang-rt/LICENSE.TXT
@@ -0,0 +1,234 @@
+==============================================================================
+The LLVM Project is under the Apache License v2.0 with LLVM Exceptions:
+==============================================================================
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+    TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+    1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+    2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+    3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+    4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+    5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+    6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+    7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+    8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+    9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+    END OF TERMS AND CONDITIONS
+
+    APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+    Copyright [yyyy] [name of copyright owner]
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+
+---- LLVM Exceptions to the Apache 2.0 License ----
+
+As an exception, if, as a result of your compiling your source code, portions
+of this Software are embedded into an Object form of such source code, you
+may redistribute such embedded portions in such Object form without complying
+with the conditions of Sections 4(a), 4(b) and 4(d) of the License.
+
+In addition, if you combine or link compiled forms of this Software with
+software that is licensed under the GPLv2 ("Combined Software") and if a
+court of competent jurisdiction determines that the patent provision (Section
+3), the indemnity provision (Section 9) or other Section of the License
+conflicts with the conditions of the GPLv2, you may retroactively and
+prospectively choose to deem waived or otherwise exclude such Section(s) of
+the License, but only in their entirety and only with respect to the Combined
+Software.
+
+==============================================================================
+Software from third parties included in the LLVM Project:
+==============================================================================
+The LLVM Project contains third party software which is under different license
+terms. All such code will be identified clearly using at least one of two
+mechanisms:
+1) It will be in a separate directory tree with its own `LICENSE.txt` or
+   `LICENSE` file at the top containing the specific license and restrictions
+   which apply to that software, or
+2) It will contain specific license and restriction terms at the top of every
+   file.
diff --git a/flang-rt/README.md b/flang-rt/README.md
new file mode 100644
index 000000000000000..76993f27f8e4f2c
--- /dev/null
+++ b/flang-rt/README.md
@@ -0,0 +1,188 @@
+<!--===- README.md
+
+   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
+
+-->
+
+# Fortran Runtime (Flang-RT)
+
+Flang-RT is the runtime library for code emitted by the Flang compiler
+(https://flang.llvm.org).
+
+
+## Getting Started
+
+There are two build modes for the Flang-RT. The bootstrap build, also
+called the in-tree build, and the runtime-only build, also called the
+out-of-tree build.
+Not to be confused with the terms
+[in-source and out-of-source](https://cmake.org/cmake/help/latest/manual/cmake.1.html#introduction-to-cmake-buildsystems)
+builds as defined by CMake. In an in-source build, the source directory and the
+build directory are identical, whereas with an out-of-source build the
+build artifacts are stored somewhere else, possibly in a subdirectory of the
+source directory. LLVM does not support in-source builds.
+
+
+### Requirements
+
+Requirements:
+  * [Same as LLVM](https://llvm.org/docs/GettingStarted.html#requirements).
+
+
+### Bootstrapping Runtimes Build
+
+The bootstrapping build will first build Clang and Flang, then use these
+compilers to compile Flang-RT. CMake will create a secondary build tree
+configured to use these just-built compilers. The secondary build will reuse
+the same build options (Flags, Debug/Release, ...) as the primary build.
+It will also ensure that once built, Flang-RT is found by Flang from either
+the build- or install-prefix. To enable, add `flang-rt` to
+`LLVM_ENABLE_RUNTIMES`:
+
+```bash
+cmake -S <path-to-llvm-project-source>/llvm \
+  -GNinja                                   \
+  -DLLVM_ENABLE_PROJECTS="clang;flang"      \
+  -DLLVM_ENABLE_RUNTIMES=flang-rt           \
+  ...
+```
+
+It is recommended to enable building OpenMP alongside Flang and Flang-RT
+as well. This will build `omp_lib.mod` required to use OpenMP from Fortran.
+Building Compiler-RT may also be required, particularly on platforms that do
+not provide all C-ABI functionality (such as Windows).
+
+```bash
+cmake -S <path-to-llvm-project-source>/llvm     \
+  -GNinja                                       \
+  -DCMAKE_BUILD_TYPE=Release                    \
+  -DLLVM_ENABLE_PROJECTS="clang;flang;openmp"   \
+  -DLLVM_ENABLE_RUNTIMES="compiler-rt;flang-rt" \
+  ...
+```
+
+By default, the enabled runtimes will only be built for the host platform
+(`-DLLVM_RUNTIME_TARGETS=default`). To add additional targets to support
+cross-compilation via `flang --target=<target-triple>`, add more triples to
+`LLVM_RUNTIME_TARGETS`, such as
+`-DLLVM_RUNTIME_TARGETS="default;aarch64-linux-gnu"`.
+
+After configuration, build, test, and install the runtime(s) via
+
+```shell
+$ ninja flang-rt
+$ ninja check-flang-rt
+$ ninja install
+```
+
+
+### Standalone Runtimes Build
+
+Instead of building Clang and Flang from scratch, the standalone Runtime build
+uses CMake's environment introspection to find a C, C++, and Fortran compiler.
+The compiler to be used can be controlled using CMake's standard mechanisms such
+as `CMAKE_CXX_COMPILER`, `CMAKE_CXX_COMPILER`, and `CMAKE_Fortran_COMPILER`.
+`CMAKE_Fortran_COMPILER` must be `flang` built from the same Git commit as
+Flang-RT to ensure they are using the same ABI. The C and C++ compiler
+can be any compiler supporting the same ABI.
+
+In addition to the compiler, the build be able to find LLVM development tools
+such as `lit` and `FileCheck` that are not found in an LLVM's install
+directory. Use `CMAKE_BINARY_DIR` to point to directory where LLVM has
+been built. A simple build configuration might look like the following:
+
+```bash
+cmake -S <path-to-llvm-project-source>/runtimes              \
+  -GNinja                                                    \
+  -DLLVM_BINARY_DIR=<path-to-llvm-builddir>                  \
+  -DCMAKE_Fortran_COMPILER=<path-to-llvm-builddir>/bin/flang \
+  -DCMAKE_Fortran_COMPILER_WORKS=yes                         \
+  -DLLVM_ENABLE_RUNTIMES=flang-rt                            \
+  ...
+```
+
+The `CMAKE_Fortran_COMPILER_WORKS` parameter must be set because otherwise CMake
+will test whether the Fortran compiler can compile and link programs which will
+obviously fail without a runtime library available yet.
+
+Building Flang-RT for cross-compilation triple, the target triple can
+be selected using `LLVM_DEFAULT_TARGET_TRIPLE` AND `LLVM_RUNTIMES_TARGET`.
+Of course, Flang-RT can be built multiple times with different build
+configurations, but have to be located manually when using with the Flang
+driver using the `-L` option.
+
+After configuration, build, test, and install the runtime via
+
+```shell
+$ ninja
+$ ninja check-flang-rt
+$ ninja install
+```
+
+
+## Configuration Option Reference
+
+Flang-RT has the followign configuration options. This is in
+addition to the build options the LLVM_ENABLE_RUNTIMES mechanism and
+CMake itself provide.
+
+ * `FLANG_RT_INCLUDE_TESTS` (boolean; default: `ON`)
+
+   When `OFF`, does not add any tests and unittests. The `check-flang-rt`
+   build target will do nothing.
+
+ * `FLANG_RUNTIME_F128_MATH_LIB` (default: `""`)
+
+   Determines the implementation of `REAL(16)` math functions. If set to
+   `libquadmath`, uses `quadmath.h` and `-lquadmath` typically distributed with
+   gcc. If empty, disables `REAL(16)` support. For any other value, introspects
+   the compiler for `__float128` or 128-bit `long double` support.
+   [More details](docs/Real16MathSupport.md).
+
+ * `FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT` (values: `"CUDA"`,`"OpenMP"`, `""` default: `""`)
+
+   When set to `CUDA`, builds Flang-RT with experimental support for GPU
+   accelerators using CUDA. `CMAKE_CUDA_COMPILER` must be set if not
+   automatically detected by CMake. `nvcc` as well as `clang` are supported.
+
+   When set to `OpenMP`, builds Flang-RT with experimental support for
+   GPU accelerators using OpenMP offloading. Only Clang is supported for
+   `CMAKE_C_COMPILER` and `CMAKE_CXX_COMPILER`.
+
+ * `FLANG_RT_ENABLE_CUF` (bool, default: `OFF`)
+
+   Compiles the `libCufRuntime_cuda_<CUDA-version>.a/.so` library. This is
+   independent of `FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA` and only
+   requires a
+   [CUDA Toolkit installation](https://cmake.org/cmake/help/latest/module/FindCUDAToolkit.html)
+   (no `CMAKE_CUDA_COMPILER`).
+
+
+### Experimental CUDA Support
+
+With `-DFLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA`, the following
+additional configuration options become available.
+
+ * `FLANG_RT_LIBCUDACXX_PATH` (path, default: `""`)
+
+   Path to libcu++ package installation.
+
+ * `FLANG_RT_CUDA_RUNTIME_PTX_WITHOUT_GLOBAL_VARS` (boolean, default: `OFF`)
+
+   Do not compile global variables' definitions when producing PTX library.
+   Default is `OFF`, meaning global variable definitions are compiled by
+   default.
+
+
+### Experimental OpenMP Offload Support
+
+With `-DFLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT=OpenMP`, the following
+additional configuration options become available.
+
+ * `FLANG_RT_DEVICE_ARCHITECTURES` (default: `"all"`)
+
+   A list of device architectures that Flang-RT is going to support.
+   If `"all"` uses a pre-defined list of architectures. Same purpose as
+   `LIBOMPTARGET_DEVICE_ARCHITECTURES` from liboffload.
diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
new file mode 100644
index 000000000000000..aa8adedf61752ad
--- /dev/null
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -0,0 +1,186 @@
+#===-- cmake/modules/AddFlangRT.cmake --------------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+# Builds a library with common options for flang-rt.
+#
+# Usage:
+#
+# add_flangrt_library(name sources ...
+#   SHARED
+#     Build a dynamic (.so/.dll) library
+#   STATIC
+#     Build a static (.a/.lib) library
+#   OBJECT
+#     Create only object files without static/dynamic library
+#   INSTALL_WITH_TOOLCHAIN
+#     Install library into Clang's resource directory so it can be found by the
+#     Flang driver during compilation, including tests
+#   EXCLUDE_FROM_ALL
+#     Do not build library by default; typically used for libraries needed for
+#     testing only, no install
+#   LINK_TO_LLVM
+#     Library requires include path and linking to LLVM's Support component
+#   ADDITIONAL_HEADERS
+#     May specify header files for IDE generators.
+#   INCLUDE_DIRECTORIES
+#     Additional include_directories for all added targets
+#   TARGET_PROPERTIES
+#     Set target properties of all added targets
+# )
+function (add_flangrt_library name)
+  set(options STATIC SHARED OBJECT INSTALL_WITH_TOOLCHAIN EXCLUDE_FROM_ALL LINK_TO_LLVM)
+  set(multiValueArgs ADDITIONAL_HEADERS INCLUDE_DIRECTORIES TARGET_PROPERTIES)
+  cmake_parse_arguments(ARG
+    "${options}"
+    ""
+    "${multiValueArgs}"
+    ${ARGN})
+
+  if (ARG_INSTALL_WITH_TOOLCHAIN AND ARG_EXCLUDE_FROM_ALL)
+    message(SEND_ERROR "add_flangrt_library(${name} ...):
+        INSTALL_WITH_TOOLCHAIN and EXCLUDE_FROM_ALL are in conflict. When
+        installing an artifact it must have been built first in the 'all' target.
+      ")
+  endif ()
+
+  # Forward libtype to add_library
+  set(extra_args "")
+  if (ARG_SHARED)
+    list(APPEND extra_args SHARED)
+  endif ()
+  if (ARG_STATIC)
+    list(APPEND extra_args STATIC)
+  endif ()
+  if (ARG_OBJECT)
+    list(APPEND extra_args OBJECT)
+  endif ()
+  if (ARG_EXCLUDE_FROM_ALL)
+    list(APPEND extra_args EXCLUDE_FROM_ALL)
+  endif ()
+
+  # Also add header files to IDEs to list as part of the library.
+  set_source_files_properties(${ARG_ADDITIONAL_HEADERS} PROPERTIES HEADER_FILE_ONLY ON)
+
+  add_library(${name} ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
+
+  if (ARG_INSTALL_WITH_TOOLCHAIN)
+    set_target_properties(${name} PROPERTIES FOLDER "Flang-RT/Toolchain Libraries")
+  elseif (ARG_OBJECT)
+    set_target_properties(${name} PROPERTIES FOLDER "Flang-RT/Object Libraries")
+  else ()
+    set_target_properties(${name} PROPERTIES FOLDER "Flang-RT/Libraries")
+  endif ()
+
+  # Minimum required C++ version for Flang-RT, even if CMAKE_CXX_STANDARD is defined to something else.
+  target_compile_features(${name} PRIVATE cxx_std_17)
+
+  # Use compiler-specific options to disable exceptions and RTTI.
+  if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
+    target_compile_options(${name} PRIVATE
+        $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables>
+      )
+  elseif (MSVC)
+    target_compile_options(${name} PRIVATE
+        $<$<COMPILE_LANGUAGE:CXX>:/EHs-c- /GR->
+      )
+  elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
+    target_compile_options(${name} PRIVATE
+        $<$<COMPILE_LANGUAGE:CXX>:-qnoeh -qnortti>
+      )
+  endif ()
+
+  # Also for CUDA source when compiling with FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA
+  if (CMAKE_CUDA_COMPILER_ID MATCHES "NVIDIA")
+    # Assuming gcc as host compiler.
+    target_compile_options(${name} PRIVATE
+        $<$<COMPILE_LANGUAGE:CUDA>:--no-exceptions -Xcompiler -fno-rtti -Xcompiler -fno-unwind-tables -Xcompiler -fno-asynchronous-unwind-tables>
+      )
+  else ()
+    # Assuming a clang-compatible CUDA compiler.
+    target_compile_options(${name} PRIVATE
+        $<$<COMPILE_LANGUAGE:CUDA>:-fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables>
+      )
+  endif ()
+
+  # Flang-RT's public headers
+  target_include_directories(${name} PRIVATE "${FLANG_RT_SOURCE_DIR}/include")
+
+  # For ISO_Fortran_binding.h to be found by the runtime itself (Accessed as #include "flang/ISO_Fortran_binding.h")
+  # User applications can use #include <ISO_Fortran_binding.h>
+  target_include_directories(${name} PRIVATE "${FLANG_SOURCE_DIR}/include")
+
+  # For Flang-RT's configured config.h to be found
+  target_include_directories(${name} PRIVATE "${FLANG_RT_BINARY_DIR}")
+
+  # Disable libstdc++/libc++ assertions, even in an LLVM_ENABLE_ASSERTIONS
+  # build, to avoid an unwanted dependency on libstdc++/libc++.so.
+  if (FLANG_RT_SUPPORTS_UNDEFINE_FLAG)
+    target_compile_options(${name} PUBLIC -U_GLIBCXX_ASSERTIONS)
+    target_compile_options(${name} PUBLIC -U_LIBCPP_ENABLE_ASSERTIONS)
+  endif ()
+
+  # Flang/Clang (including clang-cl) -compiled programs targeting the MSVC ABI
+  # should only depend on msvcrt/ucrt. LLVM still emits libgcc/compiler-rt
+  # functions in some cases like 128-bit integer math (__udivti3, __modti3,
+  # __fixsfti, __floattidf, ...) that msvc does not support. We are injecting a
+  # dependency to Compiler-RT's builtin library where these are implemented.
+  if (MSVC AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+    if (FLANG_RT_BUILTINS_LIBRARY)
+      target_compile_options(${name} PRIVATE "$<$<COMPILE_LANGUAGE:CXX,C>:-Xclang>" "$<$<COMPILE_LANGUAGE:CXX,C>:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>")
+    endif ()
+  endif ()
+  if (MSVC AND CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
+    if (FLANG_RT_BUILTINS_LIBRARY)
+      target_compile_options(${name} PRIVATE "$<$<COMPILE_LANGUAGE:Fortran>:-Xflang>" "$<$<COMPILE_LANGUAGE:Fortran>:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>")
+    else ()
+      message(WARNING "Did not find libclang_rt.builtins.lib.
+        LLVM may emit builtins that are not implemented in msvcrt/ucrt and
+        instead falls back to builtins from Compiler-RT. Linking with ${name}
+        may result in a linker error.")
+    endif ()
+  endif ()
+
+  # Non-GTest unittests depend on LLVMSupport
+  if (ARG_LINK_TO_LLVM)
+    if (LLVM_LINK_LLVM_DYLIB)
+      set(llvm_libs LLVM)
+    else()
+      llvm_map_components_to_libnames(llvm_libs Support)
+    endif()
+    target_link_libraries(${name} PUBLIC ${llvm_libs})
+    target_include_directories(${name} PUBLIC ${LLVM_INCLUDE_DIRS})
+  endif ()
+
+  if (ARG_INCLUDE_DIRECTORIES)
+    target_include_directories(${name} ${ARG_INCLUDE_DIRECTORIES})
+  endif ()
+
+  # If this is part of the toolchain, put it into the compiler's resource
+  # directory. Otherwise it is part of testing and is not installed at all.
+  # TODO: Consider multi-configuration builds (MSVC_IDE, "Ninja Multi-Config")
+  if (ARG_INSTALL_WITH_TOOLCHAIN)
+    set_target_properties(${name}
+      PROPERTIES
+        ARCHIVE_OUTPUT_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_LIB_DIR}"
+      )
+
+    install(TARGETS ${name}
+        ARCHIVE DESTINATION "${FLANG_RT_INSTALL_RESOURCE_LIB_PATH}"
+      )
+  endif ()
+
+  if (ARG_TARGET_PROPERTIES)
+    set_target_properties(${name} PROPERTIES ${ARG_TARGET_PROPERTIES})
+  endif ()
+
+  # flang-rt should build all the Flang-RT targets that are built in an
+  # 'all' build.
+  if (NOT ARG_EXCLUDE_FROM_ALL)
+    add_dependencies(flang-rt ${name})
+  endif ()
+endfunction (add_flangrt_library)
diff --git a/flang-rt/cmake/modules/AddFlangRTOffload.cmake b/flang-rt/cmake/modules/AddFlangRTOffload.cmake
new file mode 100644
index 000000000000000..b055646cb0604cc
--- /dev/null
+++ b/flang-rt/cmake/modules/AddFlangRTOffload.cmake
@@ -0,0 +1,101 @@
+#===-- cmake/modules/AddFortranRTOffload.txt -------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+macro(enable_cuda_compilation name files)
+  if (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "CUDA")
+    enable_language(CUDA)
+
+    set_target_properties(${name}
+        PROPERTIES
+          CUDA_SEPARABLE_COMPILATION ON
+      )
+
+    # Treat all supported sources as CUDA files.
+    set_source_files_properties(${files} PROPERTIES LANGUAGE CUDA)
+    set(CUDA_COMPILE_OPTIONS)
+    if ("${CMAKE_CUDA_COMPILER_ID}" MATCHES "Clang")
+      # Allow varargs.
+      set(CUDA_COMPILE_OPTIONS
+        -Xclang -fcuda-allow-variadic-functions
+        )
+    endif()
+    if ("${CMAKE_CUDA_COMPILER_ID}" MATCHES "NVIDIA")
+      set(CUDA_COMPILE_OPTIONS
+        --expt-relaxed-constexpr
+        # Disable these warnings:
+        #   'long double' is treated as 'double' in device code
+        -Xcudafe --diag_suppress=20208
+        -Xcudafe --display_error_number
+        )
+    endif()
+    set_source_files_properties(${files} PROPERTIES COMPILE_OPTIONS
+      "${CUDA_COMPILE_OPTIONS}"
+      )
+
+    # Create a .a library consisting of CUDA PTX.
+    # This is different from a regular static library. The CUDA_PTX_COMPILATION
+    # property can only be applied to object libraries and create *.ptx files
+    # instead of *.o files. The .a will consist of those *.ptx files only.
+    add_flangrt_library(obj.${name}PTX OBJECT ${files})
+    set_property(TARGET obj.${name}PTX PROPERTY CUDA_PTX_COMPILATION ON)
+    add_flangrt_library(${name}PTX STATIC "$<TARGET_OBJECTS:obj.${name}PTX>")
+
+    # Apply configuration options
+    if (FLANG_RT_CUDA_RUNTIME_PTX_WITHOUT_GLOBAL_VARS)
+      target_compile_definitions(obj.${name}PTX
+        PRIVATE FLANG_RUNTIME_NO_GLOBAL_VAR_DEFS
+        )
+    endif()
+
+    # When using libcudacxx headers files, we have to use them
+    # for all files of Flang-RT.
+    if (EXISTS "${FLANG_RT_LIBCUDACXX_PATH}/include")
+      foreach (tgt IN ITEMS "${name}" "obj.${name}PTX")
+        target_include_directories(${tgt} AFTER PRIVATE "${FLANG_RT_LIBCUDACXX_PATH}/include")
+        target_compile_definitions(${tgt} PRIVATE RT_USE_LIBCUDACXX=1)
+      endforeach ()
+    endif ()
+  endif()
+endmacro()
+
+macro(enable_omp_offload_compilation name files)
+  if (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "OpenMP")
+    # OpenMP offload build only works with Clang compiler currently.
+
+    if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND
+        "${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
+
+      string(REPLACE ";" "," compile_for_architectures
+        "${FLANG_RT_DEVICE_ARCHITECTURES}"
+        )
+
+      set(OMP_COMPILE_OPTIONS
+        -fopenmp
+        -fvisibility=hidden
+        -fopenmp-cuda-mode
+        --offload-arch=${compile_for_architectures}
+        # Force LTO for the device part.
+        -foffload-lto
+        )
+      set_source_files_properties(${files} PROPERTIES COMPILE_OPTIONS
+        "${OMP_COMPILE_OPTIONS}"
+        )
+      target_link_options(${name} PUBLIC ${OMP_COMPILE_OPTIONS})
+
+      # Enable "declare target" in the source code.
+      set_source_files_properties(${files}
+        PROPERTIES COMPILE_DEFINITIONS OMP_OFFLOAD_BUILD
+        )
+    else()
+      message(FATAL_ERROR
+        "Flang-rt build with OpenMP offload is not supported for these compilers:\n"
+        "CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}\n"
+        "CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}")
+    endif()
+  endif()
+endmacro()
diff --git a/flang-rt/cmake/modules/GetToolchainDirs.cmake b/flang-rt/cmake/modules/GetToolchainDirs.cmake
new file mode 100644
index 000000000000000..426a5e8e801f3d4
--- /dev/null
+++ b/flang-rt/cmake/modules/GetToolchainDirs.cmake
@@ -0,0 +1,125 @@
+#===-- cmake/modules/GetToolchainDirs.cmake --------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+
+# Determine the subdirectory relative to Clang's resource dir/sysroot where to
+# install target-specific libraries, to be found by Clang/Flang driver. This was
+# adapted from Compiler-RT's mechanism to find the path for
+# libclang_rt.builtins.a.
+#
+# Compiler-RT has two mechanisms for the path (simplified):
+#
+# * LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=1: lib/${oslibname}/libclang_rt.builtins-${arch}.a
+# * LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=0: lib/${triple}/libclang_rt.builtins.a
+#
+# LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON is the newer scheme, but the old one is
+# currently still used for some platforms such as Windows. Clang looks for which
+# of the files exist before passing the path to the linker. Hence, the
+# directories have to match what Clang is looking for, which is done in
+# ToolChain::getArchSpecificLibPaths(..), ToolChain::getRuntimePath(),
+# ToolChain::getCompilerRTPath(), and ToolChain::getCompilerRT(..), not entirely
+# consistent between these functions, Compiler-RT's CMake code, and overrides
+# in different toolchains.
+#
+# For Fortran, Flang always assumes the library name libflang_rt.a without
+# architecture suffix. Hence, we always use the second scheme even as if
+# LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON, even if it actually set to OFF. It as
+# added unconditionally to the library search path by
+# ToolChain::getArchSpecificLibPaths(...).
+function (get_toolchain_library_subdir outvar)
+  if (NOT APPLE)
+    set(outval "lib")
+  else ()
+    # Required to be "darwin" for MachO toolchain.
+    get_toolchain_os_dirname(os_dirname)
+    set(outval "lib/${os_dirname}")
+  endif ()
+
+  get_toolchain_arch_dirname(arch_dirname)
+  set(outval "lib/${arch_dirname}")
+
+  set(${outvar} "${outval}" PARENT_SCOPE)
+endfunction ()
+
+
+# Corresponds to Clang's ToolChain::getOSLibName(). Adapted from Compiler-RT.
+function (get_toolchain_os_dirname outvar)
+  if (ANDROID)
+    # The CMAKE_SYSTEM_NAME for Android is "Android", but the OS is Linux and the
+    # driver will search for libraries in the "linux" directory.
+    set(outval "linux")
+  else ()
+    string(TOLOWER "${CMAKE_SYSTEM_NAME}" outval)
+  endif ()
+  set(${outvar} "${outval}" PARENT_SCOPE)
+endfunction ()
+
+
+# Corresponds to Clang's ToolChain::getRuntimePath(). Adapted from Compiler-RT.
+function (get_toolchain_arch_dirname outvar)
+  string(REPLACE "-" ";" triple_list ${LLVM_TARGET_TRIPLE})
+  list(GET triple_list 0 arch)
+
+  if("${arch}" MATCHES "^i.86$")
+    # Android uses i686, but that's remapped at a later stage.
+    set(arch "i386")
+  endif()
+
+  string(FIND ${LLVM_TARGET_TRIPLE} "-" dash_index)
+  string(SUBSTRING ${LLVM_TARGET_TRIPLE} ${dash_index} -1 triple_suffix)
+  string(SUBSTRING ${LLVM_TARGET_TRIPLE} 0 ${dash_index} triple_cpu)
+  set(arch "${triple_cpu}")
+  if("${arch}" MATCHES "^i.86$")
+    # Android uses i686, but that's remapped at a later stage.
+    set(arch "i386")
+  endif()
+
+  if(ANDROID AND ${arch} STREQUAL "i386")
+    set(target "i686${triple_suffix}")
+  elseif(${arch} STREQUAL "amd64")
+    set(target "x86_64${triple_suffix}")
+  elseif(${arch} STREQUAL "sparc64")
+    set(target "sparcv9${triple_suffix}")
+  elseif("${arch}" MATCHES "mips64|mips64el")
+    string(REGEX REPLACE "-gnu.*" "-gnuabi64" triple_suffix_gnu "${triple_suffix}")
+    string(REGEX REPLACE "mipsisa32" "mipsisa64" triple_cpu_mips "${triple_cpu}")
+    string(REGEX REPLACE "^mips$" "mips64" triple_cpu_mips "${triple_cpu_mips}")
+    string(REGEX REPLACE "^mipsel$" "mips64el" triple_cpu_mips "${triple_cpu_mips}")
+    set(target "${triple_cpu_mips}${triple_suffix_gnu}")
+  elseif("${arch}" MATCHES "mips|mipsel")
+    string(REGEX REPLACE "-gnuabi.*" "-gnu" triple_suffix_gnu "${triple_suffix}")
+    string(REGEX REPLACE "mipsisa64" "mipsisa32" triple_cpu_mips "${triple_cpu}")
+    string(REGEX REPLACE "mips64" "mips" triple_cpu_mips "${triple_cpu_mips}")
+    set(target "${triple_cpu_mips}${triple_suffix_gnu}")
+  elseif("${arch}" MATCHES "^arm")
+    # FIXME: Handle arch other than arm, armhf, armv6m
+    if (${arch} STREQUAL "armhf")
+      # If we are building for hard float but our ABI is soft float.
+      if ("${triple_suffix}" MATCHES ".*eabi$")
+        # Change "eabi" -> "eabihf"
+        set(triple_suffix "${triple_suffix}hf")
+      endif()
+      # ABI is already set in the triple, don't repeat it in the architecture.
+      set(arch "arm")
+    else ()
+      # If we are building for soft float, but the triple's ABI is hard float.
+      if ("${triple_suffix}" MATCHES ".*eabihf$")
+        # Change "eabihf" -> "eabi"
+        string(REGEX REPLACE "hf$" "" triple_suffix "${triple_suffix}")
+      endif()
+    endif()
+    set(target "${arch}${triple_suffix}")
+  elseif("${arch}" MATCHES "^amdgcn")
+    set(target "amdgcn-amd-amdhsa")
+  elseif("${arch}" MATCHES "^nvptx")
+    set(target "nvptx64-nvidia-cuda")
+  else()
+    set(target "${arch}${triple_suffix}")
+  endif()
+  set(${outvar} "${target}" PARENT_SCOPE)
+endfunction()
diff --git a/flang-rt/lib/CMakeLists.txt b/flang-rt/lib/CMakeLists.txt
new file mode 100644
index 000000000000000..8e2e0fbbbc24fd9
--- /dev/null
+++ b/flang-rt/lib/CMakeLists.txt
@@ -0,0 +1,18 @@
+#===-- lib/CMakeLists.txt --------------------------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+add_subdirectory(FortranFloat128Math)
+add_subdirectory(flang_rt)
+
+if (FLANG_RT_ENABLE_CUF)
+  add_subdirectory(flang_rt/CUDA)
+endif()
+
+if (FLANG_RT_INCLUDE_TESTS)
+  add_subdirectory(Testing)
+endif ()
diff --git a/flang-rt/lib/FortranFloat128Math/CMakeLists.txt b/flang-rt/lib/FortranFloat128Math/CMakeLists.txt
new file mode 100644
index 000000000000000..c0b2ee32248f877
--- /dev/null
+++ b/flang-rt/lib/FortranFloat128Math/CMakeLists.txt
@@ -0,0 +1,136 @@
+#===-- lib/FortranFloat128Math/CMakeLists.txt ------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+# FortranFloat128 implements IEEE-754 128-bit float math functions.
+# It is a thin wapper and it currently relies on third-party
+# libraries available for the target.
+# It is distributed as a static library only.
+# Fortran programs/libraries that end up linking any of the provided
+# will have a dependency on the third-party library that is being
+# used for building this FortranFloat128Math library.
+
+include(CheckLibraryExists)
+include(CheckIncludeFile)
+
+set(sources
+  acos.cpp
+  acosh.cpp
+  asin.cpp
+  asinh.cpp
+  atan.cpp
+  atan2.cpp
+  atanh.cpp
+  ceil.cpp
+  complex-math.c
+  cos.cpp
+  cosh.cpp
+  erf.cpp
+  erfc.cpp
+  exp.cpp
+  exponent.cpp
+  floor.cpp
+  fma.cpp
+  fraction.cpp
+  hypot.cpp
+  j0.cpp
+  j1.cpp
+  jn.cpp
+  lgamma.cpp
+  llround.cpp
+  log.cpp
+  log10.cpp
+  lround.cpp
+  mod-real.cpp
+  modulo-real.cpp
+  nearest.cpp
+  nearbyint.cpp
+  norm2.cpp
+  pow.cpp
+  random.cpp
+  remainder.cpp
+  round.cpp
+  rrspacing.cpp
+  scale.cpp
+  set-exponent.cpp
+  sin.cpp
+  sinh.cpp
+  spacing.cpp
+  sqrt.cpp
+  tan.cpp
+  tanh.cpp
+  tgamma.cpp
+  trunc.cpp
+  y0.cpp
+  y1.cpp
+  yn.cpp
+  )
+
+include_directories(AFTER "${CMAKE_CURRENT_SOURCE_DIR}/..")
+add_library(FortranFloat128MathILib INTERFACE)
+target_include_directories(FortranFloat128MathILib INTERFACE
+  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
+  )
+
+if (FLANG_RUNTIME_F128_MATH_LIB)
+  if (${FLANG_RUNTIME_F128_MATH_LIB} STREQUAL "libquadmath")
+    check_include_file(quadmath.h FOUND_QUADMATH_HEADER)
+    if(FOUND_QUADMATH_HEADER)
+      add_compile_definitions(HAS_QUADMATHLIB)
+    else()
+      message(FATAL_ERROR
+        "FLANG_RUNTIME_F128_MATH_LIB setting requires quadmath.h "
+        "to be available: ${FLANG_RUNTIME_F128_MATH_LIB}"
+        )
+    endif()
+  else()
+    message(FATAL_ERROR
+      "Unsupported third-party library for Fortran F128 math runtime: "
+      "${FLANG_RUNTIME_F128_MATH_LIB}"
+      )
+  endif()
+
+  if (WIN32)
+    # Do not create a FortranFloat128Math library under Windows, the Flang
+    # driver never links it. Instead, add the sources to flang_rt itself.
+    target_sources(FortranFloat128MathILib INTERFACE ${sources})
+    target_compile_definitions(FortranFloat128MathILib INTERFACE HAS_QUADMATHLIB)
+  else ()
+    add_flangrt_library(FortranFloat128Math STATIC INSTALL_WITH_TOOLCHAIN
+      ${sources})
+    target_include_directories(FortranFloat128Math PRIVATE
+        "${FLANG_RT_SOURCE_DIR}/lib/flang_rt"
+      )
+  endif ()
+elseif (HAVE_LDBL_MANT_DIG_113)
+  # We can use 'long double' versions from libc.
+  check_library_exists(m sinl "" FOUND_LIBM)
+  if (FOUND_LIBM)
+    target_compile_definitions(FortranFloat128MathILib INTERFACE
+      HAS_LIBM
+      )
+    target_include_directories(FortranFloat128MathILib INTERFACE
+      "${FLANG_RT_SOURCE_DIR}/lib/flang_rt"
+      )
+    target_sources(FortranFloat128MathILib INTERFACE ${sources})
+  else()
+    message(FATAL_ERROR "Flang-RT cannot build without libm")
+  endif()
+else()
+  # We can use '__float128' version from libc, if it has them.
+  check_library_exists(m sinf128 "" FOUND_LIBMF128)
+  if (FOUND_LIBMF128)
+    target_compile_definitions(FortranFloat128MathILib INTERFACE
+      HAS_LIBMF128
+      )
+    target_include_directories(FortranFloat128MathILib INTERFACE
+      "${FLANG_RT_SOURCE_DIR}/lib/flang_rt"
+      )
+    # Enable this, when math-entries.h and complex-math.h is ready.
+    # target_sources(FortranFloat128MathILib INTERFACE ${sources})
+  endif()
+endif()
diff --git a/flang-rt/lib/Testing/CMakeLists.txt b/flang-rt/lib/Testing/CMakeLists.txt
new file mode 100644
index 000000000000000..19c20ad44c0251a
--- /dev/null
+++ b/flang-rt/lib/Testing/CMakeLists.txt
@@ -0,0 +1,20 @@
+#===-- lib/Testing/CMakeLists.txt ------------------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+set(public_headers "")
+file(GLOB_RECURSE public_headers
+  "${FLANG_SOURCE_DIR}/lib/Testing/*.h"
+)
+
+add_flangrt_library(NonGTestTesting EXCLUDE_FROM_ALL LINK_TO_LLVM
+      ${FLANG_SOURCE_DIR}/lib/Testing/testing.cpp
+      ${FLANG_SOURCE_DIR}/lib/Testing/fp-testing.cpp
+
+    ADDITIONAL_HEADERS
+      ${public_headers}
+  )
diff --git a/flang-rt/lib/flang_rt/CMakeLists.txt b/flang-rt/lib/flang_rt/CMakeLists.txt
new file mode 100644
index 000000000000000..4db1a1cbb874fc2
--- /dev/null
+++ b/flang-rt/lib/flang_rt/CMakeLists.txt
@@ -0,0 +1,213 @@
+#===-- lib/flang_rt/CMakeLists.txt -----------------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+include(AddFlangRTOffload)
+# function checks
+find_package(Backtrace)
+set(HAVE_BACKTRACE ${Backtrace_FOUND})
+set(BACKTRACE_HEADER ${Backtrace_HEADER})
+
+
+# List of files that are buildable for all devices.
+set(supported_sources
+  ${FLANG_SOURCE_DIR}/lib/Decimal/binary-to-decimal.cpp
+  ${FLANG_SOURCE_DIR}/lib/Decimal/decimal-to-binary.cpp
+  ISO_Fortran_binding.cpp
+  allocator-registry.cpp
+  allocatable.cpp
+  array-constructor.cpp
+  assign.cpp
+  buffer.cpp
+  character.cpp
+  connection.cpp
+  copy.cpp
+  derived-api.cpp
+  derived.cpp
+  descriptor-io.cpp
+  descriptor.cpp
+  dot-product.cpp
+  edit-input.cpp
+  edit-output.cpp
+  environment.cpp
+  external-unit.cpp
+  extrema.cpp
+  file.cpp
+  findloc.cpp
+  format.cpp
+  inquiry.cpp
+  internal-unit.cpp
+  io-api.cpp
+  io-api-minimal.cpp
+  io-error.cpp
+  io-stmt.cpp
+  iostat.cpp
+  matmul-transpose.cpp
+  matmul.cpp
+  memory.cpp
+  misc-intrinsic.cpp
+  namelist.cpp
+  non-tbp-dio.cpp
+  numeric.cpp
+  pointer.cpp
+  product.cpp
+  pseudo-unit.cpp
+  ragged.cpp
+  stat.cpp
+  sum.cpp
+  support.cpp
+  terminator.cpp
+  tools.cpp
+  transformational.cpp
+  type-code.cpp
+  type-info.cpp
+  unit.cpp
+  unit-map.cpp
+  utf.cpp
+)
+
+# List of source not used for GPU offloading.
+set(host_sources
+  ${FLANG_SOURCE_DIR}/module/iso_fortran_env_impl.f90
+  command.cpp
+  complex-powi.cpp
+  complex-reduction.c
+  exceptions.cpp
+  execute.cpp
+  extensions.cpp
+  main.cpp
+  random.cpp
+  reduce.cpp
+  reduction.cpp
+  stop.cpp
+  temporary-stack.cpp
+  time-intrinsic.cpp
+)
+
+file(GLOB_RECURSE public_headers
+  "${FLANG_RT_SOURCE_DIR}/include/flang_rt/*.h"
+  "${FLANG_SOURCE_DIR}/include/flang/Common/*.h"
+  )
+
+file(GLOB_RECURSE private_headers
+  "${FLANG_RT_SOURCE_DIR}/lib/flang_rt/*.h"
+  "${FLANG_SOURCE_DIR}/lib/Common/*.h"
+  )
+
+
+# Import changes from sibling FortranFloat128Math
+get_target_property(f128_sources
+  FortranFloat128MathILib INTERFACE_SOURCES
+  )
+if (f128_sources)
+  # The interface may define special macros for Float128Math files,
+  # so we need to propagate them.
+  get_target_property(f128_defs
+    FortranFloat128MathILib INTERFACE_COMPILE_DEFINITIONS
+    )
+  set_property(SOURCE ${f128_sources}
+    APPEND PROPERTY COMPILE_DEFINITIONS
+    ${f128_defs}
+    )
+  get_target_property(f128_include_dirs
+    FortranFloat128MathILib INTERFACE_INCLUDE_DIRECTORIES
+    )
+  set_property(SOURCE ${f128_sources}
+    APPEND PROPERTY INCLUDE_DIRECTORIES
+    ${f128_include_dirs}
+    )
+else ()
+  set(f128_sources "")
+endif ()
+
+
+set(sources ${supported_sources} ${host_sources} ${f128_sources})
+
+if (NOT WIN32)
+  add_flangrt_library(flang_rt STATIC
+    ${sources}
+    INSTALL_WITH_TOOLCHAIN
+    ADDITIONAL_HEADERS ${public_headers} ${private_headers}
+  )
+
+  enable_cuda_compilation(flang_rt "${supported_sources}")
+  enable_omp_offload_compilation(flang_rt "${supported_sources}")
+
+  # For unittests that depend on flang_rt. Should link to the static version
+  # of the library.
+  add_library(flang_rt.static ALIAS flang_rt)
+  add_library(flang_rt.unittest ALIAS flang_rt)
+else()
+  # Target for building all versions of the runtime
+  add_custom_target(flang_rt)
+  set_target_properties(flang_rt PROPERTIES FOLDER "Flang-RT/Meta")
+
+  function (add_win_flangrt_library libtype suffix msvc_lib)
+    set(name "flang_rt.${suffix}")
+    add_flangrt_library(${name} ${libtype}
+        ${sources}
+        ${ARGN}
+        ADDITIONAL_HEADERS ${public_headers} ${private_headers}
+      )
+
+    if (msvc_lib)
+      set_target_properties(${name}
+          PROPERTIES
+            MSVC_RUNTIME_LIBRARY "${msvc_lib}"
+        )
+    endif ()
+
+    # Setting an unique Fortran_MODULE_DIRECTORY is required for each variant to
+    # write a different .mod file.
+    set_target_properties(${name}
+        PROPERTIES
+          Fortran_MODULE_DIRECTORY "module.${suffix}"
+      )
+
+    enable_cuda_compilation(${name} "${supported_sources}")
+    enable_omp_offload_compilation(${name} "${supported_sources}")
+    add_dependencies(flang_rt ${name})
+  endfunction ()
+
+  # Variants of the static flang_rt for different versions of the msvc runtime.
+  #
+  # The dynamic/dynamic_dbg variants are not DLLs themselves, only require
+  # linking to msvcrt(d).dll.
+  # FIXME: Generating actual runtime DLLs is currently not possible. There are
+  # two roadblocks:
+  #
+  #  * Flang emits /DEFAULTLIB:flang_rt.dynamic.lib into
+  #    iso_fortran_env_impl.f90.obj. Because that file is itself part of
+  #    flang_rt.dynamic, this results in a recursive dependency when invoking
+  #    the linker.
+  #
+  #  * The externally-visible functions must either be annotated with
+  #    __declspec(dllexport), or listed in an exports file. A possible workaround
+  #    is CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS which would also export the internal
+  #    C++ symbols and still requires global data symbols to be annotated
+  #    manually.
+  add_win_flangrt_library(STATIC static      MultiThreaded         INSTALL_WITH_TOOLCHAIN)
+  add_win_flangrt_library(STATIC static_dbg  MultiThreadedDebug    INSTALL_WITH_TOOLCHAIN)
+  add_win_flangrt_library(STATIC dynamic     MultiThreadedDLL      INSTALL_WITH_TOOLCHAIN)
+  add_win_flangrt_library(STATIC dynamic_dbg MultiThreadedDebugDLL INSTALL_WITH_TOOLCHAIN)
+
+  # Unittests link against LLVMSupport which is using CMake's default runtime
+  # library selection, which is either MultiThreadedDLL or MultiThreadedDebugDLL
+  # depending on the configuration. They have to match or linking will fail.
+  if (GENERATOR_IS_MULTI_CONFIG)
+    # We cannot select an ALIAS library because it may be different
+    # per configuration. Fallback to CMake's default.
+    add_win_flangrt_library(STATIC unittest "" EXCLUDE_FROM_ALL)
+  else ()
+    string(TOLOWER ${CMAKE_BUILD_TYPE} build_type)
+    if (build_type STREQUAL "debug")
+      add_library(flang_rt.unittest ALIAS flang_rt.dynamic_dbg)
+    else ()
+      add_library(flang_rt.unittest ALIAS flang_rt.dynamic)
+    endif ()
+  endif ()
+endif()
diff --git a/flang-rt/lib/flang_rt/CUDA/CMakeLists.txt b/flang-rt/lib/flang_rt/CUDA/CMakeLists.txt
new file mode 100644
index 000000000000000..3c741ac93d64b22
--- /dev/null
+++ b/flang-rt/lib/flang_rt/CUDA/CMakeLists.txt
@@ -0,0 +1,33 @@
+#===-- lib/flang_rt/CUDA/CMakeLists.txt ------------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+
+add_flangrt_library(CufRuntime STATIC
+  allocatable.cpp
+  allocator.cpp
+  descriptor.cpp
+  kernel.cpp
+  memmove-function.cpp
+  memory.cpp
+  registration.cpp
+
+  # libCufRuntime depends on a certain version of CUDA. To be able to have
+  # multiple build of this library with different CUDA version, the version is
+  # added to the library name.
+  TARGET_PROPERTIES
+    OUTPUT_NAME "CufRuntime_cuda_${CUDAToolkit_VERSION_MAJOR}"
+
+  INCLUDE_DIRECTORIES
+    PRIVATE ${CUDAToolkit_INCLUDE_DIRS}
+)
+
+target_link_libraries(CufRuntime
+  PUBLIC
+  flang_rt
+  CUDA::cudart_static
+)
diff --git a/flang-rt/lib/flang_rt/io-api-minimal.cpp b/flang-rt/lib/flang_rt/io-api-minimal.cpp
index c3234c78c10c806..cd5cf6bd3e6991b 100644
--- a/flang-rt/lib/flang_rt/io-api-minimal.cpp
+++ b/flang-rt/lib/flang_rt/io-api-minimal.cpp
@@ -150,7 +150,7 @@ bool IODEF(OutputLogical)(Cookie cookie, bool truth) {
 // Provide own definition for `std::__libcpp_verbose_abort` to avoid dependency
 // on the version provided by libc++.
 
-void std::__libcpp_verbose_abort(char const *format, ...) {
+void std::__libcpp_verbose_abort(char const *format, ...) noexcept {
   va_list list;
   va_start(list, format);
   std::vfprintf(stderr, format, list);
diff --git a/flang-rt/test/CMakeLists.txt b/flang-rt/test/CMakeLists.txt
new file mode 100644
index 000000000000000..5ca07b66e4f9b5f
--- /dev/null
+++ b/flang-rt/test/CMakeLists.txt
@@ -0,0 +1,59 @@
+#===-- test/CMakeLists.txt -------------------------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+# Test runner infrastructure for Flang. This configures the Flang test trees
+# for use by Lit, and delegates to LLVM's lit test handlers.
+
+llvm_canonicalize_cmake_booleans(
+  FLANG_STANDALONE_BUILD
+  LLVM_BUILD_EXAMPLES
+  LLVM_BYE_LINK_INTO_TOOLS
+  LLVM_ENABLE_PLUGINS
+)
+
+configure_lit_site_cfg(
+  ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
+  ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py
+  MAIN_CONFIG
+  ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py
+)
+
+if (TARGET FlangRTUnitTests)
+  configure_lit_site_cfg(
+    ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
+    ${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py
+    MAIN_CONFIG
+    ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.cfg.py
+  )
+
+  configure_lit_site_cfg(
+    ${CMAKE_CURRENT_SOURCE_DIR}/NonGtestUnit/lit.site.cfg.py.in
+    ${CMAKE_CURRENT_BINARY_DIR}/NonGtestUnit/lit.site.cfg.py
+    MAIN_CONFIG
+    ${CMAKE_CURRENT_SOURCE_DIR}/NonGtestUnit/lit.cfg.py
+  )
+endif ()
+
+
+add_custom_target(flang-rt-test-depends)
+set_target_properties(flang-rt-test-depends PROPERTIES FOLDER "Flang-RT/Meta")
+add_dependencies(flang-rt-test-depends
+    FlangRTUnitTests
+    flang_rt.unittest
+    flang_rt.static
+  )
+
+add_lit_testsuite(check-flang-rt "Running the Flang-RT regression tests"
+    ${CMAKE_CURRENT_BINARY_DIR}
+    DEPENDS flang-rt-test-depends
+  )
+set_target_properties(check-flang-rt PROPERTIES FOLDER "Flang-RT/Meta")
+
+add_lit_testsuites(flang-rt ${CMAKE_CURRENT_SOURCE_DIR}
+    DEPENDS flang-rt-test-depends
+  )
diff --git a/flang-rt/test/Driver/ctofortran.f90 b/flang-rt/test/Driver/ctofortran.f90
index 10c7adaccc9588b..e385e7974cdc18a 100644
--- a/flang-rt/test/Driver/ctofortran.f90
+++ b/flang-rt/test/Driver/ctofortran.f90
@@ -1,8 +1,10 @@
 ! UNSUPPORTED: system-windows
-! REQUIRES: flang-rt
+! UNSUPPORTED: offload-cuda
+
 ! RUN: split-file %s %t
-! RUN: chmod +x %t/runtest.sh
-! RUN: %t/runtest.sh %t %t/ffile.f90 %t/cfile.c %flang | FileCheck %s
+! RUN: %clang -I"%include/flang" -c %t/cfile.c -o %t/cfile.o
+! RUN: %flang -L"%libdir" %t/ffile.f90 %t/cfile.o -o %t/ctofortran
+! RUN: env LD_LIBRARY_PATH="$LD_LIBRARY_PATH:%libdir" %t/ctofortran | FileCheck %s
 
 !--- ffile.f90
 program fmain
@@ -66,24 +68,3 @@ end subroutine foo
   foo(desc);
   return;
 }
-!--- runtest.sh
-#!/bin/bash
-TMPDIR=$1
-FFILE=$2
-CFILE=$3
-FLANG=$4
-shift 4
-FLAGS="$*"
-BINDIR=`dirname $FLANG`
-LIBDIR=$BINDIR/../lib
-CCOMP=$BINDIR/clang
-if [ -x $CCOMP ]
-then
-  export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBDIR
-  $CCOMP $FLAGS -c $CFILE -o $TMPDIR/cfile.o
-  $FLANG $FLAGS $FFILE $TMPDIR/cfile.o -o $TMPDIR/ctofortran
-  $TMPDIR/ctofortran # should print "PASS"
-else
-  # No clang compiler, just pass by default
-  echo "PASS"
-fi
diff --git a/flang-rt/test/Driver/exec.f90 b/flang-rt/test/Driver/exec.f90
index 9ca91ee24011c9f..5a81a1e4c3e4504 100644
--- a/flang-rt/test/Driver/exec.f90
+++ b/flang-rt/test/Driver/exec.f90
@@ -1,10 +1,10 @@
-! UNSUPPORTED: system-windows
 ! REQUIRES: flang-rt
+! UNSUPPORTED: offload-cuda
+
 ! Verify that flang can correctly build executables.
 
-! RUN: %flang %s -o %t
-! RUN: env LD_LIBRARY_PATH="$LD_LIBRARY_PATH:%llvmshlibdir" %t | FileCheck %s
-! RUN: rm -f %t
+! RUN: %flang -L"%libdir" %s -o %t
+! RUN: env LD_LIBRARY_PATH="$LD_LIBRARY_PATH:%libdir" %t | FileCheck %s
 
 ! CHECK: Hello, World!
 program hello
diff --git a/flang-rt/test/NonGtestUnit/lit.cfg.py b/flang-rt/test/NonGtestUnit/lit.cfg.py
new file mode 100644
index 000000000000000..4bee709b78f43fb
--- /dev/null
+++ b/flang-rt/test/NonGtestUnit/lit.cfg.py
@@ -0,0 +1,22 @@
+# -*- Python -*-
+
+import os
+
+import lit.formats
+
+# name: The name of this test suite.
+config.name = "flang-rt-OldUnit"
+
+# suffixes: A list of file extensions to treat as test files.
+# On Windows, ".exe" also matches the GTests and will execited redundantly.
+config.suffixes = [".test", ".exe"]
+
+# test_source_root: The root path where unit test binaries are located.
+config.test_source_root = os.path.join(config.flangrt_binary_dir, "unittests")
+
+# test_exec_root: The root path where tests should be run.
+# lit writes a '.lit_test_times.txt' file into this directory.
+config.test_exec_root = config.flang_rt_binary_test_dir
+
+# testFormat: The test format to use to interpret tests.
+config.test_format = lit.formats.ExecutableTest()
diff --git a/flang-rt/test/NonGtestUnit/lit.site.cfg.py.in b/flang-rt/test/NonGtestUnit/lit.site.cfg.py.in
new file mode 100644
index 000000000000000..8244b5b62d3aafb
--- /dev/null
+++ b/flang-rt/test/NonGtestUnit/lit.site.cfg.py.in
@@ -0,0 +1,14 @@
+ at LIT_SITE_CFG_IN_HEADER@
+
+import os
+
+config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
+config.flang_rt_source_dir = "@FLANG_RT_SOURCE_DIR@"
+config.flangrt_binary_dir = "@FLANG_RT_BINARY_DIR@"
+config.flang_rt_binary_test_dir = os.path.dirname(__file__)
+
+import lit.llvm
+lit.llvm.initialize(lit_config, config)
+
+# Let the main config do the real work.
+lit_config.load_config(config, os.path.join(config.flang_rt_source_dir, 'test', 'NonGtestUnit', 'lit.cfg.py'))
diff --git a/flang-rt/test/Runtime/no-cpp-dep.c b/flang-rt/test/Runtime/no-cpp-dep.c
index c03029f0f3c26db..2a416bdb27ee221 100644
--- a/flang-rt/test/Runtime/no-cpp-dep.c
+++ b/flang-rt/test/Runtime/no-cpp-dep.c
@@ -3,10 +3,11 @@ This test makes sure that flang's runtime does not depend on the C++ runtime
 library. It tries to link this simple file against libflang_rt.a with
 a C compiler.
 
-REQUIRES: c-compiler, flang-rt
+UNSUPPORTED: system-windows
+UNSUPPORTED: offload-cuda
 
 RUN: %if system-aix %{ export OBJECT_MODE=64 %}
-RUN: %cc -std=c99 %s -I%include %libruntime -lm  \
+RUN: %cc -std=c99 %s -I%include -L"%libdir" -lflang_rt -lm \
 RUN: %if system-aix %{-lpthread %}
 RUN: rm a.out
 */
diff --git a/flang-rt/test/Unit/lit.cfg.py b/flang-rt/test/Unit/lit.cfg.py
new file mode 100644
index 000000000000000..516bc653f413f41
--- /dev/null
+++ b/flang-rt/test/Unit/lit.cfg.py
@@ -0,0 +1,21 @@
+# -*- Python -*-
+
+import os
+
+import lit.formats
+
+# name: The name of this test suite.
+config.name = "flang-rt-Unit"
+
+# suffixes: A list of file extensions to treat as test files.
+config.suffixes = []
+
+# test_source_root: The root path where unit test binaries are located.
+config.test_source_root = os.path.join(config.flangrt_binary_dir, "unittests")
+
+# test_exec_root: The root path where tests should be run.
+# lit writes a '.lit_test_times.txt' file into this directory.
+config.test_exec_root = config.flang_rt_binary_test_dir
+
+# testFormat: The test format to use to interpret tests.
+config.test_format = lit.formats.GoogleTest(config.llvm_build_mode, "Tests")
diff --git a/flang-rt/test/Unit/lit.site.cfg.py.in b/flang-rt/test/Unit/lit.site.cfg.py.in
new file mode 100644
index 000000000000000..ed6dea07bcdde1d
--- /dev/null
+++ b/flang-rt/test/Unit/lit.site.cfg.py.in
@@ -0,0 +1,15 @@
+ at LIT_SITE_CFG_IN_HEADER@
+
+import os
+
+config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
+config.llvm_build_mode = "@LLVM_BUILD_MODE@"
+config.flang_rt_source_dir = "@FLANG_RT_SOURCE_DIR@"
+config.flangrt_binary_dir = "@FLANG_RT_BINARY_DIR@"
+config.flang_rt_binary_test_dir = os.path.dirname(__file__)
+
+import lit.llvm
+lit.llvm.initialize(lit_config, config)
+
+# Let the main config do the real work.
+lit_config.load_config(config, os.path.join(config.flang_rt_source_dir, 'test', 'Unit', 'lit.cfg.py'))
diff --git a/flang-rt/test/lit.cfg.py b/flang-rt/test/lit.cfg.py
new file mode 100644
index 000000000000000..08f3f1121c8960e
--- /dev/null
+++ b/flang-rt/test/lit.cfg.py
@@ -0,0 +1,102 @@
+# -*- Python -*-
+
+import shlex
+import lit.util
+
+from lit.llvm import llvm_config
+from lit.llvm.subst import ToolSubst, FindTool
+
+
+def shjoin(args, sep=" "):
+    return sep.join([shlex.quote(arg) for arg in args])
+
+
+# Configuration file for the 'lit' test runner.
+
+# name: The name of this test suite.
+config.name = "flang-rt"
+
+# testFormat: The test format to use to interpret tests.
+#
+# For now we require '&&' between commands, until they get globally killed and
+# the test runner updated.
+config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
+
+# suffixes: A list of file extensions to treat as test files.
+config.suffixes = [
+    ".c",
+    ".cpp",
+    ".f",
+    ".F",
+    ".ff",
+    ".FOR",
+    ".for",
+    ".f77",
+    ".f90",
+    ".F90",
+    ".ff90",
+    ".f95",
+    ".F95",
+    ".ff95",
+    ".fpp",
+    ".FPP",
+    ".cuf",
+    ".CUF",
+    ".f18",
+    ".F18",
+    ".f03",
+    ".F03",
+    ".f08",
+    ".F08",
+    ".ll",
+    ".fir",
+    ".mlir",
+]
+
+llvm_config.use_default_substitutions()
+
+# test_source_root: The root path where tests are located.
+config.test_source_root = os.path.dirname(__file__)
+
+# test_exec_root: The root path where tests should be run.
+# lit writes a '.lit_test_times.txt' file into this directory.
+config.test_exec_root = config.flang_rt_binary_test_dir
+
+# On MacOS, -isysroot is needed to build binaries.
+isysroot_flag = []
+if config.osx_sysroot:
+    isysroot_flag = ["-isysroot", config.osx_sysroot]
+
+tools = [
+    ToolSubst(
+        "%flang",
+        command=config.flang,
+        extra_args=isysroot_flag,
+        unresolved="fatal",
+    ),
+    ToolSubst(
+        "%clang",
+        command=FindTool("clang"),
+        extra_args=isysroot_flag,
+        unresolved="fatal",
+    ),
+    ToolSubst("%cc",
+        command=config.cc,
+        extra_args=isysroot_flag,
+        unresolved="fatal"
+    ),
+]
+llvm_config.add_tool_substitutions(tools)
+
+# Let tests find LLVM's standard tools (FileCheck, split-file, not, ...)
+llvm_config.with_environment("PATH", config.llvm_tools_dir, append_path=True)
+
+# Include path for C headers that define Flang's Fortran ABI.
+config.substitutions.append(("%include", os.path.join(config.flang_source_dir, "include")))
+
+# Library path of libflang_rt.a (for lib search path when using non-Flang driver for linking)
+config.substitutions.append(("%libdir", config.flang_rt_output_resource_lib_dir))
+
+# For CUDA offloading, additional steps (device linking) and libraries (cudart) are needed.
+if config.flang_rt_experimental_offload_support == "CUDA":
+    config.available_features.add("offload-cuda")
diff --git a/flang-rt/test/lit.site.cfg.py.in b/flang-rt/test/lit.site.cfg.py.in
new file mode 100644
index 000000000000000..662d076b1fe24f2
--- /dev/null
+++ b/flang-rt/test/lit.site.cfg.py.in
@@ -0,0 +1,19 @@
+ at LIT_SITE_CFG_IN_HEADER@
+
+import sys
+
+config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
+config.flang_source_dir = "@FLANG_SOURCE_DIR@"
+config.flang_rt_source_dir = "@FLANG_RT_SOURCE_DIR@"
+config.flang_rt_binary_test_dir = os.path.dirname(__file__)
+config.flang_rt_output_resource_lib_dir = "@FLANG_RT_OUTPUT_RESOURCE_LIB_DIR@"
+config.flang_rt_experimental_offload_support = "@FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT@"
+config.cc = "@CMAKE_C_COMPILER@"
+config.flang = "@CMAKE_Fortran_COMPILER@"
+config.osx_sysroot = path(r"@CMAKE_OSX_SYSROOT@")
+
+import lit.llvm
+lit.llvm.initialize(lit_config, config)
+
+# Let the main config do the real work.
+lit_config.load_config(config, os.path.join(config.flang_rt_source_dir, 'test', 'lit.cfg.py'))
diff --git a/flang-rt/unittests/CMakeLists.txt b/flang-rt/unittests/CMakeLists.txt
new file mode 100644
index 000000000000000..ca94805f1403bcd
--- /dev/null
+++ b/flang-rt/unittests/CMakeLists.txt
@@ -0,0 +1,111 @@
+#===-- unittests/CMakeLists.txt --------------------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+# LLVM uses a modified version of GTest that uses LLVMSupport for console
+# output. Therefore it also needs to include files from LLVM. Unfortunately,
+# LLVM/GTest doesn't add the include search path itself. Limiting the scope
+# using target_include_directories does not work because with
+# LLVM_INSTALL_GTEST=ON, as llvm_gtest is an IMPORT library.
+include_directories("${LLVM_INCLUDE_DIR}" "${LLVM_MAIN_INCLUDE_DIR}")
+
+# Add GTest if not already present.
+# Using a function so LLVM_SUBPROJECT_TITLE does not propagate.
+function (build_gtest)
+  set(LLVM_SUBPROJECT_TITLE "Third-Party/Google Test")
+  add_subdirectory("${LLVM_THIRD_PARTY_DIR}/unittest" "${CMAKE_CURRENT_BINARY_DIR}/third-party/unittest")
+endfunction ()
+if (NOT TARGET llvm_gtest)
+  build_gtest()
+endif ()
+
+if (CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG)
+  add_compile_options("-Wno-suggest-override")
+endif()
+
+
+# Target that depends on all unittests
+add_custom_target(FlangRTUnitTests)
+set_target_properties(FlangRTUnitTests PROPERTIES FOLDER "Flang-RT/Meta")
+
+
+function(add_flangrt_unittest_offload_properties target)
+  # Set CUDA_RESOLVE_DEVICE_SYMBOLS.
+  if (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "CUDA")
+    set_target_properties(${target}
+      PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON
+      )
+  endif()
+  # Enable OpenMP offload during linking. We may need to replace
+  # LINK_OPTIONS with COMPILE_OPTIONS when there are OpenMP offload
+  # unittests.
+  #
+  # FIXME: replace 'native' in --offload-arch option with the list
+  #        of targets that Fortran Runtime was built for.
+  if (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "OpenMP")
+    set_target_properties(${target}
+      PROPERTIES LINK_OPTIONS
+      "-fopenmp;--offload-arch=native"
+      )
+  endif()
+endfunction()
+
+
+function(add_flangrt_unittest test_dirname)
+  cmake_parse_arguments(ARG
+    ""
+    ""
+    "LINK_LIBS"
+    ${ARGN})
+
+  add_unittest(FlangRTUnitTests ${test_dirname} ${ARG_UNPARSED_ARGUMENTS})
+
+  target_include_directories(${test_dirname} PRIVATE "${FLANG_RT_SOURCE_DIR}/include")
+  target_include_directories(${test_dirname} PRIVATE "${FLANG_SOURCE_DIR}/include")
+  target_link_libraries(${test_dirname} PRIVATE ${ARG_LINK_LIBS})
+  add_flangrt_unittest_offload_properties(${test_dirname})
+
+  # Required because LLVMSupport is compiled with this option.
+  # FIXME: According to CMake documentation, this is the default. Why is it
+  #        needed? LLVM's add_unittest doesn't set it either.
+  set_target_properties(${test_dirname}
+      PROPERTIES
+        MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"
+    )
+endfunction()
+
+function(add_flangrt_nongtest_unittest test_name)
+  cmake_parse_arguments(ARG
+    "SLOW_TEST"
+    ""
+    "LINK_LIBS"
+    ${ARGN})
+
+  if(ARG_SLOW_TEST)
+      set(suffix .slow)
+  else()
+      set(suffix .test)
+  endif()
+
+  add_executable(${test_name}${suffix} EXCLUDE_FROM_ALL ${ARG_UNPARSED_ARGUMENTS})
+  set_target_properties(${test_name}${suffix} PROPERTIES FOLDER "Flang-RT/Tests/Unit")
+
+  target_include_directories(${test_name}${suffix} PRIVATE
+      "${FLANG_RT_SOURCE_DIR}/include"
+      "${FLANG_SOURCE_DIR}/include"
+    )
+  target_link_libraries(${test_name}${suffix} PRIVATE NonGTestTesting ${ARG_LINK_LIBS})
+
+  if(NOT ARG_SLOW_TEST)
+    add_dependencies(FlangRTUnitTests ${test_name}${suffix})
+  endif()
+
+  add_flangrt_unittest_offload_properties(${test_name}${suffix})
+endfunction()
+
+add_subdirectory(Evaluate)
+add_subdirectory(Runtime)
diff --git a/flang-rt/unittests/Evaluate/CMakeLists.txt b/flang-rt/unittests/Evaluate/CMakeLists.txt
new file mode 100644
index 000000000000000..15b813bcb19da1c
--- /dev/null
+++ b/flang-rt/unittests/Evaluate/CMakeLists.txt
@@ -0,0 +1,21 @@
+#===-- unittests/Evaluate/CMakeLists.txt -----------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+add_flangrt_nongtest_unittest(reshape
+  reshape.cpp
+
+  LINK_LIBS
+    flang_rt.unittest
+)
+
+add_flangrt_nongtest_unittest(ISO-Fortran-binding
+  ISO-Fortran-binding.cpp
+
+  LINK_LIBS
+    flang_rt.unittest
+)
diff --git a/flang-rt/unittests/Runtime/CMakeLists.txt b/flang-rt/unittests/Runtime/CMakeLists.txt
new file mode 100644
index 000000000000000..32f12b14caca7a4
--- /dev/null
+++ b/flang-rt/unittests/Runtime/CMakeLists.txt
@@ -0,0 +1,48 @@
+#===-- unittests/Runtime/CMakeLists.txt ------------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+add_flangrt_unittest(RuntimeTests
+  AccessTest.cpp
+  Allocatable.cpp
+  ArrayConstructor.cpp
+  BufferTest.cpp
+  CharacterTest.cpp
+  CommandTest.cpp
+  Complex.cpp
+  CrashHandlerFixture.cpp
+  Derived.cpp
+  ExternalIOTest.cpp
+  Format.cpp
+  Inquiry.cpp
+  ListInputTest.cpp
+  LogicalFormatTest.cpp
+  Matmul.cpp
+  MatmulTranspose.cpp
+  MiscIntrinsic.cpp
+  Namelist.cpp
+  Numeric.cpp
+  NumericalFormatTest.cpp
+  Pointer.cpp
+  Ragged.cpp
+  Random.cpp
+  Reduction.cpp
+  RuntimeCrashTest.cpp
+  Stop.cpp
+  Support.cpp
+  Time.cpp
+  TemporaryStack.cpp
+  Transformational.cpp
+
+  LINK_LIBS
+    flang_rt.unittest
+)
+target_compile_definitions(RuntimeTests PRIVATE NOT_EXE="${LLVM_TOOLS_DIR}/not${CMAKE_EXECUTABLE_SUFFIX}")
+
+if (FLANG_RT_ENABLE_CUF)
+  add_subdirectory(CUDA)
+endif ()
diff --git a/flang-rt/unittests/Runtime/CUDA/CMakeLists.txt b/flang-rt/unittests/Runtime/CUDA/CMakeLists.txt
new file mode 100644
index 000000000000000..37b117234208c4e
--- /dev/null
+++ b/flang-rt/unittests/Runtime/CUDA/CMakeLists.txt
@@ -0,0 +1,18 @@
+#===-- unittests/Runtime/CUDA/CMakeLists.txt -------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+add_flangrt_unittest(FlangCufRuntimeTests
+  Allocatable.cpp
+  AllocatorCUF.cpp
+  Memory.cpp
+)
+
+target_link_libraries(FlangCufRuntimeTests
+  PRIVATE
+  CufRuntime
+)
diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index 0044779da011d7c..38004c149b7835a 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -35,17 +35,6 @@ endif()
 
 option(FLANG_ENABLE_WERROR "Fail and stop building flang if a warning is triggered." OFF)
 
-# The out of tree builds of the compiler and the Fortran runtime
-# must use the same setting of FLANG_RUNTIME_F128_MATH_LIB
-# to be composable. Failure to synchronize this setting may result
-# in linking errors or fatal failures in F128 runtime functions.
-set(FLANG_RUNTIME_F128_MATH_LIB "" CACHE STRING
-  "Specifies the target library used for implementing IEEE-754 128-bit float \
-  math in F18 runtime, e.g. it might be libquadmath for targets where \
-  REAL(16) is mapped to __float128, or libm for targets where REAL(16) \
-  is mapped to long double, etc."
-  )
-
 # Check for a standalone build and configure as appropriate from
 # there.
 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
@@ -248,7 +237,25 @@ else()
   include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
 endif()
 
-option(FLANG_INCLUDE_RUNTIME "Build the runtime in-tree (deprecated; to be replaced with LLVM_ENABLE_RUNTIMES=flang-rt)" ON)
+set(FLANG_INCLUDE_RUNTIME_default ON)
+if ("flang-rt" IN_LIST LLVM_ENABLE_RUNTIMES)
+  set(FLANG_INCLUDE_RUNTIME_default OFF)
+endif ()
+option(FLANG_INCLUDE_RUNTIME "Build the runtime in-tree (deprecated; to be replaced with LLVM_ENABLE_RUNTIMES=flang-rt)" FLANG_INCLUDE_RUNTIME_default)
+if (FLANG_INCLUDE_RUNTIME)
+  if ("flang-rt" IN_LIST LLVM_ENABLE_RUNTIMES)
+    message(WARNING "Building Flang-RT using LLVM_ENABLE_RUNTIMES. FLANG_INCLUDE_RUNTIME=${FLANG_INCLUDE_RUNTIME} ignored.")
+    set(FLANG_INCLUDE_RUNTIME OFF)
+  else ()
+     message(STATUS "Building flang_rt in-tree")
+  endif ()
+else ()
+  if ("flang-rt" IN_LIST LLVM_ENABLE_RUNTIMES)
+    message(STATUS "Building Flang-RT using LLVM_ENABLE_RUNTIMES.")
+  else ()
+    message(STATUS "Not building Flang-RT. For a usable Fortran toolchain, compile a standalone Flang-RT")
+  endif ()
+endif ()
 
 set(FLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
     "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
@@ -358,20 +365,6 @@ if (FLANG_REPOSITORY_STRING)
   add_definitions(-DFLANG_REPOSITORY_STRING="${FLANG_REPOSITORY_STRING}")
 endif()
 
-if (FLANG_RUNTIME_F128_MATH_LIB)
-  add_compile_definitions(
-    FLANG_RUNTIME_F128_MATH_LIB="${FLANG_RUNTIME_F128_MATH_LIB}"
-    )
-endif()
-
-include(TestBigEndian)
-test_big_endian(IS_BIGENDIAN)
-if (IS_BIGENDIAN)
-  add_compile_definitions(FLANG_BIG_ENDIAN=1)
-else ()
-  add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
-endif ()
-
 # Configure Flang's Version.inc file.
 configure_file(
   ${CMAKE_CURRENT_SOURCE_DIR}/include/flang/Version.inc.in
@@ -469,6 +462,7 @@ if (APPLE)
 endif()
 
 include(AddFlang)
+include(FlangCommon)
 
 if (FLANG_INCLUDE_TESTS)
   add_compile_definitions(FLANG_INCLUDE_TESTS=1)
@@ -562,7 +556,12 @@ include(GetClangResourceDir)
 get_clang_resource_dir(HEADER_BINARY_DIR PREFIX ${LLVM_LIBRARY_OUTPUT_INTDIR}/.. SUBDIR include)
 configure_file(
   ${FLANG_SOURCE_DIR}/include/flang/ISO_Fortran_binding.h
-  ${HEADER_BINARY_DIR}/ISO_Fortran_binding.h)
+  ${HEADER_BINARY_DIR}/ISO_Fortran_binding.h COPYONLY)
+
+# llvm-test-suite fails if it does not find the the file in this location.
+configure_file(
+  ${FLANG_SOURCE_DIR}/include/flang/ISO_Fortran_binding.h
+  ${LLVM_RUNTIME_OUTPUT_INTDIR}/../include/flang COPYONLY)
 
 # And also install it into the install area
 get_clang_resource_dir(HEADER_INSTALL_DIR SUBDIR include)
diff --git a/flang/cmake/modules/FlangCommon.cmake b/flang/cmake/modules/FlangCommon.cmake
new file mode 100644
index 000000000000000..1b8606843b2240c
--- /dev/null
+++ b/flang/cmake/modules/FlangCommon.cmake
@@ -0,0 +1,43 @@
+#===-- cmake/modules/FlangCommon.txt ----------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+#
+# CMake definitions shared between Flang and Flang-RT
+#
+#===------------------------------------------------------------------------===#
+
+# The out of tree builds of the compiler and the Fortran runtime
+# must use the same setting of FLANG_RUNTIME_F128_MATH_LIB
+# to be composable. Failure to synchronize this setting may result
+# in linking errors or fatal failures in F128 runtime functions.
+set(FLANG_RUNTIME_F128_MATH_LIB "" CACHE STRING
+  "Specifies the target library used for implementing IEEE-754 128-bit float \
+  math in F18 runtime, e.g. it might be libquadmath for targets where \
+  REAL(16) is mapped to __float128, or libm for targets where REAL(16) \
+  is mapped to long double, etc."
+  )
+if (FLANG_RUNTIME_F128_MATH_LIB)
+  add_compile_definitions(FLANG_RUNTIME_F128_MATH_LIB="${FLANG_RUNTIME_F128_MATH_LIB}")
+endif()
+
+# Check if 128-bit float computations can be done via long double
+check_cxx_source_compiles(
+  "#include <cfloat>
+   #if LDBL_MANT_DIG != 113
+   #error LDBL_MANT_DIG != 113
+   #endif
+   int main() { return 0; }
+  "
+  HAVE_LDBL_MANT_DIG_113)
+
+include(TestBigEndian)
+test_big_endian(IS_BIGENDIAN)
+if (IS_BIGENDIAN)
+  add_compile_definitions(FLANG_BIG_ENDIAN=1)
+else ()
+  add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
+endif ()
diff --git a/flang/docs/GettingStarted.md b/flang/docs/GettingStarted.md
index ffde50b6f481e88..47ba268b3e7636b 100644
--- a/flang/docs/GettingStarted.md
+++ b/flang/docs/GettingStarted.md
@@ -30,7 +30,7 @@ https://llvm.org/docs/GettingStarted.html.
 All of the examples below use GCC as the C/C++ compilers and ninja as the build
 tool.
 
-### Building flang in tree
+### Building flang in tree with bootstrapped Flang-RT
 Building flang in tree means building flang along with all of the projects on
 which it depends.  These projects include mlir, clang, flang, openmp, and
 compiler-rt.  Note that compiler-rt is only needed to access libraries that
@@ -82,7 +82,7 @@ cmake \
   -DLLVM_TARGETS_TO_BUILD=host \
   -DLLVM_LIT_ARGS=-v \
   -DLLVM_ENABLE_PROJECTS="clang;mlir;flang;openmp" \
-  -DLLVM_ENABLE_RUNTIMES="compiler-rt" \
+  -DLLVM_ENABLE_RUNTIMES="compiler-rt;flang_rt" \
   ../llvm-project/llvm
 
 ninja
@@ -101,7 +101,7 @@ the cmake command above:
 To run the flang tests on this build, execute the command in the "build"
 directory:
 ```bash
-ninja check-flang
+ninja check-flang check-flang-rt
 ```
 
 To create the installed files:
@@ -111,34 +111,6 @@ ninja install
 echo "latest" > $INSTALLDIR/bin/versionrc
 ```
 
-To build compiler-rt:
-```bash
-cd $ROOTDIR
-rm -rf compiler-rt
-mkdir compiler-rt
-cd compiler-rt
-CC=$INSTALLDIR/bin/clang \
-CXX=$INSTALLDIR/bin/clang++ \
-cmake \
-  -G Ninja \
-  ../llvm-project/compiler-rt \
-  -DCMAKE_BUILD_TYPE=Release \
-  -DCMAKE_INSTALL_PREFIX=$INSTALLDIR \
-  -DCMAKE_CXX_STANDARD=11 \
-  -DCMAKE_C_CFLAGS=-mlong-double-128 \
-  -DCMAKE_CXX_CFLAGS=-mlong-double-128 \
-  -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-  -DCOMPILER_RT_BUILD_ORC=OFF \
-  -DCOMPILER_RT_BUILD_XRAY=OFF \
-  -DCOMPILER_RT_BUILD_MEMPROF=OFF \
-  -DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
-  -DCOMPILER_RT_BUILD_SANITIZERS=OFF \
-  -DLLVM_CONFIG_PATH=$INSTALLDIR/bin/llvm-config
-
-ninja
-ninja install
-```
-
 Note that these instructions specify flang as one of the projects to build in
 the in tree build.  This is not strictly necessary for subsequent standalone
 builds, but doing so lets you run the flang tests to verify that the source
@@ -192,31 +164,57 @@ directory:
 ninja check-flang
 ```
 
-### Building flang runtime for accelerators
+To build Flang-RT (required for linking executables):
+```bash
+cd $ROOTDIR
+rm -rf flang-rt
+mkdir flang-rt
+cd flang-rt
+CC=$INSTALLDIR/bin/clang \
+CXX=$INSTALLDIR/bin/clang++ \
+cmake \
+  -G Ninja \
+  ../llvm-project/runtimes \
+  -DCMAKE_BUILD_TYPE=Release \
+  -DCMAKE_INSTALL_PREFIX=$INSTALLDIR \
+  -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
+  -DLLVM_ENABLE_RUNTIMES=flang-rt \
+  -DLLVM_BINARY_DIR=$ROOTDIR/build \
+  -DLLVM_Fortran_COMPILER=$INSTALLDIR/bin/flang \
+  -DLLVM_Fortran_COMPILER_WORKS=ON
+
+ninja
+ninja check-flang-rt
+ninja install
+```
+
+
+### Building Flang-RT for accelerators
 Flang runtime can be built for accelerators in experimental mode, i.e.
 complete enabling is WIP.  CUDA and OpenMP target offload builds
 are currently supported.
 
-#### Building out-of-tree
+#### Bootstrapping Build of Flang-RT
 
 ##### CUDA build
 Clang with NVPTX backend and NVCC compilers are supported.
 
 ```bash
-cd llvm-project/flang
+cd llvm-project
 rm -rf build_flang_runtime
 mkdir build_flang_runtime
 cd build_flang_runtime
 
 cmake \
-  -DFLANG_EXPERIMENTAL_CUDA_RUNTIME=ON \
+  -DLLVM_ENABLE_RUNTIMES=flang-rt \
+  -DFLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA \
   -DCMAKE_CUDA_ARCHITECTURES=80 \
   -DCMAKE_C_COMPILER=clang \
   -DCMAKE_CXX_COMPILER=clang++ \
   -DCMAKE_CUDA_COMPILER=clang \
   -DCMAKE_CUDA_HOST_COMPILER=clang++ \
-  ../runtime/
-make -j flang-rt
+  ../runtimes/
+make flang-rt
 ```
 
 Note that the used version of `clang` must [support](https://releases.llvm.org/16.0.0/tools/clang/docs/ReleaseNotes.html#cuda-support)
@@ -225,21 +223,22 @@ CUDA toolkit installations, please use `-DCUDAToolkit_ROOT=/some/path`
 to specify the compatible version.
 
 ```bash
-cd llvm-project/flang
+cd llvm-project
 rm -rf build_flang_runtime
 mkdir build_flang_runtime
 cd build_flang_runtime
 
 cmake \
-  -DFLANG_EXPERIMENTAL_CUDA_RUNTIME=ON \
+  -DLLVM_ENABLE_RUNTIMES=flang-rt \
+  -DFLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA \
   -DCMAKE_CUDA_ARCHITECTURES=80 \
   -DCMAKE_C_COMPILER=clang \
   -DCMAKE_CXX_COMPILER=clang++ \
   -DCMAKE_CUDA_COMPILER=nvcc \
   -DCMAKE_CUDA_HOST_COMPILER=clang++ \
-  ../runtime/
+  ../runtimes/
 
-make -j flang-rt
+make flang-rt
 ```
 
 Note that `nvcc` might limit support to certain
@@ -251,50 +250,59 @@ code.  Note that the packaging of the libraries is different
 between [Clang](https://clang.llvm.org/docs/OffloadingDesign.html#linking-target-device-code) and NVCC, so the library must be linked using
 compatible compiler drivers.
 
-#### Building in-tree
+#### Building in-tree (bootstrap build)
 One may build Flang runtime library along with building Flang itself
 by providing these additional CMake variables on top of the Flang in-tree
 build config:
 
 For example:
 ```bash
-  -DFLANG_EXPERIMENTAL_CUDA_RUNTIME=ON \
+  -DLLVM_ENABLE_RUNTIMES=flang-rt \
+  -DFLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA \
   -DCMAKE_CUDA_ARCHITECTURES=80 \
   -DCMAKE_C_COMPILER=clang \
   -DCMAKE_CXX_COMPILER=clang++ \
   -DCMAKE_CUDA_COMPILER=clang \
   -DCMAKE_CUDA_HOST_COMPILER=clang++ \
+  ../llvm
 ```
 
 Or:
 ```bash
-  -DFLANG_EXPERIMENTAL_CUDA_RUNTIME=ON \
+  -DLLVM_ENABLE_RUNTIMES=flang-rt \
+  -DFLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA \
   -DCMAKE_CUDA_ARCHITECTURES=80 \
   -DCMAKE_C_COMPILER=gcc \
   -DCMAKE_CXX_COMPILER=g++ \
   -DCMAKE_CUDA_COMPILER=nvcc \
   -DCMAKE_CUDA_HOST_COMPILER=g++ \
+  ../llvm
 ```
 
-Normal `make -j check-flang` will work with such CMake configuration.
+Normal `make check-flang` will work with such CMake configuration.
+Consider building in parallel using the `-j<jobs>` flag, where `<jobs>` is a
+number low enough for all build jobs to fit into the available RAM. Using
+the number of harware threads (`nprocs`) is likely too much for most
+commodity computers.
 
 ##### OpenMP target offload build
 Only Clang compiler is currently supported.
 
 ```bash
-cd llvm-project/flang
+cd llvm-project
 rm -rf build_flang_runtime
 mkdir build_flang_runtime
 cd build_flang_runtime
 
 cmake \
-  -DFLANG_EXPERIMENTAL_OMP_OFFLOAD_BUILD="host_device" \
+  -DLLVM_ENABLE_RUNTIMES=flang-rt \
+  -DFLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT="OpenMP" \
   -DCMAKE_C_COMPILER=clang \
   -DCMAKE_CXX_COMPILER=clang++ \
-  -DFLANG_OMP_DEVICE_ARCHITECTURES="all" \
-  ../runtime/
+  -DFLANG_RT_DEVICE_ARCHITECTURES=all \
+  ../runtimes/
 
-make -j flang-rt
+make flang-rt
 ```
 
 The result of the build is a "device-only" library, i.e. the host
diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md
index a163b4f4ed67dee..2633b27f0093c66 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -36,7 +36,13 @@ page](https://llvm.org/releases/).
 
 ## Build System Changes
 
- * FortranRuntime has been renamed to `flang_rt`.
+ * The Fortran Runtime library has been move to a new top-level directory
+   named "flang-rt". The library was also renamed from `FortranRuntime` to
+   `flang_rt`. It now supports the
+   LLVM_ENABLE_RUNTIMES mechanism to build flang-rt for multiple target
+   triples. libflang_rt.a will now be emitted into Clang's per-target
+   resource directory (next to libclang_rt.*.*) where it is also found by
+   Flang's driver.
 
 ## New Issues Found
 
diff --git a/flang/module/iso_fortran_env_impl.f90 b/flang/module/iso_fortran_env_impl.f90
index 5408e7de3700116..3ee00acfde4d7ce 100644
--- a/flang/module/iso_fortran_env_impl.f90
+++ b/flang/module/iso_fortran_env_impl.f90
@@ -8,7 +8,7 @@
 
 ! This MODULE implements part of the ISO_FORTRAN_ENV module file, which
 ! partially requires linkable symbols for some entities defined
-! (e.g., real_kinds).
+! (e.g., real_kinds). This file is also used by Flang-RT.
 
 module iso_fortran_env_impl
   implicit none
diff --git a/flang/test/lit.cfg.py b/flang/test/lit.cfg.py
index 08ba586e1226d3c..0ba80f9a03f2ec8 100644
--- a/flang/test/lit.cfg.py
+++ b/flang/test/lit.cfg.py
@@ -166,26 +166,6 @@
 if config.flang_include_runtime:
     config.available_features.add("flang-rt")
 
-# Define some variables to help us test that the flang runtime doesn't depend on
-# the C++ runtime libraries. For this we need a C compiler. If for some reason
-# we don't have one, we can just disable the test.
-if config.flang_include_runtime and config.cc:
-    libruntime = os.path.join(config.flang_lib_dir, "libflang_rt.a")
-    include = os.path.join(config.flang_src_dir, "include")
-
-    if (
-        os.path.isfile(libruntime)
-        and os.path.isdir(include)
-    ):
-        config.available_features.add("c-compiler")
-        tools.append(
-            ToolSubst(
-                "%cc", command=config.cc, extra_args=isysroot_flag, unresolved="fatal"
-            )
-        )
-        tools.append(ToolSubst("%libruntime", command=libruntime, unresolved="fatal"))
-        tools.append(ToolSubst("%include", command=include, unresolved="fatal"))
-
 # Add all the tools and their substitutions (if applicable). Use the search paths provided for
 # finding the tools.
 if config.flang_standalone_build:
diff --git a/flang/test/lit.site.cfg.py.in b/flang/test/lit.site.cfg.py.in
index 697ba3fa797633d..5b66e592bcfeece 100644
--- a/flang/test/lit.site.cfg.py.in
+++ b/flang/test/lit.site.cfg.py.in
@@ -11,18 +11,15 @@ config.llvm_target_triple_env = "@LLVM_TARGET_TRIPLE_ENV@"
 config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@"
 config.errc_messages = "@LLVM_LIT_ERRC_MESSAGES@"
 config.flang_obj_root = "@FLANG_BINARY_DIR@"
-config.flang_src_dir = "@FLANG_SOURCE_DIR@"
 config.flang_tools_dir = lit_config.substitute("@FLANG_TOOLS_DIR@")
 config.flang_intrinsic_modules_dir = "@FLANG_INTRINSIC_MODULES_DIR@"
 config.flang_llvm_tools_dir = "@CMAKE_BINARY_DIR@/bin"
-config.flang_lib_dir = "@CMAKE_BINARY_DIR@/lib"
 config.flang_test_triple = "@FLANG_TEST_TARGET_TRIPLE@"
 config.flang_examples = @LLVM_BUILD_EXAMPLES@
 config.python_executable = "@PYTHON_EXECUTABLE@"
 config.flang_standalone_build = @FLANG_STANDALONE_BUILD@
 config.has_plugins = @LLVM_ENABLE_PLUGINS@
 config.linked_bye_extension = @LLVM_BYE_LINK_INTO_TOOLS@
-config.cc = "@CMAKE_C_COMPILER@"
 config.osx_sysroot = path(r"@CMAKE_OSX_SYSROOT@")
 config.targets_to_build = "@TARGETS_TO_BUILD@"
 config.default_sysroot = "@DEFAULT_SYSROOT@"
diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index f14065ab037990e..9a1dc824658fd51 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -157,12 +157,18 @@ if ("libc" IN_LIST LLVM_ENABLE_PROJECTS)
     "https://libc.llvm.org/ for building the runtimes.")
 endif()
 
+if ("flang-rt" IN_LIST LLVM_ENABLE_RUNTIMES)
+  if (NOT "flang" IN_LIST LLVM_ENABLE_PROJECTS)
+    message(FATAL_ERROR "Flang is not enabled, but is required for the Flang-RT runtime")
+  endif ()
+endif ()
+
 # Select the runtimes to build
 #
 # As we migrate runtimes to using the bootstrapping build, the set of default runtimes
 # should grow as we remove those runtimes from LLVM_ENABLE_PROJECTS above.
 set(LLVM_DEFAULT_RUNTIMES "libcxx;libcxxabi;libunwind")
-set(LLVM_SUPPORTED_RUNTIMES "libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp;llvm-libgcc;offload")
+set(LLVM_SUPPORTED_RUNTIMES "libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp;llvm-libgcc;offload;flang-rt")
 set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
   "Semicolon-separated list of runtimes to build, or \"all\" (${LLVM_DEFAULT_RUNTIMES}). Supported runtimes are ${LLVM_SUPPORTED_RUNTIMES}.")
 if(LLVM_ENABLE_RUNTIMES STREQUAL "all")
diff --git a/llvm/cmake/modules/LLVMExternalProjectUtils.cmake b/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
index 55422c2a4c023df..caccb62d666ced6 100644
--- a/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
+++ b/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
@@ -38,6 +38,8 @@ endfunction()
 
 
 # llvm_ExternalProject_Add(name source_dir ...
+#   ENABLE_FORTRAN
+#     External project requires the Flang compiler
 #   USE_TOOLCHAIN
 #     Use just-built tools (see TOOLCHAIN_TOOLS)
 #   EXCLUDE_FROM_ALL
@@ -65,7 +67,7 @@ endfunction()
 #   )
 function(llvm_ExternalProject_Add name source_dir)
   cmake_parse_arguments(ARG
-    "USE_TOOLCHAIN;EXCLUDE_FROM_ALL;NO_INSTALL;ALWAYS_CLEAN"
+    "ENABLE_FORTRAN;USE_TOOLCHAIN;EXCLUDE_FROM_ALL;NO_INSTALL;ALWAYS_CLEAN"
     "SOURCE_DIR;FOLDER"
     "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS;PASSTHROUGH_PREFIXES;STRIP_TOOL;TARGET_TRIPLE"
     ${ARGN})
@@ -93,6 +95,9 @@ function(llvm_ExternalProject_Add name source_dir)
 
   if(NOT ARG_TOOLCHAIN_TOOLS)
     set(ARG_TOOLCHAIN_TOOLS clang)
+    if (ARG_ENABLE_FORTRAN)
+      list(APPEND ARG_TOOLCHAIN_TOOLS flang)
+    endif ()
     # AIX 64-bit XCOFF and big AR format is not yet supported in some of these tools.
     if(NOT _cmake_system_name STREQUAL AIX)
       list(APPEND ARG_TOOLCHAIN_TOOLS lld llvm-ar llvm-ranlib llvm-nm llvm-objdump)
@@ -143,6 +148,10 @@ function(llvm_ExternalProject_Add name source_dir)
     set(CLANG_IN_TOOLCHAIN On)
   endif()
 
+  if(flang IN_LIST TOOLCHAIN_TOOLS)
+    set(FLANG_IN_TOOLCHAIN On)
+  endif()
+
   if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
     list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
   endif()
@@ -225,6 +234,9 @@ function(llvm_ExternalProject_Add name source_dir)
                           -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX})
       endif()
     endif()
+    if(FLANG_IN_TOOLCHAIN)
+      list(APPEND compiler_args -DCMAKE_Fortran_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/flang${CMAKE_EXECUTABLE_SUFFIX})
+    endif()
     if(lld IN_LIST TOOLCHAIN_TOOLS)
       if(is_msvc_target)
         list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
@@ -308,6 +320,7 @@ function(llvm_ExternalProject_Add name source_dir)
     set(compiler_args -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER}
                       -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
                       -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
+                      -DCMAKE_Fortran_COMPILER=${CMAKE_Fortran_COMPILER}
                       -DCMAKE_LINKER=${CMAKE_LINKER}
                       -DCMAKE_AR=${CMAKE_AR}
                       -DCMAKE_RANLIB=${CMAKE_RANLIB}
@@ -357,6 +370,7 @@ function(llvm_ExternalProject_Add name source_dir)
   if(ARG_TARGET_TRIPLE)
     list(APPEND compiler_args -DCMAKE_C_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
     list(APPEND compiler_args -DCMAKE_CXX_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
+    list(APPEND compiler_args -DCMAKE_Fortran_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
     list(APPEND compiler_args -DCMAKE_ASM_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
   endif()
 
diff --git a/llvm/projects/CMakeLists.txt b/llvm/projects/CMakeLists.txt
index 08f2fa522420b0e..f254cf10806d75b 100644
--- a/llvm/projects/CMakeLists.txt
+++ b/llvm/projects/CMakeLists.txt
@@ -11,7 +11,8 @@ foreach(entry ${entries})
        (NOT ${entry} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}/libunwind) AND
        (NOT ${entry} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}/test-suite) AND
        (NOT ${entry} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}/openmp) AND
-       (NOT ${entry} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}/cross-project-tests))
+       (NOT ${entry} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}/cross-project-tests) AND
+       (NOT ${entry} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}/flang-rt))
       get_filename_component(entry_name "${entry}" NAME)
       add_llvm_external_project(${entry_name})
     endif()
@@ -37,6 +38,7 @@ if(${LLVM_BUILD_RUNTIME})
   if(NOT LLVM_BUILD_EXTERNAL_COMPILER_RT)
     add_llvm_external_project(compiler-rt)
   endif()
+  add_llvm_external_project(flang-rt)
 endif()
 
 add_llvm_external_project(dragonegg)
diff --git a/llvm/runtimes/CMakeLists.txt b/llvm/runtimes/CMakeLists.txt
index 70e85c123e41273..2370b41fb7f0ba6 100644
--- a/llvm/runtimes/CMakeLists.txt
+++ b/llvm/runtimes/CMakeLists.txt
@@ -230,7 +230,7 @@ foreach(entry ${runtimes})
 endforeach()
 
 function(runtime_default_target)
-  cmake_parse_arguments(ARG "" "" "DEPENDS;CMAKE_ARGS;PREFIXES" ${ARGN})
+  cmake_parse_arguments(ARG "" "" "DEPENDS;CMAKE_ARGS;PREFIXES;EXTRA_ARGS" ${ARGN})
 
   include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
   set(SUB_CHECK_TARGETS ${SUB_CHECK_TARGETS} PARENT_SCOPE)
@@ -270,14 +270,16 @@ function(runtime_default_target)
                                       -DLLVM_BUILD_TOOLS=${LLVM_BUILD_TOOLS}
                                       -DCMAKE_C_COMPILER_WORKS=ON
                                       -DCMAKE_CXX_COMPILER_WORKS=ON
+                                      -DCMAKE_Fortran_COMPILER_WORKS=ON
                                       -DCMAKE_ASM_COMPILER_WORKS=ON
                                       ${COMMON_CMAKE_ARGS}
                                       ${RUNTIMES_CMAKE_ARGS}
                                       ${ARG_CMAKE_ARGS}
                            PASSTHROUGH_PREFIXES LLVM_ENABLE_RUNTIMES
                                                 LLVM_USE_LINKER
-                                                CUDA # For runtimes that may look for the CUDA SDK (libc, offload)
+                                                CUDA CMAKE_CUDA # For runtimes that may look for the CUDA compiler and/or SDK (libc, offload, flang-rt)
                                                 FFI # offload uses libffi
+                                                FLANG_RUNTIME # Shared between Flang and Flang-RT
                                                 ${ARG_PREFIXES}
                            EXTRA_TARGETS ${extra_targets}
                                          ${test_targets}
@@ -287,7 +289,7 @@ function(runtime_default_target)
                            USE_TOOLCHAIN
                            TARGET_TRIPLE ${LLVM_TARGET_TRIPLE}
                            FOLDER "Runtimes"
-                           ${EXTRA_ARGS})
+                           ${EXTRA_ARGS} ${ARG_EXTRA_ARGS})
 endfunction()
 
 # runtime_register_target(name)
@@ -404,6 +406,7 @@ function(runtime_register_target name)
                                       -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=${LLVM_ENABLE_PER_TARGET_RUNTIME_DIR}
                                       -DCMAKE_C_COMPILER_WORKS=ON
                                       -DCMAKE_CXX_COMPILER_WORKS=ON
+                                      -DCMAKE_Fortran_COMPILER_WORKS=ON
                                       -DCMAKE_ASM_COMPILER_WORKS=ON
                                       -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
                                       -DLLVM_RUNTIMES_TARGET=${name}
@@ -463,10 +466,13 @@ if(build_runtimes)
   # together in a single CMake invocation.
   set(extra_deps "")
   set(extra_cmake_args "")
+  set(extra_args "")
 
   if(LLVM_INCLUDE_TESTS)
     foreach(dep FileCheck
                 clang
+                clang-offload-packager
+                flang
                 count
                 lld
                 lli
@@ -549,19 +555,24 @@ if(build_runtimes)
   if(LLVM_LIBC_FULL_BUILD)
     list(APPEND extra_cmake_args "-DLLVM_LIBC_FULL_BUILD=ON")
   endif()
+  if("flang-rt" IN_LIST LLVM_ENABLE_RUNTIMES)
+    list(APPEND extra_args ENABLE_FORTRAN)
+  endif ()
 
   if(NOT LLVM_RUNTIME_TARGETS)
     runtime_default_target(
       DEPENDS ${builtins_dep} ${extra_deps}
       CMAKE_ARGS ${extra_cmake_args}
-      PREFIXES ${prefixes})
+      PREFIXES ${prefixes}
+      EXTRA_ARGS ${extra_args})
     set(test_targets check-runtimes)
   else()
     if("default" IN_LIST LLVM_RUNTIME_TARGETS)
       runtime_default_target(
         DEPENDS ${builtins_dep} ${extra_deps}
         CMAKE_ARGS ${extra_cmake_args}
-        PREFIXES ${prefixes})
+        PREFIXES ${prefixes}
+        EXTRA_ARGS ${extra_args})
       list(REMOVE_ITEM LLVM_RUNTIME_TARGETS "default")
     else()
       add_custom_target(runtimes)
@@ -608,7 +619,7 @@ if(build_runtimes)
       runtime_register_target(${name}
         DEPENDS ${builtins_dep_name} ${extra_deps}
         CMAKE_ARGS -DLLVM_DEFAULT_TARGET_TRIPLE=${name} ${extra_cmake_args}
-        EXTRA_ARGS TARGET_TRIPLE ${name})
+        EXTRA_ARGS TARGET_TRIPLE ${name} ${extra_args})
     endforeach()
 
     foreach(multilib ${LLVM_RUNTIME_MULTILIBS})
@@ -620,7 +631,7 @@ if(build_runtimes)
                      -DLLVM_RUNTIMES_LIBDIR_SUBDIR=${multilib}
                      ${extra_cmake_args}
           BASE_NAME ${name}
-          EXTRA_ARGS TARGET_TRIPLE ${name})
+          EXTRA_ARGS TARGET_TRIPLE ${name} ${extra_args})
       endforeach()
     endforeach()
   endif()
diff --git a/runtimes/CMakeLists.txt b/runtimes/CMakeLists.txt
index 4a6b317a03f660f..7f1e2ae065d6c77 100644
--- a/runtimes/CMakeLists.txt
+++ b/runtimes/CMakeLists.txt
@@ -36,7 +36,7 @@ list(INSERT CMAKE_MODULE_PATH 0
 # We order libraries to mirror roughly how they are layered, except that compiler-rt can depend
 # on libc++, so we put it after.
 set(LLVM_DEFAULT_RUNTIMES "libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp;offload")
-set(LLVM_SUPPORTED_RUNTIMES "${LLVM_DEFAULT_RUNTIMES};llvm-libgcc")
+set(LLVM_SUPPORTED_RUNTIMES "${LLVM_DEFAULT_RUNTIMES};llvm-libgcc;flang-rt")
 set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
   "Semicolon-separated list of runtimes to build, or \"all\" (${LLVM_DEFAULT_RUNTIMES}). Supported runtimes are ${LLVM_SUPPORTED_RUNTIMES}.")
 if(LLVM_ENABLE_RUNTIMES STREQUAL "all" )

>From b02e9ad846a25cb8cd1d66a975e3fef1c9dec98a Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Tue, 28 Jan 2025 15:11:16 +0100
Subject: [PATCH 2/9] Suggest both options to build Flang-RT

---
 flang/CMakeLists.txt | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index 38004c149b7835a..e0f32418f7f3225 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -247,13 +247,13 @@ if (FLANG_INCLUDE_RUNTIME)
     message(WARNING "Building Flang-RT using LLVM_ENABLE_RUNTIMES. FLANG_INCLUDE_RUNTIME=${FLANG_INCLUDE_RUNTIME} ignored.")
     set(FLANG_INCLUDE_RUNTIME OFF)
   else ()
-     message(STATUS "Building flang_rt in-tree")
+     message(STATUS "Building Flang-RT in-tree")
   endif ()
 else ()
   if ("flang-rt" IN_LIST LLVM_ENABLE_RUNTIMES)
-    message(STATUS "Building Flang-RT using LLVM_ENABLE_RUNTIMES.")
+    message(STATUS "Building Flang-RT using LLVM_ENABLE_RUNTIMES")
   else ()
-    message(STATUS "Not building Flang-RT. For a usable Fortran toolchain, compile a standalone Flang-RT")
+    message(STATUS "Not building Flang-RT. For a usable Fortran toolchain, either add LLVM_ENABLE_RUNTIMES=flang-rt, or compile a standalone Flang-RT.")
   endif ()
 endif ()
 

>From b41db9690a2dc035be9bc89c30c835d31632e5b5 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Thu, 30 Jan 2025 11:24:33 +0100
Subject: [PATCH 3/9] Post-merge fixes

---
 flang-rt/lib/flang_rt/CUDA/CMakeLists.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/flang-rt/lib/flang_rt/CUDA/CMakeLists.txt b/flang-rt/lib/flang_rt/CUDA/CMakeLists.txt
index 3c741ac93d64b22..428aa3059b86988 100644
--- a/flang-rt/lib/flang_rt/CUDA/CMakeLists.txt
+++ b/flang-rt/lib/flang_rt/CUDA/CMakeLists.txt
@@ -11,6 +11,7 @@ add_flangrt_library(CufRuntime STATIC
   allocatable.cpp
   allocator.cpp
   descriptor.cpp
+  init.cpp
   kernel.cpp
   memmove-function.cpp
   memory.cpp

>From 3ce4b52dc9c4c56e1570d56a45de15e957de81d6 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Thu, 30 Jan 2025 13:33:57 +0100
Subject: [PATCH 4/9] Post-merge fixes

---
 flang-rt/cmake/modules/AddFlangRTOffload.cmake            | 2 +-
 flang-rt/include/flang-rt/CUDA/allocator.h                | 2 +-
 flang-rt/include/flang-rt/allocator-registry.h            | 2 +-
 flang-rt/include/flang-rt/array-constructor.h             | 2 +-
 flang-rt/include/flang-rt/assign-impl.h                   | 2 +-
 flang-rt/include/flang-rt/buffer.h                        | 2 +-
 flang-rt/include/flang-rt/connection.h                    | 2 +-
 flang-rt/include/flang-rt/derived.h                       | 2 +-
 flang-rt/include/flang-rt/descriptor.h                    | 2 +-
 flang-rt/include/flang-rt/emit-encoded.h                  | 2 +-
 flang-rt/include/flang-rt/environment.h                   | 2 +-
 flang-rt/include/flang-rt/file.h                          | 2 +-
 flang-rt/include/flang-rt/format-implementation.h         | 2 +-
 flang-rt/include/flang-rt/format.h                        | 2 +-
 flang-rt/include/flang-rt/internal-unit.h                 | 2 +-
 flang-rt/include/flang-rt/io-error.h                      | 2 +-
 flang-rt/include/flang-rt/io-stmt.h                       | 2 +-
 flang-rt/include/flang-rt/lock.h                          | 2 +-
 flang-rt/include/flang-rt/memory.h                        | 2 +-
 flang-rt/include/flang-rt/namelist.h                      | 2 +-
 flang-rt/include/flang-rt/non-tbp-dio.h                   | 2 +-
 flang-rt/include/flang-rt/numeric-templates.h             | 2 +-
 flang-rt/include/flang-rt/random-templates.h              | 2 +-
 flang-rt/include/flang-rt/reduction-templates.h           | 2 +-
 flang-rt/include/flang-rt/stat.h                          | 2 +-
 flang-rt/include/flang-rt/terminator.h                    | 2 +-
 flang-rt/include/flang-rt/tools.h                         | 2 +-
 flang-rt/include/flang-rt/type-code.h                     | 2 +-
 flang-rt/include/flang-rt/type-info.h                     | 2 +-
 flang-rt/include/flang-rt/utf.h                           | 2 +-
 flang-rt/lib/FortranFloat128Math/complex-math.h           | 2 +-
 flang-rt/lib/FortranFloat128Math/math-entries.h           | 2 +-
 flang-rt/lib/FortranFloat128Math/numeric-template-specs.h | 2 +-
 {flang/runtime => flang-rt/lib/flang_rt}/CUDA/init.cpp    | 6 +++---
 flang-rt/lib/flang_rt/ISO_Fortran_util.h                  | 2 +-
 flang-rt/lib/flang_rt/complex-reduction.h                 | 2 +-
 flang-rt/lib/flang_rt/copy.h                              | 2 +-
 flang-rt/lib/flang_rt/descriptor-io.h                     | 2 +-
 flang-rt/lib/flang_rt/edit-input.h                        | 2 +-
 flang-rt/lib/flang_rt/edit-output.h                       | 2 +-
 flang-rt/lib/flang_rt/io-api-common.h                     | 2 +-
 flang-rt/lib/flang_rt/stack.h                             | 2 +-
 flang-rt/lib/flang_rt/unit-map.h                          | 2 +-
 flang-rt/lib/flang_rt/unit.h                              | 2 +-
 flang-rt/unittests/Runtime/tools.h                        | 2 +-
 45 files changed, 47 insertions(+), 47 deletions(-)
 rename {flang/runtime => flang-rt/lib/flang_rt}/CUDA/init.cpp (82%)

diff --git a/flang-rt/cmake/modules/AddFlangRTOffload.cmake b/flang-rt/cmake/modules/AddFlangRTOffload.cmake
index b055646cb0604cc..df84ed8f4c8d242 100644
--- a/flang-rt/cmake/modules/AddFlangRTOffload.cmake
+++ b/flang-rt/cmake/modules/AddFlangRTOffload.cmake
@@ -1,4 +1,4 @@
-#===-- cmake/modules/AddFortranRTOffload.txt -------------------------------===#
+#===-- cmake/modules/AddFlangRTOffload.cmake -------------------------------===#
 #
 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 # See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/include/flang-rt/CUDA/allocator.h b/flang-rt/include/flang-rt/CUDA/allocator.h
index fb09b5ff2402aeb..68bb0736dd5eecb 100644
--- a/flang-rt/include/flang-rt/CUDA/allocator.h
+++ b/flang-rt/include/flang-rt/CUDA/allocator.h
@@ -33,4 +33,4 @@ void *CUFAllocUnified(std::size_t);
 void CUFFreeUnified(void *);
 
 } // namespace Fortran::runtime::cuda
-#endif /* FLANG_RT_CUDA_ALLOCATOR_H_ */
+#endif // FLANG_RT_CUDA_ALLOCATOR_H_
diff --git a/flang-rt/include/flang-rt/allocator-registry.h b/flang-rt/include/flang-rt/allocator-registry.h
index 1f004462ca6241b..ea905afc045ce45 100644
--- a/flang-rt/include/flang-rt/allocator-registry.h
+++ b/flang-rt/include/flang-rt/allocator-registry.h
@@ -55,4 +55,4 @@ RT_OFFLOAD_VAR_GROUP_END
 
 } // namespace Fortran::runtime
 
-#endif /* FLANG_RT_ALLOCATOR_REGISTRY_H_ */
+#endif // FLANG_RT_ALLOCATOR_REGISTRY_H_
diff --git a/flang-rt/include/flang-rt/array-constructor.h b/flang-rt/include/flang-rt/array-constructor.h
index 7ff0779e46c6a0c..738050f80ef65f5 100644
--- a/flang-rt/include/flang-rt/array-constructor.h
+++ b/flang-rt/include/flang-rt/array-constructor.h
@@ -54,4 +54,4 @@ static_assert(alignof(Fortran::runtime::ArrayConstructorVector) <=
     "MaxArrayConstructorVectorAlignInBytes");
 
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_ARRAY_CONSTRUCTOR_H_ */
+#endif // FLANG_RT_ARRAY_CONSTRUCTOR_H_
diff --git a/flang-rt/include/flang-rt/assign-impl.h b/flang-rt/include/flang-rt/assign-impl.h
index 7e2cef9df753bbb..2e85a451d5b5270 100644
--- a/flang-rt/include/flang-rt/assign-impl.h
+++ b/flang-rt/include/flang-rt/assign-impl.h
@@ -28,4 +28,4 @@ RT_API_ATTRS void DoFromSourceAssign(Descriptor &, const Descriptor &,
 #endif
 
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_ASSIGN_IMPL_H_ */
+#endif // FLANG_RT_ASSIGN_IMPL_H_
diff --git a/flang-rt/include/flang-rt/buffer.h b/flang-rt/include/flang-rt/buffer.h
index ba172448a93a538..bd2e8c47c6e3014 100644
--- a/flang-rt/include/flang-rt/buffer.h
+++ b/flang-rt/include/flang-rt/buffer.h
@@ -221,4 +221,4 @@ template <typename STORE, std::size_t minBuffer = 65536> class FileFrame {
   bool dirty_{false};
 };
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_BUFFER_H_ */
+#endif // FLANG_RT_BUFFER_H_
diff --git a/flang-rt/include/flang-rt/connection.h b/flang-rt/include/flang-rt/connection.h
index 7f594deee69c65f..550ddb15c72367b 100644
--- a/flang-rt/include/flang-rt/connection.h
+++ b/flang-rt/include/flang-rt/connection.h
@@ -124,4 +124,4 @@ class SavedPosition {
 };
 
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_CONNECTION_H_ */
+#endif // FLANG_RT_CONNECTION_H_
diff --git a/flang-rt/include/flang-rt/derived.h b/flang-rt/include/flang-rt/derived.h
index 797a089e767c44c..d65fb7517249554 100644
--- a/flang-rt/include/flang-rt/derived.h
+++ b/flang-rt/include/flang-rt/derived.h
@@ -48,4 +48,4 @@ RT_API_ATTRS void Destroy(const Descriptor &, bool finalize,
 RT_API_ATTRS bool HasDynamicComponent(const Descriptor &);
 
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_DERIVED_H_ */
+#endif // FLANG_RT_DERIVED_H_
diff --git a/flang-rt/include/flang-rt/descriptor.h b/flang-rt/include/flang-rt/descriptor.h
index 27f80bec4ba9329..f97b8443425dcd7 100644
--- a/flang-rt/include/flang-rt/descriptor.h
+++ b/flang-rt/include/flang-rt/descriptor.h
@@ -482,4 +482,4 @@ class alignas(Descriptor) StaticDescriptor {
 };
 
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_DESCRIPTOR_H_ */
+#endif // FLANG_RT_DESCRIPTOR_H_
diff --git a/flang-rt/include/flang-rt/emit-encoded.h b/flang-rt/include/flang-rt/emit-encoded.h
index f3cb7a9f790e42d..279f751daff2cc5 100644
--- a/flang-rt/include/flang-rt/emit-encoded.h
+++ b/flang-rt/include/flang-rt/emit-encoded.h
@@ -114,4 +114,4 @@ RT_API_ATTRS bool EmitRepeated(CONTEXT &to, char ch, std::size_t n) {
 }
 
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_EMIT_ENCODED_H_ */
+#endif // FLANG_RT_EMIT_ENCODED_H_
diff --git a/flang-rt/include/flang-rt/environment.h b/flang-rt/include/flang-rt/environment.h
index b7597ad88af6254..7fa3060768502ce 100644
--- a/flang-rt/include/flang-rt/environment.h
+++ b/flang-rt/include/flang-rt/environment.h
@@ -67,4 +67,4 @@ RT_OFFLOAD_VAR_GROUP_END
 
 } // namespace Fortran::runtime
 
-#endif /* FLANG_RT_ENVIRONMENT_H_ */
+#endif // FLANG_RT_ENVIRONMENT_H_
diff --git a/flang-rt/include/flang-rt/file.h b/flang-rt/include/flang-rt/file.h
index ca95156e1fba437..3ab21c655a38f20 100644
--- a/flang-rt/include/flang-rt/file.h
+++ b/flang-rt/include/flang-rt/file.h
@@ -113,4 +113,4 @@ RT_API_ATTRS bool MayWrite(const char *path);
 RT_API_ATTRS bool MayReadAndWrite(const char *path);
 RT_API_ATTRS std::int64_t SizeInBytes(const char *path);
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_FILE_H_ */
+#endif // FLANG_RT_FILE_H_
diff --git a/flang-rt/include/flang-rt/format-implementation.h b/flang-rt/include/flang-rt/format-implementation.h
index 52f265d06f9bf37..6a2e1078ab35096 100644
--- a/flang-rt/include/flang-rt/format-implementation.h
+++ b/flang-rt/include/flang-rt/format-implementation.h
@@ -601,4 +601,4 @@ RT_API_ATTRS void FormatControl<CONTEXT>::Finish(Context &context) {
   }
 }
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_FORMAT_IMPLEMENTATION_H_ */
+#endif // FLANG_RT_FORMAT_IMPLEMENTATION_H_
diff --git a/flang-rt/include/flang-rt/format.h b/flang-rt/include/flang-rt/format.h
index d81241e41877c97..09e151a2e98cac7 100644
--- a/flang-rt/include/flang-rt/format.h
+++ b/flang-rt/include/flang-rt/format.h
@@ -201,4 +201,4 @@ template <typename CONTEXT> class FormatControl {
   Iteration stack_[maxMaxHeight];
 };
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_FORMAT_H_ */
+#endif // FLANG_RT_FORMAT_H_
diff --git a/flang-rt/include/flang-rt/internal-unit.h b/flang-rt/include/flang-rt/internal-unit.h
index 21e64b5624196d1..946d69080e6110d 100644
--- a/flang-rt/include/flang-rt/internal-unit.h
+++ b/flang-rt/include/flang-rt/internal-unit.h
@@ -56,4 +56,4 @@ template <Direction DIR> class InternalDescriptorUnit : public ConnectionState {
 extern template class InternalDescriptorUnit<Direction::Output>;
 extern template class InternalDescriptorUnit<Direction::Input>;
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_INTERNAL_UNIT_H_ */
+#endif // FLANG_RT_INTERNAL_UNIT_H_
diff --git a/flang-rt/include/flang-rt/io-error.h b/flang-rt/include/flang-rt/io-error.h
index c74c08970f94a03..6b5c0f9f390197d 100644
--- a/flang-rt/include/flang-rt/io-error.h
+++ b/flang-rt/include/flang-rt/io-error.h
@@ -80,4 +80,4 @@ class IoErrorHandler : public Terminator {
 };
 
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_IO_ERROR_H_ */
+#endif // FLANG_RT_IO_ERROR_H_
diff --git a/flang-rt/include/flang-rt/io-stmt.h b/flang-rt/include/flang-rt/io-stmt.h
index 87fd93c353d76be..7efab320e5e1a6c 100644
--- a/flang-rt/include/flang-rt/io-stmt.h
+++ b/flang-rt/include/flang-rt/io-stmt.h
@@ -791,4 +791,4 @@ class ErroneousIoStatementState : public IoStatementBase {
 };
 
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_IO_STMT_H_ */
+#endif // FLANG_RT_IO_STMT_H_
diff --git a/flang-rt/include/flang-rt/lock.h b/flang-rt/include/flang-rt/lock.h
index f57e97184edd529..1b09434d26f4fe4 100644
--- a/flang-rt/include/flang-rt/lock.h
+++ b/flang-rt/include/flang-rt/lock.h
@@ -113,4 +113,4 @@ class CriticalSection {
 };
 } // namespace Fortran::runtime
 
-#endif /* FLANG_RT_LOCK_H_ */
+#endif // FLANG_RT_LOCK_H_
diff --git a/flang-rt/include/flang-rt/memory.h b/flang-rt/include/flang-rt/memory.h
index afbb1b34c1d2c04..64db30baf2806ac 100644
--- a/flang-rt/include/flang-rt/memory.h
+++ b/flang-rt/include/flang-rt/memory.h
@@ -170,4 +170,4 @@ template <typename A> struct Allocator {
 };
 } // namespace Fortran::runtime
 
-#endif /* FLANG_RT_MEMORY_H_ */
+#endif // FLANG_RT_MEMORY_H_
diff --git a/flang-rt/include/flang-rt/namelist.h b/flang-rt/include/flang-rt/namelist.h
index 5757bfb3b710938..2ae93ca9c20505e 100644
--- a/flang-rt/include/flang-rt/namelist.h
+++ b/flang-rt/include/flang-rt/namelist.h
@@ -51,4 +51,4 @@ class NamelistGroup {
 RT_API_ATTRS bool IsNamelistNameOrSlash(IoStatementState &);
 
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_NAMELIST_H_ */
+#endif // FLANG_RT_NAMELIST_H_
diff --git a/flang-rt/include/flang-rt/non-tbp-dio.h b/flang-rt/include/flang-rt/non-tbp-dio.h
index 58ed29ba7a10245..1088257b72f5926 100644
--- a/flang-rt/include/flang-rt/non-tbp-dio.h
+++ b/flang-rt/include/flang-rt/non-tbp-dio.h
@@ -53,4 +53,4 @@ struct NonTbpDefinedIoTable {
 };
 
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_NON_TBP_DIO_H_ */
+#endif // FLANG_RT_NON_TBP_DIO_H_
diff --git a/flang-rt/include/flang-rt/numeric-templates.h b/flang-rt/include/flang-rt/numeric-templates.h
index 7019151c3a207dc..a1475923ff3b163 100644
--- a/flang-rt/include/flang-rt/numeric-templates.h
+++ b/flang-rt/include/flang-rt/numeric-templates.h
@@ -368,4 +368,4 @@ template <typename T> inline RT_API_ATTRS T ErfcScaled(T arg) {
 
 } // namespace Fortran::runtime
 
-#endif /* FLANG_RT_NUMERIC_TEMPLATES_H_ */
+#endif // FLANG_RT_NUMERIC_TEMPLATES_H_
diff --git a/flang-rt/include/flang-rt/random-templates.h b/flang-rt/include/flang-rt/random-templates.h
index f320f2cd328f5a3..00542bd5b2553f2 100644
--- a/flang-rt/include/flang-rt/random-templates.h
+++ b/flang-rt/include/flang-rt/random-templates.h
@@ -108,4 +108,4 @@ inline void GenerateUnsigned(const Descriptor &harvest) {
 
 } // namespace Fortran::runtime::random
 
-#endif /* FLANG_RT_RANDOM_TEMPLATES_H_ */
+#endif // FLANG_RT_RANDOM_TEMPLATES_H_
diff --git a/flang-rt/include/flang-rt/reduction-templates.h b/flang-rt/include/flang-rt/reduction-templates.h
index 513b3e4478498fe..d6b3c24920cf9c2 100644
--- a/flang-rt/include/flang-rt/reduction-templates.h
+++ b/flang-rt/include/flang-rt/reduction-templates.h
@@ -416,4 +416,4 @@ template <int KIND> struct Norm2Helper {
 };
 
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_REDUCTION_TEMPLATES_H_ */
+#endif // FLANG_RT_REDUCTION_TEMPLATES_H_
diff --git a/flang-rt/include/flang-rt/stat.h b/flang-rt/include/flang-rt/stat.h
index 95ae286a03b4955..a6824f4803561d6 100644
--- a/flang-rt/include/flang-rt/stat.h
+++ b/flang-rt/include/flang-rt/stat.h
@@ -60,4 +60,4 @@ RT_API_ATTRS int ToErrmsg(const Descriptor *errmsg, int stat); // returns stat
 RT_API_ATTRS int ReturnError(Terminator &, int stat,
     const Descriptor *errmsg = nullptr, bool hasStat = false);
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_STAT_H_ */
+#endif // FLANG_RT_STAT_H_
diff --git a/flang-rt/include/flang-rt/terminator.h b/flang-rt/include/flang-rt/terminator.h
index 800ed6f3755b752..c2a4e1f64eb6653 100644
--- a/flang-rt/include/flang-rt/terminator.h
+++ b/flang-rt/include/flang-rt/terminator.h
@@ -121,4 +121,4 @@ namespace Fortran::runtime::io {
 RT_API_ATTRS void FlushOutputOnCrash(const Terminator &);
 }
 
-#endif /* FLANG_RT_TERMINATOR_H_ */
+#endif // FLANG_RT_TERMINATOR_H_
diff --git a/flang-rt/include/flang-rt/tools.h b/flang-rt/include/flang-rt/tools.h
index 38804a1f4099160..3503c49308e4dd2 100644
--- a/flang-rt/include/flang-rt/tools.h
+++ b/flang-rt/include/flang-rt/tools.h
@@ -570,4 +570,4 @@ RT_API_ATTRS void CreatePartialReductionResult(Descriptor &result,
     const char *intrinsic, TypeCode);
 
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_TOOLS_H_ */
+#endif // FLANG_RT_TOOLS_H_
diff --git a/flang-rt/include/flang-rt/type-code.h b/flang-rt/include/flang-rt/type-code.h
index 1d057481261a1de..a3b79462887c12b 100644
--- a/flang-rt/include/flang-rt/type-code.h
+++ b/flang-rt/include/flang-rt/type-code.h
@@ -75,4 +75,4 @@ class TypeCode {
   ISO::CFI_type_t raw_{CFI_type_other};
 };
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_TYPE_CODE_H_ */
+#endif // FLANG_RT_TYPE_CODE_H_
diff --git a/flang-rt/include/flang-rt/type-info.h b/flang-rt/include/flang-rt/type-info.h
index 52e208c93fe25da..dc5cd98d7523f23 100644
--- a/flang-rt/include/flang-rt/type-info.h
+++ b/flang-rt/include/flang-rt/type-info.h
@@ -321,4 +321,4 @@ class DerivedType {
 };
 
 } // namespace Fortran::runtime::typeInfo
-#endif /* FLANG_RT_TYPE_INFO_H_ */
+#endif // FLANG_RT_TYPE_INFO_H_
diff --git a/flang-rt/include/flang-rt/utf.h b/flang-rt/include/flang-rt/utf.h
index 4d1e23b9ccb941f..648937ca14f16a9 100644
--- a/flang-rt/include/flang-rt/utf.h
+++ b/flang-rt/include/flang-rt/utf.h
@@ -70,4 +70,4 @@ RT_API_ATTRS Fortran::common::optional<char32_t> DecodeUTF8(const char *);
 RT_API_ATTRS std::size_t EncodeUTF8(char *, char32_t);
 
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_UTF_H_ */
+#endif // FLANG_RT_UTF_H_
diff --git a/flang-rt/lib/FortranFloat128Math/complex-math.h b/flang-rt/lib/FortranFloat128Math/complex-math.h
index 24ccc794fd144a0..d22598d52e83291 100644
--- a/flang-rt/lib/FortranFloat128Math/complex-math.h
+++ b/flang-rt/lib/FortranFloat128Math/complex-math.h
@@ -59,4 +59,4 @@
 #error "Float128Math build with glibc>=2.26 is unsupported yet"
 #endif
 
-#endif /* FLANG_RT_FORTRANFLOAT128MATH_COMPLEX_MATH_H_ */
+#endif // FLANG_RT_FORTRANFLOAT128MATH_COMPLEX_MATH_H_
diff --git a/flang-rt/lib/FortranFloat128Math/math-entries.h b/flang-rt/lib/FortranFloat128Math/math-entries.h
index b9821163d94fb55..72b12a1645c7d88 100644
--- a/flang-rt/lib/FortranFloat128Math/math-entries.h
+++ b/flang-rt/lib/FortranFloat128Math/math-entries.h
@@ -232,4 +232,4 @@ DEFINE_SIMPLE_ALIAS(Yn, ynl)
 
 } // namespace Fortran::runtime
 
-#endif /* FLANG_RT_FORTRANFLOAT128MATH_MATH_ENTRIES_H_ */
+#endif // FLANG_RT_FORTRANFLOAT128MATH_MATH_ENTRIES_H_
diff --git a/flang-rt/lib/FortranFloat128Math/numeric-template-specs.h b/flang-rt/lib/FortranFloat128Math/numeric-template-specs.h
index fc912f75075a750..e618902b03adc21 100644
--- a/flang-rt/lib/FortranFloat128Math/numeric-template-specs.h
+++ b/flang-rt/lib/FortranFloat128Math/numeric-template-specs.h
@@ -52,4 +52,4 @@ template <> struct SQRTTy<F128Type> {
 };
 
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_FORTRANFLOAT128MATH_NUMERIC_TEMPLATE_SPECS_H_ */
+#endif // FLANG_RT_FORTRANFLOAT128MATH_NUMERIC_TEMPLATE_SPECS_H_
diff --git a/flang/runtime/CUDA/init.cpp b/flang-rt/lib/flang_rt/CUDA/init.cpp
similarity index 82%
rename from flang/runtime/CUDA/init.cpp
rename to flang-rt/lib/flang_rt/CUDA/init.cpp
index 2bffce842b95269..f2d47d258519ecc 100644
--- a/flang/runtime/CUDA/init.cpp
+++ b/flang-rt/lib/flang_rt/CUDA/init.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/CUDA/init.cpp ---------------------------------------------===//
+//===-- lib/flang_rt/CUDA/init.cpp ------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,8 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/init.h"
-#include "../environment.h"
-#include "../terminator.h"
+#include "flang-rt/environment.h"
+#include "flang-rt/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 
 #include "cuda_runtime.h"
diff --git a/flang-rt/lib/flang_rt/ISO_Fortran_util.h b/flang-rt/lib/flang_rt/ISO_Fortran_util.h
index 6128faba5bceadd..9d4a907e1addef4 100644
--- a/flang-rt/lib/flang_rt/ISO_Fortran_util.h
+++ b/flang-rt/lib/flang_rt/ISO_Fortran_util.h
@@ -99,4 +99,4 @@ static inline RT_API_ATTRS void EstablishDescriptor(CFI_cdesc_t *descriptor,
   }
 }
 } // namespace Fortran::ISO
-#endif /* FLANG_RT_ISO_FORTRAN_UTIL_H_ */
+#endif // FLANG_RT_ISO_FORTRAN_UTIL_H_
diff --git a/flang-rt/lib/flang_rt/complex-reduction.h b/flang-rt/lib/flang_rt/complex-reduction.h
index 2678dbf4e6472de..45614ea66057309 100644
--- a/flang-rt/lib/flang_rt/complex-reduction.h
+++ b/flang-rt/lib/flang_rt/complex-reduction.h
@@ -155,4 +155,4 @@ void RTNAME(ReduceComplex16DimValue)(
     REDUCE_DIM_ARGS(CFloat128ComplexType, CFloat128ComplexType_value_op));
 #endif
 
-#endif /* FLANG_RT_COMPLEX_REDUCTION_H_ */
+#endif // FLANG_RT_COMPLEX_REDUCTION_H_
diff --git a/flang-rt/lib/flang_rt/copy.h b/flang-rt/lib/flang_rt/copy.h
index 468c2439baf618f..f4abac323196b06 100644
--- a/flang-rt/lib/flang_rt/copy.h
+++ b/flang-rt/lib/flang_rt/copy.h
@@ -22,4 +22,4 @@ RT_API_ATTRS void CopyElement(const Descriptor &to, const SubscriptValue toAt[],
     const Descriptor &from, const SubscriptValue fromAt[], Terminator &);
 
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_COPY_H_ */
+#endif // FLANG_RT_COPY_H_
diff --git a/flang-rt/lib/flang_rt/descriptor-io.h b/flang-rt/lib/flang_rt/descriptor-io.h
index 763ebb1735752bb..97bedce247d8321 100644
--- a/flang-rt/lib/flang_rt/descriptor-io.h
+++ b/flang-rt/lib/flang_rt/descriptor-io.h
@@ -626,4 +626,4 @@ static RT_API_ATTRS bool DescriptorIO(IoStatementState &io,
   return false;
 }
 } // namespace Fortran::runtime::io::descr
-#endif /* FLANG_RT_DESCRIPTOR_IO_H_ */
+#endif // FLANG_RT_DESCRIPTOR_IO_H_
diff --git a/flang-rt/lib/flang_rt/edit-input.h b/flang-rt/lib/flang_rt/edit-input.h
index 2d6a3f241a0059d..fdee0f0e1131eba 100644
--- a/flang-rt/lib/flang_rt/edit-input.h
+++ b/flang-rt/lib/flang_rt/edit-input.h
@@ -50,4 +50,4 @@ extern template RT_API_ATTRS bool EditCharacterInput(
     IoStatementState &, const DataEdit &, char32_t *, std::size_t);
 
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_EDIT_INPUT_H_ */
+#endif // FLANG_RT_EDIT_INPUT_H_
diff --git a/flang-rt/lib/flang_rt/edit-output.h b/flang-rt/lib/flang_rt/edit-output.h
index 59f5d820c4ad560..729f4400bd6312b 100644
--- a/flang-rt/lib/flang_rt/edit-output.h
+++ b/flang-rt/lib/flang_rt/edit-output.h
@@ -138,4 +138,4 @@ extern template class RealOutputEditing<10>;
 extern template class RealOutputEditing<16>;
 
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_EDIT_OUTPUT_H_ */
+#endif // FLANG_RT_EDIT_OUTPUT_H_
diff --git a/flang-rt/lib/flang_rt/io-api-common.h b/flang-rt/lib/flang_rt/io-api-common.h
index ba8c7177c2811f3..5e480fdef34835a 100644
--- a/flang-rt/lib/flang_rt/io-api-common.h
+++ b/flang-rt/lib/flang_rt/io-api-common.h
@@ -94,4 +94,4 @@ RT_API_ATTRS Cookie BeginExternalListIO(
 }
 
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_IO_API_COMMON_H_ */
+#endif // FLANG_RT_IO_API_COMMON_H_
diff --git a/flang-rt/lib/flang_rt/stack.h b/flang-rt/lib/flang_rt/stack.h
index 88cd276456fbc10..6f37282f00932e6 100644
--- a/flang-rt/lib/flang_rt/stack.h
+++ b/flang-rt/lib/flang_rt/stack.h
@@ -133,4 +133,4 @@ template <typename T, unsigned N = 0> class Stack : public StackStorage<T, N> {
   Terminator &terminator_;
 };
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_STACK_H_ */
+#endif // FLANG_RT_STACK_H_
diff --git a/flang-rt/lib/flang_rt/unit-map.h b/flang-rt/lib/flang_rt/unit-map.h
index 6f7eb692b970a7a..64233008b663077 100644
--- a/flang-rt/lib/flang_rt/unit-map.h
+++ b/flang-rt/lib/flang_rt/unit-map.h
@@ -100,4 +100,4 @@ class UnitMap {
   int emergencyNewUnit_{maxNewUnits_}; // not recycled
 };
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_UNIT_MAP_H_ */
+#endif // FLANG_RT_UNIT_MAP_H_
diff --git a/flang-rt/lib/flang_rt/unit.h b/flang-rt/lib/flang_rt/unit.h
index 3b20c9870897ac4..a5fb7fb31691f9c 100644
--- a/flang-rt/lib/flang_rt/unit.h
+++ b/flang-rt/lib/flang_rt/unit.h
@@ -291,4 +291,4 @@ class ChildIo {
 };
 
 } // namespace Fortran::runtime::io
-#endif /* FLANG_RT_UNIT_H_ */
+#endif // FLANG_RT_UNIT_H_
diff --git a/flang-rt/unittests/Runtime/tools.h b/flang-rt/unittests/Runtime/tools.h
index ff39d7ccb2a244a..146aae659c3383f 100644
--- a/flang-rt/unittests/Runtime/tools.h
+++ b/flang-rt/unittests/Runtime/tools.h
@@ -54,4 +54,4 @@ static OwningPtr<Descriptor> MakeArray(const std::vector<int> &shape,
 }
 
 } // namespace Fortran::runtime
-#endif /* FLANG_RT_UNITTESTS_RUNTIME_TOOLS_H_ */
+#endif // FLANG_RT_UNITTESTS_RUNTIME_TOOLS_H_

>From c86aa904b55e4ff7de53691a6137233ef44bb934 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Mon, 3 Feb 2025 12:33:07 +0100
Subject: [PATCH 5/9] Rename to flang_rt.runtime, flang_rt.quadmath,
 flang_rt.cuda

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp    |  4 +-
 clang/lib/Driver/ToolChains/Flang.cpp         |  8 ++--
 flang-rt/CMakeLists.txt                       |  8 +++-
 flang-rt/README.md                            |  4 +-
 flang-rt/examples/CMakeLists.txt              |  9 ++++
 .../ExternalHelloWorld/CMakeLists.txt         | 17 ++++++++
 .../ExternalHelloWorld/external-hello.cpp     |  8 ++++
 flang-rt/include/flang-rt/CUDA/allocator.h    |  2 +-
 .../{ => runtime}/allocator-registry.h        |  8 ++--
 .../{ => runtime}/array-constructor.h         |  8 ++--
 .../flang-rt/{ => runtime}/assign-impl.h      |  8 ++--
 .../include/flang-rt/{ => runtime}/buffer.h   |  8 ++--
 .../flang-rt/{ => runtime}/connection.h       |  8 ++--
 .../include/flang-rt/{ => runtime}/derived.h  |  8 ++--
 .../flang-rt/{ => runtime}/descriptor.h       |  8 ++--
 .../flang-rt/{ => runtime}/emit-encoded.h     |  8 ++--
 .../flang-rt/{ => runtime}/environment.h      |  8 ++--
 .../include/flang-rt/{ => runtime}/file.h     |  8 ++--
 .../{ => runtime}/format-implementation.h     |  8 ++--
 .../include/flang-rt/{ => runtime}/format.h   |  8 ++--
 .../flang-rt/{ => runtime}/internal-unit.h    |  8 ++--
 .../include/flang-rt/{ => runtime}/io-error.h |  8 ++--
 .../include/flang-rt/{ => runtime}/io-stmt.h  |  8 ++--
 .../include/flang-rt/{ => runtime}/lock.h     |  8 ++--
 .../include/flang-rt/{ => runtime}/memory.h   |  8 ++--
 .../include/flang-rt/{ => runtime}/namelist.h |  8 ++--
 .../flang-rt/{ => runtime}/non-tbp-dio.h      |  8 ++--
 .../{ => runtime}/numeric-templates.h         |  8 ++--
 .../flang-rt/{ => runtime}/random-templates.h |  8 ++--
 .../{ => runtime}/reduction-templates.h       |  8 ++--
 .../include/flang-rt/{ => runtime}/stat.h     |  8 ++--
 .../flang-rt/{ => runtime}/terminator.h       |  8 ++--
 .../include/flang-rt/{ => runtime}/tools.h    |  8 ++--
 .../flang-rt/{ => runtime}/type-code.h        |  8 ++--
 .../flang-rt/{ => runtime}/type-info.h        |  8 ++--
 flang-rt/include/flang-rt/{ => runtime}/utf.h |  8 ++--
 flang-rt/lib/CMakeLists.txt                   |  9 ++--
 .../{flang_rt/CUDA => cuda}/CMakeLists.txt    | 12 +++---
 .../{flang_rt/CUDA => cuda}/allocatable.cpp   | 10 ++---
 .../lib/{flang_rt/CUDA => cuda}/allocator.cpp | 14 +++----
 .../{flang_rt/CUDA => cuda}/descriptor.cpp    |  8 ++--
 flang-rt/lib/{flang_rt/CUDA => cuda}/init.cpp |  6 +--
 .../lib/{flang_rt/CUDA => cuda}/kernel.cpp    |  4 +-
 .../CUDA => cuda}/memmove-function.cpp        |  4 +-
 .../lib/{flang_rt/CUDA => cuda}/memory.cpp    |  6 +--
 .../lib/{flang_rt/CUDA => cuda}/pointer.cpp   | 10 ++---
 .../{flang_rt/CUDA => cuda}/registration.cpp  |  4 +-
 .../CMakeLists.txt                            |  8 ++--
 .../acos.cpp                                  |  2 +-
 .../acosh.cpp                                 |  2 +-
 .../asin.cpp                                  |  2 +-
 .../asinh.cpp                                 |  2 +-
 .../atan.cpp                                  |  2 +-
 .../atan2.cpp                                 |  2 +-
 .../atanh.cpp                                 |  2 +-
 .../ceil.cpp                                  |  2 +-
 .../complex-math.c                            |  0
 .../complex-math.h                            |  8 ++--
 .../{FortranFloat128Math => quadmath}/cos.cpp |  2 +-
 .../cosh.cpp                                  |  2 +-
 .../{FortranFloat128Math => quadmath}/erf.cpp |  2 +-
 .../erfc.cpp                                  |  2 +-
 .../{FortranFloat128Math => quadmath}/exp.cpp |  2 +-
 .../exponent.cpp                              |  2 +-
 .../floor.cpp                                 |  2 +-
 .../{FortranFloat128Math => quadmath}/fma.cpp |  2 +-
 .../fraction.cpp                              |  2 +-
 .../hypot.cpp                                 |  2 +-
 .../{FortranFloat128Math => quadmath}/j0.cpp  |  2 +-
 .../{FortranFloat128Math => quadmath}/j1.cpp  |  2 +-
 .../{FortranFloat128Math => quadmath}/jn.cpp  |  2 +-
 .../lgamma.cpp                                |  2 +-
 .../llround.cpp                               |  2 +-
 .../{FortranFloat128Math => quadmath}/log.cpp |  2 +-
 .../log10.cpp                                 |  2 +-
 .../lround.cpp                                |  2 +-
 .../math-entries.h                            | 12 +++---
 .../mod-real.cpp                              |  2 +-
 .../modulo-real.cpp                           |  2 +-
 .../nearbyint.cpp                             |  2 +-
 .../nearest.cpp                               |  2 +-
 .../norm2.cpp                                 |  4 +-
 .../numeric-template-specs.h                  | 10 ++---
 .../{FortranFloat128Math => quadmath}/pow.cpp |  2 +-
 .../random.cpp                                |  4 +-
 .../remainder.cpp                             |  2 +-
 .../round.cpp                                 |  2 +-
 .../rrspacing.cpp                             |  2 +-
 .../scale.cpp                                 |  2 +-
 .../set-exponent.cpp                          |  2 +-
 .../{FortranFloat128Math => quadmath}/sin.cpp |  2 +-
 .../sinh.cpp                                  |  2 +-
 .../spacing.cpp                               |  2 +-
 .../sqrt.cpp                                  |  2 +-
 .../{FortranFloat128Math => quadmath}/tan.cpp |  2 +-
 .../tanh.cpp                                  |  2 +-
 .../tgamma.cpp                                |  2 +-
 .../trunc.cpp                                 |  2 +-
 .../{FortranFloat128Math => quadmath}/y0.cpp  |  2 +-
 .../{FortranFloat128Math => quadmath}/y1.cpp  |  2 +-
 .../{FortranFloat128Math => quadmath}/yn.cpp  |  2 +-
 .../lib/{flang_rt => runtime}/CMakeLists.txt  | 34 +++++++--------
 .../ISO_Fortran_binding.cpp                   |  8 ++--
 .../{flang_rt => runtime}/ISO_Fortran_util.h  | 14 +++----
 .../lib/{flang_rt => runtime}/allocatable.cpp | 14 +++----
 .../allocator-registry.cpp                    |  6 +--
 .../array-constructor.cpp                     | 14 +++----
 flang-rt/lib/{flang_rt => runtime}/assign.cpp | 16 ++++----
 flang-rt/lib/{flang_rt => runtime}/buffer.cpp |  4 +-
 .../lib/{flang_rt => runtime}/character.cpp   |  8 ++--
 .../lib/{flang_rt => runtime}/command.cpp     | 12 +++---
 .../{flang_rt => runtime}/complex-powi.cpp    |  2 +-
 .../{flang_rt => runtime}/complex-reduction.c |  0
 .../{flang_rt => runtime}/complex-reduction.h |  8 ++--
 .../lib/{flang_rt => runtime}/connection.cpp  |  8 ++--
 flang-rt/lib/{flang_rt => runtime}/copy.cpp   |  8 ++--
 flang-rt/lib/{flang_rt => runtime}/copy.h     | 10 ++---
 .../lib/{flang_rt => runtime}/derived-api.cpp | 12 +++---
 .../lib/{flang_rt => runtime}/derived.cpp     | 14 +++----
 .../{flang_rt => runtime}/descriptor-io.cpp   |  2 +-
 .../lib/{flang_rt => runtime}/descriptor-io.h | 18 ++++----
 .../lib/{flang_rt => runtime}/descriptor.cpp  | 16 ++++----
 .../lib/{flang_rt => runtime}/dot-product.cpp |  8 ++--
 .../lib/{flang_rt => runtime}/edit-input.cpp  |  6 +--
 .../lib/{flang_rt => runtime}/edit-input.h    | 12 +++---
 .../lib/{flang_rt => runtime}/edit-output.cpp |  6 +--
 .../lib/{flang_rt => runtime}/edit-output.h   | 12 +++---
 .../environment-default-list.h                |  0
 .../lib/{flang_rt => runtime}/environment.cpp |  6 +--
 .../lib/{flang_rt => runtime}/exceptions.cpp  |  4 +-
 .../lib/{flang_rt => runtime}/execute.cpp     | 12 +++---
 .../lib/{flang_rt => runtime}/extensions.cpp  |  8 ++--
 .../{flang_rt => runtime}/external-unit.cpp   |  8 ++--
 .../lib/{flang_rt => runtime}/extrema.cpp     |  4 +-
 flang-rt/lib/{flang_rt => runtime}/file.cpp   |  8 ++--
 .../lib/{flang_rt => runtime}/findloc.cpp     |  4 +-
 flang-rt/lib/{flang_rt => runtime}/format.cpp |  4 +-
 .../lib/{flang_rt => runtime}/inquiry.cpp     |  8 ++--
 .../{flang_rt => runtime}/internal-unit.cpp   |  8 ++--
 .../lib/{flang_rt => runtime}/io-api-common.h | 12 +++---
 .../{flang_rt => runtime}/io-api-minimal.cpp  | 10 ++---
 flang-rt/lib/{flang_rt => runtime}/io-api.cpp | 16 ++++----
 .../lib/{flang_rt => runtime}/io-error.cpp    |  6 +--
 .../lib/{flang_rt => runtime}/io-stmt.cpp     | 16 ++++----
 flang-rt/lib/{flang_rt => runtime}/iostat.cpp |  2 +-
 flang-rt/lib/{flang_rt => runtime}/main.cpp   |  6 +--
 .../matmul-transpose.cpp                      |  8 ++--
 flang-rt/lib/{flang_rt => runtime}/matmul.cpp |  8 ++--
 flang-rt/lib/{flang_rt => runtime}/memory.cpp |  8 ++--
 .../{flang_rt => runtime}/misc-intrinsic.cpp  |  8 ++--
 .../lib/{flang_rt => runtime}/namelist.cpp    |  8 ++--
 .../lib/{flang_rt => runtime}/non-tbp-dio.cpp |  6 +--
 .../lib/{flang_rt => runtime}/numeric.cpp     |  8 ++--
 .../lib/{flang_rt => runtime}/pointer.cpp     | 16 ++++----
 .../lib/{flang_rt => runtime}/product.cpp     |  4 +-
 .../lib/{flang_rt => runtime}/pseudo-unit.cpp |  6 +--
 flang-rt/lib/{flang_rt => runtime}/ragged.cpp |  4 +-
 flang-rt/lib/{flang_rt => runtime}/random.cpp | 10 ++---
 flang-rt/lib/{flang_rt => runtime}/reduce.cpp | 10 ++---
 .../lib/{flang_rt => runtime}/reduction.cpp   |  6 +--
 flang-rt/lib/{flang_rt => runtime}/stack.h    | 12 +++---
 flang-rt/lib/{flang_rt => runtime}/stat.cpp   | 10 ++---
 flang-rt/lib/{flang_rt => runtime}/stop.cpp   | 10 ++---
 flang-rt/lib/{flang_rt => runtime}/sum.cpp    |  4 +-
 .../lib/{flang_rt => runtime}/support.cpp     |  6 +--
 .../{flang_rt => runtime}/temporary-stack.cpp |  8 ++--
 .../lib/{flang_rt => runtime}/terminator.cpp  |  4 +-
 .../{flang_rt => runtime}/time-intrinsic.cpp  |  8 ++--
 flang-rt/lib/{flang_rt => runtime}/tools.cpp  |  6 +--
 .../transformational.cpp                      |  8 ++--
 .../lib/{flang_rt => runtime}/type-code.cpp   |  4 +-
 .../lib/{flang_rt => runtime}/type-info.cpp   |  8 ++--
 .../lib/{flang_rt => runtime}/unit-map.cpp    |  2 +-
 flang-rt/lib/{flang_rt => runtime}/unit-map.h | 12 +++---
 flang-rt/lib/{flang_rt => runtime}/unit.cpp   |  8 ++--
 flang-rt/lib/{flang_rt => runtime}/unit.h     | 30 +++++++-------
 flang-rt/lib/{flang_rt => runtime}/utf.cpp    |  4 +-
 flang-rt/test/CMakeLists.txt                  |  4 +-
 flang-rt/test/lit.cfg.py                      |  2 +-
 .../Evaluate/ISO-Fortran-binding.cpp          |  2 +-
 flang-rt/unittests/Evaluate/reshape.cpp       |  2 +-
 flang-rt/unittests/Runtime/Allocatable.cpp    |  2 +-
 .../unittests/Runtime/ArrayConstructor.cpp    |  6 +--
 flang-rt/unittests/Runtime/BufferTest.cpp     |  2 +-
 flang-rt/unittests/Runtime/CMakeLists.txt     |  2 +-
 .../unittests/Runtime/CUDA/Allocatable.cpp    |  8 ++--
 .../unittests/Runtime/CUDA/AllocatorCUF.cpp   |  8 ++--
 .../unittests/Runtime/CUDA/CMakeLists.txt     |  2 +-
 flang-rt/unittests/Runtime/CUDA/Memory.cpp    |  6 +--
 flang-rt/unittests/Runtime/CharacterTest.cpp  |  2 +-
 flang-rt/unittests/Runtime/CommandTest.cpp    |  2 +-
 .../unittests/Runtime/CrashHandlerFixture.cpp |  2 +-
 flang-rt/unittests/Runtime/Derived.cpp        |  4 +-
 flang-rt/unittests/Runtime/ExternalIOTest.cpp |  2 +-
 flang-rt/unittests/Runtime/Format.cpp         |  6 +--
 flang-rt/unittests/Runtime/Inquiry.cpp        |  2 +-
 flang-rt/unittests/Runtime/ListInputTest.cpp  |  4 +-
 .../unittests/Runtime/LogicalFormatTest.cpp   |  2 +-
 flang-rt/unittests/Runtime/Matmul.cpp         |  4 +-
 .../unittests/Runtime/MatmulTranspose.cpp     |  4 +-
 flang-rt/unittests/Runtime/MiscIntrinsic.cpp  |  2 +-
 flang-rt/unittests/Runtime/Namelist.cpp       |  4 +-
 .../unittests/Runtime/NumericalFormatTest.cpp |  2 +-
 flang-rt/unittests/Runtime/Pointer.cpp        |  4 +-
 flang-rt/unittests/Runtime/Random.cpp         |  4 +-
 flang-rt/unittests/Runtime/Reduction.cpp      |  4 +-
 .../unittests/Runtime/RuntimeCrashTest.cpp    |  2 +-
 flang-rt/unittests/Runtime/Stop.cpp           |  2 +-
 flang-rt/unittests/Runtime/Support.cpp        |  2 +-
 flang-rt/unittests/Runtime/TemporaryStack.cpp |  6 +--
 .../unittests/Runtime/Transformational.cpp    |  2 +-
 flang-rt/unittests/Runtime/tools.h            |  4 +-
 flang/docs/FlangDriver.md                     |  6 +--
 flang/docs/GettingStarted.md                  | 10 ++---
 flang/docs/OpenACC-descriptor-management.md   |  2 +-
 flang/docs/ReleaseNotes.md                    |  6 +--
 flang/examples/CMakeLists.txt                 |  4 +-
 .../ExternalHelloWorld/CMakeLists.txt         |  4 +-
 flang/runtime/CMakeLists.txt                  | 41 ++++++++++---------
 flang/runtime/CUDA/CMakeLists.txt             |  8 ++--
 flang/test/CMakeLists.txt                     |  2 +-
 .../test/Driver/gcc-toolchain-install-dir.f90 |  2 +-
 flang/test/Driver/linker-flags.f90            |  8 ++--
 .../test/Driver/msvc-dependent-lib-flags.f90  |  8 ++--
 flang/test/Driver/nostdlib.f90                |  2 +-
 flang/unittests/Evaluate/CMakeLists.txt       |  4 +-
 flang/unittests/Runtime/CMakeLists.txt        |  2 +-
 flang/unittests/Runtime/CUDA/CMakeLists.txt   |  4 +-
 lld/COFF/MinGW.cpp                            |  2 +-
 229 files changed, 727 insertions(+), 685 deletions(-)
 create mode 100644 flang-rt/examples/CMakeLists.txt
 create mode 100644 flang-rt/examples/ExternalHelloWorld/CMakeLists.txt
 rename {flang => flang-rt}/examples/ExternalHelloWorld/external-hello.cpp (80%)
 rename flang-rt/include/flang-rt/{ => runtime}/allocator-registry.h (87%)
 rename flang-rt/include/flang-rt/{ => runtime}/array-constructor.h (90%)
 rename flang-rt/include/flang-rt/{ => runtime}/assign-impl.h (85%)
 rename flang-rt/include/flang-rt/{ => runtime}/buffer.h (97%)
 rename flang-rt/include/flang-rt/{ => runtime}/connection.h (96%)
 rename flang-rt/include/flang-rt/{ => runtime}/derived.h (91%)
 rename flang-rt/include/flang-rt/{ => runtime}/descriptor.h (98%)
 rename flang-rt/include/flang-rt/{ => runtime}/emit-encoded.h (94%)
 rename flang-rt/include/flang-rt/{ => runtime}/environment.h (91%)
 rename flang-rt/include/flang-rt/{ => runtime}/file.h (95%)
 rename flang-rt/include/flang-rt/{ => runtime}/format-implementation.h (98%)
 rename flang-rt/include/flang-rt/{ => runtime}/format.h (97%)
 rename flang-rt/include/flang-rt/{ => runtime}/internal-unit.h (91%)
 rename flang-rt/include/flang-rt/{ => runtime}/io-error.h (94%)
 rename flang-rt/include/flang-rt/{ => runtime}/io-stmt.h (99%)
 rename flang-rt/include/flang-rt/{ => runtime}/lock.h (94%)
 rename flang-rt/include/flang-rt/{ => runtime}/memory.h (96%)
 rename flang-rt/include/flang-rt/{ => runtime}/namelist.h (91%)
 rename flang-rt/include/flang-rt/{ => runtime}/non-tbp-dio.h (91%)
 rename flang-rt/include/flang-rt/{ => runtime}/numeric-templates.h (98%)
 rename flang-rt/include/flang-rt/{ => runtime}/random-templates.h (94%)
 rename flang-rt/include/flang-rt/{ => runtime}/reduction-templates.h (98%)
 rename flang-rt/include/flang-rt/{ => runtime}/stat.h (93%)
 rename flang-rt/include/flang-rt/{ => runtime}/terminator.h (95%)
 rename flang-rt/include/flang-rt/{ => runtime}/tools.h (99%)
 rename flang-rt/include/flang-rt/{ => runtime}/type-code.h (93%)
 rename flang-rt/include/flang-rt/{ => runtime}/type-info.h (98%)
 rename flang-rt/include/flang-rt/{ => runtime}/utf.h (94%)
 rename flang-rt/lib/{flang_rt/CUDA => cuda}/CMakeLists.txt (68%)
 rename flang-rt/lib/{flang_rt/CUDA => cuda}/allocatable.cpp (94%)
 rename flang-rt/lib/{flang_rt/CUDA => cuda}/allocator.cpp (86%)
 rename flang-rt/lib/{flang_rt/CUDA => cuda}/descriptor.cpp (90%)
 rename flang-rt/lib/{flang_rt/CUDA => cuda}/init.cpp (83%)
 rename flang-rt/lib/{flang_rt/CUDA => cuda}/kernel.cpp (98%)
 rename flang-rt/lib/{flang_rt/CUDA => cuda}/memmove-function.cpp (91%)
 rename flang-rt/lib/{flang_rt/CUDA => cuda}/memory.cpp (97%)
 rename flang-rt/lib/{flang_rt/CUDA => cuda}/pointer.cpp (92%)
 rename flang-rt/lib/{flang_rt/CUDA => cuda}/registration.cpp (93%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/CMakeLists.txt (94%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/acos.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/acosh.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/asin.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/asinh.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/atan.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/atan2.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/atanh.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/ceil.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/complex-math.c (100%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/complex-math.h (88%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/cos.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/cosh.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/erf.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/erfc.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/exp.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/exponent.cpp (92%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/floor.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/fma.cpp (91%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/fraction.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/hypot.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/j0.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/j1.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/jn.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/lgamma.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/llround.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/log.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/log10.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/lround.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/math-entries.h (96%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/mod-real.cpp (91%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/modulo-real.cpp (91%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/nearbyint.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/nearest.cpp (91%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/norm2.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/numeric-template-specs.h (82%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/pow.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/random.cpp (85%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/remainder.cpp (91%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/round.cpp (91%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/rrspacing.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/scale.cpp (92%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/set-exponent.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/sin.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/sinh.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/spacing.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/sqrt.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/tan.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/tanh.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/tgamma.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/trunc.cpp (91%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/y0.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/y1.cpp (90%)
 rename flang-rt/lib/{FortranFloat128Math => quadmath}/yn.cpp (90%)
 rename flang-rt/lib/{flang_rt => runtime}/CMakeLists.txt (83%)
 rename flang-rt/lib/{flang_rt => runtime}/ISO_Fortran_binding.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/ISO_Fortran_util.h (90%)
 rename flang-rt/lib/{flang_rt => runtime}/allocatable.cpp (96%)
 rename flang-rt/lib/{flang_rt => runtime}/allocator-registry.cpp (89%)
 rename flang-rt/lib/{flang_rt => runtime}/array-constructor.cpp (96%)
 rename flang-rt/lib/{flang_rt => runtime}/assign.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/buffer.cpp (90%)
 rename flang-rt/lib/{flang_rt => runtime}/character.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/command.cpp (96%)
 rename flang-rt/lib/{flang_rt => runtime}/complex-powi.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/complex-reduction.c (100%)
 rename flang-rt/lib/{flang_rt => runtime}/complex-reduction.h (97%)
 rename flang-rt/lib/{flang_rt => runtime}/connection.cpp (91%)
 rename flang-rt/lib/{flang_rt => runtime}/copy.cpp (97%)
 rename flang-rt/lib/{flang_rt => runtime}/copy.h (78%)
 rename flang-rt/lib/{flang_rt => runtime}/derived-api.cpp (95%)
 rename flang-rt/lib/{flang_rt => runtime}/derived.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/descriptor-io.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/descriptor-io.h (98%)
 rename flang-rt/lib/{flang_rt => runtime}/descriptor.cpp (97%)
 rename flang-rt/lib/{flang_rt => runtime}/dot-product.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/edit-input.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/edit-input.h (87%)
 rename flang-rt/lib/{flang_rt => runtime}/edit-output.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/edit-output.h (95%)
 rename flang-rt/lib/{flang_rt => runtime}/environment-default-list.h (100%)
 rename flang-rt/lib/{flang_rt => runtime}/environment.cpp (97%)
 rename flang-rt/lib/{flang_rt => runtime}/exceptions.cpp (97%)
 rename flang-rt/lib/{flang_rt => runtime}/execute.cpp (97%)
 rename flang-rt/lib/{flang_rt => runtime}/extensions.cpp (97%)
 rename flang-rt/lib/{flang_rt => runtime}/external-unit.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/extrema.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/file.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/findloc.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/format.cpp (89%)
 rename flang-rt/lib/{flang_rt => runtime}/inquiry.cpp (94%)
 rename flang-rt/lib/{flang_rt => runtime}/internal-unit.cpp (96%)
 rename flang-rt/lib/{flang_rt => runtime}/io-api-common.h (92%)
 rename flang-rt/lib/{flang_rt => runtime}/io-api-minimal.cpp (96%)
 rename flang-rt/lib/{flang_rt => runtime}/io-api.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/io-error.cpp (97%)
 rename flang-rt/lib/{flang_rt => runtime}/io-stmt.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/iostat.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/main.cpp (90%)
 rename flang-rt/lib/{flang_rt => runtime}/matmul-transpose.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/matmul.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/memory.cpp (87%)
 rename flang-rt/lib/{flang_rt => runtime}/misc-intrinsic.cpp (95%)
 rename flang-rt/lib/{flang_rt => runtime}/namelist.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/non-tbp-dio.cpp (86%)
 rename flang-rt/lib/{flang_rt => runtime}/numeric.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/pointer.cpp (96%)
 rename flang-rt/lib/{flang_rt => runtime}/product.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/pseudo-unit.cpp (97%)
 rename flang-rt/lib/{flang_rt => runtime}/ragged.cpp (96%)
 rename flang-rt/lib/{flang_rt => runtime}/random.cpp (96%)
 rename flang-rt/lib/{flang_rt => runtime}/reduce.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/reduction.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/stack.h (93%)
 rename flang-rt/lib/{flang_rt => runtime}/stat.cpp (93%)
 rename flang-rt/lib/{flang_rt => runtime}/stop.cpp (95%)
 rename flang-rt/lib/{flang_rt => runtime}/sum.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/support.cpp (91%)
 rename flang-rt/lib/{flang_rt => runtime}/temporary-stack.cpp (97%)
 rename flang-rt/lib/{flang_rt => runtime}/terminator.cpp (96%)
 rename flang-rt/lib/{flang_rt => runtime}/time-intrinsic.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/tools.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/transformational.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/type-code.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/type-info.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/unit-map.cpp (98%)
 rename flang-rt/lib/{flang_rt => runtime}/unit-map.h (92%)
 rename flang-rt/lib/{flang_rt => runtime}/unit.cpp (99%)
 rename flang-rt/lib/{flang_rt => runtime}/unit.h (95%)
 rename flang-rt/lib/{flang_rt => runtime}/utf.cpp (97%)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 361cf41544b0642..dcfa80602a04214 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1321,7 +1321,7 @@ void tools::addOpenMPHostOffloadingArgs(const Compilation &C,
 /// Add Fortran runtime libs
 void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
                                   llvm::opt::ArgStringList &CmdArgs) {
-  // Link flang_rt
+  // Link flang_rt.runtime
   // These are handled earlier on Windows by telling the frontend driver to
   // add the correct libraries to link against as dependents in the object
   // file.
@@ -1337,7 +1337,7 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
       if (AsNeeded)
         addAsNeededOption(TC, Args, CmdArgs, /*as_needed=*/false);
     }
-    CmdArgs.push_back("-lflang_rt");
+    CmdArgs.push_back("-lflang_rt.runtime");
     addArchSpecificRPath(TC, Args, CmdArgs);
   }
 
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index a40b4026e3f57cf..f6ff58d07e822ea 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -367,26 +367,26 @@ static void processVSRuntimeLibrary(const ToolChain &TC, const ArgList &Args,
   case options::OPT__SLASH_MT:
     CmdArgs.push_back("-D_MT");
     CmdArgs.push_back("--dependent-lib=libcmt");
-    CmdArgs.push_back("--dependent-lib=flang_rt.static.lib");
+    CmdArgs.push_back("--dependent-lib=flang_rt.runtime.static.lib");
     break;
   case options::OPT__SLASH_MTd:
     CmdArgs.push_back("-D_MT");
     CmdArgs.push_back("-D_DEBUG");
     CmdArgs.push_back("--dependent-lib=libcmtd");
-    CmdArgs.push_back("--dependent-lib=flang_rt.static_dbg.lib");
+    CmdArgs.push_back("--dependent-lib=flang_rt.runtime.static_dbg.lib");
     break;
   case options::OPT__SLASH_MD:
     CmdArgs.push_back("-D_MT");
     CmdArgs.push_back("-D_DLL");
     CmdArgs.push_back("--dependent-lib=msvcrt");
-    CmdArgs.push_back("--dependent-lib=flang_rt.dynamic.lib");
+    CmdArgs.push_back("--dependent-lib=flang_rt.runtime.dynamic.lib");
     break;
   case options::OPT__SLASH_MDd:
     CmdArgs.push_back("-D_MT");
     CmdArgs.push_back("-D_DEBUG");
     CmdArgs.push_back("-D_DLL");
     CmdArgs.push_back("--dependent-lib=msvcrtd");
-    CmdArgs.push_back("--dependent-lib=flang_rt.dynamic_dbg.lib");
+    CmdArgs.push_back("--dependent-lib=flang_rt.runtime.dynamic_dbg.lib");
     break;
   }
 }
diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt
index 655d0a55b400446..37f49ebc0d7bb74 100644
--- a/flang-rt/CMakeLists.txt
+++ b/flang-rt/CMakeLists.txt
@@ -168,8 +168,8 @@ else ()
 endif ()
 
 
-option(FLANG_RT_ENABLE_CUF "Compile CUDA Fortran runtime sources" OFF)
-if (FLANG_RT_ENABLE_CUF)
+option(FLANG_RT_INCLUDE_CUF "Build the CUDA Fortran runtime (libflang_rt.cuda.a)" OFF)
+if (FLANG_RT_INCLUDE_CUF)
   find_package(CUDAToolkit REQUIRED)
 endif()
 
@@ -240,6 +240,10 @@ add_custom_target(flang-rt)
 
 add_subdirectory(lib)
 
+if (LLVM_INCLUDE_EXAMPLES)
+  add_subdirectory(examples)
+endif ()
+
 if (FLANG_RT_INCLUDE_TESTS)
   add_subdirectory(unittests)
   add_subdirectory(test)
diff --git a/flang-rt/README.md b/flang-rt/README.md
index 76993f27f8e4f2c..aba64e245387231 100644
--- a/flang-rt/README.md
+++ b/flang-rt/README.md
@@ -151,9 +151,9 @@ CMake itself provide.
    GPU accelerators using OpenMP offloading. Only Clang is supported for
    `CMAKE_C_COMPILER` and `CMAKE_CXX_COMPILER`.
 
- * `FLANG_RT_ENABLE_CUF` (bool, default: `OFF`)
+ * `FLANG_RT_INCLUDE_CUF` (bool, default: `OFF`)
 
-   Compiles the `libCufRuntime_cuda_<CUDA-version>.a/.so` library. This is
+   Compiles the `libflang_rt.cuda_<CUDA-version>.a/.so` library. This is
    independent of `FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA` and only
    requires a
    [CUDA Toolkit installation](https://cmake.org/cmake/help/latest/module/FindCUDAToolkit.html)
diff --git a/flang-rt/examples/CMakeLists.txt b/flang-rt/examples/CMakeLists.txt
new file mode 100644
index 000000000000000..f45a95d18b64137
--- /dev/null
+++ b/flang-rt/examples/CMakeLists.txt
@@ -0,0 +1,9 @@
+#===-- examples/CMakeLists.txt ---------------------------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+add_subdirectory(ExternalHelloWorld)
diff --git a/flang-rt/examples/ExternalHelloWorld/CMakeLists.txt b/flang-rt/examples/ExternalHelloWorld/CMakeLists.txt
new file mode 100644
index 000000000000000..e9f347f18cc437f
--- /dev/null
+++ b/flang-rt/examples/ExternalHelloWorld/CMakeLists.txt
@@ -0,0 +1,17 @@
+#===-- examples/ExternalHelloWorld/CMakeLists.txt --------------------------===#
+#
+# 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
+#
+#===------------------------------------------------------------------------===#
+
+# This test is not run by default as it requires input.
+add_llvm_example(external-hello-world
+  external-hello.cpp
+)
+
+target_link_libraries(external-hello-world
+  PRIVATE
+  flang_rt.runtime
+  )
diff --git a/flang/examples/ExternalHelloWorld/external-hello.cpp b/flang-rt/examples/ExternalHelloWorld/external-hello.cpp
similarity index 80%
rename from flang/examples/ExternalHelloWorld/external-hello.cpp
rename to flang-rt/examples/ExternalHelloWorld/external-hello.cpp
index 4991bf9eba99913..7c8a12476295ce9 100644
--- a/flang/examples/ExternalHelloWorld/external-hello.cpp
+++ b/flang-rt/examples/ExternalHelloWorld/external-hello.cpp
@@ -1,3 +1,11 @@
+//===-- examples/ExternalHelloWorld/external-hello.cpp ----------*- 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
+//
+//===----------------------------------------------------------------------===//
+
 #include "flang/Runtime/io-api.h"
 #include "flang/Runtime/main.h"
 #include "flang/Runtime/stop.h"
diff --git a/flang-rt/include/flang-rt/CUDA/allocator.h b/flang-rt/include/flang-rt/CUDA/allocator.h
index 68bb0736dd5eecb..5bc61cd04ee26f4 100644
--- a/flang-rt/include/flang-rt/CUDA/allocator.h
+++ b/flang-rt/include/flang-rt/CUDA/allocator.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/CUDA/allocator.h -----------------------*- C++ -*-===//
+//===-- include/flang-rt/cuda/allocator.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.
diff --git a/flang-rt/include/flang-rt/allocator-registry.h b/flang-rt/include/flang-rt/runtime/allocator-registry.h
similarity index 87%
rename from flang-rt/include/flang-rt/allocator-registry.h
rename to flang-rt/include/flang-rt/runtime/allocator-registry.h
index ea905afc045ce45..1a59ec8b1ef5b1e 100644
--- a/flang-rt/include/flang-rt/allocator-registry.h
+++ b/flang-rt/include/flang-rt/runtime/allocator-registry.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/allocator-registry.h -------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/allocator-registry.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.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_ALLOCATOR_REGISTRY_H_
-#define FLANG_RT_ALLOCATOR_REGISTRY_H_
+#ifndef FLANG_RT_RUNTIME_ALLOCATOR_REGISTRY_H_
+#define FLANG_RT_RUNTIME_ALLOCATOR_REGISTRY_H_
 
 #include "flang/Common/api-attrs.h"
 #include "flang/Runtime/allocator-registry-consts.h"
@@ -55,4 +55,4 @@ RT_OFFLOAD_VAR_GROUP_END
 
 } // namespace Fortran::runtime
 
-#endif // FLANG_RT_ALLOCATOR_REGISTRY_H_
+#endif // FLANG_RT_RUNTIME_ALLOCATOR_REGISTRY_H_
diff --git a/flang-rt/include/flang-rt/array-constructor.h b/flang-rt/include/flang-rt/runtime/array-constructor.h
similarity index 90%
rename from flang-rt/include/flang-rt/array-constructor.h
rename to flang-rt/include/flang-rt/runtime/array-constructor.h
index 738050f80ef65f5..9c037177161c0ef 100644
--- a/flang-rt/include/flang-rt/array-constructor.h
+++ b/flang-rt/include/flang-rt/runtime/array-constructor.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/array-constructor.h --------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/array-constructor.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.
@@ -9,8 +9,8 @@
 // External APIs to create temporary storage for array constructors when their
 // final extents or length parameters cannot be pre-computed.
 
-#ifndef FLANG_RT_ARRAY_CONSTRUCTOR_H_
-#define FLANG_RT_ARRAY_CONSTRUCTOR_H_
+#ifndef FLANG_RT_RUNTIME_ARRAY_CONSTRUCTOR_H_
+#define FLANG_RT_RUNTIME_ARRAY_CONSTRUCTOR_H_
 
 #include "descriptor.h"
 #include "flang/Runtime/array-constructor-consts.h"
@@ -54,4 +54,4 @@ static_assert(alignof(Fortran::runtime::ArrayConstructorVector) <=
     "MaxArrayConstructorVectorAlignInBytes");
 
 } // namespace Fortran::runtime
-#endif // FLANG_RT_ARRAY_CONSTRUCTOR_H_
+#endif // FLANG_RT_RUNTIME_ARRAY_CONSTRUCTOR_H_
diff --git a/flang-rt/include/flang-rt/assign-impl.h b/flang-rt/include/flang-rt/runtime/assign-impl.h
similarity index 85%
rename from flang-rt/include/flang-rt/assign-impl.h
rename to flang-rt/include/flang-rt/runtime/assign-impl.h
index 2e85a451d5b5270..cc931f6713cd8fe 100644
--- a/flang-rt/include/flang-rt/assign-impl.h
+++ b/flang-rt/include/flang-rt/runtime/assign-impl.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/assign-impl.h --------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/assign-impl.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.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_ASSIGN_IMPL_H_
-#define FLANG_RT_ASSIGN_IMPL_H_
+#ifndef FLANG_RT_RUNTIME_ASSIGN_IMPL_H_
+#define FLANG_RT_RUNTIME_ASSIGN_IMPL_H_
 
 #include "flang/Runtime/freestanding-tools.h"
 
@@ -28,4 +28,4 @@ RT_API_ATTRS void DoFromSourceAssign(Descriptor &, const Descriptor &,
 #endif
 
 } // namespace Fortran::runtime
-#endif // FLANG_RT_ASSIGN_IMPL_H_
+#endif // FLANG_RT_RUNTIME_ASSIGN_IMPL_H_
diff --git a/flang-rt/include/flang-rt/buffer.h b/flang-rt/include/flang-rt/runtime/buffer.h
similarity index 97%
rename from flang-rt/include/flang-rt/buffer.h
rename to flang-rt/include/flang-rt/runtime/buffer.h
index bd2e8c47c6e3014..b5a9ce9e35e91da 100644
--- a/flang-rt/include/flang-rt/buffer.h
+++ b/flang-rt/include/flang-rt/runtime/buffer.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/buffer.h -------------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/buffer.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.
@@ -8,8 +8,8 @@
 
 // External file buffering
 
-#ifndef FLANG_RT_BUFFER_H_
-#define FLANG_RT_BUFFER_H_
+#ifndef FLANG_RT_RUNTIME_BUFFER_H_
+#define FLANG_RT_RUNTIME_BUFFER_H_
 
 #include "io-error.h"
 #include "memory.h"
@@ -221,4 +221,4 @@ template <typename STORE, std::size_t minBuffer = 65536> class FileFrame {
   bool dirty_{false};
 };
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_BUFFER_H_
+#endif // FLANG_RT_RUNTIME_BUFFER_H_
diff --git a/flang-rt/include/flang-rt/connection.h b/flang-rt/include/flang-rt/runtime/connection.h
similarity index 96%
rename from flang-rt/include/flang-rt/connection.h
rename to flang-rt/include/flang-rt/runtime/connection.h
index 550ddb15c72367b..03d9658e7067bb9 100644
--- a/flang-rt/include/flang-rt/connection.h
+++ b/flang-rt/include/flang-rt/runtime/connection.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/connection.h ---------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/connection.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.
@@ -8,8 +8,8 @@
 
 // Fortran I/O connection state (abstracted over internal & external units)
 
-#ifndef FLANG_RT_CONNECTION_H_
-#define FLANG_RT_CONNECTION_H_
+#ifndef FLANG_RT_RUNTIME_CONNECTION_H_
+#define FLANG_RT_RUNTIME_CONNECTION_H_
 
 #include "format.h"
 #include "flang/Common/optional.h"
@@ -124,4 +124,4 @@ class SavedPosition {
 };
 
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_CONNECTION_H_
+#endif // FLANG_RT_RUNTIME_CONNECTION_H_
diff --git a/flang-rt/include/flang-rt/derived.h b/flang-rt/include/flang-rt/runtime/derived.h
similarity index 91%
rename from flang-rt/include/flang-rt/derived.h
rename to flang-rt/include/flang-rt/runtime/derived.h
index d65fb7517249554..ac6962c57168cff 100644
--- a/flang-rt/include/flang-rt/derived.h
+++ b/flang-rt/include/flang-rt/runtime/derived.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/derived.h ------------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/derived.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.
@@ -8,8 +8,8 @@
 
 // Internal runtime utilities for derived type operations.
 
-#ifndef FLANG_RT_DERIVED_H_
-#define FLANG_RT_DERIVED_H_
+#ifndef FLANG_RT_RUNTIME_DERIVED_H_
+#define FLANG_RT_RUNTIME_DERIVED_H_
 
 #include "flang/Common/api-attrs.h"
 
@@ -48,4 +48,4 @@ RT_API_ATTRS void Destroy(const Descriptor &, bool finalize,
 RT_API_ATTRS bool HasDynamicComponent(const Descriptor &);
 
 } // namespace Fortran::runtime
-#endif // FLANG_RT_DERIVED_H_
+#endif // FLANG_RT_RUNTIME_DERIVED_H_
diff --git a/flang-rt/include/flang-rt/descriptor.h b/flang-rt/include/flang-rt/runtime/descriptor.h
similarity index 98%
rename from flang-rt/include/flang-rt/descriptor.h
rename to flang-rt/include/flang-rt/runtime/descriptor.h
index f97b8443425dcd7..19e1a0bf6a1ddd4 100644
--- a/flang-rt/include/flang-rt/descriptor.h
+++ b/flang-rt/include/flang-rt/runtime/descriptor.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/descriptor.h ---------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/descriptor.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.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_DESCRIPTOR_H_
-#define FLANG_RT_DESCRIPTOR_H_
+#ifndef FLANG_RT_RUNTIME_DESCRIPTOR_H_
+#define FLANG_RT_RUNTIME_DESCRIPTOR_H_
 
 // Defines data structures used during execution of a Fortran program
 // to implement nontrivial dummy arguments, pointers, allocatables,
@@ -482,4 +482,4 @@ class alignas(Descriptor) StaticDescriptor {
 };
 
 } // namespace Fortran::runtime
-#endif // FLANG_RT_DESCRIPTOR_H_
+#endif // FLANG_RT_RUNTIME_DESCRIPTOR_H_
diff --git a/flang-rt/include/flang-rt/emit-encoded.h b/flang-rt/include/flang-rt/runtime/emit-encoded.h
similarity index 94%
rename from flang-rt/include/flang-rt/emit-encoded.h
rename to flang-rt/include/flang-rt/runtime/emit-encoded.h
index 279f751daff2cc5..d99f56b29558e15 100644
--- a/flang-rt/include/flang-rt/emit-encoded.h
+++ b/flang-rt/include/flang-rt/runtime/emit-encoded.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/emit-encoded.h -------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/emit-encoded.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.
@@ -8,8 +8,8 @@
 
 // Templates for emitting CHARACTER values with conversion
 
-#ifndef FLANG_RT_EMIT_ENCODED_H_
-#define FLANG_RT_EMIT_ENCODED_H_
+#ifndef FLANG_RT_RUNTIME_EMIT_ENCODED_H_
+#define FLANG_RT_RUNTIME_EMIT_ENCODED_H_
 
 #include "connection.h"
 #include "environment.h"
@@ -114,4 +114,4 @@ RT_API_ATTRS bool EmitRepeated(CONTEXT &to, char ch, std::size_t n) {
 }
 
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_EMIT_ENCODED_H_
+#endif // FLANG_RT_RUNTIME_EMIT_ENCODED_H_
diff --git a/flang-rt/include/flang-rt/environment.h b/flang-rt/include/flang-rt/runtime/environment.h
similarity index 91%
rename from flang-rt/include/flang-rt/environment.h
rename to flang-rt/include/flang-rt/runtime/environment.h
index 7fa3060768502ce..142add432b5f74c 100644
--- a/flang-rt/include/flang-rt/environment.h
+++ b/flang-rt/include/flang-rt/runtime/environment.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/environment.h --------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/environment.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.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_ENVIRONMENT_H_
-#define FLANG_RT_ENVIRONMENT_H_
+#ifndef FLANG_RT_RUNTIME_ENVIRONMENT_H_
+#define FLANG_RT_RUNTIME_ENVIRONMENT_H_
 
 #include "flang/Common/optional.h"
 #include "flang/Decimal/decimal.h"
@@ -67,4 +67,4 @@ RT_OFFLOAD_VAR_GROUP_END
 
 } // namespace Fortran::runtime
 
-#endif // FLANG_RT_ENVIRONMENT_H_
+#endif // FLANG_RT_RUNTIME_ENVIRONMENT_H_
diff --git a/flang-rt/include/flang-rt/file.h b/flang-rt/include/flang-rt/runtime/file.h
similarity index 95%
rename from flang-rt/include/flang-rt/file.h
rename to flang-rt/include/flang-rt/runtime/file.h
index 3ab21c655a38f20..3bba29722b3b8d5 100644
--- a/flang-rt/include/flang-rt/file.h
+++ b/flang-rt/include/flang-rt/runtime/file.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/file.h ---------------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/file.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.
@@ -8,8 +8,8 @@
 
 // Raw system I/O wrappers
 
-#ifndef FLANG_RT_FILE_H_
-#define FLANG_RT_FILE_H_
+#ifndef FLANG_RT_RUNTIME_FILE_H_
+#define FLANG_RT_RUNTIME_FILE_H_
 
 #include "io-error.h"
 #include "memory.h"
@@ -113,4 +113,4 @@ RT_API_ATTRS bool MayWrite(const char *path);
 RT_API_ATTRS bool MayReadAndWrite(const char *path);
 RT_API_ATTRS std::int64_t SizeInBytes(const char *path);
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_FILE_H_
+#endif // FLANG_RT_RUNTIME_FILE_H_
diff --git a/flang-rt/include/flang-rt/format-implementation.h b/flang-rt/include/flang-rt/runtime/format-implementation.h
similarity index 98%
rename from flang-rt/include/flang-rt/format-implementation.h
rename to flang-rt/include/flang-rt/runtime/format-implementation.h
index 6a2e1078ab35096..8f4eb1161dd141c 100644
--- a/flang-rt/include/flang-rt/format-implementation.h
+++ b/flang-rt/include/flang-rt/runtime/format-implementation.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/format-implementation.h ----------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/format-implementation.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.
@@ -8,8 +8,8 @@
 
 // Implements out-of-line member functions of template class FormatControl
 
-#ifndef FLANG_RT_FORMAT_IMPLEMENTATION_H_
-#define FLANG_RT_FORMAT_IMPLEMENTATION_H_
+#ifndef FLANG_RT_RUNTIME_FORMAT_IMPLEMENTATION_H_
+#define FLANG_RT_RUNTIME_FORMAT_IMPLEMENTATION_H_
 
 #include "emit-encoded.h"
 #include "format.h"
@@ -601,4 +601,4 @@ RT_API_ATTRS void FormatControl<CONTEXT>::Finish(Context &context) {
   }
 }
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_FORMAT_IMPLEMENTATION_H_
+#endif // FLANG_RT_RUNTIME_FORMAT_IMPLEMENTATION_H_
diff --git a/flang-rt/include/flang-rt/format.h b/flang-rt/include/flang-rt/runtime/format.h
similarity index 97%
rename from flang-rt/include/flang-rt/format.h
rename to flang-rt/include/flang-rt/runtime/format.h
index 09e151a2e98cac7..b169d63fa49990b 100644
--- a/flang-rt/include/flang-rt/format.h
+++ b/flang-rt/include/flang-rt/runtime/format.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/format.h -------------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/format.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.
@@ -8,8 +8,8 @@
 
 // FORMAT string processing
 
-#ifndef FLANG_RT_FORMAT_H_
-#define FLANG_RT_FORMAT_H_
+#ifndef FLANG_RT_RUNTIME_FORMAT_H_
+#define FLANG_RT_RUNTIME_FORMAT_H_
 
 #include "environment.h"
 #include "io-error.h"
@@ -201,4 +201,4 @@ template <typename CONTEXT> class FormatControl {
   Iteration stack_[maxMaxHeight];
 };
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_FORMAT_H_
+#endif // FLANG_RT_RUNTIME_FORMAT_H_
diff --git a/flang-rt/include/flang-rt/internal-unit.h b/flang-rt/include/flang-rt/runtime/internal-unit.h
similarity index 91%
rename from flang-rt/include/flang-rt/internal-unit.h
rename to flang-rt/include/flang-rt/runtime/internal-unit.h
index 946d69080e6110d..429d3489e01125a 100644
--- a/flang-rt/include/flang-rt/internal-unit.h
+++ b/flang-rt/include/flang-rt/runtime/internal-unit.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/internal-unit.h ------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/internal-unit.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.
@@ -8,8 +8,8 @@
 
 // Fortran internal I/O "units"
 
-#ifndef FLANG_RT_INTERNAL_UNIT_H_
-#define FLANG_RT_INTERNAL_UNIT_H_
+#ifndef FLANG_RT_RUNTIME_INTERNAL_UNIT_H_
+#define FLANG_RT_RUNTIME_INTERNAL_UNIT_H_
 
 #include "connection.h"
 #include "descriptor.h"
@@ -56,4 +56,4 @@ template <Direction DIR> class InternalDescriptorUnit : public ConnectionState {
 extern template class InternalDescriptorUnit<Direction::Output>;
 extern template class InternalDescriptorUnit<Direction::Input>;
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_INTERNAL_UNIT_H_
+#endif // FLANG_RT_RUNTIME_INTERNAL_UNIT_H_
diff --git a/flang-rt/include/flang-rt/io-error.h b/flang-rt/include/flang-rt/runtime/io-error.h
similarity index 94%
rename from flang-rt/include/flang-rt/io-error.h
rename to flang-rt/include/flang-rt/runtime/io-error.h
index 6b5c0f9f390197d..1cef6a208f374c7 100644
--- a/flang-rt/include/flang-rt/io-error.h
+++ b/flang-rt/include/flang-rt/runtime/io-error.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/io-error.h -----------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/io-error.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.
@@ -12,8 +12,8 @@
 // IOSTAT error codes are raw errno values augmented with values for
 // Fortran-specific errors.
 
-#ifndef FLANG_RT_IO_ERROR_H_
-#define FLANG_RT_IO_ERROR_H_
+#ifndef FLANG_RT_RUNTIME_IO_ERROR_H_
+#define FLANG_RT_RUNTIME_IO_ERROR_H_
 
 #include "memory.h"
 #include "terminator.h"
@@ -80,4 +80,4 @@ class IoErrorHandler : public Terminator {
 };
 
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_IO_ERROR_H_
+#endif // FLANG_RT_RUNTIME_IO_ERROR_H_
diff --git a/flang-rt/include/flang-rt/io-stmt.h b/flang-rt/include/flang-rt/runtime/io-stmt.h
similarity index 99%
rename from flang-rt/include/flang-rt/io-stmt.h
rename to flang-rt/include/flang-rt/runtime/io-stmt.h
index 7efab320e5e1a6c..a364ddfd9b3c71f 100644
--- a/flang-rt/include/flang-rt/io-stmt.h
+++ b/flang-rt/include/flang-rt/runtime/io-stmt.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/io-stmt.h ------------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/io-stmt.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.
@@ -8,8 +8,8 @@
 
 // Representations of the state of an I/O statement in progress
 
-#ifndef FLANG_RT_IO_STMT_H_
-#define FLANG_RT_IO_STMT_H_
+#ifndef FLANG_RT_RUNTIME_IO_STMT_H_
+#define FLANG_RT_RUNTIME_IO_STMT_H_
 
 #include "connection.h"
 #include "descriptor.h"
@@ -791,4 +791,4 @@ class ErroneousIoStatementState : public IoStatementBase {
 };
 
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_IO_STMT_H_
+#endif // FLANG_RT_RUNTIME_IO_STMT_H_
diff --git a/flang-rt/include/flang-rt/lock.h b/flang-rt/include/flang-rt/runtime/lock.h
similarity index 94%
rename from flang-rt/include/flang-rt/lock.h
rename to flang-rt/include/flang-rt/runtime/lock.h
index 1b09434d26f4fe4..7c88534245733a3 100644
--- a/flang-rt/include/flang-rt/lock.h
+++ b/flang-rt/include/flang-rt/runtime/lock.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/lock.h ---------------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/lock.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.
@@ -8,8 +8,8 @@
 
 // Wraps a mutex
 
-#ifndef FLANG_RT_LOCK_H_
-#define FLANG_RT_LOCK_H_
+#ifndef FLANG_RT_RUNTIME_LOCK_H_
+#define FLANG_RT_RUNTIME_LOCK_H_
 
 #include "terminator.h"
 #include "tools.h"
@@ -113,4 +113,4 @@ class CriticalSection {
 };
 } // namespace Fortran::runtime
 
-#endif // FLANG_RT_LOCK_H_
+#endif // FLANG_RT_RUNTIME_LOCK_H_
diff --git a/flang-rt/include/flang-rt/memory.h b/flang-rt/include/flang-rt/runtime/memory.h
similarity index 96%
rename from flang-rt/include/flang-rt/memory.h
rename to flang-rt/include/flang-rt/runtime/memory.h
index 64db30baf2806ac..93b477afa981453 100644
--- a/flang-rt/include/flang-rt/memory.h
+++ b/flang-rt/include/flang-rt/runtime/memory.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/memory.h -------------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/memory.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.
@@ -9,8 +9,8 @@
 // Thin wrapper around malloc()/free() to isolate the dependency,
 // ease porting, and provide an owning pointer.
 
-#ifndef FLANG_RT_MEMORY_H_
-#define FLANG_RT_MEMORY_H_
+#ifndef FLANG_RT_RUNTIME_MEMORY_H_
+#define FLANG_RT_RUNTIME_MEMORY_H_
 
 #include "flang/Common/api-attrs.h"
 #include <cassert>
@@ -170,4 +170,4 @@ template <typename A> struct Allocator {
 };
 } // namespace Fortran::runtime
 
-#endif // FLANG_RT_MEMORY_H_
+#endif // FLANG_RT_RUNTIME_MEMORY_H_
diff --git a/flang-rt/include/flang-rt/namelist.h b/flang-rt/include/flang-rt/runtime/namelist.h
similarity index 91%
rename from flang-rt/include/flang-rt/namelist.h
rename to flang-rt/include/flang-rt/runtime/namelist.h
index 2ae93ca9c20505e..17d7bf310cc96d5 100644
--- a/flang-rt/include/flang-rt/namelist.h
+++ b/flang-rt/include/flang-rt/runtime/namelist.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/namelist.h -----------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/namelist.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.
@@ -8,8 +8,8 @@
 
 // Defines the data structure used for NAMELIST I/O
 
-#ifndef FLANG_RT_NAMELIST_H_
-#define FLANG_RT_NAMELIST_H_
+#ifndef FLANG_RT_RUNTIME_NAMELIST_H_
+#define FLANG_RT_RUNTIME_NAMELIST_H_
 
 #include "non-tbp-dio.h"
 #include "flang/Common/api-attrs.h"
@@ -51,4 +51,4 @@ class NamelistGroup {
 RT_API_ATTRS bool IsNamelistNameOrSlash(IoStatementState &);
 
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_NAMELIST_H_
+#endif // FLANG_RT_RUNTIME_NAMELIST_H_
diff --git a/flang-rt/include/flang-rt/non-tbp-dio.h b/flang-rt/include/flang-rt/runtime/non-tbp-dio.h
similarity index 91%
rename from flang-rt/include/flang-rt/non-tbp-dio.h
rename to flang-rt/include/flang-rt/runtime/non-tbp-dio.h
index 1088257b72f5926..2bbbfa7f97f79c2 100644
--- a/flang-rt/include/flang-rt/non-tbp-dio.h
+++ b/flang-rt/include/flang-rt/runtime/non-tbp-dio.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/non-tbp-dio.h --------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/non-tbp-dio.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.
@@ -19,8 +19,8 @@
 // a containing scope has become inaccessible in a nested scope due
 // to the use of "IMPORT, NONE" or "IMPORT, ONLY:".
 
-#ifndef FLANG_RT_NON_TBP_DIO_H_
-#define FLANG_RT_NON_TBP_DIO_H_
+#ifndef FLANG_RT_RUNTIME_NON_TBP_DIO_H_
+#define FLANG_RT_RUNTIME_NON_TBP_DIO_H_
 
 #include "flang/Common/Fortran-consts.h"
 #include "flang/Common/api-attrs.h"
@@ -53,4 +53,4 @@ struct NonTbpDefinedIoTable {
 };
 
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_NON_TBP_DIO_H_
+#endif // FLANG_RT_RUNTIME_NON_TBP_DIO_H_
diff --git a/flang-rt/include/flang-rt/numeric-templates.h b/flang-rt/include/flang-rt/runtime/numeric-templates.h
similarity index 98%
rename from flang-rt/include/flang-rt/numeric-templates.h
rename to flang-rt/include/flang-rt/runtime/numeric-templates.h
index a1475923ff3b163..1253d56f3c0fdd8 100644
--- a/flang-rt/include/flang-rt/numeric-templates.h
+++ b/flang-rt/include/flang-rt/runtime/numeric-templates.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/numeric-templates.h --------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/numeric-templates.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.
@@ -15,8 +15,8 @@
 // for the data type corresponding to CppTypeFor<TypeCategory::Real, 16>
 // on the target.
 
-#ifndef FLANG_RT_NUMERIC_TEMPLATES_H_
-#define FLANG_RT_NUMERIC_TEMPLATES_H_
+#ifndef FLANG_RT_RUNTIME_NUMERIC_TEMPLATES_H_
+#define FLANG_RT_RUNTIME_NUMERIC_TEMPLATES_H_
 
 #include "terminator.h"
 #include "tools.h"
@@ -368,4 +368,4 @@ template <typename T> inline RT_API_ATTRS T ErfcScaled(T arg) {
 
 } // namespace Fortran::runtime
 
-#endif // FLANG_RT_NUMERIC_TEMPLATES_H_
+#endif // FLANG_RT_RUNTIME_NUMERIC_TEMPLATES_H_
diff --git a/flang-rt/include/flang-rt/random-templates.h b/flang-rt/include/flang-rt/runtime/random-templates.h
similarity index 94%
rename from flang-rt/include/flang-rt/random-templates.h
rename to flang-rt/include/flang-rt/runtime/random-templates.h
index 00542bd5b2553f2..895c5ad4fc8bb35 100644
--- a/flang-rt/include/flang-rt/random-templates.h
+++ b/flang-rt/include/flang-rt/runtime/random-templates.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/random-templates.h ---------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/random-templates.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.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_RANDOM_TEMPLATES_H_
-#define FLANG_RT_RANDOM_TEMPLATES_H_
+#ifndef FLANG_RT_RUNTIME_RANDOM_TEMPLATES_H_
+#define FLANG_RT_RUNTIME_RANDOM_TEMPLATES_H_
 
 #include "descriptor.h"
 #include "lock.h"
@@ -108,4 +108,4 @@ inline void GenerateUnsigned(const Descriptor &harvest) {
 
 } // namespace Fortran::runtime::random
 
-#endif // FLANG_RT_RANDOM_TEMPLATES_H_
+#endif // FLANG_RT_RUNTIME_RANDOM_TEMPLATES_H_
diff --git a/flang-rt/include/flang-rt/reduction-templates.h b/flang-rt/include/flang-rt/runtime/reduction-templates.h
similarity index 98%
rename from flang-rt/include/flang-rt/reduction-templates.h
rename to flang-rt/include/flang-rt/runtime/reduction-templates.h
index d6b3c24920cf9c2..8c6f838b8dadf53 100644
--- a/flang-rt/include/flang-rt/reduction-templates.h
+++ b/flang-rt/include/flang-rt/runtime/reduction-templates.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/reduction-templates.h ------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/reduction-templates.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.
@@ -18,8 +18,8 @@
 // * Character-valued reductions (MAXVAL & MINVAL) return arbitrary
 //   length results, dynamically allocated in a caller-supplied descriptor
 
-#ifndef FLANG_RT_REDUCTION_TEMPLATES_H_
-#define FLANG_RT_REDUCTION_TEMPLATES_H_
+#ifndef FLANG_RT_RUNTIME_REDUCTION_TEMPLATES_H_
+#define FLANG_RT_RUNTIME_REDUCTION_TEMPLATES_H_
 
 #include "descriptor.h"
 #include "numeric-templates.h"
@@ -416,4 +416,4 @@ template <int KIND> struct Norm2Helper {
 };
 
 } // namespace Fortran::runtime
-#endif // FLANG_RT_REDUCTION_TEMPLATES_H_
+#endif // FLANG_RT_RUNTIME_REDUCTION_TEMPLATES_H_
diff --git a/flang-rt/include/flang-rt/stat.h b/flang-rt/include/flang-rt/runtime/stat.h
similarity index 93%
rename from flang-rt/include/flang-rt/stat.h
rename to flang-rt/include/flang-rt/runtime/stat.h
index a6824f4803561d6..070d0bf8673fb5c 100644
--- a/flang-rt/include/flang-rt/stat.h
+++ b/flang-rt/include/flang-rt/runtime/stat.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/stat.h ---------------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/stat.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.
@@ -9,8 +9,8 @@
 // Defines the values returned by the runtime for STAT= specifiers
 // on executable statements.
 
-#ifndef FLANG_RT_STAT_H_
-#define FLANG_RT_STAT_H_
+#ifndef FLANG_RT_RUNTIME_STAT_H_
+#define FLANG_RT_RUNTIME_STAT_H_
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
 #include "flang/Common/api-attrs.h"
 #include "flang/Runtime/magic-numbers.h"
@@ -60,4 +60,4 @@ RT_API_ATTRS int ToErrmsg(const Descriptor *errmsg, int stat); // returns stat
 RT_API_ATTRS int ReturnError(Terminator &, int stat,
     const Descriptor *errmsg = nullptr, bool hasStat = false);
 } // namespace Fortran::runtime
-#endif // FLANG_RT_STAT_H_
+#endif // FLANG_RT_RUNTIME_STAT_H_
diff --git a/flang-rt/include/flang-rt/terminator.h b/flang-rt/include/flang-rt/runtime/terminator.h
similarity index 95%
rename from flang-rt/include/flang-rt/terminator.h
rename to flang-rt/include/flang-rt/runtime/terminator.h
index c2a4e1f64eb6653..4815f0674c8499a 100644
--- a/flang-rt/include/flang-rt/terminator.h
+++ b/flang-rt/include/flang-rt/runtime/terminator.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/terminator.h ---------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/terminator.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.
@@ -8,8 +8,8 @@
 
 // Termination of the image
 
-#ifndef FLANG_RT_TERMINATOR_H_
-#define FLANG_RT_TERMINATOR_H_
+#ifndef FLANG_RT_RUNTIME_TERMINATOR_H_
+#define FLANG_RT_RUNTIME_TERMINATOR_H_
 
 #include "flang/Common/api-attrs.h"
 #include <cstdarg>
@@ -121,4 +121,4 @@ namespace Fortran::runtime::io {
 RT_API_ATTRS void FlushOutputOnCrash(const Terminator &);
 }
 
-#endif // FLANG_RT_TERMINATOR_H_
+#endif // FLANG_RT_RUNTIME_TERMINATOR_H_
diff --git a/flang-rt/include/flang-rt/tools.h b/flang-rt/include/flang-rt/runtime/tools.h
similarity index 99%
rename from flang-rt/include/flang-rt/tools.h
rename to flang-rt/include/flang-rt/runtime/tools.h
index 3503c49308e4dd2..792aec7710a15b1 100644
--- a/flang-rt/include/flang-rt/tools.h
+++ b/flang-rt/include/flang-rt/runtime/tools.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/tools.h --------------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/tools.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.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_TOOLS_H_
-#define FLANG_RT_TOOLS_H_
+#ifndef FLANG_RT_RUNTIME_TOOLS_H_
+#define FLANG_RT_RUNTIME_TOOLS_H_
 
 #include "descriptor.h"
 #include "memory.h"
@@ -570,4 +570,4 @@ RT_API_ATTRS void CreatePartialReductionResult(Descriptor &result,
     const char *intrinsic, TypeCode);
 
 } // namespace Fortran::runtime
-#endif // FLANG_RT_TOOLS_H_
+#endif // FLANG_RT_RUNTIME_TOOLS_H_
diff --git a/flang-rt/include/flang-rt/type-code.h b/flang-rt/include/flang-rt/runtime/type-code.h
similarity index 93%
rename from flang-rt/include/flang-rt/type-code.h
rename to flang-rt/include/flang-rt/runtime/type-code.h
index a3b79462887c12b..9416a2816fd433c 100644
--- a/flang-rt/include/flang-rt/type-code.h
+++ b/flang-rt/include/flang-rt/runtime/type-code.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/type-code.h ----------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/type-code.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.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_TYPE_CODE_H_
-#define FLANG_RT_TYPE_CODE_H_
+#ifndef FLANG_RT_RUNTIME_TYPE_CODE_H_
+#define FLANG_RT_RUNTIME_TYPE_CODE_H_
 
 #include "flang/Common/Fortran-consts.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
@@ -75,4 +75,4 @@ class TypeCode {
   ISO::CFI_type_t raw_{CFI_type_other};
 };
 } // namespace Fortran::runtime
-#endif // FLANG_RT_TYPE_CODE_H_
+#endif // FLANG_RT_RUNTIME_TYPE_CODE_H_
diff --git a/flang-rt/include/flang-rt/type-info.h b/flang-rt/include/flang-rt/runtime/type-info.h
similarity index 98%
rename from flang-rt/include/flang-rt/type-info.h
rename to flang-rt/include/flang-rt/runtime/type-info.h
index dc5cd98d7523f23..9891fcecdcb25df 100644
--- a/flang-rt/include/flang-rt/type-info.h
+++ b/flang-rt/include/flang-rt/runtime/type-info.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/type-info.h ----------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/type-info.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.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_TYPE_INFO_H_
-#define FLANG_RT_TYPE_INFO_H_
+#ifndef FLANG_RT_RUNTIME_TYPE_INFO_H_
+#define FLANG_RT_RUNTIME_TYPE_INFO_H_
 
 // A C++ perspective of the derived type description schemata in
 // flang/module/__fortran_type_info.f90.
@@ -321,4 +321,4 @@ class DerivedType {
 };
 
 } // namespace Fortran::runtime::typeInfo
-#endif // FLANG_RT_TYPE_INFO_H_
+#endif // FLANG_RT_RUNTIME_TYPE_INFO_H_
diff --git a/flang-rt/include/flang-rt/utf.h b/flang-rt/include/flang-rt/runtime/utf.h
similarity index 94%
rename from flang-rt/include/flang-rt/utf.h
rename to flang-rt/include/flang-rt/runtime/utf.h
index 648937ca14f16a9..b5add823124fccc 100644
--- a/flang-rt/include/flang-rt/utf.h
+++ b/flang-rt/include/flang-rt/runtime/utf.h
@@ -1,4 +1,4 @@
-//===-- include/flang-rt/utf.h ----------------------------------*- C++ -*-===//
+//===-- include/flang-rt/runtime/utf.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.
@@ -38,8 +38,8 @@
 // standard maximum.  However, we support extended forms up to 32 bits so that
 // CHARACTER(KIND=4) can be abused to hold arbitrary 32-bit data.
 
-#ifndef FLANG_RT_UTF_H_
-#define FLANG_RT_UTF_H_
+#ifndef FLANG_RT_RUNTIME_UTF_H_
+#define FLANG_RT_RUNTIME_UTF_H_
 
 #include "flang/Common/optional.h"
 #include <cstddef>
@@ -70,4 +70,4 @@ RT_API_ATTRS Fortran::common::optional<char32_t> DecodeUTF8(const char *);
 RT_API_ATTRS std::size_t EncodeUTF8(char *, char32_t);
 
 } // namespace Fortran::runtime
-#endif // FLANG_RT_UTF_H_
+#endif // FLANG_RT_RUNTIME_UTF_H_
diff --git a/flang-rt/lib/CMakeLists.txt b/flang-rt/lib/CMakeLists.txt
index 8e2e0fbbbc24fd9..aee51dcc9fa248e 100644
--- a/flang-rt/lib/CMakeLists.txt
+++ b/flang-rt/lib/CMakeLists.txt
@@ -6,11 +6,10 @@
 #
 #===------------------------------------------------------------------------===#
 
-add_subdirectory(FortranFloat128Math)
-add_subdirectory(flang_rt)
-
-if (FLANG_RT_ENABLE_CUF)
-  add_subdirectory(flang_rt/CUDA)
+add_subdirectory(quadmath)
+add_subdirectory(runtime)
+if (FLANG_RT_INCLUDE_CUF)
+  add_subdirectory(cuda)
 endif()
 
 if (FLANG_RT_INCLUDE_TESTS)
diff --git a/flang-rt/lib/flang_rt/CUDA/CMakeLists.txt b/flang-rt/lib/cuda/CMakeLists.txt
similarity index 68%
rename from flang-rt/lib/flang_rt/CUDA/CMakeLists.txt
rename to flang-rt/lib/cuda/CMakeLists.txt
index 428aa3059b86988..d5ca354c1029fd6 100644
--- a/flang-rt/lib/flang_rt/CUDA/CMakeLists.txt
+++ b/flang-rt/lib/cuda/CMakeLists.txt
@@ -1,4 +1,4 @@
-#===-- lib/flang_rt/CUDA/CMakeLists.txt ------------------------------------===#
+#===-- lib/cuda/CMakeLists.txt ---------------------------------------------===#
 #
 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 # See https://llvm.org/LICENSE.txt for license information.
@@ -7,7 +7,7 @@
 #===------------------------------------------------------------------------===#
 
 
-add_flangrt_library(CufRuntime STATIC
+add_flangrt_library(flang_rt.cuda STATIC
   allocatable.cpp
   allocator.cpp
   descriptor.cpp
@@ -17,18 +17,18 @@ add_flangrt_library(CufRuntime STATIC
   memory.cpp
   registration.cpp
 
-  # libCufRuntime depends on a certain version of CUDA. To be able to have
+  # libflang_rt.runtime depends on a certain version of CUDA. To be able to have
   # multiple build of this library with different CUDA version, the version is
   # added to the library name.
   TARGET_PROPERTIES
-    OUTPUT_NAME "CufRuntime_cuda_${CUDAToolkit_VERSION_MAJOR}"
+    OUTPUT_NAME "flang_rt.cuda_${CUDAToolkit_VERSION_MAJOR}"
 
   INCLUDE_DIRECTORIES
     PRIVATE ${CUDAToolkit_INCLUDE_DIRS}
 )
 
-target_link_libraries(CufRuntime
+target_link_libraries(flang_rt.cuda
   PUBLIC
-  flang_rt
+  flang_rt.runtime
   CUDA::cudart_static
 )
diff --git a/flang-rt/lib/flang_rt/CUDA/allocatable.cpp b/flang-rt/lib/cuda/allocatable.cpp
similarity index 94%
rename from flang-rt/lib/flang_rt/CUDA/allocatable.cpp
rename to flang-rt/lib/cuda/allocatable.cpp
index 2281e2148d4aef7..2f549b604fe585b 100644
--- a/flang-rt/lib/flang_rt/CUDA/allocatable.cpp
+++ b/flang-rt/lib/cuda/allocatable.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/CUDA/allocatable.cpp -----------------------*- C++ -*-===//
+//===-- lib/cuda/allocatable.cpp --------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,10 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/allocatable.h"
-#include "flang-rt/assign-impl.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/stat.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/assign-impl.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 #include "flang/Runtime/CUDA/descriptor.h"
 #include "flang/Runtime/CUDA/memmove-function.h"
diff --git a/flang-rt/lib/flang_rt/CUDA/allocator.cpp b/flang-rt/lib/cuda/allocator.cpp
similarity index 86%
rename from flang-rt/lib/flang_rt/CUDA/allocator.cpp
rename to flang-rt/lib/cuda/allocator.cpp
index dc1ce5920e22213..511543f6a48635a 100644
--- a/flang-rt/lib/flang_rt/CUDA/allocator.cpp
+++ b/flang-rt/lib/cuda/allocator.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/CUDA/allocator.cpp -------------------------*- C++ -*-===//
+//===-- lib/cuda/allocator.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,12 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/CUDA/allocator.h"
-#include "flang-rt/allocator-registry.h"
-#include "flang-rt/derived.h"
-#include "flang-rt/stat.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/cuda/allocator.h"
+#include "flang-rt/runtime/allocator-registry.h"
+#include "flang-rt/runtime/derived.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/type-info.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
 #include "flang/Runtime/CUDA/common.h"
 #include "flang/Support/Fortran.h"
diff --git a/flang-rt/lib/flang_rt/CUDA/descriptor.cpp b/flang-rt/lib/cuda/descriptor.cpp
similarity index 90%
rename from flang-rt/lib/flang_rt/CUDA/descriptor.cpp
rename to flang-rt/lib/cuda/descriptor.cpp
index 9ec79ccea22c336..60e096acfde3220 100644
--- a/flang-rt/lib/flang_rt/CUDA/descriptor.cpp
+++ b/flang-rt/lib/cuda/descriptor.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/CUDA/descriptor.cpp ------------------------*- C++ -*-===//
+//===-- lib/cuda/descriptor.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/descriptor.h"
-#include "flang-rt/CUDA/allocator.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/cuda/allocator.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 
 #include "cuda_runtime.h"
diff --git a/flang-rt/lib/flang_rt/CUDA/init.cpp b/flang-rt/lib/cuda/init.cpp
similarity index 83%
rename from flang-rt/lib/flang_rt/CUDA/init.cpp
rename to flang-rt/lib/cuda/init.cpp
index f2d47d258519ecc..d79bffc32424d5b 100644
--- a/flang-rt/lib/flang_rt/CUDA/init.cpp
+++ b/flang-rt/lib/cuda/init.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/CUDA/init.cpp ------------------------------*- C++ -*-===//
+//===-- lib/cuda/init.cpp ---------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,8 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/init.h"
-#include "flang-rt/environment.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/environment.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 
 #include "cuda_runtime.h"
diff --git a/flang-rt/lib/flang_rt/CUDA/kernel.cpp b/flang-rt/lib/cuda/kernel.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/CUDA/kernel.cpp
rename to flang-rt/lib/cuda/kernel.cpp
index 7f9786319d574f7..75eb639817b9a4b 100644
--- a/flang-rt/lib/flang_rt/CUDA/kernel.cpp
+++ b/flang-rt/lib/cuda/kernel.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/CUDA/kernel.cpp ----------------------------*- C++ -*-===//
+//===-- lib/cuda/kernel.cpp -------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/kernel.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 
 #include "cuda_runtime.h"
diff --git a/flang-rt/lib/flang_rt/CUDA/memmove-function.cpp b/flang-rt/lib/cuda/memmove-function.cpp
similarity index 91%
rename from flang-rt/lib/flang_rt/CUDA/memmove-function.cpp
rename to flang-rt/lib/cuda/memmove-function.cpp
index ca04e20379d038b..a7eb0cf1a3e7adf 100644
--- a/flang-rt/lib/flang_rt/CUDA/memmove-function.cpp
+++ b/flang-rt/lib/cuda/memmove-function.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/CUDA/memmove-function.cpp ------------------*- C++ -*-===//
+//===-- lib/cuda/memmove-function.cpp ---------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/memmove-function.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 
 #include "cuda_runtime.h"
diff --git a/flang-rt/lib/flang_rt/CUDA/memory.cpp b/flang-rt/lib/cuda/memory.cpp
similarity index 97%
rename from flang-rt/lib/flang_rt/CUDA/memory.cpp
rename to flang-rt/lib/cuda/memory.cpp
index 3d4a74f5f9e52e4..7ead0dd35e1d9ff 100644
--- a/flang-rt/lib/flang_rt/CUDA/memory.cpp
+++ b/flang-rt/lib/cuda/memory.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/CUDA/memory.cpp ----------------------------*- C++ -*-===//
+//===-- lib/cuda/memory.cpp -------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,8 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/memory.h"
-#include "flang-rt/assign-impl.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/assign-impl.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 #include "flang/Runtime/CUDA/descriptor.h"
 #include "flang/Runtime/CUDA/memmove-function.h"
diff --git a/flang-rt/lib/flang_rt/CUDA/pointer.cpp b/flang-rt/lib/cuda/pointer.cpp
similarity index 92%
rename from flang-rt/lib/flang_rt/CUDA/pointer.cpp
rename to flang-rt/lib/cuda/pointer.cpp
index b6e8033933834a6..d94e3958b91536d 100644
--- a/flang-rt/lib/flang_rt/CUDA/pointer.cpp
+++ b/flang-rt/lib/cuda/pointer.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/CUDA/pointer.cpp ---------------------------*- C++ -*-===//
+//===-- lib/cuda/pointer.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,10 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/pointer.h"
-#include "flang-rt/assign-impl.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/stat.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/assign-impl.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/descriptor.h"
 #include "flang/Runtime/CUDA/memmove-function.h"
 #include "flang/Runtime/pointer.h"
diff --git a/flang-rt/lib/flang_rt/CUDA/registration.cpp b/flang-rt/lib/cuda/registration.cpp
similarity index 93%
rename from flang-rt/lib/flang_rt/CUDA/registration.cpp
rename to flang-rt/lib/cuda/registration.cpp
index f8a7b197bed3b45..60b0e491b6ffd3c 100644
--- a/flang-rt/lib/flang_rt/CUDA/registration.cpp
+++ b/flang-rt/lib/cuda/registration.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/CUDA/registration.cpp ----------------------*- C++ -*-===//
+//===-- lib/cuda/registration.cpp -------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/registration.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 
 #include "cuda_runtime.h"
diff --git a/flang-rt/lib/FortranFloat128Math/CMakeLists.txt b/flang-rt/lib/quadmath/CMakeLists.txt
similarity index 94%
rename from flang-rt/lib/FortranFloat128Math/CMakeLists.txt
rename to flang-rt/lib/quadmath/CMakeLists.txt
index c0b2ee32248f877..2eccf23e3d16677 100644
--- a/flang-rt/lib/FortranFloat128Math/CMakeLists.txt
+++ b/flang-rt/lib/quadmath/CMakeLists.txt
@@ -1,4 +1,4 @@
-#===-- lib/FortranFloat128Math/CMakeLists.txt ------------------------------===#
+#===-- lib/quadmath/CMakeLists.txt -----------------------------------------===#
 #
 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 # See https://llvm.org/LICENSE.txt for license information.
@@ -96,13 +96,13 @@ if (FLANG_RUNTIME_F128_MATH_LIB)
 
   if (WIN32)
     # Do not create a FortranFloat128Math library under Windows, the Flang
-    # driver never links it. Instead, add the sources to flang_rt itself.
+    # driver never links it. Instead, add the sources to flang_rt.runtime.
     target_sources(FortranFloat128MathILib INTERFACE ${sources})
     target_compile_definitions(FortranFloat128MathILib INTERFACE HAS_QUADMATHLIB)
   else ()
-    add_flangrt_library(FortranFloat128Math STATIC INSTALL_WITH_TOOLCHAIN
+    add_flangrt_library(flang_rt.quadmath STATIC INSTALL_WITH_TOOLCHAIN
       ${sources})
-    target_include_directories(FortranFloat128Math PRIVATE
+    target_include_directories(flang_rt.quadmath PRIVATE
         "${FLANG_RT_SOURCE_DIR}/lib/flang_rt"
       )
   endif ()
diff --git a/flang-rt/lib/FortranFloat128Math/acos.cpp b/flang-rt/lib/quadmath/acos.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/acos.cpp
rename to flang-rt/lib/quadmath/acos.cpp
index 7f2065475275438..d094121f0f678b9 100644
--- a/flang-rt/lib/FortranFloat128Math/acos.cpp
+++ b/flang-rt/lib/quadmath/acos.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/acos.cpp ------------------------*- C++ -*-===//
+//===-- lib/quadmath/acos.cpp -----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/acosh.cpp b/flang-rt/lib/quadmath/acosh.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/acosh.cpp
rename to flang-rt/lib/quadmath/acosh.cpp
index 73b64230f08c71c..968aa489d15a221 100644
--- a/flang-rt/lib/FortranFloat128Math/acosh.cpp
+++ b/flang-rt/lib/quadmath/acosh.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/acosh.cpp -----------------------*- C++ -*-===//
+//===-- lib/quadmath/acosh.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/asin.cpp b/flang-rt/lib/quadmath/asin.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/asin.cpp
rename to flang-rt/lib/quadmath/asin.cpp
index b51ecb6b674d3af..c3345cd3d748a25 100644
--- a/flang-rt/lib/FortranFloat128Math/asin.cpp
+++ b/flang-rt/lib/quadmath/asin.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/asin.cpp ------------------------*- C++ -*-===//
+//===-- lib/quadmath/asin.cpp -----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/asinh.cpp b/flang-rt/lib/quadmath/asinh.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/asinh.cpp
rename to flang-rt/lib/quadmath/asinh.cpp
index 2fc181d075aec9b..1023b678b613167 100644
--- a/flang-rt/lib/FortranFloat128Math/asinh.cpp
+++ b/flang-rt/lib/quadmath/asinh.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/asinh.cpp -----------------------*- C++ -*-===//
+//===-- lib/quadmath/asinh.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/atan.cpp b/flang-rt/lib/quadmath/atan.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/atan.cpp
rename to flang-rt/lib/quadmath/atan.cpp
index ff1ec39bfdebd75..6379df3275c03d8 100644
--- a/flang-rt/lib/FortranFloat128Math/atan.cpp
+++ b/flang-rt/lib/quadmath/atan.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/atan.cpp ------------------------*- C++ -*-===//
+//===-- lib/quadmath/atan.cpp -----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/atan2.cpp b/flang-rt/lib/quadmath/atan2.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/atan2.cpp
rename to flang-rt/lib/quadmath/atan2.cpp
index 4e809cb547a1b82..7527b224cb3a52e 100644
--- a/flang-rt/lib/FortranFloat128Math/atan2.cpp
+++ b/flang-rt/lib/quadmath/atan2.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/atan2.cpp -----------------------*- C++ -*-===//
+//===-- lib/quadmath/atan2.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/atanh.cpp b/flang-rt/lib/quadmath/atanh.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/atanh.cpp
rename to flang-rt/lib/quadmath/atanh.cpp
index d4e3e7426b16d91..c7455fcb7ca6779 100644
--- a/flang-rt/lib/FortranFloat128Math/atanh.cpp
+++ b/flang-rt/lib/quadmath/atanh.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/atanh.cpp -----------------------*- C++ -*-===//
+//===-- lib/quadmath/atanh.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/ceil.cpp b/flang-rt/lib/quadmath/ceil.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/ceil.cpp
rename to flang-rt/lib/quadmath/ceil.cpp
index f439f22c0565aad..03a98bedfdc035d 100644
--- a/flang-rt/lib/FortranFloat128Math/ceil.cpp
+++ b/flang-rt/lib/quadmath/ceil.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/ceil.cpp ------------------------*- C++ -*-===//
+//===-- lib/quadmath/ceil.cpp -----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/complex-math.c b/flang-rt/lib/quadmath/complex-math.c
similarity index 100%
rename from flang-rt/lib/FortranFloat128Math/complex-math.c
rename to flang-rt/lib/quadmath/complex-math.c
diff --git a/flang-rt/lib/FortranFloat128Math/complex-math.h b/flang-rt/lib/quadmath/complex-math.h
similarity index 88%
rename from flang-rt/lib/FortranFloat128Math/complex-math.h
rename to flang-rt/lib/quadmath/complex-math.h
index d22598d52e83291..d0c53935b30c39c 100644
--- a/flang-rt/lib/FortranFloat128Math/complex-math.h
+++ b/flang-rt/lib/quadmath/complex-math.h
@@ -1,4 +1,4 @@
-/*===-- lib/FortranFloat128Math/complex-math.h ----------------------*- C -*-===
+/*===-- lib/quadmath/complex-math.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.
@@ -6,8 +6,8 @@
  *
  *===----------------------------------------------------------------------===*/
 
-#ifndef FLANG_RT_FORTRANFLOAT128MATH_COMPLEX_MATH_H_
-#define FLANG_RT_FORTRANFLOAT128MATH_COMPLEX_MATH_H_
+#ifndef FLANG_RT_QUADMATH_COMPLEX_MATH_H_
+#define FLANG_RT_QUADMATH_COMPLEX_MATH_H_
 
 #include "flang/Common/float128.h"
 #include "flang/Runtime/entry-names.h"
@@ -59,4 +59,4 @@
 #error "Float128Math build with glibc>=2.26 is unsupported yet"
 #endif
 
-#endif // FLANG_RT_FORTRANFLOAT128MATH_COMPLEX_MATH_H_
+#endif // FLANG_RT_QUADMATH_COMPLEX_MATH_H_
diff --git a/flang-rt/lib/FortranFloat128Math/cos.cpp b/flang-rt/lib/quadmath/cos.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/cos.cpp
rename to flang-rt/lib/quadmath/cos.cpp
index aed337c325836e9..1116080c53d2a8b 100644
--- a/flang-rt/lib/FortranFloat128Math/cos.cpp
+++ b/flang-rt/lib/quadmath/cos.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/cos.cpp -------------------------*- C++ -*-===//
+//===-- lib/quadmath/cos.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/cosh.cpp b/flang-rt/lib/quadmath/cosh.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/cosh.cpp
rename to flang-rt/lib/quadmath/cosh.cpp
index bddbff766b5758a..dd5978e5e5f0881 100644
--- a/flang-rt/lib/FortranFloat128Math/cosh.cpp
+++ b/flang-rt/lib/quadmath/cosh.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/cosh.cpp ------------------------*- C++ -*-===//
+//===-- lib/quadmath/cosh.cpp -----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/erf.cpp b/flang-rt/lib/quadmath/erf.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/erf.cpp
rename to flang-rt/lib/quadmath/erf.cpp
index a56430040248dcf..0021b7900f6a108 100644
--- a/flang-rt/lib/FortranFloat128Math/erf.cpp
+++ b/flang-rt/lib/quadmath/erf.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/erf.cpp -------------------------*- C++ -*-===//
+//===-- lib/quadmath/erf.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/erfc.cpp b/flang-rt/lib/quadmath/erfc.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/erfc.cpp
rename to flang-rt/lib/quadmath/erfc.cpp
index 255a2a7b4edac09..5b80fb475b3fca1 100644
--- a/flang-rt/lib/FortranFloat128Math/erfc.cpp
+++ b/flang-rt/lib/quadmath/erfc.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/erfc.cpp ------------------------*- C++ -*-===//
+//===-- lib/quadmath/erfc.cpp -----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/exp.cpp b/flang-rt/lib/quadmath/exp.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/exp.cpp
rename to flang-rt/lib/quadmath/exp.cpp
index 9bc87d959cf6917..94e444c3b00c7b9 100644
--- a/flang-rt/lib/FortranFloat128Math/exp.cpp
+++ b/flang-rt/lib/quadmath/exp.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/exp.cpp -------------------------*- C++ -*-===//
+//===-- lib/quadmath/exp.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/exponent.cpp b/flang-rt/lib/quadmath/exponent.cpp
similarity index 92%
rename from flang-rt/lib/FortranFloat128Math/exponent.cpp
rename to flang-rt/lib/quadmath/exponent.cpp
index bf27a26a734276f..0d2fa6478cca8e2 100644
--- a/flang-rt/lib/FortranFloat128Math/exponent.cpp
+++ b/flang-rt/lib/quadmath/exponent.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/exponent.cpp --------------------*- C++ -*-===//
+//===-- lib/quadmath/exponent.cpp -------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/floor.cpp b/flang-rt/lib/quadmath/floor.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/floor.cpp
rename to flang-rt/lib/quadmath/floor.cpp
index abda5fadfd29650..e5dfb33db82cec8 100644
--- a/flang-rt/lib/FortranFloat128Math/floor.cpp
+++ b/flang-rt/lib/quadmath/floor.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/floor.cpp -----------------------*- C++ -*-===//
+//===-- lib/quadmath/floor.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/fma.cpp b/flang-rt/lib/quadmath/fma.cpp
similarity index 91%
rename from flang-rt/lib/FortranFloat128Math/fma.cpp
rename to flang-rt/lib/quadmath/fma.cpp
index 2e7aabedf1e8c5e..910303af3233923 100644
--- a/flang-rt/lib/FortranFloat128Math/fma.cpp
+++ b/flang-rt/lib/quadmath/fma.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/fma.cpp -------------------------*- C++ -*-===//
+//===-- lib/quadmath/fma.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/fraction.cpp b/flang-rt/lib/quadmath/fraction.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/fraction.cpp
rename to flang-rt/lib/quadmath/fraction.cpp
index bf1da6f63e5ce78..a9927666a7b0084 100644
--- a/flang-rt/lib/FortranFloat128Math/fraction.cpp
+++ b/flang-rt/lib/quadmath/fraction.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/fraction.cpp --------------------*- C++ -*-===//
+//===-- lib/quadmath/fraction.cpp -------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/hypot.cpp b/flang-rt/lib/quadmath/hypot.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/hypot.cpp
rename to flang-rt/lib/quadmath/hypot.cpp
index 910bda798c1a066..3090d0b2aff74c9 100644
--- a/flang-rt/lib/FortranFloat128Math/hypot.cpp
+++ b/flang-rt/lib/quadmath/hypot.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/hypot.cpp -----------------------*- C++ -*-===//
+//===-- lib/quadmath/hypot.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/j0.cpp b/flang-rt/lib/quadmath/j0.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/j0.cpp
rename to flang-rt/lib/quadmath/j0.cpp
index d2fac98c3ec694c..06df1c2aca452e8 100644
--- a/flang-rt/lib/FortranFloat128Math/j0.cpp
+++ b/flang-rt/lib/quadmath/j0.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/j0.cpp --------------------------*- C++ -*-===//
+//===-- lib/quadmath/j0.cpp -------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/j1.cpp b/flang-rt/lib/quadmath/j1.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/j1.cpp
rename to flang-rt/lib/quadmath/j1.cpp
index 60401030124c88e..d8a1f123b95e6a0 100644
--- a/flang-rt/lib/FortranFloat128Math/j1.cpp
+++ b/flang-rt/lib/quadmath/j1.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/j1.cpp --------------------------*- C++ -*-===//
+//===-- lib/quadmath/j1.cpp -------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/jn.cpp b/flang-rt/lib/quadmath/jn.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/jn.cpp
rename to flang-rt/lib/quadmath/jn.cpp
index 71e11335d083079..a53e305bb874680 100644
--- a/flang-rt/lib/FortranFloat128Math/jn.cpp
+++ b/flang-rt/lib/quadmath/jn.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/jn.cpp --------------------------*- C++ -*-===//
+//===-- lib/quadmath/jn.cpp -------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/lgamma.cpp b/flang-rt/lib/quadmath/lgamma.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/lgamma.cpp
rename to flang-rt/lib/quadmath/lgamma.cpp
index 1ae4cc4d95d61c0..b96dff1d0d72a7a 100644
--- a/flang-rt/lib/FortranFloat128Math/lgamma.cpp
+++ b/flang-rt/lib/quadmath/lgamma.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/lgamma.cpp ----------------------*- C++ -*-===//
+//===-- lib/quadmath/lgamma.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/llround.cpp b/flang-rt/lib/quadmath/llround.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/llround.cpp
rename to flang-rt/lib/quadmath/llround.cpp
index 5bd4cc26dd49789..8f2913d390431bd 100644
--- a/flang-rt/lib/FortranFloat128Math/llround.cpp
+++ b/flang-rt/lib/quadmath/llround.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/llround.cpp ---------------------*- C++ -*-===//
+//===-- lib/quadmath/llround.cpp --------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/log.cpp b/flang-rt/lib/quadmath/log.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/log.cpp
rename to flang-rt/lib/quadmath/log.cpp
index 414e41ae0ce2910..0c489c922a3fcba 100644
--- a/flang-rt/lib/FortranFloat128Math/log.cpp
+++ b/flang-rt/lib/quadmath/log.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/log.cpp -------------------------*- C++ -*-===//
+//===-- lib/quadmath/log.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/log10.cpp b/flang-rt/lib/quadmath/log10.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/log10.cpp
rename to flang-rt/lib/quadmath/log10.cpp
index 55db1b1a2dc1ff3..a2f222e15a147e1 100644
--- a/flang-rt/lib/FortranFloat128Math/log10.cpp
+++ b/flang-rt/lib/quadmath/log10.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/log10.cpp -----------------------*- C++ -*-===//
+//===-- lib/quadmath/log10.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/lround.cpp b/flang-rt/lib/quadmath/lround.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/lround.cpp
rename to flang-rt/lib/quadmath/lround.cpp
index a0bea492436817d..539ee107a3881fc 100644
--- a/flang-rt/lib/FortranFloat128Math/lround.cpp
+++ b/flang-rt/lib/quadmath/lround.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/lround.cpp ----------------------*- C++ -*-===//
+//===-- lib/quadmath/lround.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/math-entries.h b/flang-rt/lib/quadmath/math-entries.h
similarity index 96%
rename from flang-rt/lib/FortranFloat128Math/math-entries.h
rename to flang-rt/lib/quadmath/math-entries.h
index 72b12a1645c7d88..6e47f32cc8a4333 100644
--- a/flang-rt/lib/FortranFloat128Math/math-entries.h
+++ b/flang-rt/lib/quadmath/math-entries.h
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/math-entries.h ------------------*- C++ -*-===//
+//===-- lib/quadmath/math-entries.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.
@@ -6,11 +6,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_FORTRANFLOAT128MATH_MATH_ENTRIES_H_
-#define FLANG_RT_FORTRANFLOAT128MATH_MATH_ENTRIES_H_
+#ifndef FLANG_RT_QUADMATH_MATH_ENTRIES_H_
+#define FLANG_RT_QUADMATH_MATH_ENTRIES_H_
 
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Common/float128.h"
 #include "flang/Runtime/entry-names.h"
 #include <cfloat>
@@ -232,4 +232,4 @@ DEFINE_SIMPLE_ALIAS(Yn, ynl)
 
 } // namespace Fortran::runtime
 
-#endif // FLANG_RT_FORTRANFLOAT128MATH_MATH_ENTRIES_H_
+#endif // FLANG_RT_QUADMATH_MATH_ENTRIES_H_
diff --git a/flang-rt/lib/FortranFloat128Math/mod-real.cpp b/flang-rt/lib/quadmath/mod-real.cpp
similarity index 91%
rename from flang-rt/lib/FortranFloat128Math/mod-real.cpp
rename to flang-rt/lib/quadmath/mod-real.cpp
index beb412f719c5207..0230964e3ddc2e3 100644
--- a/flang-rt/lib/FortranFloat128Math/mod-real.cpp
+++ b/flang-rt/lib/quadmath/mod-real.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/mod-real.cpp --------------------*- C++ -*-===//
+//===-- lib/quadmath/mod-real.cpp -------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/modulo-real.cpp b/flang-rt/lib/quadmath/modulo-real.cpp
similarity index 91%
rename from flang-rt/lib/FortranFloat128Math/modulo-real.cpp
rename to flang-rt/lib/quadmath/modulo-real.cpp
index c8160b1d2745130..0f28747b86985e8 100644
--- a/flang-rt/lib/FortranFloat128Math/modulo-real.cpp
+++ b/flang-rt/lib/quadmath/modulo-real.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/modulo-real.cpp -----------------*- C++ -*-===//
+//===-- lib/quadmath/modulo-real.cpp ----------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/nearbyint.cpp b/flang-rt/lib/quadmath/nearbyint.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/nearbyint.cpp
rename to flang-rt/lib/quadmath/nearbyint.cpp
index d572641e2b0d463..3811fc53d1d8286 100644
--- a/flang-rt/lib/FortranFloat128Math/nearbyint.cpp
+++ b/flang-rt/lib/quadmath/nearbyint.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/nearbyint.cpp -------------------*- C++ -*-===//
+//===-- lib/quadmath/nearbyint.cpp ------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/nearest.cpp b/flang-rt/lib/quadmath/nearest.cpp
similarity index 91%
rename from flang-rt/lib/FortranFloat128Math/nearest.cpp
rename to flang-rt/lib/quadmath/nearest.cpp
index 3e32de7ea9d0aed..8c1969a7b596cbf 100644
--- a/flang-rt/lib/FortranFloat128Math/nearest.cpp
+++ b/flang-rt/lib/quadmath/nearest.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/nearest.cpp ---------------------*- C++ -*-===//
+//===-- lib/quadmath/nearest.cpp --------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/norm2.cpp b/flang-rt/lib/quadmath/norm2.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/norm2.cpp
rename to flang-rt/lib/quadmath/norm2.cpp
index 8f6b2c87eec3263..e98f4007737d141 100644
--- a/flang-rt/lib/FortranFloat128Math/norm2.cpp
+++ b/flang-rt/lib/quadmath/norm2.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/norm2.cpp -----------------------*- C++ -*-===//
+//===-- lib/quadmath/norm2.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -8,7 +8,7 @@
 
 #include "math-entries.h"
 #include "numeric-template-specs.h"
-#include "flang-rt/reduction-templates.h"
+#include "flang-rt/runtime/reduction-templates.h"
 
 namespace Fortran::runtime {
 extern "C" {
diff --git a/flang-rt/lib/FortranFloat128Math/numeric-template-specs.h b/flang-rt/lib/quadmath/numeric-template-specs.h
similarity index 82%
rename from flang-rt/lib/FortranFloat128Math/numeric-template-specs.h
rename to flang-rt/lib/quadmath/numeric-template-specs.h
index e618902b03adc21..e215ad70eca14af 100644
--- a/flang-rt/lib/FortranFloat128Math/numeric-template-specs.h
+++ b/flang-rt/lib/quadmath/numeric-template-specs.h
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/numeric-template-specs.h --------*- C++ -*-===//
+//===-- lib/quadmath/numeric-template-specs.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.
@@ -6,11 +6,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_FORTRANFLOAT128MATH_NUMERIC_TEMPLATE_SPECS_H_
-#define FLANG_RT_FORTRANFLOAT128MATH_NUMERIC_TEMPLATE_SPECS_H_
+#ifndef FLANG_RT_QUADMATH_NUMERIC_TEMPLATE_SPECS_H_
+#define FLANG_RT_QUADMATH_NUMERIC_TEMPLATE_SPECS_H_
 
 #include "math-entries.h"
-#include "flang-rt/numeric-templates.h"
+#include "flang-rt/runtime/numeric-templates.h"
 
 namespace Fortran::runtime {
 using F128Type = CppTypeFor<TypeCategory::Real, 16>;
@@ -52,4 +52,4 @@ template <> struct SQRTTy<F128Type> {
 };
 
 } // namespace Fortran::runtime
-#endif // FLANG_RT_FORTRANFLOAT128MATH_NUMERIC_TEMPLATE_SPECS_H_
+#endif // FLANG_RT_QUADMATH_NUMERIC_TEMPLATE_SPECS_H_
diff --git a/flang-rt/lib/FortranFloat128Math/pow.cpp b/flang-rt/lib/quadmath/pow.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/pow.cpp
rename to flang-rt/lib/quadmath/pow.cpp
index bbdda217c5108c5..29c053625465811 100644
--- a/flang-rt/lib/FortranFloat128Math/pow.cpp
+++ b/flang-rt/lib/quadmath/pow.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/pow.cpp -------------------------*- C++ -*-===//
+//===-- lib/quadmath/pow.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/random.cpp b/flang-rt/lib/quadmath/random.cpp
similarity index 85%
rename from flang-rt/lib/FortranFloat128Math/random.cpp
rename to flang-rt/lib/quadmath/random.cpp
index 701f9429fcb6cb6..a6d22733ebce42e 100644
--- a/flang-rt/lib/FortranFloat128Math/random.cpp
+++ b/flang-rt/lib/quadmath/random.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/random.cpp ----------------------*- C++ -*-===//
+//===-- lib/quadmath/random.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -8,7 +8,7 @@
 
 #include "math-entries.h"
 #include "numeric-template-specs.h"
-#include "flang-rt/random-templates.h"
+#include "flang-rt/runtime/random-templates.h"
 
 using namespace Fortran::runtime::random;
 extern "C" {
diff --git a/flang-rt/lib/FortranFloat128Math/remainder.cpp b/flang-rt/lib/quadmath/remainder.cpp
similarity index 91%
rename from flang-rt/lib/FortranFloat128Math/remainder.cpp
rename to flang-rt/lib/quadmath/remainder.cpp
index ca4165a357eda99..4b68cdd6ac9dee2 100644
--- a/flang-rt/lib/FortranFloat128Math/remainder.cpp
+++ b/flang-rt/lib/quadmath/remainder.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/remainder.cpp -------------------*- C++ -*-===//
+//===-- lib/quadmath/remainder.cpp ------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/round.cpp b/flang-rt/lib/quadmath/round.cpp
similarity index 91%
rename from flang-rt/lib/FortranFloat128Math/round.cpp
rename to flang-rt/lib/quadmath/round.cpp
index 348f7ff8a67712a..844338f5e6413d1 100644
--- a/flang-rt/lib/FortranFloat128Math/round.cpp
+++ b/flang-rt/lib/quadmath/round.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/round.cpp -----------------------*- C++ -*-===//
+//===-- lib/quadmath/round.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/rrspacing.cpp b/flang-rt/lib/quadmath/rrspacing.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/rrspacing.cpp
rename to flang-rt/lib/quadmath/rrspacing.cpp
index 0a87a5c572652cb..e8613f4d7d7e27f 100644
--- a/flang-rt/lib/FortranFloat128Math/rrspacing.cpp
+++ b/flang-rt/lib/quadmath/rrspacing.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/rrspacing.cpp -------------------*- C++ -*-===//
+//===-- lib/quadmath/rrspacing.cpp ------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/scale.cpp b/flang-rt/lib/quadmath/scale.cpp
similarity index 92%
rename from flang-rt/lib/FortranFloat128Math/scale.cpp
rename to flang-rt/lib/quadmath/scale.cpp
index a316a2a0cd7062e..3d919f85a448775 100644
--- a/flang-rt/lib/FortranFloat128Math/scale.cpp
+++ b/flang-rt/lib/quadmath/scale.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/scale.cpp -----------------------*- C++ -*-===//
+//===-- lib/quadmath/scale.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/set-exponent.cpp b/flang-rt/lib/quadmath/set-exponent.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/set-exponent.cpp
rename to flang-rt/lib/quadmath/set-exponent.cpp
index 335c68f632f1852..d6b582e7c4f38ba 100644
--- a/flang-rt/lib/FortranFloat128Math/set-exponent.cpp
+++ b/flang-rt/lib/quadmath/set-exponent.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/set-exponent.cpp ----------------*- C++ -*-===//
+//===-- lib/quadmath/set-exponent.cpp ---------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/sin.cpp b/flang-rt/lib/quadmath/sin.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/sin.cpp
rename to flang-rt/lib/quadmath/sin.cpp
index bd5f62808fc31e3..dcff2f9ce02ca56 100644
--- a/flang-rt/lib/FortranFloat128Math/sin.cpp
+++ b/flang-rt/lib/quadmath/sin.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/sin.cpp -------------------------*- C++ -*-===//
+//===-- lib/quadmath/sin.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/sinh.cpp b/flang-rt/lib/quadmath/sinh.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/sinh.cpp
rename to flang-rt/lib/quadmath/sinh.cpp
index 07f12d437ed56b7..3ab7280f705a6ba 100644
--- a/flang-rt/lib/FortranFloat128Math/sinh.cpp
+++ b/flang-rt/lib/quadmath/sinh.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/sinh.cpp ------------------------*- C++ -*-===//
+//===-- lib/quadmath/sinh.cpp -----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/spacing.cpp b/flang-rt/lib/quadmath/spacing.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/spacing.cpp
rename to flang-rt/lib/quadmath/spacing.cpp
index b10351ab95555a6..1d7ecdb4852d293 100644
--- a/flang-rt/lib/FortranFloat128Math/spacing.cpp
+++ b/flang-rt/lib/quadmath/spacing.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/spacing.cpp ---------------------*- C++ -*-===//
+//===-- lib/quadmath/spacing.cpp --------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/sqrt.cpp b/flang-rt/lib/quadmath/sqrt.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/sqrt.cpp
rename to flang-rt/lib/quadmath/sqrt.cpp
index b6fd6eabc267ba9..6e0d11a6697f0ea 100644
--- a/flang-rt/lib/FortranFloat128Math/sqrt.cpp
+++ b/flang-rt/lib/quadmath/sqrt.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/sqrt.cpp ------------------------*- C++ -*-===//
+//===-- lib/quadmath/sqrt.cpp -----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/tan.cpp b/flang-rt/lib/quadmath/tan.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/tan.cpp
rename to flang-rt/lib/quadmath/tan.cpp
index 437c0692191e860..6f09b93060228e9 100644
--- a/flang-rt/lib/FortranFloat128Math/tan.cpp
+++ b/flang-rt/lib/quadmath/tan.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/tan.cpp -------------------------*- C++ -*-===//
+//===-- lib/quadmath/tan.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/tanh.cpp b/flang-rt/lib/quadmath/tanh.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/tanh.cpp
rename to flang-rt/lib/quadmath/tanh.cpp
index 48544d4bb75f40a..214a18d5c3778c5 100644
--- a/flang-rt/lib/FortranFloat128Math/tanh.cpp
+++ b/flang-rt/lib/quadmath/tanh.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/tanh.cpp ------------------------*- C++ -*-===//
+//===-- lib/quadmath/tanh.cpp -----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/tgamma.cpp b/flang-rt/lib/quadmath/tgamma.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/tgamma.cpp
rename to flang-rt/lib/quadmath/tgamma.cpp
index 8d7963caa61c5ae..2b05a60dcaabb17 100644
--- a/flang-rt/lib/FortranFloat128Math/tgamma.cpp
+++ b/flang-rt/lib/quadmath/tgamma.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/tgamma.cpp ----------------------*- C++ -*-===//
+//===-- lib/quadmath/tgamma.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/trunc.cpp b/flang-rt/lib/quadmath/trunc.cpp
similarity index 91%
rename from flang-rt/lib/FortranFloat128Math/trunc.cpp
rename to flang-rt/lib/quadmath/trunc.cpp
index 4a3dddeb1326cd9..cd7c27b569fc3e1 100644
--- a/flang-rt/lib/FortranFloat128Math/trunc.cpp
+++ b/flang-rt/lib/quadmath/trunc.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/trunc.cpp -----------------------*- C++ -*-===//
+//===-- lib/quadmath/trunc.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/y0.cpp b/flang-rt/lib/quadmath/y0.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/y0.cpp
rename to flang-rt/lib/quadmath/y0.cpp
index c5b266b5cec03f5..9db04277660adec 100644
--- a/flang-rt/lib/FortranFloat128Math/y0.cpp
+++ b/flang-rt/lib/quadmath/y0.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/y0.cpp --------------------------*- C++ -*-===//
+//===-- lib/quadmath/y0.cpp -------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/y1.cpp b/flang-rt/lib/quadmath/y1.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/y1.cpp
rename to flang-rt/lib/quadmath/y1.cpp
index 8b492c69e374156..92e658195f3d909 100644
--- a/flang-rt/lib/FortranFloat128Math/y1.cpp
+++ b/flang-rt/lib/quadmath/y1.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/y1.cpp --------------------------*- C++ -*-===//
+//===-- lib/quadmath/y1.cpp -------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/FortranFloat128Math/yn.cpp b/flang-rt/lib/quadmath/yn.cpp
similarity index 90%
rename from flang-rt/lib/FortranFloat128Math/yn.cpp
rename to flang-rt/lib/quadmath/yn.cpp
index cb212b400f6e0da..20c0bc9d5218ecf 100644
--- a/flang-rt/lib/FortranFloat128Math/yn.cpp
+++ b/flang-rt/lib/quadmath/yn.cpp
@@ -1,4 +1,4 @@
-//===-- lib/FortranFloat128Math/yn.cpp --------------------------*- C++ -*-===//
+//===-- lib/quadmath/yn.cpp -------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/flang_rt/CMakeLists.txt b/flang-rt/lib/runtime/CMakeLists.txt
similarity index 83%
rename from flang-rt/lib/flang_rt/CMakeLists.txt
rename to flang-rt/lib/runtime/CMakeLists.txt
index 4db1a1cbb874fc2..1fcac510a507b6d 100644
--- a/flang-rt/lib/flang_rt/CMakeLists.txt
+++ b/flang-rt/lib/runtime/CMakeLists.txt
@@ -1,4 +1,4 @@
-#===-- lib/flang_rt/CMakeLists.txt -----------------------------------------===#
+#===-- lib/runtime/CMakeLists.txt ------------------------------------------===#
 #
 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 # See https://llvm.org/LICENSE.txt for license information.
@@ -128,26 +128,26 @@ endif ()
 set(sources ${supported_sources} ${host_sources} ${f128_sources})
 
 if (NOT WIN32)
-  add_flangrt_library(flang_rt STATIC
+  add_flangrt_library(flang_rt.runtime STATIC
     ${sources}
     INSTALL_WITH_TOOLCHAIN
     ADDITIONAL_HEADERS ${public_headers} ${private_headers}
   )
 
-  enable_cuda_compilation(flang_rt "${supported_sources}")
-  enable_omp_offload_compilation(flang_rt "${supported_sources}")
+  enable_cuda_compilation(flang_rt.runtime "${supported_sources}")
+  enable_omp_offload_compilation(flang_rt.runtime "${supported_sources}")
 
   # For unittests that depend on flang_rt. Should link to the static version
   # of the library.
-  add_library(flang_rt.static ALIAS flang_rt)
-  add_library(flang_rt.unittest ALIAS flang_rt)
+  add_library(flang_rt.runtime.static ALIAS flang_rt.runtime)
+  add_library(flang_rt.runtime.unittest ALIAS flang_rt.runtime)
 else()
   # Target for building all versions of the runtime
-  add_custom_target(flang_rt)
-  set_target_properties(flang_rt PROPERTIES FOLDER "Flang-RT/Meta")
+  add_custom_target(flang_rt.runtime)
+  set_target_properties(flang_rt.runtime PROPERTIES FOLDER "Flang-RT/Meta")
 
-  function (add_win_flangrt_library libtype suffix msvc_lib)
-    set(name "flang_rt.${suffix}")
+  function (add_win_flangrt_runtime libtype suffix msvc_lib)
+    set(name "flang_rt.runtime.${suffix}")
     add_flangrt_library(${name} ${libtype}
         ${sources}
         ${ARGN}
@@ -190,10 +190,10 @@ else()
   #    is CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS which would also export the internal
   #    C++ symbols and still requires global data symbols to be annotated
   #    manually.
-  add_win_flangrt_library(STATIC static      MultiThreaded         INSTALL_WITH_TOOLCHAIN)
-  add_win_flangrt_library(STATIC static_dbg  MultiThreadedDebug    INSTALL_WITH_TOOLCHAIN)
-  add_win_flangrt_library(STATIC dynamic     MultiThreadedDLL      INSTALL_WITH_TOOLCHAIN)
-  add_win_flangrt_library(STATIC dynamic_dbg MultiThreadedDebugDLL INSTALL_WITH_TOOLCHAIN)
+  add_win_flangrt_runtime(STATIC static      MultiThreaded         INSTALL_WITH_TOOLCHAIN)
+  add_win_flangrt_runtime(STATIC static_dbg  MultiThreadedDebug    INSTALL_WITH_TOOLCHAIN)
+  add_win_flangrt_runtime(STATIC dynamic     MultiThreadedDLL      INSTALL_WITH_TOOLCHAIN)
+  add_win_flangrt_runtime(STATIC dynamic_dbg MultiThreadedDebugDLL INSTALL_WITH_TOOLCHAIN)
 
   # Unittests link against LLVMSupport which is using CMake's default runtime
   # library selection, which is either MultiThreadedDLL or MultiThreadedDebugDLL
@@ -201,13 +201,13 @@ else()
   if (GENERATOR_IS_MULTI_CONFIG)
     # We cannot select an ALIAS library because it may be different
     # per configuration. Fallback to CMake's default.
-    add_win_flangrt_library(STATIC unittest "" EXCLUDE_FROM_ALL)
+    add_win_flangrt_runtime(STATIC unittest "" EXCLUDE_FROM_ALL)
   else ()
     string(TOLOWER ${CMAKE_BUILD_TYPE} build_type)
     if (build_type STREQUAL "debug")
-      add_library(flang_rt.unittest ALIAS flang_rt.dynamic_dbg)
+      add_library(flang_rt.runtime.unittest ALIAS flang_rt.runtime.dynamic_dbg)
     else ()
-      add_library(flang_rt.unittest ALIAS flang_rt.dynamic)
+      add_library(flang_rt.runtime.unittest ALIAS flang_rt.runtime.dynamic)
     endif ()
   endif ()
 endif()
diff --git a/flang-rt/lib/flang_rt/ISO_Fortran_binding.cpp b/flang-rt/lib/runtime/ISO_Fortran_binding.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/ISO_Fortran_binding.cpp
rename to flang-rt/lib/runtime/ISO_Fortran_binding.cpp
index d9b0bfe9aeb6b53..a5f8b357ae0b8e6 100644
--- a/flang-rt/lib/flang_rt/ISO_Fortran_binding.cpp
+++ b/flang-rt/lib/runtime/ISO_Fortran_binding.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/ISO_Fortran_binding.cpp --------------------*- C++ -*-===//
+//===-- lib/runtime/ISO_Fortran_binding.cpp ---------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -10,9 +10,9 @@
 // as specified in section 18.5.5 of Fortran 2018.
 
 #include "ISO_Fortran_util.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/type-code.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
 #include "flang/Runtime/pointer.h"
 #include <cstdlib>
diff --git a/flang-rt/lib/flang_rt/ISO_Fortran_util.h b/flang-rt/lib/runtime/ISO_Fortran_util.h
similarity index 90%
rename from flang-rt/lib/flang_rt/ISO_Fortran_util.h
rename to flang-rt/lib/runtime/ISO_Fortran_util.h
index 9d4a907e1addef4..9bbc03eefc49087 100644
--- a/flang-rt/lib/flang_rt/ISO_Fortran_util.h
+++ b/flang-rt/lib/runtime/ISO_Fortran_util.h
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/ISO_Fortran_util.h -------------------------*- C++ -*-===//
+//===-- lib/runtime/ISO_Fortran_util.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.
@@ -6,14 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_ISO_FORTRAN_UTIL_H_
-#define FLANG_RT_ISO_FORTRAN_UTIL_H_
+#ifndef FLANG_RT_RUNTIME_ISO_FORTRAN_UTIL_H_
+#define FLANG_RT_RUNTIME_ISO_FORTRAN_UTIL_H_
 
 // Internal utils for establishing CFI_cdesc_t descriptors.
 
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/type-code.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
 #include <cstdlib>
 
@@ -99,4 +99,4 @@ static inline RT_API_ATTRS void EstablishDescriptor(CFI_cdesc_t *descriptor,
   }
 }
 } // namespace Fortran::ISO
-#endif // FLANG_RT_ISO_FORTRAN_UTIL_H_
+#endif // FLANG_RT_RUNTIME_ISO_FORTRAN_UTIL_H_
diff --git a/flang-rt/lib/flang_rt/allocatable.cpp b/flang-rt/lib/runtime/allocatable.cpp
similarity index 96%
rename from flang-rt/lib/flang_rt/allocatable.cpp
rename to flang-rt/lib/runtime/allocatable.cpp
index 63ef99b8a72d116..a51816129199ae6 100644
--- a/flang-rt/lib/flang_rt/allocatable.cpp
+++ b/flang-rt/lib/runtime/allocatable.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/allocatable.cpp ----------------------------*- C++ -*-===//
+//===-- lib/runtime/allocatable.cpp -----------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,12 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/allocatable.h"
-#include "flang-rt/assign-impl.h"
-#include "flang-rt/derived.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/stat.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/runtime/assign-impl.h"
+#include "flang-rt/runtime/derived.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/type-info.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
 #include "flang/Runtime/assign.h"
 
diff --git a/flang-rt/lib/flang_rt/allocator-registry.cpp b/flang-rt/lib/runtime/allocator-registry.cpp
similarity index 89%
rename from flang-rt/lib/flang_rt/allocator-registry.cpp
rename to flang-rt/lib/runtime/allocator-registry.cpp
index 11847de68724d89..f8a8daaf8e748f0 100644
--- a/flang-rt/lib/flang_rt/allocator-registry.cpp
+++ b/flang-rt/lib/runtime/allocator-registry.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/allocator-registry.cpp ---------------------*- C++ -*-===//
+//===-- lib/runtime/allocator-registry.cpp ----------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/allocator-registry.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/allocator-registry.h"
+#include "flang-rt/runtime/terminator.h"
 
 namespace Fortran::runtime {
 
diff --git a/flang-rt/lib/flang_rt/array-constructor.cpp b/flang-rt/lib/runtime/array-constructor.cpp
similarity index 96%
rename from flang-rt/lib/flang_rt/array-constructor.cpp
rename to flang-rt/lib/runtime/array-constructor.cpp
index d2765c9439b75cd..7e267e714927fcc 100644
--- a/flang-rt/lib/flang_rt/array-constructor.cpp
+++ b/flang-rt/lib/runtime/array-constructor.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/array-constructor.cpp ----------------------*- C++ -*-===//
+//===-- lib/runtime/array-constructor.cpp -----------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,12 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/array-constructor.h"
-#include "flang-rt/derived.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/runtime/array-constructor.h"
+#include "flang-rt/runtime/derived.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
+#include "flang-rt/runtime/type-info.h"
 #include "flang/Runtime/allocatable.h"
 #include "flang/Runtime/assign.h"
 
diff --git a/flang-rt/lib/flang_rt/assign.cpp b/flang-rt/lib/runtime/assign.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/assign.cpp
rename to flang-rt/lib/runtime/assign.cpp
index 0d6045eac503a13..a1f3715f278c16b 100644
--- a/flang-rt/lib/flang_rt/assign.cpp
+++ b/flang-rt/lib/runtime/assign.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/assign.cpp ---------------------------------*- C++ -*-===//
+//===-- lib/runtime/assign.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,13 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/assign.h"
-#include "flang-rt/assign-impl.h"
-#include "flang-rt/derived.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/stat.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/runtime/assign-impl.h"
+#include "flang-rt/runtime/derived.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
+#include "flang-rt/runtime/type-info.h"
 
 namespace Fortran::runtime {
 
diff --git a/flang-rt/lib/flang_rt/buffer.cpp b/flang-rt/lib/runtime/buffer.cpp
similarity index 90%
rename from flang-rt/lib/flang_rt/buffer.cpp
rename to flang-rt/lib/runtime/buffer.cpp
index e084155068bc5ae..4cf85e13b6d367c 100644
--- a/flang-rt/lib/flang_rt/buffer.cpp
+++ b/flang-rt/lib/runtime/buffer.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/buffer.cpp ---------------------------------*- C++ -*-===//
+//===-- lib/runtime/buffer.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/buffer.h"
+#include "flang-rt/runtime/buffer.h"
 #include <algorithm>
 
 namespace Fortran::runtime::io {
diff --git a/flang-rt/lib/flang_rt/character.cpp b/flang-rt/lib/runtime/character.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/character.cpp
rename to flang-rt/lib/runtime/character.cpp
index 79d4ebdb0afbfed..10cf27c37c4d82c 100644
--- a/flang-rt/lib/flang_rt/character.cpp
+++ b/flang-rt/lib/runtime/character.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/character.cpp ------------------------------*- C++ -*-===//
+//===-- lib/runtime/character.cpp -------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/character.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Common/bit-population-count.h"
 #include "flang/Common/uint128.h"
 #include "flang/Runtime/character.h"
diff --git a/flang-rt/lib/flang_rt/command.cpp b/flang-rt/lib/runtime/command.cpp
similarity index 96%
rename from flang-rt/lib/flang_rt/command.cpp
rename to flang-rt/lib/runtime/command.cpp
index 498ae8db1722415..8a5a61ac1ad447f 100644
--- a/flang-rt/lib/flang_rt/command.cpp
+++ b/flang-rt/lib/runtime/command.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/command.cpp --------------------------------*- C++ -*-===//
+//===-- lib/runtime/command.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,11 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/command.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/environment.h"
-#include "flang-rt/stat.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/environment.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include <cstdlib>
 #include <limits>
 
diff --git a/flang-rt/lib/flang_rt/complex-powi.cpp b/flang-rt/lib/runtime/complex-powi.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/complex-powi.cpp
rename to flang-rt/lib/runtime/complex-powi.cpp
index 287bafc4220575d..a561d114591cf22 100644
--- a/flang-rt/lib/flang_rt/complex-powi.cpp
+++ b/flang-rt/lib/runtime/complex-powi.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/complex-powi.cpp ---------------------------*- C++ -*-===//
+//===-- lib/runtime/complex-powi.cpp ----------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/flang_rt/complex-reduction.c b/flang-rt/lib/runtime/complex-reduction.c
similarity index 100%
rename from flang-rt/lib/flang_rt/complex-reduction.c
rename to flang-rt/lib/runtime/complex-reduction.c
diff --git a/flang-rt/lib/flang_rt/complex-reduction.h b/flang-rt/lib/runtime/complex-reduction.h
similarity index 97%
rename from flang-rt/lib/flang_rt/complex-reduction.h
rename to flang-rt/lib/runtime/complex-reduction.h
index 45614ea66057309..44c52fb02fa4385 100644
--- a/flang-rt/lib/flang_rt/complex-reduction.h
+++ b/flang-rt/lib/runtime/complex-reduction.h
@@ -1,4 +1,4 @@
-/*===-- lib/flang_rt/complex-reduction.h ----------------------------*- C -*-===
+/*===-- lib/runtime/complex-reduction.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.
@@ -11,8 +11,8 @@
  * with C++ build compilers that don't support C's _Complex.
  */
 
-#ifndef FLANG_RT_COMPLEX_REDUCTION_H_
-#define FLANG_RT_COMPLEX_REDUCTION_H_
+#ifndef FLANG_RT_RUNTIME_COMPLEX_REDUCTION_H_
+#define FLANG_RT_RUNTIME_COMPLEX_REDUCTION_H_
 
 #include "flang/Common/float128.h"
 #include "flang/Runtime/entry-names.h"
@@ -155,4 +155,4 @@ void RTNAME(ReduceComplex16DimValue)(
     REDUCE_DIM_ARGS(CFloat128ComplexType, CFloat128ComplexType_value_op));
 #endif
 
-#endif // FLANG_RT_COMPLEX_REDUCTION_H_
+#endif // FLANG_RT_RUNTIME_COMPLEX_REDUCTION_H_
diff --git a/flang-rt/lib/flang_rt/connection.cpp b/flang-rt/lib/runtime/connection.cpp
similarity index 91%
rename from flang-rt/lib/flang_rt/connection.cpp
rename to flang-rt/lib/runtime/connection.cpp
index 1c454b81389acce..2f01dbbb959201c 100644
--- a/flang-rt/lib/flang_rt/connection.cpp
+++ b/flang-rt/lib/runtime/connection.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/connection.cpp -----------------------------*- C++ -*-===//
+//===-- lib/runtime/connection.cpp ------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,9 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/connection.h"
-#include "flang-rt/environment.h"
-#include "flang-rt/io-stmt.h"
+#include "flang-rt/runtime/connection.h"
+#include "flang-rt/runtime/environment.h"
+#include "flang-rt/runtime/io-stmt.h"
 #include <algorithm>
 
 namespace Fortran::runtime::io {
diff --git a/flang-rt/lib/flang_rt/copy.cpp b/flang-rt/lib/runtime/copy.cpp
similarity index 97%
rename from flang-rt/lib/flang_rt/copy.cpp
rename to flang-rt/lib/runtime/copy.cpp
index 0ad6946d3161ac6..5956642dd725891 100644
--- a/flang-rt/lib/flang_rt/copy.cpp
+++ b/flang-rt/lib/runtime/copy.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/copy.cpp -----------------------------------*- C++ -*-===//
+//===-- lib/runtime/copy.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -8,9 +8,9 @@
 
 #include "copy.h"
 #include "stack.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/type-info.h"
 #include "flang/Runtime/allocatable.h"
 #include <cstring>
 
diff --git a/flang-rt/lib/flang_rt/copy.h b/flang-rt/lib/runtime/copy.h
similarity index 78%
rename from flang-rt/lib/flang_rt/copy.h
rename to flang-rt/lib/runtime/copy.h
index f4abac323196b06..836c9d4a1ef8910 100644
--- a/flang-rt/lib/flang_rt/copy.h
+++ b/flang-rt/lib/runtime/copy.h
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/copy.h -------------------------------------*- C++ -*-===//
+//===-- lib/runtime/copy.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.
@@ -9,10 +9,10 @@
 // Utilities that copy data in a type-aware fashion, allocating & duplicating
 // allocatable/automatic components of derived types along the way.
 
-#ifndef FLANG_RT_COPY_H_
-#define FLANG_RT_COPY_H_
+#ifndef FLANG_RT_RUNTIME_COPY_H_
+#define FLANG_RT_RUNTIME_COPY_H_
 
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 
 namespace Fortran::runtime {
 
@@ -22,4 +22,4 @@ RT_API_ATTRS void CopyElement(const Descriptor &to, const SubscriptValue toAt[],
     const Descriptor &from, const SubscriptValue fromAt[], Terminator &);
 
 } // namespace Fortran::runtime
-#endif // FLANG_RT_COPY_H_
+#endif // FLANG_RT_RUNTIME_COPY_H_
diff --git a/flang-rt/lib/flang_rt/derived-api.cpp b/flang-rt/lib/runtime/derived-api.cpp
similarity index 95%
rename from flang-rt/lib/flang_rt/derived-api.cpp
rename to flang-rt/lib/runtime/derived-api.cpp
index 353bc4a83ebd984..884fa8ee7d09554 100644
--- a/flang-rt/lib/flang_rt/derived-api.cpp
+++ b/flang-rt/lib/runtime/derived-api.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/derived-api.cpp ----------------------------*- C++ -*-===//
+//===-- lib/runtime/derived-api.cpp -----------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,11 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/derived-api.h"
-#include "flang-rt/derived.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/runtime/derived.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
+#include "flang-rt/runtime/type-info.h"
 
 namespace Fortran::runtime {
 
diff --git a/flang-rt/lib/flang_rt/derived.cpp b/flang-rt/lib/runtime/derived.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/derived.cpp
rename to flang-rt/lib/runtime/derived.cpp
index cace9e1074e6592..87e4b29d08c28e2 100644
--- a/flang-rt/lib/flang_rt/derived.cpp
+++ b/flang-rt/lib/runtime/derived.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/derived.cpp --------------------------------*- C++ -*-===//
+//===-- lib/runtime/derived.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,12 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/derived.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/stat.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/runtime/derived.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
+#include "flang-rt/runtime/type-info.h"
 
 namespace Fortran::runtime {
 
diff --git a/flang-rt/lib/flang_rt/descriptor-io.cpp b/flang-rt/lib/runtime/descriptor-io.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/descriptor-io.cpp
rename to flang-rt/lib/runtime/descriptor-io.cpp
index c5cfa4d7fc59727..3db1455af52febf 100644
--- a/flang-rt/lib/flang_rt/descriptor-io.cpp
+++ b/flang-rt/lib/runtime/descriptor-io.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/descriptor-io.cpp --------------------------*- C++ -*-===//
+//===-- lib/runtime/descriptor-io.cpp ---------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/flang_rt/descriptor-io.h b/flang-rt/lib/runtime/descriptor-io.h
similarity index 98%
rename from flang-rt/lib/flang_rt/descriptor-io.h
rename to flang-rt/lib/runtime/descriptor-io.h
index 97bedce247d8321..dd399164325cbef 100644
--- a/flang-rt/lib/flang_rt/descriptor-io.h
+++ b/flang-rt/lib/runtime/descriptor-io.h
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/descriptor-io.h ----------------------------*- C++ -*-===//
+//===-- lib/runtime/descriptor-io.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.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_DESCRIPTOR_IO_H_
-#define FLANG_RT_DESCRIPTOR_IO_H_
+#ifndef FLANG_RT_RUNTIME_DESCRIPTOR_IO_H_
+#define FLANG_RT_RUNTIME_DESCRIPTOR_IO_H_
 
 // Implementation of I/O data list item transfers based on descriptors.
 // (All I/O items come through here so that the code is exercised for test;
@@ -17,11 +17,11 @@
 #include "edit-input.h"
 #include "edit-output.h"
 #include "unit.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/io-stmt.h"
-#include "flang-rt/namelist.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/io-stmt.h"
+#include "flang-rt/runtime/namelist.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/type-info.h"
 #include "flang/Common/optional.h"
 #include "flang/Common/uint128.h"
 #include "flang/Runtime/cpp-type.h"
@@ -626,4 +626,4 @@ static RT_API_ATTRS bool DescriptorIO(IoStatementState &io,
   return false;
 }
 } // namespace Fortran::runtime::io::descr
-#endif // FLANG_RT_DESCRIPTOR_IO_H_
+#endif // FLANG_RT_RUNTIME_DESCRIPTOR_IO_H_
diff --git a/flang-rt/lib/flang_rt/descriptor.cpp b/flang-rt/lib/runtime/descriptor.cpp
similarity index 97%
rename from flang-rt/lib/flang_rt/descriptor.cpp
rename to flang-rt/lib/runtime/descriptor.cpp
index cdf01f12e713478..8241a34a4990c42 100644
--- a/flang-rt/lib/flang_rt/descriptor.cpp
+++ b/flang-rt/lib/runtime/descriptor.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/descriptor.cpp -----------------------------*- C++ -*-===//
+//===-- lib/runtime/descriptor.cpp ------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,15 +6,15 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "ISO_Fortran_util.h"
 #include "memory.h"
-#include "flang-rt/allocator-registry.h"
-#include "flang-rt/derived.h"
-#include "flang-rt/stat.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/runtime/allocator-registry.h"
+#include "flang-rt/runtime/derived.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
+#include "flang-rt/runtime/type-info.h"
 #include <cassert>
 #include <cstdlib>
 #include <cstring>
diff --git a/flang-rt/lib/flang_rt/dot-product.cpp b/flang-rt/lib/runtime/dot-product.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/dot-product.cpp
rename to flang-rt/lib/runtime/dot-product.cpp
index ad600a4228463b4..20612f1876c153f 100644
--- a/flang-rt/lib/flang_rt/dot-product.cpp
+++ b/flang-rt/lib/runtime/dot-product.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/dot-product.cpp ----------------------------*- C++ -*-===//
+//===-- lib/runtime/dot-product.cpp -----------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "float.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Common/float128.h"
 #include "flang/Runtime/cpp-type.h"
 #include "flang/Runtime/reduction.h"
diff --git a/flang-rt/lib/flang_rt/edit-input.cpp b/flang-rt/lib/runtime/edit-input.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/edit-input.cpp
rename to flang-rt/lib/runtime/edit-input.cpp
index 7121f6e42dea658..99a266648f95c57 100644
--- a/flang-rt/lib/flang_rt/edit-input.cpp
+++ b/flang-rt/lib/runtime/edit-input.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/edit-input.cpp -----------------------------*- C++ -*-===//
+//===-- lib/runtime/edit-input.cpp ------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,8 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "edit-input.h"
-#include "flang-rt/namelist.h"
-#include "flang-rt/utf.h"
+#include "flang-rt/runtime/namelist.h"
+#include "flang-rt/runtime/utf.h"
 #include "flang/Common/optional.h"
 #include "flang/Common/real.h"
 #include "flang/Common/uint128.h"
diff --git a/flang-rt/lib/flang_rt/edit-input.h b/flang-rt/lib/runtime/edit-input.h
similarity index 87%
rename from flang-rt/lib/flang_rt/edit-input.h
rename to flang-rt/lib/runtime/edit-input.h
index fdee0f0e1131eba..686cd461b3e34d8 100644
--- a/flang-rt/lib/flang_rt/edit-input.h
+++ b/flang-rt/lib/runtime/edit-input.h
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/edit-input.h -------------------------------*- C++ -*-===//
+//===-- lib/runtime/edit-input.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.
@@ -6,11 +6,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_EDIT_INPUT_H_
-#define FLANG_RT_EDIT_INPUT_H_
+#ifndef FLANG_RT_RUNTIME_EDIT_INPUT_H_
+#define FLANG_RT_RUNTIME_EDIT_INPUT_H_
 
-#include "flang-rt/format.h"
-#include "flang-rt/io-stmt.h"
+#include "flang-rt/runtime/format.h"
+#include "flang-rt/runtime/io-stmt.h"
 #include "flang/Decimal/decimal.h"
 
 namespace Fortran::runtime::io {
@@ -50,4 +50,4 @@ extern template RT_API_ATTRS bool EditCharacterInput(
     IoStatementState &, const DataEdit &, char32_t *, std::size_t);
 
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_EDIT_INPUT_H_
+#endif // FLANG_RT_RUNTIME_EDIT_INPUT_H_
diff --git a/flang-rt/lib/flang_rt/edit-output.cpp b/flang-rt/lib/runtime/edit-output.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/edit-output.cpp
rename to flang-rt/lib/runtime/edit-output.cpp
index 096bd4567a42a60..36bbc638ff5fc02 100644
--- a/flang-rt/lib/flang_rt/edit-output.cpp
+++ b/flang-rt/lib/runtime/edit-output.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/edit-output.cpp ----------------------------*- C++ -*-===//
+//===-- lib/runtime/edit-output.cpp -----------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,8 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "edit-output.h"
-#include "flang-rt/emit-encoded.h"
-#include "flang-rt/utf.h"
+#include "flang-rt/runtime/emit-encoded.h"
+#include "flang-rt/runtime/utf.h"
 #include "flang/Common/real.h"
 #include "flang/Common/uint128.h"
 #include <algorithm>
diff --git a/flang-rt/lib/flang_rt/edit-output.h b/flang-rt/lib/runtime/edit-output.h
similarity index 95%
rename from flang-rt/lib/flang_rt/edit-output.h
rename to flang-rt/lib/runtime/edit-output.h
index 729f4400bd6312b..51a47405e49e408 100644
--- a/flang-rt/lib/flang_rt/edit-output.h
+++ b/flang-rt/lib/runtime/edit-output.h
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/edit-output.h ------------------------------*- C++ -*-===//
+//===-- lib/runtime/edit-output.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.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_EDIT_OUTPUT_H_
-#define FLANG_RT_EDIT_OUTPUT_H_
+#ifndef FLANG_RT_RUNTIME_EDIT_OUTPUT_H_
+#define FLANG_RT_RUNTIME_EDIT_OUTPUT_H_
 
 // Output data editing templates implementing the FORMAT data editing
 // descriptors E, EN, ES, EX, D, F, and G for REAL data (and COMPLEX
@@ -18,8 +18,8 @@
 // Drives the same fast binary-to-decimal formatting templates used
 // in the f18 front-end.
 
-#include "flang-rt/format.h"
-#include "flang-rt/io-stmt.h"
+#include "flang-rt/runtime/format.h"
+#include "flang-rt/runtime/io-stmt.h"
 #include "flang/Common/uint128.h"
 #include "flang/Decimal/decimal.h"
 
@@ -138,4 +138,4 @@ extern template class RealOutputEditing<10>;
 extern template class RealOutputEditing<16>;
 
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_EDIT_OUTPUT_H_
+#endif // FLANG_RT_RUNTIME_EDIT_OUTPUT_H_
diff --git a/flang-rt/lib/flang_rt/environment-default-list.h b/flang-rt/lib/runtime/environment-default-list.h
similarity index 100%
rename from flang-rt/lib/flang_rt/environment-default-list.h
rename to flang-rt/lib/runtime/environment-default-list.h
diff --git a/flang-rt/lib/flang_rt/environment.cpp b/flang-rt/lib/runtime/environment.cpp
similarity index 97%
rename from flang-rt/lib/flang_rt/environment.cpp
rename to flang-rt/lib/runtime/environment.cpp
index e3727398b898fa8..15380ba148df5e3 100644
--- a/flang-rt/lib/flang_rt/environment.cpp
+++ b/flang-rt/lib/runtime/environment.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/environment.cpp ----------------------------*- C++ -*-===//
+//===-- lib/runtime/environment.cpp -----------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,10 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/environment.h"
+#include "flang-rt/runtime/environment.h"
 #include "environment-default-list.h"
 #include "memory.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/tools.h"
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
diff --git a/flang-rt/lib/flang_rt/exceptions.cpp b/flang-rt/lib/runtime/exceptions.cpp
similarity index 97%
rename from flang-rt/lib/flang_rt/exceptions.cpp
rename to flang-rt/lib/runtime/exceptions.cpp
index f417aa8810f1cf2..d676a0ad53aa98e 100644
--- a/flang-rt/lib/flang_rt/exceptions.cpp
+++ b/flang-rt/lib/runtime/exceptions.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/exceptions.cpp -----------------------------*- C++ -*-===//
+//===-- lib/runtime/exceptions.cpp ------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -9,7 +9,7 @@
 // Runtime exception support.
 
 #include "flang/Runtime/exceptions.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include <cfenv>
 #if defined(__aarch64__) && !defined(_WIN32)
 #include <fpu_control.h>
diff --git a/flang-rt/lib/flang_rt/execute.cpp b/flang-rt/lib/runtime/execute.cpp
similarity index 97%
rename from flang-rt/lib/flang_rt/execute.cpp
rename to flang-rt/lib/runtime/execute.cpp
index 8d56581c12a49f2..f180da846a32c3a 100644
--- a/flang-rt/lib/flang_rt/execute.cpp
+++ b/flang-rt/lib/runtime/execute.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/execute.cpp --------------------------------*- C++ -*-===//
+//===-- lib/runtime/execute.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,11 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/execute.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/environment.h"
-#include "flang-rt/stat.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/environment.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include <cstdlib>
 #include <errno.h>
 #include <future>
diff --git a/flang-rt/lib/flang_rt/extensions.cpp b/flang-rt/lib/runtime/extensions.cpp
similarity index 97%
rename from flang-rt/lib/flang_rt/extensions.cpp
rename to flang-rt/lib/runtime/extensions.cpp
index ef205c5aed68980..75195c33a6c2139 100644
--- a/flang-rt/lib/flang_rt/extensions.cpp
+++ b/flang-rt/lib/runtime/extensions.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/extensions.cpp -----------------------------*- C++ -*-===//
+//===-- lib/runtime/extensions.cpp ------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -10,9 +10,9 @@
 // extensions that will eventually be implemented in Fortran.
 
 #include "flang/Runtime/extensions.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Runtime/command.h"
 #include "flang/Runtime/entry-names.h"
 #include "flang/Runtime/io-api.h"
diff --git a/flang-rt/lib/flang_rt/external-unit.cpp b/flang-rt/lib/runtime/external-unit.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/external-unit.cpp
rename to flang-rt/lib/runtime/external-unit.cpp
index 4be90cbbcc4c889..b8004d6315994d9 100644
--- a/flang-rt/lib/flang_rt/external-unit.cpp
+++ b/flang-rt/lib/runtime/external-unit.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/external-unit.cpp --------------------------*- C++ -*-===//
+//===-- lib/runtime/external-unit.cpp ---------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -12,9 +12,9 @@
 
 #include "unit-map.h"
 #include "unit.h"
-#include "flang-rt/io-error.h"
-#include "flang-rt/lock.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/io-error.h"
+#include "flang-rt/runtime/lock.h"
+#include "flang-rt/runtime/tools.h"
 
 // NOTE: the header files above may define OpenMP declare target
 // variables, so they have to be included unconditionally
diff --git a/flang-rt/lib/flang_rt/extrema.cpp b/flang-rt/lib/runtime/extrema.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/extrema.cpp
rename to flang-rt/lib/runtime/extrema.cpp
index 8ecd7fbe91d7725..3d84daa38044198 100644
--- a/flang-rt/lib/flang_rt/extrema.cpp
+++ b/flang-rt/lib/runtime/extrema.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/extrema.cpp --------------------------------*- C++ -*-===//
+//===-- lib/runtime/extrema.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -10,7 +10,7 @@
 // and shapes and (for MAXLOC & MINLOC) result integer kinds.  Also implements
 // NORM2 using common infrastructure.
 
-#include "flang-rt/reduction-templates.h"
+#include "flang-rt/runtime/reduction-templates.h"
 #include "flang/Common/float128.h"
 #include "flang/Runtime/character.h"
 #include "flang/Runtime/reduction.h"
diff --git a/flang-rt/lib/flang_rt/file.cpp b/flang-rt/lib/runtime/file.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/file.cpp
rename to flang-rt/lib/runtime/file.cpp
index c9fcf0f97b718fd..16e73db48872723 100644
--- a/flang-rt/lib/flang_rt/file.cpp
+++ b/flang-rt/lib/runtime/file.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/file.cpp -----------------------------------*- C++ -*-===//
+//===-- lib/runtime/file.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,9 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/file.h"
-#include "flang-rt/memory.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/file.h"
+#include "flang-rt/runtime/memory.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Runtime/magic-numbers.h"
 #include <algorithm>
 #include <cerrno>
diff --git a/flang-rt/lib/flang_rt/findloc.cpp b/flang-rt/lib/runtime/findloc.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/findloc.cpp
rename to flang-rt/lib/runtime/findloc.cpp
index ac432676f5efcbc..95986aefb86a458 100644
--- a/flang-rt/lib/flang_rt/findloc.cpp
+++ b/flang-rt/lib/runtime/findloc.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/findloc.cpp --------------------------------*- C++ -*-===//
+//===-- lib/runtime/findloc.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -9,7 +9,7 @@
 // Implements FINDLOC for all required operand types and shapes and result
 // integer kinds.
 
-#include "flang-rt/reduction-templates.h"
+#include "flang-rt/runtime/reduction-templates.h"
 #include "flang/Runtime/character.h"
 #include "flang/Runtime/reduction.h"
 #include <cinttypes>
diff --git a/flang-rt/lib/flang_rt/format.cpp b/flang-rt/lib/runtime/format.cpp
similarity index 89%
rename from flang-rt/lib/flang_rt/format.cpp
rename to flang-rt/lib/runtime/format.cpp
index abd01b56919a4dd..ee0059f5f0729fa 100644
--- a/flang-rt/lib/flang_rt/format.cpp
+++ b/flang-rt/lib/runtime/format.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/format.cpp ---------------------------------*- C++ -*-===//
+//===-- lib/runtime/format.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/format-implementation.h"
+#include "flang-rt/runtime/format-implementation.h"
 
 namespace Fortran::runtime::io {
 RT_OFFLOAD_API_GROUP_BEGIN
diff --git a/flang-rt/lib/flang_rt/inquiry.cpp b/flang-rt/lib/runtime/inquiry.cpp
similarity index 94%
rename from flang-rt/lib/flang_rt/inquiry.cpp
rename to flang-rt/lib/runtime/inquiry.cpp
index e98d3900987dc73..b6a7fce7a1e78fa 100644
--- a/flang-rt/lib/flang_rt/inquiry.cpp
+++ b/flang-rt/lib/runtime/inquiry.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/inquiry.cpp --------------------------------*- C++ -*-===//
+//===-- lib/runtime/inquiry.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -11,9 +11,9 @@
 
 #include "flang/Runtime/inquiry.h"
 #include "copy.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include <algorithm>
 
 namespace Fortran::runtime {
diff --git a/flang-rt/lib/flang_rt/internal-unit.cpp b/flang-rt/lib/runtime/internal-unit.cpp
similarity index 96%
rename from flang-rt/lib/flang_rt/internal-unit.cpp
rename to flang-rt/lib/runtime/internal-unit.cpp
index 12d702de4c59800..e344b01e8b34ed7 100644
--- a/flang-rt/lib/flang_rt/internal-unit.cpp
+++ b/flang-rt/lib/runtime/internal-unit.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/internal-unit.cpp --------------------------*- C++ -*-===//
+//===-- lib/runtime/internal-unit.cpp ---------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,9 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/internal-unit.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/io-error.h"
+#include "flang-rt/runtime/internal-unit.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/io-error.h"
 #include "flang/Runtime/freestanding-tools.h"
 #include <algorithm>
 #include <type_traits>
diff --git a/flang-rt/lib/flang_rt/io-api-common.h b/flang-rt/lib/runtime/io-api-common.h
similarity index 92%
rename from flang-rt/lib/flang_rt/io-api-common.h
rename to flang-rt/lib/runtime/io-api-common.h
index 5e480fdef34835a..b91ff9ff16863c7 100644
--- a/flang-rt/lib/flang_rt/io-api-common.h
+++ b/flang-rt/lib/runtime/io-api-common.h
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/io-api-common.h ----------------------------*- C++ -*-===//
+//===-- lib/runtime/io-api-common.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.
@@ -6,12 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FLANG_RT_IO_API_COMMON_H_
-#define FLANG_RT_IO_API_COMMON_H_
+#ifndef FLANG_RT_RUNTIME_IO_API_COMMON_H_
+#define FLANG_RT_RUNTIME_IO_API_COMMON_H_
 
 #include "unit.h"
-#include "flang-rt/io-stmt.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/io-stmt.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Common/api-attrs.h"
 #include "flang/Common/optional.h"
 #include "flang/Runtime/io-api.h"
@@ -94,4 +94,4 @@ RT_API_ATTRS Cookie BeginExternalListIO(
 }
 
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_IO_API_COMMON_H_
+#endif // FLANG_RT_RUNTIME_IO_API_COMMON_H_
diff --git a/flang-rt/lib/flang_rt/io-api-minimal.cpp b/flang-rt/lib/runtime/io-api-minimal.cpp
similarity index 96%
rename from flang-rt/lib/flang_rt/io-api-minimal.cpp
rename to flang-rt/lib/runtime/io-api-minimal.cpp
index cd5cf6bd3e6991b..c706a3aa239a53d 100644
--- a/flang-rt/lib/flang_rt/io-api-minimal.cpp
+++ b/flang-rt/lib/runtime/io-api-minimal.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/io-api-minimal.cpp -------------------------*- C++ -*-===//
+//===-- lib/runtime/io-api-minimal.cpp --------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -12,10 +12,10 @@
 #include "edit-output.h"
 #include "io-api-common.h"
 #include "unit.h"
-#include "flang-rt/format.h"
-#include "flang-rt/io-stmt.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/format.h"
+#include "flang-rt/runtime/io-stmt.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Runtime/io-api.h"
 
 namespace Fortran::runtime::io {
diff --git a/flang-rt/lib/flang_rt/io-api.cpp b/flang-rt/lib/runtime/io-api.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/io-api.cpp
rename to flang-rt/lib/runtime/io-api.cpp
index c904352cba7f50f..72041ae4294673c 100644
--- a/flang-rt/lib/flang_rt/io-api.cpp
+++ b/flang-rt/lib/runtime/io-api.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/io-api.cpp ---------------------------------*- C++ -*-===//
+//===-- lib/runtime/io-api.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -19,13 +19,13 @@
 #include "edit-output.h"
 #include "io-api-common.h"
 #include "unit.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/environment.h"
-#include "flang-rt/format.h"
-#include "flang-rt/io-stmt.h"
-#include "flang-rt/memory.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/environment.h"
+#include "flang-rt/runtime/format.h"
+#include "flang-rt/runtime/io-stmt.h"
+#include "flang-rt/runtime/memory.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Common/optional.h"
 #include <cstdlib>
 #include <memory>
diff --git a/flang-rt/lib/flang_rt/io-error.cpp b/flang-rt/lib/runtime/io-error.cpp
similarity index 97%
rename from flang-rt/lib/flang_rt/io-error.cpp
rename to flang-rt/lib/runtime/io-error.cpp
index b356642c95f85f7..b350fb66fc25b6e 100644
--- a/flang-rt/lib/flang_rt/io-error.cpp
+++ b/flang-rt/lib/runtime/io-error.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/io-error.cpp -------------------------------*- C++ -*-===//
+//===-- lib/runtime/io-error.cpp --------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,9 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/io-error.h"
+#include "flang-rt/runtime/io-error.h"
 #include "config.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Runtime/magic-numbers.h"
 #include <cerrno>
 #include <cstdarg>
diff --git a/flang-rt/lib/flang_rt/io-stmt.cpp b/flang-rt/lib/runtime/io-stmt.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/io-stmt.cpp
rename to flang-rt/lib/runtime/io-stmt.cpp
index a07bf70a6593e1d..b0823ffd9e7039a 100644
--- a/flang-rt/lib/flang_rt/io-stmt.cpp
+++ b/flang-rt/lib/runtime/io-stmt.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/io-stmt.cpp --------------------------------*- C++ -*-===//
+//===-- lib/runtime/io-stmt.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,14 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/io-stmt.h"
+#include "flang-rt/runtime/io-stmt.h"
 #include "unit.h"
-#include "flang-rt/connection.h"
-#include "flang-rt/emit-encoded.h"
-#include "flang-rt/format.h"
-#include "flang-rt/memory.h"
-#include "flang-rt/tools.h"
-#include "flang-rt/utf.h"
+#include "flang-rt/runtime/connection.h"
+#include "flang-rt/runtime/emit-encoded.h"
+#include "flang-rt/runtime/format.h"
+#include "flang-rt/runtime/memory.h"
+#include "flang-rt/runtime/tools.h"
+#include "flang-rt/runtime/utf.h"
 #include <algorithm>
 #include <cstdio>
 #include <cstring>
diff --git a/flang-rt/lib/flang_rt/iostat.cpp b/flang-rt/lib/runtime/iostat.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/iostat.cpp
rename to flang-rt/lib/runtime/iostat.cpp
index ab2fc63cf7b2110..0f8bfb884e54444 100644
--- a/flang-rt/lib/flang_rt/iostat.cpp
+++ b/flang-rt/lib/runtime/iostat.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/iostat.cpp ---------------------------------*- C++ -*-===//
+//===-- lib/runtime/iostat.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/flang_rt/main.cpp b/flang-rt/lib/runtime/main.cpp
similarity index 90%
rename from flang-rt/lib/flang_rt/main.cpp
rename to flang-rt/lib/runtime/main.cpp
index dac7268f82b166a..b3f066cda3732ad 100644
--- a/flang-rt/lib/flang_rt/main.cpp
+++ b/flang-rt/lib/runtime/main.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/main.cpp -----------------------------------*- C++ -*-===//
+//===-- lib/runtime/main.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,8 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/main.h"
-#include "flang-rt/environment.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/environment.h"
+#include "flang-rt/runtime/terminator.h"
 #include <cfenv>
 #include <cstdio>
 #include <cstdlib>
diff --git a/flang-rt/lib/flang_rt/matmul-transpose.cpp b/flang-rt/lib/runtime/matmul-transpose.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/matmul-transpose.cpp
rename to flang-rt/lib/runtime/matmul-transpose.cpp
index 307995bb8ab1e7f..e20abbdddcd308e 100644
--- a/flang-rt/lib/flang_rt/matmul-transpose.cpp
+++ b/flang-rt/lib/runtime/matmul-transpose.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/matmul-transpose.cpp -----------------------*- C++ -*-===//
+//===-- lib/runtime/matmul-transpose.cpp ------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -21,9 +21,9 @@
 // to use the faster BLAS routines.
 
 #include "flang/Runtime/matmul-transpose.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Common/optional.h"
 #include "flang/Runtime/c-or-cpp.h"
 #include "flang/Runtime/cpp-type.h"
diff --git a/flang-rt/lib/flang_rt/matmul.cpp b/flang-rt/lib/runtime/matmul.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/matmul.cpp
rename to flang-rt/lib/runtime/matmul.cpp
index c5036e24f86f4c8..f14cea922d21eb9 100644
--- a/flang-rt/lib/flang_rt/matmul.cpp
+++ b/flang-rt/lib/runtime/matmul.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/matmul.cpp ---------------------------------*- C++ -*-===//
+//===-- lib/runtime/matmul.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -20,9 +20,9 @@
 // Places where BLAS routines could be called are marked as TODO items.
 
 #include "flang/Runtime/matmul.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Common/optional.h"
 #include "flang/Runtime/c-or-cpp.h"
 #include "flang/Runtime/cpp-type.h"
diff --git a/flang-rt/lib/flang_rt/memory.cpp b/flang-rt/lib/runtime/memory.cpp
similarity index 87%
rename from flang-rt/lib/flang_rt/memory.cpp
rename to flang-rt/lib/runtime/memory.cpp
index e09e45898da8e64..79c7e3377756921 100644
--- a/flang-rt/lib/flang_rt/memory.cpp
+++ b/flang-rt/lib/runtime/memory.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/memory.cpp ---------------------------------*- C++ -*-===//
+//===-- lib/runtime/memory.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,9 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/memory.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/memory.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Runtime/freestanding-tools.h"
 #include <cstdlib>
 
diff --git a/flang-rt/lib/flang_rt/misc-intrinsic.cpp b/flang-rt/lib/runtime/misc-intrinsic.cpp
similarity index 95%
rename from flang-rt/lib/flang_rt/misc-intrinsic.cpp
rename to flang-rt/lib/runtime/misc-intrinsic.cpp
index 1a5bc8114e8aec8..b7335e9f6799e41 100644
--- a/flang-rt/lib/flang_rt/misc-intrinsic.cpp
+++ b/flang-rt/lib/runtime/misc-intrinsic.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/misc-intrinsic.cpp -------------------------*- C++ -*-===//
+//===-- lib/runtime/misc-intrinsic.cpp --------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/misc-intrinsic.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Common/optional.h"
 #include <algorithm>
 #include <cstdio>
diff --git a/flang-rt/lib/flang_rt/namelist.cpp b/flang-rt/lib/runtime/namelist.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/namelist.cpp
rename to flang-rt/lib/runtime/namelist.cpp
index 4cf1cd2062ac514..b0cf2180fc6d467 100644
--- a/flang-rt/lib/flang_rt/namelist.cpp
+++ b/flang-rt/lib/runtime/namelist.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/namelist.cpp -------------------------------*- C++ -*-===//
+//===-- lib/runtime/namelist.cpp --------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,10 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/namelist.h"
+#include "flang-rt/runtime/namelist.h"
 #include "descriptor-io.h"
-#include "flang-rt/emit-encoded.h"
-#include "flang-rt/io-stmt.h"
+#include "flang-rt/runtime/emit-encoded.h"
+#include "flang-rt/runtime/io-stmt.h"
 #include "flang/Runtime/io-api.h"
 #include <algorithm>
 #include <cstring>
diff --git a/flang-rt/lib/flang_rt/non-tbp-dio.cpp b/flang-rt/lib/runtime/non-tbp-dio.cpp
similarity index 86%
rename from flang-rt/lib/flang_rt/non-tbp-dio.cpp
rename to flang-rt/lib/runtime/non-tbp-dio.cpp
index 5663c2ee338d725..72101b06e0c6ec3 100644
--- a/flang-rt/lib/flang_rt/non-tbp-dio.cpp
+++ b/flang-rt/lib/runtime/non-tbp-dio.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/non-tbp-dio.cpp ----------------------------*- C++ -*-===//
+//===-- lib/runtime/non-tbp-dio.cpp -----------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/non-tbp-dio.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/runtime/non-tbp-dio.h"
+#include "flang-rt/runtime/type-info.h"
 
 namespace Fortran::runtime::io {
 
diff --git a/flang-rt/lib/flang_rt/numeric.cpp b/flang-rt/lib/runtime/numeric.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/numeric.cpp
rename to flang-rt/lib/runtime/numeric.cpp
index b63c604cadcfb30..37638765dc65007 100644
--- a/flang-rt/lib/flang_rt/numeric.cpp
+++ b/flang-rt/lib/runtime/numeric.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/numeric.cpp --------------------------------*- C++ -*-===//
+//===-- lib/runtime/numeric.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/numeric.h"
-#include "flang-rt/numeric-templates.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/numeric-templates.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Common/float128.h"
 #include <cfloat>
 #include <climits>
diff --git a/flang-rt/lib/flang_rt/pointer.cpp b/flang-rt/lib/runtime/pointer.cpp
similarity index 96%
rename from flang-rt/lib/flang_rt/pointer.cpp
rename to flang-rt/lib/runtime/pointer.cpp
index 3138a54f1d122e4..93ba82e0583c010 100644
--- a/flang-rt/lib/flang_rt/pointer.cpp
+++ b/flang-rt/lib/runtime/pointer.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/pointer.cpp --------------------------------*- C++ -*-===//
+//===-- lib/runtime/pointer.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,13 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/pointer.h"
-#include "flang-rt/assign-impl.h"
-#include "flang-rt/derived.h"
-#include "flang-rt/environment.h"
-#include "flang-rt/stat.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/runtime/assign-impl.h"
+#include "flang-rt/runtime/derived.h"
+#include "flang-rt/runtime/environment.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
+#include "flang-rt/runtime/type-info.h"
 
 namespace Fortran::runtime {
 extern "C" {
diff --git a/flang-rt/lib/flang_rt/product.cpp b/flang-rt/lib/runtime/product.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/product.cpp
rename to flang-rt/lib/runtime/product.cpp
index c7b6d23d3014e40..02fdc2bfcd57677 100644
--- a/flang-rt/lib/flang_rt/product.cpp
+++ b/flang-rt/lib/runtime/product.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/product.cpp --------------------------------*- C++ -*-===//
+//===-- lib/runtime/product.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -8,7 +8,7 @@
 
 // Implements PRODUCT for all required operand types and shapes.
 
-#include "flang-rt/reduction-templates.h"
+#include "flang-rt/runtime/reduction-templates.h"
 #include "flang/Common/float128.h"
 #include "flang/Runtime/reduction.h"
 #include <cfloat>
diff --git a/flang-rt/lib/flang_rt/pseudo-unit.cpp b/flang-rt/lib/runtime/pseudo-unit.cpp
similarity index 97%
rename from flang-rt/lib/flang_rt/pseudo-unit.cpp
rename to flang-rt/lib/runtime/pseudo-unit.cpp
index f4d204890581ec5..7e1f3bc86b294ef 100644
--- a/flang-rt/lib/flang_rt/pseudo-unit.cpp
+++ b/flang-rt/lib/runtime/pseudo-unit.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/pseudo-unit.cpp ----------------------------*- C++ -*-===//
+//===-- lib/runtime/pseudo-unit.cpp -----------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -12,8 +12,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "unit.h"
-#include "flang-rt/io-error.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/io-error.h"
+#include "flang-rt/runtime/tools.h"
 
 // NOTE: the header files above may define OpenMP declare target
 // variables, so they have to be included unconditionally
diff --git a/flang-rt/lib/flang_rt/ragged.cpp b/flang-rt/lib/runtime/ragged.cpp
similarity index 96%
rename from flang-rt/lib/flang_rt/ragged.cpp
rename to flang-rt/lib/runtime/ragged.cpp
index 798b3464e7bcac6..dddc3ccdfd85801 100644
--- a/flang-rt/lib/flang_rt/ragged.cpp
+++ b/flang-rt/lib/runtime/ragged.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/ragged.cpp ---------------------------------*- C++ -*-===//
+//===-- lib/runtime/ragged.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/ragged.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/tools.h"
 #include <cstdlib>
 
 namespace Fortran::runtime {
diff --git a/flang-rt/lib/flang_rt/random.cpp b/flang-rt/lib/runtime/random.cpp
similarity index 96%
rename from flang-rt/lib/flang_rt/random.cpp
rename to flang-rt/lib/runtime/random.cpp
index 76971cf3fc888ef..dc74f2725ed512b 100644
--- a/flang-rt/lib/flang_rt/random.cpp
+++ b/flang-rt/lib/runtime/random.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/random.cpp ---------------------------------*- C++ -*-===//
+//===-- lib/runtime/random.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -10,10 +10,10 @@
 // RANDOM_SEED.
 
 #include "flang/Runtime/random.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/lock.h"
-#include "flang-rt/random-templates.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/lock.h"
+#include "flang-rt/runtime/random-templates.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Common/float128.h"
 #include "flang/Common/leading-zero-bit-count.h"
 #include "flang/Common/uint128.h"
diff --git a/flang-rt/lib/flang_rt/reduce.cpp b/flang-rt/lib/runtime/reduce.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/reduce.cpp
rename to flang-rt/lib/runtime/reduce.cpp
index 4bb6dc10ea9dac4..3c5e815e32d2bba 100644
--- a/flang-rt/lib/flang_rt/reduce.cpp
+++ b/flang-rt/lib/runtime/reduce.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/reduce.cpp ---------------------------------*- C++ -*-===//
+//===-- lib/runtime/reduce.cpp ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -9,10 +9,10 @@
 // REDUCE() implementation
 
 #include "flang/Runtime/reduce.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/reduction-templates.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/reduction-templates.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 
 namespace Fortran::runtime {
 
diff --git a/flang-rt/lib/flang_rt/reduction.cpp b/flang-rt/lib/runtime/reduction.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/reduction.cpp
rename to flang-rt/lib/runtime/reduction.cpp
index 9ceaeffc6541ae2..5e45870489479ee 100644
--- a/flang-rt/lib/flang_rt/reduction.cpp
+++ b/flang-rt/lib/runtime/reduction.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/reduction.cpp ------------------------------*- C++ -*-===//
+//===-- lib/runtime/reduction.cpp -------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -14,8 +14,8 @@
 // NORM2, MAXLOC, MINLOC, MAXVAL, and MINVAL are in extrema.cpp.
 
 #include "flang/Runtime/reduction.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/reduction-templates.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/reduction-templates.h"
 #include <cinttypes>
 
 namespace Fortran::runtime {
diff --git a/flang-rt/lib/flang_rt/stack.h b/flang-rt/lib/runtime/stack.h
similarity index 93%
rename from flang-rt/lib/flang_rt/stack.h
rename to flang-rt/lib/runtime/stack.h
index 6f37282f00932e6..38364ff541f16b4 100644
--- a/flang-rt/lib/flang_rt/stack.h
+++ b/flang-rt/lib/runtime/stack.h
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/stack.h ------------------------------------*- C++ -*-===//
+//===-- lib/runtime/stack.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.
@@ -10,11 +10,11 @@
 // It is a list based stack with dynamic allocation/deallocation
 // of the list nodes.
 
-#ifndef FLANG_RT_STACK_H_
-#define FLANG_RT_STACK_H_
+#ifndef FLANG_RT_RUNTIME_STACK_H_
+#define FLANG_RT_RUNTIME_STACK_H_
 
-#include "flang-rt/memory.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/memory.h"
+#include "flang-rt/runtime/terminator.h"
 
 namespace Fortran::runtime {
 // Storage for the Stack elements of type T.
@@ -133,4 +133,4 @@ template <typename T, unsigned N = 0> class Stack : public StackStorage<T, N> {
   Terminator &terminator_;
 };
 } // namespace Fortran::runtime
-#endif // FLANG_RT_STACK_H_
+#endif // FLANG_RT_RUNTIME_STACK_H_
diff --git a/flang-rt/lib/flang_rt/stat.cpp b/flang-rt/lib/runtime/stat.cpp
similarity index 93%
rename from flang-rt/lib/flang_rt/stat.cpp
rename to flang-rt/lib/runtime/stat.cpp
index 883e85d0fbf2f66..322b7282b702417 100644
--- a/flang-rt/lib/flang_rt/stat.cpp
+++ b/flang-rt/lib/runtime/stat.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/stat.cpp -----------------------------------*- C++ -*-===//
+//===-- lib/runtime/stat.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,10 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/stat.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 
 namespace Fortran::runtime {
 RT_OFFLOAD_API_GROUP_BEGIN
diff --git a/flang-rt/lib/flang_rt/stop.cpp b/flang-rt/lib/runtime/stop.cpp
similarity index 95%
rename from flang-rt/lib/flang_rt/stop.cpp
rename to flang-rt/lib/runtime/stop.cpp
index 91f8e4a8f6bc1bc..1d70a137377aa4a 100644
--- a/flang-rt/lib/flang_rt/stop.cpp
+++ b/flang-rt/lib/runtime/stop.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/stop.cpp -----------------------------------*- C++ -*-===//
+//===-- lib/runtime/stop.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -9,10 +9,10 @@
 #include "flang/Runtime/stop.h"
 #include "config.h"
 #include "unit.h"
-#include "flang-rt/environment.h"
-#include "flang-rt/file.h"
-#include "flang-rt/io-error.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/environment.h"
+#include "flang-rt/runtime/file.h"
+#include "flang-rt/runtime/io-error.h"
+#include "flang-rt/runtime/terminator.h"
 #include <cfenv>
 #include <cstdio>
 #include <cstdlib>
diff --git a/flang-rt/lib/flang_rt/sum.cpp b/flang-rt/lib/runtime/sum.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/sum.cpp
rename to flang-rt/lib/runtime/sum.cpp
index fd98525b8c9b280..a76e228f18a4efa 100644
--- a/flang-rt/lib/flang_rt/sum.cpp
+++ b/flang-rt/lib/runtime/sum.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/sum.cpp ------------------------------------*- C++ -*-===//
+//===-- lib/runtime/sum.cpp -------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -12,7 +12,7 @@
 // cancellation on intermediate results by using "Kahan summation"
 // (basically the same as manual "double-double").
 
-#include "flang-rt/reduction-templates.h"
+#include "flang-rt/runtime/reduction-templates.h"
 #include "flang/Common/float128.h"
 #include "flang/Runtime/reduction.h"
 #include <cfloat>
diff --git a/flang-rt/lib/flang_rt/support.cpp b/flang-rt/lib/runtime/support.cpp
similarity index 91%
rename from flang-rt/lib/flang_rt/support.cpp
rename to flang-rt/lib/runtime/support.cpp
index 2673176bd81858b..5a2b0c920aa800a 100644
--- a/flang-rt/lib/flang_rt/support.cpp
+++ b/flang-rt/lib/runtime/support.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/support.cpp --------------------------------*- C++ -*-===//
+//===-- lib/runtime/support.cpp ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -8,8 +8,8 @@
 
 #include "flang/Runtime/support.h"
 #include "ISO_Fortran_util.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/type-info.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/type-info.h"
 
 namespace Fortran::runtime {
 extern "C" {
diff --git a/flang-rt/lib/flang_rt/temporary-stack.cpp b/flang-rt/lib/runtime/temporary-stack.cpp
similarity index 97%
rename from flang-rt/lib/flang_rt/temporary-stack.cpp
rename to flang-rt/lib/runtime/temporary-stack.cpp
index 32ec4dacc872d1c..ea89d0c17bb6b5f 100644
--- a/flang-rt/lib/flang_rt/temporary-stack.cpp
+++ b/flang-rt/lib/runtime/temporary-stack.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/temporary-stack.cpp ------------------------*- C++ -*-===//
+//===-- lib/runtime/temporary-stack.cpp -------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -10,9 +10,9 @@
 // temporaries. For use in HLFIR lowering.
 
 #include "flang/Runtime/temporary-stack.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/memory.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/memory.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
 #include "flang/Runtime/assign.h"
 
diff --git a/flang-rt/lib/flang_rt/terminator.cpp b/flang-rt/lib/runtime/terminator.cpp
similarity index 96%
rename from flang-rt/lib/flang_rt/terminator.cpp
rename to flang-rt/lib/runtime/terminator.cpp
index 0dfc08eb7037095..8a57ba06b130454 100644
--- a/flang-rt/lib/flang_rt/terminator.cpp
+++ b/flang-rt/lib/runtime/terminator.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/terminator.cpp -----------------------------*- C++ -*-===//
+//===-- lib/runtime/terminator.cpp ------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include <cstdio>
 #include <cstdlib>
 
diff --git a/flang-rt/lib/flang_rt/time-intrinsic.cpp b/flang-rt/lib/runtime/time-intrinsic.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/time-intrinsic.cpp
rename to flang-rt/lib/runtime/time-intrinsic.cpp
index 1929fccbd4cf38a..69c344f5d24bcb5 100644
--- a/flang-rt/lib/flang_rt/time-intrinsic.cpp
+++ b/flang-rt/lib/runtime/time-intrinsic.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/time-intrinsic.cpp -------------------------*- C++ -*-===//
+//===-- lib/runtime/time-intrinsic.cpp --------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -9,9 +9,9 @@
 // Implements time-related intrinsic subroutines.
 
 #include "flang/Runtime/time-intrinsic.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Runtime/cpp-type.h"
 #include <algorithm>
 #include <cstdint>
diff --git a/flang-rt/lib/flang_rt/tools.cpp b/flang-rt/lib/runtime/tools.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/tools.cpp
rename to flang-rt/lib/runtime/tools.cpp
index 9b460f81ad5c213..b9d9ca4fc378c9e 100644
--- a/flang-rt/lib/flang_rt/tools.cpp
+++ b/flang-rt/lib/runtime/tools.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/tools.cpp ----------------------------------*- C++ -*-===//
+//===-- lib/runtime/tools.cpp -----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/tools.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/tools.h"
+#include "flang-rt/runtime/terminator.h"
 #include <algorithm>
 #include <cstdint>
 #include <cstdlib>
diff --git a/flang-rt/lib/flang_rt/transformational.cpp b/flang-rt/lib/runtime/transformational.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/transformational.cpp
rename to flang-rt/lib/runtime/transformational.cpp
index 7453bf18323e9fe..eb694a9f2c83320 100644
--- a/flang-rt/lib/flang_rt/transformational.cpp
+++ b/flang-rt/lib/runtime/transformational.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/transformational.cpp -----------------------*- C++ -*-===//
+//===-- lib/runtime/transformational.cpp ------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -18,9 +18,9 @@
 
 #include "flang/Runtime/transformational.h"
 #include "copy.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Common/float128.h"
 
 namespace Fortran::runtime {
diff --git a/flang-rt/lib/flang_rt/type-code.cpp b/flang-rt/lib/runtime/type-code.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/type-code.cpp
rename to flang-rt/lib/runtime/type-code.cpp
index 432c7591520cbcb..8cfec9a4ec2fbba 100644
--- a/flang-rt/lib/flang_rt/type-code.cpp
+++ b/flang-rt/lib/runtime/type-code.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/type-code.cpp ------------------------------*- C++ -*-===//
+//===-- lib/runtime/type-code.cpp -------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/type-code.h"
 
 namespace Fortran::runtime {
 
diff --git a/flang-rt/lib/flang_rt/type-info.cpp b/flang-rt/lib/runtime/type-info.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/type-info.cpp
rename to flang-rt/lib/runtime/type-info.cpp
index e1859e4864ca137..82182696d70c697 100644
--- a/flang-rt/lib/flang_rt/type-info.cpp
+++ b/flang-rt/lib/runtime/type-info.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/type-info.cpp ------------------------------*- C++ -*-===//
+//===-- lib/runtime/type-info.cpp -------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,9 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/type-info.h"
-#include "flang-rt/terminator.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/type-info.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
 #include <cstdio>
 
 namespace Fortran::runtime::typeInfo {
diff --git a/flang-rt/lib/flang_rt/unit-map.cpp b/flang-rt/lib/runtime/unit-map.cpp
similarity index 98%
rename from flang-rt/lib/flang_rt/unit-map.cpp
rename to flang-rt/lib/runtime/unit-map.cpp
index 0fda53ac071b271..41a03f3319d64e4 100644
--- a/flang-rt/lib/flang_rt/unit-map.cpp
+++ b/flang-rt/lib/runtime/unit-map.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/unit-map.cpp -------------------------------*- C++ -*-===//
+//===-- lib/runtime/unit-map.cpp --------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/flang_rt/unit-map.h b/flang-rt/lib/runtime/unit-map.h
similarity index 92%
rename from flang-rt/lib/flang_rt/unit-map.h
rename to flang-rt/lib/runtime/unit-map.h
index 64233008b663077..fa61288a1e18d5b 100644
--- a/flang-rt/lib/flang_rt/unit-map.h
+++ b/flang-rt/lib/runtime/unit-map.h
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/unit-map.h ---------------------------------*- C++ -*-===//
+//===-- lib/runtime/unit-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.
@@ -9,12 +9,12 @@
 // Maps Fortran unit numbers to their ExternalFileUnit instances.
 // A simple hash table with forward-linked chains per bucket.
 
-#ifndef FLANG_RT_UNIT_MAP_H_
-#define FLANG_RT_UNIT_MAP_H_
+#ifndef FLANG_RT_RUNTIME_UNIT_MAP_H_
+#define FLANG_RT_RUNTIME_UNIT_MAP_H_
 
 #include "unit.h"
-#include "flang-rt/lock.h"
-#include "flang-rt/memory.h"
+#include "flang-rt/runtime/lock.h"
+#include "flang-rt/runtime/memory.h"
 #include "flang/Common/fast-int-set.h"
 #include <cstdint>
 #include <cstdlib>
@@ -100,4 +100,4 @@ class UnitMap {
   int emergencyNewUnit_{maxNewUnits_}; // not recycled
 };
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_UNIT_MAP_H_
+#endif // FLANG_RT_RUNTIME_UNIT_MAP_H_
diff --git a/flang-rt/lib/flang_rt/unit.cpp b/flang-rt/lib/runtime/unit.cpp
similarity index 99%
rename from flang-rt/lib/flang_rt/unit.cpp
rename to flang-rt/lib/runtime/unit.cpp
index f05c4b5236dd57d..1d4d54ae0195660 100644
--- a/flang-rt/lib/flang_rt/unit.cpp
+++ b/flang-rt/lib/runtime/unit.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/unit.cpp -----------------------------------*- C++ -*-===//
+//===-- lib/runtime/unit.cpp ------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -11,9 +11,9 @@
 //
 //===----------------------------------------------------------------------===//
 #include "unit.h"
-#include "flang-rt/io-error.h"
-#include "flang-rt/lock.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/io-error.h"
+#include "flang-rt/runtime/lock.h"
+#include "flang-rt/runtime/tools.h"
 #include <limits>
 #include <utility>
 
diff --git a/flang-rt/lib/flang_rt/unit.h b/flang-rt/lib/runtime/unit.h
similarity index 95%
rename from flang-rt/lib/flang_rt/unit.h
rename to flang-rt/lib/runtime/unit.h
index a5fb7fb31691f9c..eb762a2d3b2359b 100644
--- a/flang-rt/lib/flang_rt/unit.h
+++ b/flang-rt/lib/runtime/unit.h
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/unit.h -------------------------------------*- C++ -*-===//
+//===-- lib/runtime/unit.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.
@@ -8,19 +8,19 @@
 
 // Fortran external I/O units
 
-#ifndef FLANG_RT_UNIT_H_
-#define FLANG_RT_UNIT_H_
-
-#include "flang-rt/buffer.h"
-#include "flang-rt/connection.h"
-#include "flang-rt/environment.h"
-#include "flang-rt/file.h"
-#include "flang-rt/format.h"
-#include "flang-rt/io-error.h"
-#include "flang-rt/io-stmt.h"
-#include "flang-rt/lock.h"
-#include "flang-rt/memory.h"
-#include "flang-rt/terminator.h"
+#ifndef FLANG_RT_RUNTIME_UNIT_H_
+#define FLANG_RT_RUNTIME_UNIT_H_
+
+#include "flang-rt/runtime/buffer.h"
+#include "flang-rt/runtime/connection.h"
+#include "flang-rt/runtime/environment.h"
+#include "flang-rt/runtime/file.h"
+#include "flang-rt/runtime/format.h"
+#include "flang-rt/runtime/io-error.h"
+#include "flang-rt/runtime/io-stmt.h"
+#include "flang-rt/runtime/lock.h"
+#include "flang-rt/runtime/memory.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Common/constexpr-bitset.h"
 #include "flang/Common/optional.h"
 #include <cstdlib>
@@ -291,4 +291,4 @@ class ChildIo {
 };
 
 } // namespace Fortran::runtime::io
-#endif // FLANG_RT_UNIT_H_
+#endif // FLANG_RT_RUNTIME_UNIT_H_
diff --git a/flang-rt/lib/flang_rt/utf.cpp b/flang-rt/lib/runtime/utf.cpp
similarity index 97%
rename from flang-rt/lib/flang_rt/utf.cpp
rename to flang-rt/lib/runtime/utf.cpp
index dd74889c025c5ca..ef9df49f24f66f9 100644
--- a/flang-rt/lib/flang_rt/utf.cpp
+++ b/flang-rt/lib/runtime/utf.cpp
@@ -1,4 +1,4 @@
-//===-- lib/flang_rt/utf.cpp ------------------------------------*- C++ -*-===//
+//===-- lib/runtime/utf.cpp -------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/utf.h"
+#include "flang-rt/runtime/utf.h"
 
 namespace Fortran::runtime {
 
diff --git a/flang-rt/test/CMakeLists.txt b/flang-rt/test/CMakeLists.txt
index 5ca07b66e4f9b5f..f5f7b8832d38111 100644
--- a/flang-rt/test/CMakeLists.txt
+++ b/flang-rt/test/CMakeLists.txt
@@ -44,8 +44,8 @@ add_custom_target(flang-rt-test-depends)
 set_target_properties(flang-rt-test-depends PROPERTIES FOLDER "Flang-RT/Meta")
 add_dependencies(flang-rt-test-depends
     FlangRTUnitTests
-    flang_rt.unittest
-    flang_rt.static
+    flang_rt.runtime
+    flang_rt.runtime.unittest
   )
 
 add_lit_testsuite(check-flang-rt "Running the Flang-RT regression tests"
diff --git a/flang-rt/test/lit.cfg.py b/flang-rt/test/lit.cfg.py
index 08f3f1121c8960e..909dbf6eae0ffc1 100644
--- a/flang-rt/test/lit.cfg.py
+++ b/flang-rt/test/lit.cfg.py
@@ -94,7 +94,7 @@ def shjoin(args, sep=" "):
 # Include path for C headers that define Flang's Fortran ABI.
 config.substitutions.append(("%include", os.path.join(config.flang_source_dir, "include")))
 
-# Library path of libflang_rt.a (for lib search path when using non-Flang driver for linking)
+# Library path of libflang_rt.runtime.a (for lib search path when using non-Flang driver for linking)
 config.substitutions.append(("%libdir", config.flang_rt_output_resource_lib_dir))
 
 # For CUDA offloading, additional steps (device linking) and libraries (cudart) are needed.
diff --git a/flang-rt/unittests/Evaluate/ISO-Fortran-binding.cpp b/flang-rt/unittests/Evaluate/ISO-Fortran-binding.cpp
index d1417b715e959c5..8c0a6f29b6967cd 100644
--- a/flang-rt/unittests/Evaluate/ISO-Fortran-binding.cpp
+++ b/flang-rt/unittests/Evaluate/ISO-Fortran-binding.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
 #include "flang/Testing/testing.h"
 #include "llvm/Support/raw_ostream.h"
diff --git a/flang-rt/unittests/Evaluate/reshape.cpp b/flang-rt/unittests/Evaluate/reshape.cpp
index a45e06ad0f69d1d..2abe46c0969fb9d 100644
--- a/flang-rt/unittests/Evaluate/reshape.cpp
+++ b/flang-rt/unittests/Evaluate/reshape.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Runtime/transformational.h"
 #include "flang/Testing/testing.h"
 #include <cinttypes>
diff --git a/flang-rt/unittests/Runtime/Allocatable.cpp b/flang-rt/unittests/Runtime/Allocatable.cpp
index b03003a93fe09a0..4702f48e0f44071 100644
--- a/flang-rt/unittests/Runtime/Allocatable.cpp
+++ b/flang-rt/unittests/Runtime/Allocatable.cpp
@@ -8,7 +8,7 @@
 
 #include "flang/Runtime/allocatable.h"
 #include "gtest/gtest.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/tools.h"
 
 using namespace Fortran::runtime;
 
diff --git a/flang-rt/unittests/Runtime/ArrayConstructor.cpp b/flang-rt/unittests/Runtime/ArrayConstructor.cpp
index 5c84cd162a29315..5f791e7f4a7c387 100644
--- a/flang-rt/unittests/Runtime/ArrayConstructor.cpp
+++ b/flang-rt/unittests/Runtime/ArrayConstructor.cpp
@@ -8,9 +8,9 @@
 
 #include "tools.h"
 #include "gtest/gtest.h"
-#include "flang-rt/array-constructor.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/array-constructor.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/type-code.h"
 #include "flang/Runtime/allocatable.h"
 #include "flang/Runtime/cpp-type.h"
 
diff --git a/flang-rt/unittests/Runtime/BufferTest.cpp b/flang-rt/unittests/Runtime/BufferTest.cpp
index 7795a79593eb2c1..f4b9e901d6fcbc6 100644
--- a/flang-rt/unittests/Runtime/BufferTest.cpp
+++ b/flang-rt/unittests/Runtime/BufferTest.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/buffer.h"
+#include "flang-rt/runtime/buffer.h"
 #include "CrashHandlerFixture.h"
 #include "gtest/gtest.h"
 #include <algorithm>
diff --git a/flang-rt/unittests/Runtime/CMakeLists.txt b/flang-rt/unittests/Runtime/CMakeLists.txt
index 32f12b14caca7a4..d647e690c06805b 100644
--- a/flang-rt/unittests/Runtime/CMakeLists.txt
+++ b/flang-rt/unittests/Runtime/CMakeLists.txt
@@ -43,6 +43,6 @@ add_flangrt_unittest(RuntimeTests
 )
 target_compile_definitions(RuntimeTests PRIVATE NOT_EXE="${LLVM_TOOLS_DIR}/not${CMAKE_EXECUTABLE_SUFFIX}")
 
-if (FLANG_RT_ENABLE_CUF)
+if (FLANG_RT_INCLUDE_CUF)
   add_subdirectory(CUDA)
 endif ()
diff --git a/flang-rt/unittests/Runtime/CUDA/Allocatable.cpp b/flang-rt/unittests/Runtime/CUDA/Allocatable.cpp
index db6589990be184d..90d4da849552a5c 100644
--- a/flang-rt/unittests/Runtime/CUDA/Allocatable.cpp
+++ b/flang-rt/unittests/Runtime/CUDA/Allocatable.cpp
@@ -9,10 +9,10 @@
 #include "flang/Runtime/allocatable.h"
 #include "cuda_runtime.h"
 #include "gtest/gtest.h"
-#include "flang-rt/CUDA/allocator.h"
-#include "flang-rt/allocator-registry.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/cuda/allocator.h"
+#include "flang-rt/runtime/allocator-registry.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 #include "flang/Runtime/CUDA/descriptor.h"
 #include "flang/Support/Fortran.h"
diff --git a/flang-rt/unittests/Runtime/CUDA/AllocatorCUF.cpp b/flang-rt/unittests/Runtime/CUDA/AllocatorCUF.cpp
index 86c13d0dd290c24..6e46a80aa8f3cd3 100644
--- a/flang-rt/unittests/Runtime/CUDA/AllocatorCUF.cpp
+++ b/flang-rt/unittests/Runtime/CUDA/AllocatorCUF.cpp
@@ -8,10 +8,10 @@
 
 #include "cuda_runtime.h"
 #include "gtest/gtest.h"
-#include "flang-rt/CUDA/allocator.h"
-#include "flang-rt/allocator-registry.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/cuda/allocator.h"
+#include "flang-rt/runtime/allocator-registry.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/descriptor.h"
 #include "flang/Runtime/allocatable.h"
 #include "flang/Support/Fortran.h"
diff --git a/flang-rt/unittests/Runtime/CUDA/CMakeLists.txt b/flang-rt/unittests/Runtime/CUDA/CMakeLists.txt
index 37b117234208c4e..cd69a6f472873f5 100644
--- a/flang-rt/unittests/Runtime/CUDA/CMakeLists.txt
+++ b/flang-rt/unittests/Runtime/CUDA/CMakeLists.txt
@@ -14,5 +14,5 @@ add_flangrt_unittest(FlangCufRuntimeTests
 
 target_link_libraries(FlangCufRuntimeTests
   PRIVATE
-  CufRuntime
+  flang_rt.cuda
 )
diff --git a/flang-rt/unittests/Runtime/CUDA/Memory.cpp b/flang-rt/unittests/Runtime/CUDA/Memory.cpp
index 32609795c01699b..c43b165509a9826 100644
--- a/flang-rt/unittests/Runtime/CUDA/Memory.cpp
+++ b/flang-rt/unittests/Runtime/CUDA/Memory.cpp
@@ -10,9 +10,9 @@
 #include "cuda_runtime.h"
 #include "../tools.h"
 #include "gtest/gtest.h"
-#include "flang-rt/CUDA/allocator.h"
-#include "flang-rt/allocator-registry.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/cuda/allocator.h"
+#include "flang-rt/runtime/allocator-registry.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 #include "flang/Runtime/CUDA/descriptor.h"
 #include "flang/Runtime/allocatable.h"
diff --git a/flang-rt/unittests/Runtime/CharacterTest.cpp b/flang-rt/unittests/Runtime/CharacterTest.cpp
index 696ab6b06e9e721..83ec9b36d9b0cf6 100644
--- a/flang-rt/unittests/Runtime/CharacterTest.cpp
+++ b/flang-rt/unittests/Runtime/CharacterTest.cpp
@@ -11,7 +11,7 @@
 
 #include "flang/Runtime/character.h"
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include <cstring>
 #include <functional>
 #include <tuple>
diff --git a/flang-rt/unittests/Runtime/CommandTest.cpp b/flang-rt/unittests/Runtime/CommandTest.cpp
index a34c329750f39eb..72fe7629dbbb85f 100644
--- a/flang-rt/unittests/Runtime/CommandTest.cpp
+++ b/flang-rt/unittests/Runtime/CommandTest.cpp
@@ -9,7 +9,7 @@
 #include "flang/Runtime/command.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Runtime/execute.h"
 #include "flang/Runtime/extensions.h"
 #include "flang/Runtime/main.h"
diff --git a/flang-rt/unittests/Runtime/CrashHandlerFixture.cpp b/flang-rt/unittests/Runtime/CrashHandlerFixture.cpp
index 939cc62c433f096..8213edd1f922533 100644
--- a/flang-rt/unittests/Runtime/CrashHandlerFixture.cpp
+++ b/flang-rt/unittests/Runtime/CrashHandlerFixture.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "CrashHandlerFixture.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include <cstdarg>
 #include <cstdlib>
 
diff --git a/flang-rt/unittests/Runtime/Derived.cpp b/flang-rt/unittests/Runtime/Derived.cpp
index 2f45dfb80f7fd4f..3196ba796ad19e5 100644
--- a/flang-rt/unittests/Runtime/Derived.cpp
+++ b/flang-rt/unittests/Runtime/Derived.cpp
@@ -7,8 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Runtime/derived-api.h"
 
 using namespace Fortran::runtime;
diff --git a/flang-rt/unittests/Runtime/ExternalIOTest.cpp b/flang-rt/unittests/Runtime/ExternalIOTest.cpp
index a5481e071916d3e..c83535ca82bd3f3 100644
--- a/flang-rt/unittests/Runtime/ExternalIOTest.cpp
+++ b/flang-rt/unittests/Runtime/ExternalIOTest.cpp
@@ -12,7 +12,7 @@
 
 #include "CrashHandlerFixture.h"
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Runtime/io-api-consts.h"
 #include "flang/Runtime/main.h"
 #include "flang/Runtime/stop.h"
diff --git a/flang-rt/unittests/Runtime/Format.cpp b/flang-rt/unittests/Runtime/Format.cpp
index 83650e0f999ac11..fe7403f26700be0 100644
--- a/flang-rt/unittests/Runtime/Format.cpp
+++ b/flang-rt/unittests/Runtime/Format.cpp
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "CrashHandlerFixture.h"
-#include "flang-rt/connection.h"
-#include "flang-rt/format-implementation.h"
-#include "flang-rt/io-error.h"
+#include "flang-rt/runtime/connection.h"
+#include "flang-rt/runtime/format-implementation.h"
+#include "flang-rt/runtime/io-error.h"
 #include <optional>
 #include <string>
 #include <tuple>
diff --git a/flang-rt/unittests/Runtime/Inquiry.cpp b/flang-rt/unittests/Runtime/Inquiry.cpp
index f6097484bd4815c..e79c037864d9cfc 100644
--- a/flang-rt/unittests/Runtime/Inquiry.cpp
+++ b/flang-rt/unittests/Runtime/Inquiry.cpp
@@ -9,7 +9,7 @@
 #include "flang/Runtime/inquiry.h"
 #include "tools.h"
 #include "gtest/gtest.h"
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/type-code.h"
 
 using namespace Fortran::runtime;
 using Fortran::common::TypeCategory;
diff --git a/flang-rt/unittests/Runtime/ListInputTest.cpp b/flang-rt/unittests/Runtime/ListInputTest.cpp
index 0c6c59d6a325e5b..310c41a5c3fa5e7 100644
--- a/flang-rt/unittests/Runtime/ListInputTest.cpp
+++ b/flang-rt/unittests/Runtime/ListInputTest.cpp
@@ -7,8 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "CrashHandlerFixture.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/io-error.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/io-error.h"
 #include "flang/Runtime/io-api-consts.h"
 
 using namespace Fortran::runtime;
diff --git a/flang-rt/unittests/Runtime/LogicalFormatTest.cpp b/flang-rt/unittests/Runtime/LogicalFormatTest.cpp
index 07f62ee02fc66d5..bc933292181c17d 100644
--- a/flang-rt/unittests/Runtime/LogicalFormatTest.cpp
+++ b/flang-rt/unittests/Runtime/LogicalFormatTest.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "CrashHandlerFixture.h"
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Runtime/io-api-consts.h"
 #include <algorithm>
 #include <array>
diff --git a/flang-rt/unittests/Runtime/Matmul.cpp b/flang-rt/unittests/Runtime/Matmul.cpp
index 28bf2327da949ea..1b716fd01fd4297 100644
--- a/flang-rt/unittests/Runtime/Matmul.cpp
+++ b/flang-rt/unittests/Runtime/Matmul.cpp
@@ -9,8 +9,8 @@
 #include "flang/Runtime/matmul.h"
 #include "tools.h"
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/type-code.h"
 #include "flang/Runtime/allocatable.h"
 #include "flang/Runtime/cpp-type.h"
 
diff --git a/flang-rt/unittests/Runtime/MatmulTranspose.cpp b/flang-rt/unittests/Runtime/MatmulTranspose.cpp
index b06afa29ce28ef8..cc14ab755505a69 100644
--- a/flang-rt/unittests/Runtime/MatmulTranspose.cpp
+++ b/flang-rt/unittests/Runtime/MatmulTranspose.cpp
@@ -8,8 +8,8 @@
 
 #include "tools.h"
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/type-code.h"
 #include "flang/Runtime/allocatable.h"
 #include "flang/Runtime/cpp-type.h"
 #include "flang/Runtime/matmul-transpose.h"
diff --git a/flang-rt/unittests/Runtime/MiscIntrinsic.cpp b/flang-rt/unittests/Runtime/MiscIntrinsic.cpp
index 61b05b31f45a907..c6783381bfc35d4 100644
--- a/flang-rt/unittests/Runtime/MiscIntrinsic.cpp
+++ b/flang-rt/unittests/Runtime/MiscIntrinsic.cpp
@@ -8,7 +8,7 @@
 
 #include "tools.h"
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Runtime/allocatable.h"
 #include "flang/Runtime/cpp-type.h"
 #include "flang/Runtime/misc-intrinsic.h"
diff --git a/flang-rt/unittests/Runtime/Namelist.cpp b/flang-rt/unittests/Runtime/Namelist.cpp
index d48a0273bf556fa..040dedb8cd47c6a 100644
--- a/flang-rt/unittests/Runtime/Namelist.cpp
+++ b/flang-rt/unittests/Runtime/Namelist.cpp
@@ -6,10 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang-rt/namelist.h"
+#include "flang-rt/runtime/namelist.h"
 #include "CrashHandlerFixture.h"
 #include "tools.h"
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Runtime/io-api-consts.h"
 #include <algorithm>
 #include <cinttypes>
diff --git a/flang-rt/unittests/Runtime/NumericalFormatTest.cpp b/flang-rt/unittests/Runtime/NumericalFormatTest.cpp
index a86e6b807fc53f7..5a8ead48dcef9d8 100644
--- a/flang-rt/unittests/Runtime/NumericalFormatTest.cpp
+++ b/flang-rt/unittests/Runtime/NumericalFormatTest.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "CrashHandlerFixture.h"
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Runtime/io-api-consts.h"
 #include <algorithm>
 #include <array>
diff --git a/flang-rt/unittests/Runtime/Pointer.cpp b/flang-rt/unittests/Runtime/Pointer.cpp
index 13f1922e6c5c3f1..a4645f5b6bf9856 100644
--- a/flang-rt/unittests/Runtime/Pointer.cpp
+++ b/flang-rt/unittests/Runtime/Pointer.cpp
@@ -8,8 +8,8 @@
 
 #include "flang/Runtime/pointer.h"
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/tools.h"
 
 using namespace Fortran::runtime;
 
diff --git a/flang-rt/unittests/Runtime/Random.cpp b/flang-rt/unittests/Runtime/Random.cpp
index dba480cffa9c953..bf4b540e768fd1f 100644
--- a/flang-rt/unittests/Runtime/Random.cpp
+++ b/flang-rt/unittests/Runtime/Random.cpp
@@ -8,8 +8,8 @@
 
 #include "flang/Runtime/random.h"
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/type-code.h"
 #include <cmath>
 
 using namespace Fortran::runtime;
diff --git a/flang-rt/unittests/Runtime/Reduction.cpp b/flang-rt/unittests/Runtime/Reduction.cpp
index cb84512639933fa..3701a32042c589c 100644
--- a/flang-rt/unittests/Runtime/Reduction.cpp
+++ b/flang-rt/unittests/Runtime/Reduction.cpp
@@ -9,8 +9,8 @@
 #include "flang/Runtime/reduction.h"
 #include "tools.h"
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/type-code.h"
 #include "flang/Common/float128.h"
 #include "flang/Runtime/allocatable.h"
 #include "flang/Runtime/cpp-type.h"
diff --git a/flang-rt/unittests/Runtime/RuntimeCrashTest.cpp b/flang-rt/unittests/Runtime/RuntimeCrashTest.cpp
index 23a60d98bf6adef..e716dac2d120394 100644
--- a/flang-rt/unittests/Runtime/RuntimeCrashTest.cpp
+++ b/flang-rt/unittests/Runtime/RuntimeCrashTest.cpp
@@ -12,7 +12,7 @@
 //===----------------------------------------------------------------------===//
 #include "CrashHandlerFixture.h"
 #include "tools.h"
-#include "flang-rt/terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/io-api-consts.h"
 #include "flang/Runtime/transformational.h"
 #include <gtest/gtest.h>
diff --git a/flang-rt/unittests/Runtime/Stop.cpp b/flang-rt/unittests/Runtime/Stop.cpp
index 102fecd1afb22d5..5d1af20e77de7fc 100644
--- a/flang-rt/unittests/Runtime/Stop.cpp
+++ b/flang-rt/unittests/Runtime/Stop.cpp
@@ -12,7 +12,7 @@
 
 #include "flang/Runtime/stop.h"
 #include "CrashHandlerFixture.h"
-#include "flang-rt/environment.h"
+#include "flang-rt/runtime/environment.h"
 #include <cstdlib>
 #include <gtest/gtest.h>
 
diff --git a/flang-rt/unittests/Runtime/Support.cpp b/flang-rt/unittests/Runtime/Support.cpp
index 0159bd74aa60cc4..c97a6eae3a155e7 100644
--- a/flang-rt/unittests/Runtime/Support.cpp
+++ b/flang-rt/unittests/Runtime/Support.cpp
@@ -9,7 +9,7 @@
 #include "flang/Runtime/support.h"
 #include "tools.h"
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 
 using namespace Fortran::runtime;
 using Fortran::common::TypeCategory;
diff --git a/flang-rt/unittests/Runtime/TemporaryStack.cpp b/flang-rt/unittests/Runtime/TemporaryStack.cpp
index d5cb9e5bfb9da1d..ff8efe123d7cc21 100644
--- a/flang-rt/unittests/Runtime/TemporaryStack.cpp
+++ b/flang-rt/unittests/Runtime/TemporaryStack.cpp
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/tools.h"
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/tools.h"
+#include "flang-rt/runtime/type-code.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
 #include "flang/Runtime/allocatable.h"
 #include "flang/Runtime/cpp-type.h"
diff --git a/flang-rt/unittests/Runtime/Transformational.cpp b/flang-rt/unittests/Runtime/Transformational.cpp
index 8805613738a609f..06df96a3cc45ae5 100644
--- a/flang-rt/unittests/Runtime/Transformational.cpp
+++ b/flang-rt/unittests/Runtime/Transformational.cpp
@@ -9,7 +9,7 @@
 #include "flang/Runtime/transformational.h"
 #include "tools.h"
 #include "gtest/gtest.h"
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/type-code.h"
 #include "flang/Common/float128.h"
 #include <vector>
 
diff --git a/flang-rt/unittests/Runtime/tools.h b/flang-rt/unittests/Runtime/tools.h
index 146aae659c3383f..36a4c2bd9c8b798 100644
--- a/flang-rt/unittests/Runtime/tools.h
+++ b/flang-rt/unittests/Runtime/tools.h
@@ -10,8 +10,8 @@
 #define FLANG_RT_UNITTESTS_RUNTIME_TOOLS_H_
 
 #include "gtest/gtest.h"
-#include "flang-rt/descriptor.h"
-#include "flang-rt/type-code.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/type-code.h"
 #include "flang/Runtime/allocatable.h"
 #include "flang/Runtime/cpp-type.h"
 #include <cstdint>
diff --git a/flang/docs/FlangDriver.md b/flang/docs/FlangDriver.md
index e8dc3de3577db2c..97744f0bee069b1 100644
--- a/flang/docs/FlangDriver.md
+++ b/flang/docs/FlangDriver.md
@@ -175,18 +175,18 @@ like this:
 
 ```
 $ flang -v -o example example.o
-"/usr/bin/ld" [...] example.o [...] "-lflang_rt" [...]
+"/usr/bin/ld" [...] example.o [...] "-lflang_rt.runtime" [...]
 ```
 
 The automatically added libraries are:
 
-* `flang_rt`: Provides most of the Flang runtime library.
+* `flang_rt.runtime`: Provides most of the Flang runtime library.
 
 If the code is C/C++ based and invokes Fortran routines, one can either use Clang
 or Flang as the linker driver.  If Clang is used, it will automatically all
 required runtime libraries needed by C++ (e.g., for STL) to the linker invocation.
 In this case, one has to explicitly provide the Fortran runtime library
-`flang_rt`.  An alternative is to use Flang to link.
+`flang_rt.runtime`.  An alternative is to use Flang to link.
 In this case, it may be required to explicitly supply C++ runtime libraries.
 
 On Darwin, the logical root where the system libraries are located (sysroot)
diff --git a/flang/docs/GettingStarted.md b/flang/docs/GettingStarted.md
index 47ba268b3e7636b..0523b4596615135 100644
--- a/flang/docs/GettingStarted.md
+++ b/flang/docs/GettingStarted.md
@@ -82,7 +82,7 @@ cmake \
   -DLLVM_TARGETS_TO_BUILD=host \
   -DLLVM_LIT_ARGS=-v \
   -DLLVM_ENABLE_PROJECTS="clang;mlir;flang;openmp" \
-  -DLLVM_ENABLE_RUNTIMES="compiler-rt;flang_rt" \
+  -DLLVM_ENABLE_RUNTIMES="compiler-rt;flang-rt" \
   ../llvm-project/llvm
 
 ninja
@@ -194,7 +194,7 @@ Flang runtime can be built for accelerators in experimental mode, i.e.
 complete enabling is WIP.  CUDA and OpenMP target offload builds
 are currently supported.
 
-#### Bootstrapping Build of Flang-RT
+#### Building out-of-tree
 
 ##### CUDA build
 Clang with NVPTX backend and NVCC compilers are supported.
@@ -250,7 +250,7 @@ code.  Note that the packaging of the libraries is different
 between [Clang](https://clang.llvm.org/docs/OffloadingDesign.html#linking-target-device-code) and NVCC, so the library must be linked using
 compatible compiler drivers.
 
-#### Building in-tree (bootstrap build)
+#### Building in-tree (bootstrapping build)
 One may build Flang runtime library along with building Flang itself
 by providing these additional CMake variables on top of the Flang in-tree
 build config:
@@ -281,9 +281,9 @@ Or:
 
 Normal `make check-flang` will work with such CMake configuration.
 Consider building in parallel using the `-j<jobs>` flag, where `<jobs>` is a
-number low enough for all build jobs to fit into the available RAM. Using
+number sufficiently low for all build jobs to fit into the available RAM. Using
 the number of harware threads (`nprocs`) is likely too much for most
-commodity computers.
+commodity machines.
 
 ##### OpenMP target offload build
 Only Clang compiler is currently supported.
diff --git a/flang/docs/OpenACC-descriptor-management.md b/flang/docs/OpenACC-descriptor-management.md
index 6c147a56894c8f5..008c57937e23b89 100644
--- a/flang/docs/OpenACC-descriptor-management.md
+++ b/flang/docs/OpenACC-descriptor-management.md
@@ -427,7 +427,7 @@ The implementation's behavior may be described as (OpenACC 2.7.2):
 
 All the "is-present" checks and the data actions for the auxiliary pointers must be performed atomically with regards to the present counters bookkeeping.
 
-The API relies on the primitives provided by `liboffload`, so it is provided by a new F18 runtime library, e.g. `FortranOffloadRuntime`, that depends on `flang_rt` and `liboffload`.  The F18 driver adds `FortranOffloadRuntime` for linking under `-fopenacc`/`-fopenmp` (and maybe additional switches like `-fopenmp-targets`).
+The API relies on the primitives provided by `liboffload`, so it is provided by a new F18 runtime library, e.g. `FortranOffloadRuntime`, that depends on `flang_rt.runtime` and `liboffload`.  The F18 driver adds `FortranOffloadRuntime` for linking under `-fopenacc`/`-fopenmp` (and maybe additional switches like `-fopenmp-targets`).
 
 ## TODOs:
 
diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md
index 2633b27f0093c66..689f5bf58684f67 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -38,9 +38,9 @@ page](https://llvm.org/releases/).
 
  * The Fortran Runtime library has been move to a new top-level directory
    named "flang-rt". The library was also renamed from `FortranRuntime` to
-   `flang_rt`. It now supports the
-   LLVM_ENABLE_RUNTIMES mechanism to build flang-rt for multiple target
-   triples. libflang_rt.a will now be emitted into Clang's per-target
+   `flang_rt.runtime`. It now supports the
+   LLVM_ENABLE_RUNTIMES mechanism to build Flang-RT for multiple target
+   triples. libflang_rt.runtime.a will now be emitted into Clang's per-target
    resource directory (next to libclang_rt.*.*) where it is also found by
    Flang's driver.
 
diff --git a/flang/examples/CMakeLists.txt b/flang/examples/CMakeLists.txt
index 8cc66ddbbbb0ef4..23c6e790791fba2 100644
--- a/flang/examples/CMakeLists.txt
+++ b/flang/examples/CMakeLists.txt
@@ -1,4 +1,6 @@
-add_subdirectory(ExternalHelloWorld)
+if (FLANG_INCLUDE_RUNTIME)
+  add_subdirectory(ExternalHelloWorld)
+endif ()
 add_subdirectory(PrintFlangFunctionNames)
 add_subdirectory(FlangOmpReport)
 add_subdirectory(FeatureList)
diff --git a/flang/examples/ExternalHelloWorld/CMakeLists.txt b/flang/examples/ExternalHelloWorld/CMakeLists.txt
index 213fc462ac2b1a0..dbb69475976dd99 100644
--- a/flang/examples/ExternalHelloWorld/CMakeLists.txt
+++ b/flang/examples/ExternalHelloWorld/CMakeLists.txt
@@ -1,9 +1,9 @@
 # This test is not run by default as it requires input.
 add_llvm_example(external-hello-world
-  external-hello.cpp
+  ${FLANG_RT_SOURCE_DIR}/examples/ExternalHelloWorld/external-hello.cpp
 )
 
 target_link_libraries(external-hello-world
   PRIVATE
-  flang_rt
+  flang_rt.runtime
   )
diff --git a/flang/runtime/CMakeLists.txt b/flang/runtime/CMakeLists.txt
index 2bdcfca4cba857d..4e547a200a417dd 100644
--- a/flang/runtime/CMakeLists.txt
+++ b/flang/runtime/CMakeLists.txt
@@ -72,7 +72,7 @@ function (runtime_source_files outvar)
 
   set(new_sources "")
   foreach (source IN LISTS "${outvar}")
-    set(new_source "${FLANG_RT_SOURCE_DIR}/lib/flang_rt")
+    set(new_source "${FLANG_RT_SOURCE_DIR}/lib")
     cmake_path(APPEND new_source "${ARG_SUBDIR}")
     cmake_path(APPEND new_source "${source}")
     list(APPEND new_sources "${new_source}")
@@ -260,7 +260,7 @@ set(supported_files
   utf.cpp
   )
 
-runtime_source_files(supported_files)
+runtime_source_files(supported_files SUBDIR "runtime")
 enable_cuda_compilation(flang_rt "${supported_files}")
 enable_omp_offload_compilation("${supported_files}")
 
@@ -294,39 +294,42 @@ if (NOT TARGET FortranFloat128Math)
   endif()
 endif()
 
-runtime_source_files(sources)
+runtime_source_files(sources SUBDIR "runtime")
 
 
 if (NOT DEFINED MSVC)
-  add_flang_library(flang_rt
+  add_flang_library(flang_rt.runtime
     ${sources}
 
     INSTALL_WITH_TOOLCHAIN
   )
 else()
-  add_flang_library(flang_rt
+  add_flang_library(flang_rt.runtime
     ${sources}
   )
   set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
-  add_flang_library(flang_rt.static ${sources}
+  add_flang_library(flang_rt.runtime.static ${sources}
     INSTALL_WITH_TOOLCHAIN)
-  set_target_properties(flang_rt.static PROPERTIES FOLDER "Flang/Runtime Libraries")
+  set_target_properties(flang_rt.runtime.static PROPERTIES FOLDER "Flang/Runtime Libraries")
   set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
-  add_flang_library(flang_rt.dynamic ${sources}
+  add_flang_library(flang_rt.runtime.dynamic ${sources}
     INSTALL_WITH_TOOLCHAIN)
-  set_target_properties(flang_rt.dynamic PROPERTIES FOLDER "Flang/Runtime Libraries")
+  set_target_properties(flang_rt.runtime.dynamic PROPERTIES FOLDER "Flang/Runtime Libraries")
   set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
-  add_flang_library(flang_rt.static_dbg ${sources}
+  add_flang_library(flang_rt.runtime.static_dbg ${sources}
     INSTALL_WITH_TOOLCHAIN)
-  set_target_properties(flang_rt.static_dbg PROPERTIES FOLDER "Flang/Runtime Libraries")
+  set_target_properties(flang_rt.runtime.static_dbg PROPERTIES FOLDER "Flang/Runtime Libraries")
   set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
-  add_flang_library(flang_rt.dynamic_dbg ${sources}
+  add_flang_library(flang_rt.runtime.dynamic_dbg ${sources}
     INSTALL_WITH_TOOLCHAIN)
-  set_target_properties(flang_rt.dynamic_dbg PROPERTIES FOLDER "Flang/Runtime Libraries")
-  add_dependencies(flang_rt flang_rt.static flang_rt.dynamic
-    flang_rt.static_dbg flang_rt.dynamic_dbg)
+  set_target_properties(flang_rt.runtime.dynamic_dbg PROPERTIES FOLDER "Flang/Runtime Libraries")
+  add_dependencies(flang_rt.runtime
+    flang_rt.runtime.static
+    flang_rt.runtime.dynamic
+    flang_rt.runtime.static_dbg
+    flang_rt.runtime.dynamic_dbg)
 endif()
-set_target_properties(flang_rt PROPERTIES FOLDER "Flang/Runtime Libraries")
+set_target_properties(flang_rt.runtime PROPERTIES FOLDER "Flang/Runtime Libraries")
 
 # If flang_rt is part of a Flang build (and not a separate build) then
 # add dependency to make sure that Fortran runtime library is being built after
@@ -336,7 +339,7 @@ set_target_properties(flang_rt PROPERTIES FOLDER "Flang/Runtime Libraries")
 # TODO: This is a workaround and should be updated when runtime build procedure
 # is changed to a regular runtime build.  See discussion in PR #95388.
 if (TARGET flang AND TARGET module_files)
-  add_dependencies(flang_rt flang module_files)
+  add_dependencies(flang_rt.runtime flang module_files)
 endif()
 
 if (FLANG_CUF_RUNTIME)
@@ -345,6 +348,6 @@ endif()
 
 # Compatibility targets.
 add_custom_target(flang-rt)
-add_dependencies(flang-rt flang_rt)
+add_dependencies(flang-rt flang_rt.runtime)
 add_custom_target(FortranRuntime)
-add_dependencies(FortranRuntime flang_rt)
+add_dependencies(FortranRuntime flang_rt.runtime)
diff --git a/flang/runtime/CUDA/CMakeLists.txt b/flang/runtime/CUDA/CMakeLists.txt
index f202a5a5173e718..aac1f626618100a 100644
--- a/flang/runtime/CUDA/CMakeLists.txt
+++ b/flang/runtime/CUDA/CMakeLists.txt
@@ -8,10 +8,10 @@
 
 include_directories(${CUDAToolkit_INCLUDE_DIRS})
 
-# libCufRuntime depends on a certain version of CUDA. To be able to have
+# libflang_rt.cuda depends on a certain version of CUDA. To be able to have
 # multiple build of this library with different CUDA version, the version is
 # added to the library name.
-set(CUFRT_LIBNAME CufRuntime_cuda_${CUDAToolkit_VERSION_MAJOR})
+set(CUFRT_LIBNAME flang_rt.cuda_${CUDAToolkit_VERSION_MAJOR})
 
 set(sources
   allocator.cpp
@@ -24,7 +24,7 @@ set(sources
   pointer.cpp
   registration.cpp
 )
-runtime_source_files(sources SUBDIR "CUDA")
+runtime_source_files(sources SUBDIR "cuda")
 
 add_flang_library(${CUFRT_LIBNAME}
   ${sources}
@@ -38,6 +38,6 @@ endif()
 
 target_link_libraries(${CUFRT_LIBNAME}
   PRIVATE
-  flang_rt
+  flang_rt.runtime
   ${CUDA_RT_TARGET}
 )
diff --git a/flang/test/CMakeLists.txt b/flang/test/CMakeLists.txt
index e8e8099d4eaf7bc..3fac8717e9bd9da 100644
--- a/flang/test/CMakeLists.txt
+++ b/flang/test/CMakeLists.txt
@@ -75,7 +75,7 @@ set(FLANG_TEST_DEPENDS
 )
 
 if (FLANG_INCLUDE_RUNTIME)
-  list(APPEND FLANG_TEST_DEPENDS flang_rt)
+  list(APPEND FLANG_TEST_DEPENDS flang_rt.runtime)
 endif ()
 
 if (LLVM_ENABLE_PLUGINS AND NOT WIN32)
diff --git a/flang/test/Driver/gcc-toolchain-install-dir.f90 b/flang/test/Driver/gcc-toolchain-install-dir.f90
index d9160e1971250fe..e195bdde6d2c93a 100644
--- a/flang/test/Driver/gcc-toolchain-install-dir.f90
+++ b/flang/test/Driver/gcc-toolchain-install-dir.f90
@@ -1,5 +1,5 @@
 !! Test that --gcc-toolchain and --gcc-install-dir options are working as expected.
-!! It does not test cross-compiling (--sysroot), so crtbegin.o, libgcc/compiler-rt, libc, libflang_rt, etc. are not supposed to be affected.
+!! It does not test cross-compiling (--sysroot), so crtbegin.o, libgcc/compiler-rt, libc, libflang_rt.runtime, etc. are not supposed to be affected.
 !! PREFIX is captured twice because the driver escapes backslashes (occuring in Windows paths) in the -### output, but not on the "Selected GCC installation:" line.
 
 ! RUN: %flang 2>&1 -### -v -o %t %s -no-integrated-as -fuse-ld=ld --target=i386-unknown-linux-gnu --gcc-install-dir=%S/Inputs/basic_cross_linux_tree/usr/lib/gcc/i386-unknown-linux-gnu/10.2.0 | FileCheck %s --check-prefix=CHECK-I386
diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index 5dc89fe45c8cc0d..2e9dadbfb671b76 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -33,26 +33,26 @@
 ! SOLARIS-F128NONE-NOT: FortranFloat128Math
 ! UNIX-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "--as-needed" "-lquadmath" "--no-as-needed"
 ! SOLARIS-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "-z" "ignore" "-lquadmath" "-z" "record"
-! UNIX-SAME: "-lflang_rt" "-lm"
+! UNIX-SAME: "-lflang_rt.runtime" "-lm"
 ! COMPILER-RT: "{{.*}}{{\\|/}}libclang_rt.builtins.a"
 
 ! DARWIN-LABEL:  "{{.*}}ld{{(\.exe)?}}"
 ! DARWIN-SAME: "[[object_file]]"
 ! DARWIN-F128NONE-NOT: FortranFloat128Math
 ! DARWIN-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "--as-needed" "-lquadmath" "--no-as-needed"
-! DARWIN-SAME: -lflang_rt
+! DARWIN-SAME: -lflang_rt.runtime
 
 ! HAIKU-LABEL:  "{{.*}}ld{{(\.exe)?}}"
 ! HAIKU-SAME: "[[object_file]]"
 ! HAIKU-F128NONE-NOT: FortranFloat128Math
 ! HAIKU-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "--as-needed" "-lquadmath" "--no-as-needed"
-! HAIKU-SAME: "-lflang_rt"
+! HAIKU-SAME: "-lflang_rt.runtime"
 
 ! MINGW-LABEL:  "{{.*}}ld{{(\.exe)?}}"
 ! MINGW-SAME: "[[object_file]]"
 ! MINGW-F128NONE-NOT: FortranFloat128Math
 ! MINGW-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "--as-needed" "-lquadmath" "--no-as-needed"
-! MINGW-SAME: -lflang_rt
+! MINGW-SAME: -lflang_rt.runtime
 
 ! NOTE: This also matches lld-link (when CLANG_DEFAULT_LINKER=lld) and
 !       any .exe suffix that is added when resolving to the full path of
diff --git a/flang/test/Driver/msvc-dependent-lib-flags.f90 b/flang/test/Driver/msvc-dependent-lib-flags.f90
index dabac56d602d1e1..641c73912c4d1e5 100644
--- a/flang/test/Driver/msvc-dependent-lib-flags.f90
+++ b/flang/test/Driver/msvc-dependent-lib-flags.f90
@@ -7,21 +7,21 @@
 ! MSVC-SAME: --dependent-lib=clang_rt.builtins.lib
 ! MSVC-SAME: -D_MT
 ! MSVC-SAME: --dependent-lib=libcmt
-! MSVC-SAME: --dependent-lib=flang_rt.static.lib
+! MSVC-SAME: --dependent-lib=flang_rt.runtime.static.lib
 
 ! MSVC-DEBUG: -fc1
 ! MSVC-DEBUG-SAME: --dependent-lib=clang_rt.builtins.lib
 ! MSVC-DEBUG-SAME: -D_MT
 ! MSVC-DEBUG-SAME: -D_DEBUG
 ! MSVC-DEBUG-SAME: --dependent-lib=libcmtd
-! MSVC-DEBUG-SAME: --dependent-lib=flang_rt.static_dbg.lib
+! MSVC-DEBUG-SAME: --dependent-lib=flang_rt.runtime.static_dbg.lib
 
 ! MSVC-DLL: -fc1
 ! MSVC-DLL-SAME: --dependent-lib=clang_rt.builtins.lib
 ! MSVC-DLL-SAME: -D_MT
 ! MSVC-DLL-SAME: -D_DLL
 ! MSVC-DLL-SAME: --dependent-lib=msvcrt
-! MSVC-DLL-SAME: --dependent-lib=flang_rt.dynamic.lib
+! MSVC-DLL-SAME: --dependent-lib=flang_rt.runtime.dynamic.lib
 
 ! MSVC-DLL-DEBUG: -fc1
 ! MSVC-DLL-DEBUG-SAME: --dependent-lib=clang_rt.builtins.lib
@@ -29,4 +29,4 @@
 ! MSVC-DLL-DEBUG-SAME: -D_DEBUG
 ! MSVC-DLL-DEBUG-SAME: -D_DLL
 ! MSVC-DLL-DEBUG-SAME: --dependent-lib=msvcrtd
-! MSVC-DLL-DEBUG-SAME: --dependent-lib=flang_rt.dynamic_dbg.lib
+! MSVC-DLL-DEBUG-SAME: --dependent-lib=flang_rt.runtime.dynamic_dbg.lib
diff --git a/flang/test/Driver/nostdlib.f90 b/flang/test/Driver/nostdlib.f90
index 17ed10fa0615d58..dc23be646237697 100644
--- a/flang/test/Driver/nostdlib.f90
+++ b/flang/test/Driver/nostdlib.f90
@@ -24,5 +24,5 @@
 ! in certain cases. But it is not clear that it is worth checking for each
 ! platform individually.
 
-! CHECK-NOT: "-lflang_rt"
+! CHECK-NOT: "-lflang_rt.runtime"
 ! CHECK-NOT: "-lgcc"
diff --git a/flang/unittests/Evaluate/CMakeLists.txt b/flang/unittests/Evaluate/CMakeLists.txt
index 9e19b2cdfbb90c4..2278d61febcb11d 100644
--- a/flang/unittests/Evaluate/CMakeLists.txt
+++ b/flang/unittests/Evaluate/CMakeLists.txt
@@ -60,14 +60,14 @@ if (FLANG_INCLUDE_RUNTIME)
     NonGTestTesting
     FortranSemantics
     FortranEvaluate
-    flang_rt
+    flang_rt.runtime
   )
 
   add_flang_nongtest_unittest(ISO-Fortran-binding
     NonGTestTesting
     FortranEvaluate
     FortranSemantics
-    flang_rt
+    flang_rt.runtime
   )
 endif ()
 
diff --git a/flang/unittests/Runtime/CMakeLists.txt b/flang/unittests/Runtime/CMakeLists.txt
index 2e8739140274429..40afec3e113f3e8 100644
--- a/flang/unittests/Runtime/CMakeLists.txt
+++ b/flang/unittests/Runtime/CMakeLists.txt
@@ -33,7 +33,7 @@ add_flang_unittest(FlangRuntimeTests
 
 target_link_libraries(FlangRuntimeTests
   PRIVATE
-  flang_rt
+  flang_rt.runtime
 )
 
 target_compile_definitions(FlangRuntimeTests PRIVATE NOT_EXE="$<TARGET_FILE:not>")
diff --git a/flang/unittests/Runtime/CUDA/CMakeLists.txt b/flang/unittests/Runtime/CUDA/CMakeLists.txt
index 762df4b46010776..6901da3920a4612 100644
--- a/flang/unittests/Runtime/CUDA/CMakeLists.txt
+++ b/flang/unittests/Runtime/CUDA/CMakeLists.txt
@@ -15,8 +15,8 @@ endif()
 target_link_libraries(FlangCufRuntimeTests
   PRIVATE
   ${CUDA_RT_TARGET}
-  CufRuntime_cuda_${CUDAToolkit_VERSION_MAJOR}
-  flang_rt
+  flang_rt.cuda_${CUDAToolkit_VERSION_MAJOR}
+  flang_rt.runtime
 )
 
 target_include_directories(FlangCufRuntimeTests PRIVATE ${CUDAToolkit_INCLUDE_DIRS})
diff --git a/lld/COFF/MinGW.cpp b/lld/COFF/MinGW.cpp
index e188dfb503c1aa5..baaf06fa021a2c2 100644
--- a/lld/COFF/MinGW.cpp
+++ b/lld/COFF/MinGW.cpp
@@ -50,7 +50,7 @@ AutoExporter::AutoExporter(
       "libclang_rt.profile-x86_64",
       "libc++",
       "libc++abi",
-      "libflang_rt",
+      "libflang_rt.runtime",
       "libunwind",
       "libmsvcrt",
       "libucrtbase",

>From f97c774ef20851e15d3f1cc0d2892ff15896c7d3 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Mon, 3 Feb 2025 23:12:11 +0100
Subject: [PATCH 6/9] build fixes

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp    |   2 +-
 flang-rt/cmake/modules/AddFlangRT.cmake       |   6 +-
 .../cmake/modules/AddFlangRTOffload.cmake     |   3 +-
 .../ExternalHelloWorld/CMakeLists.txt         |   2 +-
 flang-rt/include/flang-rt/runtime/tools.h     |   2 +-
 flang-rt/lib/quadmath/CMakeLists.txt          |   4 +-
 flang-rt/lib/quadmath/complex-math.c          |   2 +-
 flang-rt/lib/quadmath/complex-math.h          |   2 +-
 flang-rt/lib/runtime/CMakeLists.txt           |   4 +-
 flang-rt/test/Runtime/no-cpp-dep.c            |   4 +-
 flang-rt/unittests/CMakeLists.txt             |   6 -
 flang-rt/unittests/Evaluate/CMakeLists.txt    |   4 +-
 flang-rt/unittests/Runtime/CMakeLists.txt     |   2 +-
 flang/cmake/modules/AddFlang.cmake            |   2 +-
 flang/docs/GettingStarted.md                  |   2 +-
 flang/docs/Real16MathSupport.md               |   4 +-
 flang/docs/ReleaseNotes.md                    |  17 ++-
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp |   4 +-
 flang/runtime/CMakeLists.txt                  |  15 ++-
 flang/runtime/Float128Math/CMakeLists.txt     | 115 +++++++++---------
 flang/test/Driver/linker-flags.f90            |  20 +--
 21 files changed, 113 insertions(+), 109 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index dcfa80602a04214..a213a055956cecc 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1330,7 +1330,7 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
     F128LibName.consume_front_insensitive("lib");
     if (!F128LibName.empty()) {
       bool AsNeeded = !TC.getTriple().isOSAIX();
-      CmdArgs.push_back("-lFortranFloat128Math");
+      CmdArgs.push_back("-lflang_rt.quadmath");
       if (AsNeeded)
         addAsNeededOption(TC, Args, CmdArgs, /*as_needed=*/true);
       CmdArgs.push_back(Args.MakeArgString("-l" + F128LibName));
diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index aa8adedf61752ad..dc96d2b5f0a1596 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -6,7 +6,7 @@
 #
 #===------------------------------------------------------------------------===#
 
-# Builds a library with common options for flang-rt.
+# Builds a library with common options for Flang-RT.
 #
 # Usage:
 #
@@ -108,11 +108,11 @@ function (add_flangrt_library name)
   endif ()
 
   # Flang-RT's public headers
-  target_include_directories(${name} PRIVATE "${FLANG_RT_SOURCE_DIR}/include")
+  target_include_directories(${name} PUBLIC "${FLANG_RT_SOURCE_DIR}/include")
 
   # For ISO_Fortran_binding.h to be found by the runtime itself (Accessed as #include "flang/ISO_Fortran_binding.h")
   # User applications can use #include <ISO_Fortran_binding.h>
-  target_include_directories(${name} PRIVATE "${FLANG_SOURCE_DIR}/include")
+  target_include_directories(${name} PUBLIC "${FLANG_SOURCE_DIR}/include")
 
   # For Flang-RT's configured config.h to be found
   target_include_directories(${name} PRIVATE "${FLANG_RT_BINARY_DIR}")
diff --git a/flang-rt/cmake/modules/AddFlangRTOffload.cmake b/flang-rt/cmake/modules/AddFlangRTOffload.cmake
index df84ed8f4c8d242..4e4bd60c6354590 100644
--- a/flang-rt/cmake/modules/AddFlangRTOffload.cmake
+++ b/flang-rt/cmake/modules/AddFlangRTOffload.cmake
@@ -34,8 +34,7 @@ macro(enable_cuda_compilation name files)
         )
     endif()
     set_source_files_properties(${files} PROPERTIES COMPILE_OPTIONS
-      "${CUDA_COMPILE_OPTIONS}"
-      )
+      "${CUDA_COMPILE_OPTIONS}")
 
     # Create a .a library consisting of CUDA PTX.
     # This is different from a regular static library. The CUDA_PTX_COMPILATION
diff --git a/flang-rt/examples/ExternalHelloWorld/CMakeLists.txt b/flang-rt/examples/ExternalHelloWorld/CMakeLists.txt
index e9f347f18cc437f..4fd04f8f2769aed 100644
--- a/flang-rt/examples/ExternalHelloWorld/CMakeLists.txt
+++ b/flang-rt/examples/ExternalHelloWorld/CMakeLists.txt
@@ -13,5 +13,5 @@ add_llvm_example(external-hello-world
 
 target_link_libraries(external-hello-world
   PRIVATE
-  flang_rt.runtime
+    flang_rt.runtime
   )
diff --git a/flang-rt/include/flang-rt/runtime/tools.h b/flang-rt/include/flang-rt/runtime/tools.h
index 792aec7710a15b1..91a026bf2ac146f 100644
--- a/flang-rt/include/flang-rt/runtime/tools.h
+++ b/flang-rt/include/flang-rt/runtime/tools.h
@@ -348,7 +348,7 @@ inline RT_API_ATTRS RESULT ApplyFloatingPointKind(
     if constexpr (HasCppTypeFor<TypeCategory::Real, 16>) {
       // If FUNC implemenation relies on FP math functions,
       // then we should not be here. The compiler should have
-      // generated a call to an entry in FortranFloat128Math
+      // generated a call to an entry in the libflang_rt.quadmath
       // library.
       if constexpr (!NEEDSMATH) {
         return FUNC<16>{}(std::forward<A>(x)...);
diff --git a/flang-rt/lib/quadmath/CMakeLists.txt b/flang-rt/lib/quadmath/CMakeLists.txt
index 2eccf23e3d16677..4f113216b42c86a 100644
--- a/flang-rt/lib/quadmath/CMakeLists.txt
+++ b/flang-rt/lib/quadmath/CMakeLists.txt
@@ -12,7 +12,7 @@
 # It is distributed as a static library only.
 # Fortran programs/libraries that end up linking any of the provided
 # will have a dependency on the third-party library that is being
-# used for building this FortranFloat128Math library.
+# used for building this libflang_rt.quadmath library.
 
 include(CheckLibraryExists)
 include(CheckIncludeFile)
@@ -95,7 +95,7 @@ if (FLANG_RUNTIME_F128_MATH_LIB)
   endif()
 
   if (WIN32)
-    # Do not create a FortranFloat128Math library under Windows, the Flang
+    # Do not create a flang_rt.quadmath library under Windows, the Flang
     # driver never links it. Instead, add the sources to flang_rt.runtime.
     target_sources(FortranFloat128MathILib INTERFACE ${sources})
     target_compile_definitions(FortranFloat128MathILib INTERFACE HAS_QUADMATHLIB)
diff --git a/flang-rt/lib/quadmath/complex-math.c b/flang-rt/lib/quadmath/complex-math.c
index fed37dfce6332a5..e485fd7eb5dbbc9 100644
--- a/flang-rt/lib/quadmath/complex-math.c
+++ b/flang-rt/lib/quadmath/complex-math.c
@@ -1,4 +1,4 @@
-/*===-- lib/FortranFloat128Math/complex-math.c ----------------------*- C -*-===
+/*===-- lib/quadmath/complex-math.c ---------------------------------*- C -*-===
  *
  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  * See https://llvm.org/LICENSE.txt for license information.
diff --git a/flang-rt/lib/quadmath/complex-math.h b/flang-rt/lib/quadmath/complex-math.h
index d0c53935b30c39c..424ed84da4e01f0 100644
--- a/flang-rt/lib/quadmath/complex-math.h
+++ b/flang-rt/lib/quadmath/complex-math.h
@@ -59,4 +59,4 @@
 #error "Float128Math build with glibc>=2.26 is unsupported yet"
 #endif
 
-#endif // FLANG_RT_QUADMATH_COMPLEX_MATH_H_
+#endif /* FLANG_RT_QUADMATH_COMPLEX_MATH_H_ */
diff --git a/flang-rt/lib/runtime/CMakeLists.txt b/flang-rt/lib/runtime/CMakeLists.txt
index 1fcac510a507b6d..6340dee2c5c07c2 100644
--- a/flang-rt/lib/runtime/CMakeLists.txt
+++ b/flang-rt/lib/runtime/CMakeLists.txt
@@ -99,7 +99,7 @@ file(GLOB_RECURSE private_headers
   )
 
 
-# Import changes from sibling FortranFloat128Math
+# Import changes from flang_rt.quadmath
 get_target_property(f128_sources
   FortranFloat128MathILib INTERFACE_SOURCES
   )
@@ -170,7 +170,7 @@ else()
 
     enable_cuda_compilation(${name} "${supported_sources}")
     enable_omp_offload_compilation(${name} "${supported_sources}")
-    add_dependencies(flang_rt ${name})
+    add_dependencies(flang_rt.runtime ${name})
   endfunction ()
 
   # Variants of the static flang_rt for different versions of the msvc runtime.
diff --git a/flang-rt/test/Runtime/no-cpp-dep.c b/flang-rt/test/Runtime/no-cpp-dep.c
index 2a416bdb27ee221..c98678b69aa1473 100644
--- a/flang-rt/test/Runtime/no-cpp-dep.c
+++ b/flang-rt/test/Runtime/no-cpp-dep.c
@@ -1,13 +1,13 @@
 /*
 This test makes sure that flang's runtime does not depend on the C++ runtime
-library. It tries to link this simple file against libflang_rt.a with
+library. It tries to link this simple file against libflang_rt.runtime.a with
 a C compiler.
 
 UNSUPPORTED: system-windows
 UNSUPPORTED: offload-cuda
 
 RUN: %if system-aix %{ export OBJECT_MODE=64 %}
-RUN: %cc -std=c99 %s -I%include -L"%libdir" -lflang_rt -lm \
+RUN: %cc -std=c99 %s -I%include -L"%libdir" -lflang_rt.runtime -lm \
 RUN: %if system-aix %{-lpthread %}
 RUN: rm a.out
 */
diff --git a/flang-rt/unittests/CMakeLists.txt b/flang-rt/unittests/CMakeLists.txt
index ca94805f1403bcd..e2a50d8b40a9d77 100644
--- a/flang-rt/unittests/CMakeLists.txt
+++ b/flang-rt/unittests/CMakeLists.txt
@@ -64,8 +64,6 @@ function(add_flangrt_unittest test_dirname)
 
   add_unittest(FlangRTUnitTests ${test_dirname} ${ARG_UNPARSED_ARGUMENTS})
 
-  target_include_directories(${test_dirname} PRIVATE "${FLANG_RT_SOURCE_DIR}/include")
-  target_include_directories(${test_dirname} PRIVATE "${FLANG_SOURCE_DIR}/include")
   target_link_libraries(${test_dirname} PRIVATE ${ARG_LINK_LIBS})
   add_flangrt_unittest_offload_properties(${test_dirname})
 
@@ -94,10 +92,6 @@ function(add_flangrt_nongtest_unittest test_name)
   add_executable(${test_name}${suffix} EXCLUDE_FROM_ALL ${ARG_UNPARSED_ARGUMENTS})
   set_target_properties(${test_name}${suffix} PROPERTIES FOLDER "Flang-RT/Tests/Unit")
 
-  target_include_directories(${test_name}${suffix} PRIVATE
-      "${FLANG_RT_SOURCE_DIR}/include"
-      "${FLANG_SOURCE_DIR}/include"
-    )
   target_link_libraries(${test_name}${suffix} PRIVATE NonGTestTesting ${ARG_LINK_LIBS})
 
   if(NOT ARG_SLOW_TEST)
diff --git a/flang-rt/unittests/Evaluate/CMakeLists.txt b/flang-rt/unittests/Evaluate/CMakeLists.txt
index 15b813bcb19da1c..526ec234d57da6d 100644
--- a/flang-rt/unittests/Evaluate/CMakeLists.txt
+++ b/flang-rt/unittests/Evaluate/CMakeLists.txt
@@ -10,12 +10,12 @@ add_flangrt_nongtest_unittest(reshape
   reshape.cpp
 
   LINK_LIBS
-    flang_rt.unittest
+    flang_rt.runtime.unittest
 )
 
 add_flangrt_nongtest_unittest(ISO-Fortran-binding
   ISO-Fortran-binding.cpp
 
   LINK_LIBS
-    flang_rt.unittest
+    flang_rt.runtime.unittest
 )
diff --git a/flang-rt/unittests/Runtime/CMakeLists.txt b/flang-rt/unittests/Runtime/CMakeLists.txt
index d647e690c06805b..61d0aba93b14b6a 100644
--- a/flang-rt/unittests/Runtime/CMakeLists.txt
+++ b/flang-rt/unittests/Runtime/CMakeLists.txt
@@ -39,7 +39,7 @@ add_flangrt_unittest(RuntimeTests
   Transformational.cpp
 
   LINK_LIBS
-    flang_rt.unittest
+    flang_rt.runtime.unittest
 )
 target_compile_definitions(RuntimeTests PRIVATE NOT_EXE="${LLVM_TOOLS_DIR}/not${CMAKE_EXECUTABLE_SUFFIX}")
 
diff --git a/flang/cmake/modules/AddFlang.cmake b/flang/cmake/modules/AddFlang.cmake
index 1f178772067edc7..c9f65eb73fef00a 100644
--- a/flang/cmake/modules/AddFlang.cmake
+++ b/flang/cmake/modules/AddFlang.cmake
@@ -57,7 +57,7 @@ function(add_flang_library name)
     set(LIBTYPE SHARED)
   elseif(ARG_STATIC)
     # If BUILD_SHARED_LIBS and ARG_STATIC are both set, llvm_add_library prioritizes STATIC.
-    # This is required behavior for libFortranFloat128Math.
+    # This is required behavior for libflang_rt.quadmath.
     set(LIBTYPE STATIC)
   else()
     # Let llvm_add_library decide, taking BUILD_SHARED_LIBS into account.
diff --git a/flang/docs/GettingStarted.md b/flang/docs/GettingStarted.md
index 0523b4596615135..0b3b551ffbfba72 100644
--- a/flang/docs/GettingStarted.md
+++ b/flang/docs/GettingStarted.md
@@ -317,7 +317,7 @@ The same set of CMake variables works for Flang in-tree build.
 One may provide optional CMake variables to customize the build. Available options:
 
 * `-DFLANG_RUNTIME_F128_MATH_LIB=libquadmath`: enables build of
-  `FortranFloat128Math` library that provides `REAL(16)` math APIs
+  `flang_rt.quadmath` library that provides `REAL(16)` math APIs
   for intrinsics such as `SIN`, `COS`, etc. GCC `libquadmath`'s header file
   `quadmath.h` must be available to the build compiler.
   [More details](Real16MathSupport.md).
diff --git a/flang/docs/Real16MathSupport.md b/flang/docs/Real16MathSupport.md
index 21482c7be21af6d..93492c8b767c394 100644
--- a/flang/docs/Real16MathSupport.md
+++ b/flang/docs/Real16MathSupport.md
@@ -12,9 +12,9 @@ To support most `REAL(16)` (i.e. 128-bit float) math intrinsics Flang relies
 on third-party libraries providing the implementation.
 
 `-DFLANG_RUNTIME_F128_MATH_LIB=libquadmath` CMake option can be used
-to build `FortranFloat128Math` library that has unresolved references
+to build `libflang_rt.quadmath` library that has unresolved references
 to GCC `libquadmath` library. A Flang driver built with this option
-will automatically link `FortranFloat128Math` and `libquadmath` libraries
+will automatically link `libflang_rt.quadmath` and `libquadmath` libraries
 to any Fortran program. This implies that `libquadmath` library
 has to be available in the standard library paths, so that linker
 can find it. The `libquadmath` library installation into Flang project
diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md
index 689f5bf58684f67..9396d956e223376 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -36,13 +36,18 @@ page](https://llvm.org/releases/).
 
 ## Build System Changes
 
+ * The FortranRuntime library has been renamed to `flang_rt.runtime`.
+
+ * The FortranFloat128Math library has been renamed to `flang_rt.quadmath`.
+
+ * The CufRuntime_cuda_${version} library has been renamed to
+   `flang_rt.cuda_${version}`.
+
  * The Fortran Runtime library has been move to a new top-level directory
-   named "flang-rt". The library was also renamed from `FortranRuntime` to
-   `flang_rt.runtime`. It now supports the
-   LLVM_ENABLE_RUNTIMES mechanism to build Flang-RT for multiple target
-   triples. libflang_rt.runtime.a will now be emitted into Clang's per-target
-   resource directory (next to libclang_rt.*.*) where it is also found by
-   Flang's driver.
+   named "flang-rt". It now supports the LLVM_ENABLE_RUNTIMES mechanism to
+   build Flang-RT for multiple target triples. libflang_rt.runtime.{a|so} will
+   now be emitted into Clang's per-target resource directory
+   (next to libclang_rt.*.*) where it is also found by Flang's driver.
 
 ## New Issues Found
 
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index ea78103d78239d9..d7d1471535db8d9 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -808,8 +808,8 @@ prettyPrintIntrinsicName(fir::FirOpBuilder &builder, mlir::Location loc,
 // Generate a call to the Fortran runtime library providing
 // support for 128-bit float math.
 // On 'HAS_LDBL128' targets the implementation
-// is provided by flang_rt, otherwise, it is done via
-// FortranFloat128Math library. In the latter case the compiler
+// is provided by flang_rt, otherwise, it is done via the
+// libflang_rt.quadmath library. In the latter case the compiler
 // has to be built with FLANG_RUNTIME_F128_MATH_LIB to guarantee
 // proper linking actions in the driver.
 static mlir::Value genLibF128Call(fir::FirOpBuilder &builder,
diff --git a/flang/runtime/CMakeLists.txt b/flang/runtime/CMakeLists.txt
index 4e547a200a417dd..04e120cc5d37a18 100644
--- a/flang/runtime/CMakeLists.txt
+++ b/flang/runtime/CMakeLists.txt
@@ -259,14 +259,14 @@ set(supported_files
   unit.cpp
   utf.cpp
   )
-
 runtime_source_files(supported_files SUBDIR "runtime")
+
 enable_cuda_compilation(flang_rt "${supported_files}")
 enable_omp_offload_compilation("${supported_files}")
 
-if (NOT TARGET FortranFloat128Math)
-  # If FortranFloat128Math is not defined, then we are not building
-  # standalone FortranFloat128Math library. Instead, include
+if (NOT TARGET flang_rt.quadmath)
+  # If flang_rt.quadmath is not defined, then we are not building
+  # standalone flang_rt.quadmath library. Instead, include
   # the relevant sources into flang_rt itself.
   # The information is provided via FortranFloat128MathILib
   # interface library.
@@ -293,7 +293,6 @@ if (NOT TARGET FortranFloat128Math)
     list(APPEND sources ${f128_sources})
   endif()
 endif()
-
 runtime_source_files(sources SUBDIR "runtime")
 
 
@@ -349,5 +348,11 @@ endif()
 # Compatibility targets.
 add_custom_target(flang-rt)
 add_dependencies(flang-rt flang_rt.runtime)
+if (TARGET flang_rt.quadmath)
+  add_dependencies(flang-rt flang_rt.quadmath)
+endif ()
+if (TARGET flang_rt.cuda_${CUDAToolkit_VERSION_MAJOR})
+  add_dependencies(flang-rt flang_rt.cuda_${CUDAToolkit_VERSION_MAJOR})
+endif ()
 add_custom_target(FortranRuntime)
 add_dependencies(FortranRuntime flang_rt.runtime)
diff --git a/flang/runtime/Float128Math/CMakeLists.txt b/flang/runtime/Float128Math/CMakeLists.txt
index 2cd7e46c80407d2..cdac5f893cf9170 100644
--- a/flang/runtime/Float128Math/CMakeLists.txt
+++ b/flang/runtime/Float128Math/CMakeLists.txt
@@ -12,62 +12,63 @@
 # It is distributed as a static library only.
 # Fortran programs/libraries that end up linking any of the provided
 # will have a dependency on the third-party library that is being
-# used for building this FortranFloat128Math library.
+# used for building this flang_rt.quadmath library.
 
 include(CheckLibraryExists)
 
 set(sources
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/acos.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/acosh.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/asin.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/asinh.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/atan.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/atan2.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/atanh.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/ceil.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/complex-math.c
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/cos.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/cosh.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/erf.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/erfc.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/exp.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/exponent.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/floor.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/fma.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/fraction.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/hypot.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/j0.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/j1.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/jn.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/lgamma.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/llround.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/log.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/log10.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/lround.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/mod-real.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/modulo-real.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/nearest.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/nearbyint.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/norm2.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/pow.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/random.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/remainder.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/round.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/rrspacing.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/scale.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/set-exponent.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/sin.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/sinh.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/spacing.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/sqrt.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/tan.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/tanh.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/tgamma.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/trunc.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/y0.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/y1.cpp
-  ${FLANG_RT_SOURCE_DIR}/lib/FortranFloat128Math/yn.cpp
+  acos.cpp
+  acosh.cpp
+  asin.cpp
+  asinh.cpp
+  atan.cpp
+  atan2.cpp
+  atanh.cpp
+  ceil.cpp
+  complex-math.c
+  cos.cpp
+  cosh.cpp
+  erf.cpp
+  erfc.cpp
+  exp.cpp
+  exponent.cpp
+  floor.cpp
+  fma.cpp
+  fraction.cpp
+  hypot.cpp
+  j0.cpp
+  j1.cpp
+  jn.cpp
+  lgamma.cpp
+  llround.cpp
+  log.cpp
+  log10.cpp
+  lround.cpp
+  mod-real.cpp
+  modulo-real.cpp
+  nearest.cpp
+  nearbyint.cpp
+  norm2.cpp
+  pow.cpp
+  random.cpp
+  remainder.cpp
+  round.cpp
+  rrspacing.cpp
+  scale.cpp
+  set-exponent.cpp
+  sin.cpp
+  sinh.cpp
+  spacing.cpp
+  sqrt.cpp
+  tan.cpp
+  tanh.cpp
+  tgamma.cpp
+  trunc.cpp
+  y0.cpp
+  y1.cpp
+  yn.cpp
   )
+runtime_source_files(sources SUBDIR "quadmath")
 
 include_directories(AFTER "${CMAKE_CURRENT_SOURCE_DIR}/..")
 add_library(FortranFloat128MathILib INTERFACE)
@@ -93,20 +94,20 @@ if (FLANG_RUNTIME_F128_MATH_LIB)
       )
   endif()
 
-  add_flang_library(FortranFloat128Math STATIC INSTALL_WITH_TOOLCHAIN
+  add_flang_library(flang_rt.quadmath STATIC INSTALL_WITH_TOOLCHAIN
     ${sources})
 
   if (DEFINED MSVC)
     set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
-    add_flang_library(FortranFloat128Math.static STATIC INSTALL_WITH_TOOLCHAIN
+    add_flang_library(flang_rt.quadmath.static STATIC INSTALL_WITH_TOOLCHAIN
       ${sources}
       )
     set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
-    add_flang_library(FortranFloat128Math.static_dbg STATIC INSTALL_WITH_TOOLCHAIN
+    add_flang_library(flang_rt.quadmath.static_dbg STATIC INSTALL_WITH_TOOLCHAIN
       ${sources}
       )
-    add_dependencies(FortranFloat128Math FortranFloat128Math.static
-      FortranFloat128Math.static_dbg
+    add_dependencies(flang_rt.quadmath flang_rt.quadmath.static
+      flang_rt.quadmath.static_dbg
       )
   endif()
 elseif (HAVE_LDBL_MANT_DIG_113)
@@ -118,7 +119,7 @@ elseif (HAVE_LDBL_MANT_DIG_113)
       )
     target_sources(FortranFloat128MathILib INTERFACE ${sources})
   else()
-    message(FATAL_ERROR "flang_rt cannot build without libm")
+    message(FATAL_ERROR "flang_rt.quadmath cannot build without libm")
   endif()
 else()
   # We can use '__float128' version from libc, if it has them.
diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index 2e9dadbfb671b76..0b6eff80432476d 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -29,29 +29,29 @@
 !       executable and may find the GNU linker from MinGW or Cygwin.
 ! UNIX-LABEL:  "{{.*}}ld{{(\.exe)?}}"
 ! UNIX-SAME: "[[object_file]]"
-! UNIX-F128NONE-NOT: FortranFloat128Math
-! SOLARIS-F128NONE-NOT: FortranFloat128Math
-! UNIX-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "--as-needed" "-lquadmath" "--no-as-needed"
-! SOLARIS-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "-z" "ignore" "-lquadmath" "-z" "record"
+! UNIX-F128NONE-NOT: libflang_rt.quadmath
+! SOLARIS-F128NONE-NOT: libflang_rt.quadmath
+! UNIX-F128LIBQUADMATH-SAME: "-lflang_rt.quadmath" "--as-needed" "-lquadmath" "--no-as-needed"
+! SOLARIS-F128LIBQUADMATH-SAME: "-lflang_rt.quadmath" "-z" "ignore" "-lquadmath" "-z" "record"
 ! UNIX-SAME: "-lflang_rt.runtime" "-lm"
 ! COMPILER-RT: "{{.*}}{{\\|/}}libclang_rt.builtins.a"
 
 ! DARWIN-LABEL:  "{{.*}}ld{{(\.exe)?}}"
 ! DARWIN-SAME: "[[object_file]]"
-! DARWIN-F128NONE-NOT: FortranFloat128Math
-! DARWIN-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "--as-needed" "-lquadmath" "--no-as-needed"
+! DARWIN-F128NONE-NOT: libflang_rt.quadmath
+! DARWIN-F128LIBQUADMATH-SAME: "-lflang_rt.quadmath" "--as-needed" "-lquadmath" "--no-as-needed"
 ! DARWIN-SAME: -lflang_rt.runtime
 
 ! HAIKU-LABEL:  "{{.*}}ld{{(\.exe)?}}"
 ! HAIKU-SAME: "[[object_file]]"
-! HAIKU-F128NONE-NOT: FortranFloat128Math
-! HAIKU-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "--as-needed" "-lquadmath" "--no-as-needed"
+! HAIKU-F128NONE-NOT: libflang_rt.quadmath
+! HAIKU-F128LIBQUADMATH-SAME: "-lflang_rt.quadmath" "--as-needed" "-lquadmath" "--no-as-needed"
 ! HAIKU-SAME: "-lflang_rt.runtime"
 
 ! MINGW-LABEL:  "{{.*}}ld{{(\.exe)?}}"
 ! MINGW-SAME: "[[object_file]]"
-! MINGW-F128NONE-NOT: FortranFloat128Math
-! MINGW-F128LIBQUADMATH-SAME: "-lFortranFloat128Math" "--as-needed" "-lquadmath" "--no-as-needed"
+! MINGW-F128NONE-NOT: libflang_rt.quadmath
+! MINGW-F128LIBQUADMATH-SAME: "-lflang_rt.quadmath" "--as-needed" "-lquadmath" "--no-as-needed"
 ! MINGW-SAME: -lflang_rt.runtime
 
 ! NOTE: This also matches lld-link (when CLANG_DEFAULT_LINKER=lld) and

>From 0282bd6d23585b435cdbec1b37fd99c136ee795b Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Mon, 3 Feb 2025 23:47:53 +0100
Subject: [PATCH 7/9] Avoid change noise

---
 flang/module/iso_fortran_env_impl.f90 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/module/iso_fortran_env_impl.f90 b/flang/module/iso_fortran_env_impl.f90
index 3ee00acfde4d7ce..5408e7de3700116 100644
--- a/flang/module/iso_fortran_env_impl.f90
+++ b/flang/module/iso_fortran_env_impl.f90
@@ -8,7 +8,7 @@
 
 ! This MODULE implements part of the ISO_FORTRAN_ENV module file, which
 ! partially requires linkable symbols for some entities defined
-! (e.g., real_kinds). This file is also used by Flang-RT.
+! (e.g., real_kinds).
 
 module iso_fortran_env_impl
   implicit none

>From d0cf50915755f0200b5c0169eea1b5ee41642aa0 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Tue, 4 Feb 2025 02:57:48 +0100
Subject: [PATCH 8/9] Post-merge fixes

---
 flang-rt/CMakeLists.txt                 |  6 ++++++
 flang-rt/cmake/config.h.cmake.in        |  5 +++++
 flang-rt/cmake/modules/AddFlangRT.cmake | 10 ++++++++--
 flang-rt/lib/runtime/CMakeLists.txt     |  4 +++-
 4 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt
index 37f49ebc0d7bb74..8060ed84eb8406b 100644
--- a/flang-rt/CMakeLists.txt
+++ b/flang-rt/CMakeLists.txt
@@ -205,6 +205,12 @@ endif ()
 check_cxx_compiler_flag("-UTESTFLAG" FLANG_RT_SUPPORTS_UNDEFINE_FLAG)
 
 
+# function checks
+find_package(Backtrace)
+set(HAVE_BACKTRACE ${Backtrace_FOUND})
+set(BACKTRACE_HEADER ${Backtrace_HEADER})
+
+
 #####################
 # Build Preparation #
 #####################
diff --git a/flang-rt/cmake/config.h.cmake.in b/flang-rt/cmake/config.h.cmake.in
index 8a4668b90addda8..84c234d86bae677 100644
--- a/flang-rt/cmake/config.h.cmake.in
+++ b/flang-rt/cmake/config.h.cmake.in
@@ -16,4 +16,9 @@
    don't. */
 #cmakedefine01 HAVE_DECL_STRERROR_S
 
+/* Define to 1 if you have the `backtrace' function. */
+#cmakedefine HAVE_BACKTRACE ${HAVE_BACKTRACE}
+
+#define BACKTRACE_HEADER <${BACKTRACE_HEADER}>
+
 #endif
diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index dc96d2b5f0a1596..d78dacb0f6d537c 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -28,13 +28,15 @@
 #   ADDITIONAL_HEADERS
 #     May specify header files for IDE generators.
 #   INCLUDE_DIRECTORIES
-#     Additional include_directories for all added targets
+#     Additional target_include_directories for all added targets
+#   LINK_LIBRARIES
+#     Additional target_link_libraries for all added targets
 #   TARGET_PROPERTIES
 #     Set target properties of all added targets
 # )
 function (add_flangrt_library name)
   set(options STATIC SHARED OBJECT INSTALL_WITH_TOOLCHAIN EXCLUDE_FROM_ALL LINK_TO_LLVM)
-  set(multiValueArgs ADDITIONAL_HEADERS INCLUDE_DIRECTORIES TARGET_PROPERTIES)
+  set(multiValueArgs ADDITIONAL_HEADERS INCLUDE_DIRECTORIES LINK_LIBRARIES TARGET_PROPERTIES)
   cmake_parse_arguments(ARG
     "${options}"
     ""
@@ -160,6 +162,10 @@ function (add_flangrt_library name)
     target_include_directories(${name} ${ARG_INCLUDE_DIRECTORIES})
   endif ()
 
+  if (ARG_LINK_LIBRARIES)
+    target_link_libraries(${name} PUBLIC ${ARG_LINK_LIBRARIES})
+  endif ()
+
   # If this is part of the toolchain, put it into the compiler's resource
   # directory. Otherwise it is part of testing and is not installed at all.
   # TODO: Consider multi-configuration builds (MSVC_IDE, "Ninja Multi-Config")
diff --git a/flang-rt/lib/runtime/CMakeLists.txt b/flang-rt/lib/runtime/CMakeLists.txt
index 6340dee2c5c07c2..0afcbf278353337 100644
--- a/flang-rt/lib/runtime/CMakeLists.txt
+++ b/flang-rt/lib/runtime/CMakeLists.txt
@@ -124,12 +124,13 @@ else ()
   set(f128_sources "")
 endif ()
 
-
 set(sources ${supported_sources} ${host_sources} ${f128_sources})
 
+
 if (NOT WIN32)
   add_flangrt_library(flang_rt.runtime STATIC
     ${sources}
+    LINK_LIBRARIES ${Backtrace_LIBRARY}
     INSTALL_WITH_TOOLCHAIN
     ADDITIONAL_HEADERS ${public_headers} ${private_headers}
   )
@@ -151,6 +152,7 @@ else()
     add_flangrt_library(${name} ${libtype}
         ${sources}
         ${ARGN}
+        LINK_LIBRARIES ${Backtrace_LIBRARY}
         ADDITIONAL_HEADERS ${public_headers} ${private_headers}
       )
 

>From e3098d3f1699ada527a61f38049a6fd5d55789da Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Fri, 7 Feb 2025 13:00:39 +0100
Subject: [PATCH 9/9] [Flang] Optionally do not compile the runtime in-tree

Introduce the CMake switch FLANG_INCLUDE_RUNTIME. When set to off, do not add build instructions for the runtime.

This is required for Flang-RT (#110217) and the current runtime CMake code to co-exist. When using `LLVM_ENABLE_RUNTIME=flang-rt`, the in-tree build instructions are in conflict and must be disabled.
---
 flang/CMakeLists.txt                    |  6 +++++-
 flang/examples/CMakeLists.txt           |  4 +++-
 flang/test/CMakeLists.txt               |  6 +++++-
 flang/test/Driver/ctofortran.f90        |  1 +
 flang/test/Driver/exec.f90              |  1 +
 flang/test/Runtime/no-cpp-dep.c         |  2 +-
 flang/test/lit.cfg.py                   |  5 ++++-
 flang/test/lit.site.cfg.py.in           |  2 ++
 flang/tools/f18/CMakeLists.txt          |  2 +-
 flang/unittests/CMakeLists.txt          | 11 +++++++++-
 flang/unittests/Evaluate/CMakeLists.txt | 27 +++++++++++++------------
 11 files changed, 47 insertions(+), 20 deletions(-)

diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index 2e27bc2279ac479..e6de8df5cef1505 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -247,6 +247,8 @@ else()
   include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
 endif()
 
+option(FLANG_INCLUDE_RUNTIME "Build the runtime in-tree (deprecated; to be replaced with LLVM_ENABLE_RUNTIMES=flang-rt)" ON)
+
 set(FLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
     "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
 mark_as_advanced(FLANG_TOOLS_INSTALL_DIR)
@@ -487,7 +489,9 @@ if (FLANG_BUILD_TOOLS)
   add_subdirectory(tools)
 endif()
 
-add_subdirectory(runtime)
+if (FLANG_INCLUDE_RUNTIME)
+  add_subdirectory(runtime)
+endif ()
 
 if (LLVM_INCLUDE_EXAMPLES)
   add_subdirectory(examples)
diff --git a/flang/examples/CMakeLists.txt b/flang/examples/CMakeLists.txt
index 8cc66ddbbbb0ef4..23c6e790791fba2 100644
--- a/flang/examples/CMakeLists.txt
+++ b/flang/examples/CMakeLists.txt
@@ -1,4 +1,6 @@
-add_subdirectory(ExternalHelloWorld)
+if (FLANG_INCLUDE_RUNTIME)
+  add_subdirectory(ExternalHelloWorld)
+endif ()
 add_subdirectory(PrintFlangFunctionNames)
 add_subdirectory(FlangOmpReport)
 add_subdirectory(FeatureList)
diff --git a/flang/test/CMakeLists.txt b/flang/test/CMakeLists.txt
index cab214c2ef4c8c1..e398e0786147aa7 100644
--- a/flang/test/CMakeLists.txt
+++ b/flang/test/CMakeLists.txt
@@ -71,9 +71,13 @@ set(FLANG_TEST_DEPENDS
   llvm-objdump
   llvm-readobj
   split-file
-  FortranRuntime
   FortranDecimal
 )
+
+if (FLANG_INCLUDE_RUNTIME)
+  list(APPEND FLANG_TEST_DEPENDS FortranRuntime)
+endif ()
+
 if (LLVM_ENABLE_PLUGINS AND NOT WIN32)
   list(APPEND FLANG_TEST_DEPENDS Bye)
 endif()
diff --git a/flang/test/Driver/ctofortran.f90 b/flang/test/Driver/ctofortran.f90
index 78eac32133b18e1..10c7adaccc9588b 100644
--- a/flang/test/Driver/ctofortran.f90
+++ b/flang/test/Driver/ctofortran.f90
@@ -1,4 +1,5 @@
 ! UNSUPPORTED: system-windows
+! REQUIRES: flang-rt
 ! RUN: split-file %s %t
 ! RUN: chmod +x %t/runtest.sh
 ! RUN: %t/runtest.sh %t %t/ffile.f90 %t/cfile.c %flang | FileCheck %s
diff --git a/flang/test/Driver/exec.f90 b/flang/test/Driver/exec.f90
index fd174005ddf62ac..9ca91ee24011c9f 100644
--- a/flang/test/Driver/exec.f90
+++ b/flang/test/Driver/exec.f90
@@ -1,4 +1,5 @@
 ! UNSUPPORTED: system-windows
+! REQUIRES: flang-rt
 ! Verify that flang can correctly build executables.
 
 ! RUN: %flang %s -o %t
diff --git a/flang/test/Runtime/no-cpp-dep.c b/flang/test/Runtime/no-cpp-dep.c
index b1a5fa004014cc8..7303ce63fdec410 100644
--- a/flang/test/Runtime/no-cpp-dep.c
+++ b/flang/test/Runtime/no-cpp-dep.c
@@ -3,7 +3,7 @@ This test makes sure that flang's runtime does not depend on the C++ runtime
 library. It tries to link this simple file against libFortranRuntime.a with
 a C compiler.
 
-REQUIRES: c-compiler
+REQUIRES: c-compiler, flang-rt
 
 RUN: %if system-aix %{ export OBJECT_MODE=64 %}
 RUN: %cc -std=c99 %s -I%include %libruntime -lm  \
diff --git a/flang/test/lit.cfg.py b/flang/test/lit.cfg.py
index c452b6d231c89f0..f4580afc8c47b12 100644
--- a/flang/test/lit.cfg.py
+++ b/flang/test/lit.cfg.py
@@ -163,10 +163,13 @@
         ToolSubst("%not_todo_abort_cmd", command=FindTool("not"), unresolved="fatal")
     )
 
+if config.flang_include_runtime:
+    config.available_features.add("flang-rt")
+
 # Define some variables to help us test that the flang runtime doesn't depend on
 # the C++ runtime libraries. For this we need a C compiler. If for some reason
 # we don't have one, we can just disable the test.
-if config.cc:
+if config.flang_include_runtime and config.cc:
     libruntime = os.path.join(config.flang_lib_dir, "libFortranRuntime.a")
     include = os.path.join(config.flang_src_dir, "include")
 
diff --git a/flang/test/lit.site.cfg.py.in b/flang/test/lit.site.cfg.py.in
index d1a0ac763cf8a06..697ba3fa797633d 100644
--- a/flang/test/lit.site.cfg.py.in
+++ b/flang/test/lit.site.cfg.py.in
@@ -1,6 +1,7 @@
 @LIT_SITE_CFG_IN_HEADER@
 
 import sys
+import lit.util
 
 config.llvm_tools_dir = lit_config.substitute("@LLVM_TOOLS_DIR@")
 config.llvm_shlib_dir = lit_config.substitute(path(r"@SHLIBDIR@"))
@@ -32,6 +33,7 @@ else:
     config.openmp_module_dir = None
 config.flang_runtime_f128_math_lib = "@FLANG_RUNTIME_F128_MATH_LIB@"
 config.have_ldbl_mant_dig_113 = "@HAVE_LDBL_MANT_DIG_113@"
+config.flang_include_runtime = lit.util.pythonize_bool("@FLANG_INCLUDE_RUNTIME@")
 
 import lit.llvm
 lit.llvm.initialize(lit_config, config)
diff --git a/flang/tools/f18/CMakeLists.txt b/flang/tools/f18/CMakeLists.txt
index cc2bc5b8eb5ce23..85ba2c74cdeb5c4 100644
--- a/flang/tools/f18/CMakeLists.txt
+++ b/flang/tools/f18/CMakeLists.txt
@@ -109,7 +109,7 @@ if (NOT CMAKE_CROSSCOMPILING)
     set(compile_with "-fsyntax-only")
     set(object_output "")
     set(include_in_link FALSE)
-    if(${filename} IN_LIST MODULES_WITH_IMPLEMENTATION)
+    if(${filename} IN_LIST MODULES_WITH_IMPLEMENTATION AND FLANG_INCLUDE_RUNTIME)
       set(object_output "${CMAKE_CURRENT_BINARY_DIR}/${filename}${CMAKE_CXX_OUTPUT_EXTENSION}")
       set(compile_with -c -o ${object_output})
       set(include_in_link TRUE)
diff --git a/flang/unittests/CMakeLists.txt b/flang/unittests/CMakeLists.txt
index 945067fed4f82d0..ecb7d68d8f72966 100644
--- a/flang/unittests/CMakeLists.txt
+++ b/flang/unittests/CMakeLists.txt
@@ -1,3 +1,5 @@
+include(AddFlangOffloadRuntime)
+
 if (FLANG_EXPERIMENTAL_CUDA_RUNTIME)
   # If Fortran runtime is built as CUDA library, the linking
   # of targets that link FortranRuntime must be done
@@ -11,6 +13,11 @@ add_custom_target(FlangUnitTests)
 set_target_properties(FlangUnitTests PROPERTIES FOLDER "Flang/Tests")
 
 function(add_flang_unittest_offload_properties target)
+  # Do not apply runtime properties if not even compiling the runtime.
+  if (NOT FLANG_INCLUDE_RUNTIME)
+    return ()
+  endif ()
+
   # Set CUDA_RESOLVE_DEVICE_SYMBOLS.
   if (FLANG_EXPERIMENTAL_CUDA_RUNTIME)
     set_target_properties(${target}
@@ -75,5 +82,7 @@ add_subdirectory(Optimizer)
 add_subdirectory(Common)
 add_subdirectory(Decimal)
 add_subdirectory(Evaluate)
-add_subdirectory(Runtime)
+if (FLANG_INCLUDE_RUNTIME)
+  add_subdirectory(Runtime)
+endif ()
 add_subdirectory(Frontend)
diff --git a/flang/unittests/Evaluate/CMakeLists.txt b/flang/unittests/Evaluate/CMakeLists.txt
index 8111ecd72cfc7d6..1c3fac29cd2982e 100644
--- a/flang/unittests/Evaluate/CMakeLists.txt
+++ b/flang/unittests/Evaluate/CMakeLists.txt
@@ -33,7 +33,6 @@ add_flang_nongtest_unittest(intrinsics
   FortranDecimal
   FortranSemantics
   FortranParser
-  FortranRuntime
 )
 
 add_flang_nongtest_unittest(logical
@@ -56,19 +55,21 @@ add_flang_nongtest_unittest(real
 )
 llvm_update_compile_flags(real.test)
 
-add_flang_nongtest_unittest(reshape
-  NonGTestTesting
-  FortranSemantics
-  FortranEvaluate
-  FortranRuntime
-)
+if (FLANG_INCLUDE_RUNTIME)
+  add_flang_nongtest_unittest(reshape
+    NonGTestTesting
+    FortranSemantics
+    FortranEvaluate
+    FortranRuntime
+  )
 
-add_flang_nongtest_unittest(ISO-Fortran-binding
-  NonGTestTesting
-  FortranEvaluate
-  FortranSemantics
-  FortranRuntime
-)
+  add_flang_nongtest_unittest(ISO-Fortran-binding
+    NonGTestTesting
+    FortranEvaluate
+    FortranSemantics
+    FortranRuntime
+  )
+endif ()
 
 add_flang_nongtest_unittest(folding
   FortranSupport



More information about the llvm-branch-commits mailing list