[flang] [llvm] This is a test-PR to find out why the Premerge-CI is failing (PR #127081)

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 13 08:12:32 PST 2025


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

>From 34df5c6a12357e468d2ee498e4223cba4f52199c Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Thu, 13 Feb 2025 16:42:42 +0100
Subject: [PATCH 1/2] This is a test-PR to find out why the Premerge-CI is
 failing

---
 flang-rt/.clang-format                        |  25 ++
 flang-rt/.clang-tidy                          |   2 +
 flang-rt/CMakeLists.txt                       | 261 ++++++++++++++++++
 flang-rt/CODE_OWNERS.TXT                      |  14 +
 flang-rt/LICENSE.TXT                          | 234 ++++++++++++++++
 flang-rt/README.md                            | 188 +++++++++++++
 .../cmake/config.h.cmake.in                   |   8 +
 flang-rt/cmake/modules/AddFlangRT.cmake       | 199 +++++++++++++
 .../cmake/modules/AddFlangRTOffload.cmake     | 100 +++++++
 flang-rt/cmake/modules/GetToolchainDirs.cmake | 125 +++++++++
 flang-rt/examples/CMakeLists.txt              |   9 +
 .../ExternalHelloWorld/CMakeLists.txt         |  17 ++
 .../ExternalHelloWorld/external-hello.cpp     |   8 +
 .../flang-rt/runtime}/allocator-registry.h    |   8 +-
 .../flang-rt/runtime}/array-constructor.h     |  10 +-
 .../include/flang-rt}/runtime/assign-impl.h   |   8 +-
 .../include/flang-rt}/runtime/buffer.h        |  10 +-
 .../include/flang-rt}/runtime/connection.h    |   8 +-
 .../include/flang-rt}/runtime/derived.h       |   8 +-
 .../include/flang-rt/runtime}/descriptor.h    |  12 +-
 .../include/flang-rt}/runtime/emit-encoded.h  |   8 +-
 .../include/flang-rt}/runtime/environment.h   |   8 +-
 .../include/flang-rt}/runtime/file.h          |  10 +-
 .../flang-rt}/runtime/format-implementation.h |   8 +-
 .../include/flang-rt}/runtime/format.h        |   8 +-
 .../include/flang-rt}/runtime/internal-unit.h |  10 +-
 .../include/flang-rt}/runtime/io-error.h      |  10 +-
 .../include/flang-rt}/runtime/io-stmt.h       |  10 +-
 .../include/flang-rt}/runtime/lock.h          |   8 +-
 .../include/flang-rt/runtime}/memory.h        |   8 +-
 .../include/flang-rt}/runtime/namelist.h      |   8 +-
 .../include/flang-rt}/runtime/non-tbp-dio.h   |   8 +-
 .../flang-rt}/runtime/numeric-templates.h     |   8 +-
 .../flang-rt}/runtime/random-templates.h      |  10 +-
 .../flang-rt}/runtime/reduction-templates.h   |  10 +-
 .../include/flang-rt}/runtime/stat.h          |   8 +-
 .../include/flang-rt}/runtime/terminator.h    |   8 +-
 .../include/flang-rt}/runtime/tools.h         |  14 +-
 .../include/flang-rt/runtime}/type-code.h     |   8 +-
 .../include/flang-rt}/runtime/type-info.h     |  10 +-
 .../include/flang-rt}/runtime/utf.h           |   8 +-
 flang-rt/lib/CMakeLists.txt                   |  17 ++
 flang-rt/lib/Testing/CMakeLists.txt           |  20 ++
 flang-rt/lib/cuda/CMakeLists.txt              |  34 +++
 .../lib/cuda}/allocatable.cpp                 |   9 +-
 .../CUDA => flang-rt/lib/cuda}/allocator.cpp  |  12 +-
 .../CUDA => flang-rt/lib/cuda}/descriptor.cpp |   6 +-
 .../CUDA => flang-rt/lib/cuda}/init.cpp       |   6 +-
 .../CUDA => flang-rt/lib/cuda}/kernel.cpp     |   4 +-
 .../lib/cuda}/memmove-function.cpp            |   4 +-
 .../CUDA => flang-rt/lib/cuda}/memory.cpp     |   6 +-
 .../CUDA => flang-rt/lib/cuda}/pointer.cpp    |   9 +-
 .../lib/cuda}/registration.cpp                |   4 +-
 flang-rt/lib/quadmath/CMakeLists.txt          | 136 +++++++++
 .../lib/quadmath}/acos.cpp                    |   2 +-
 .../lib/quadmath}/acosh.cpp                   |   2 +-
 .../lib/quadmath}/asin.cpp                    |   2 +-
 .../lib/quadmath}/asinh.cpp                   |   2 +-
 .../lib/quadmath}/atan.cpp                    |   2 +-
 .../lib/quadmath}/atan2.cpp                   |   2 +-
 .../lib/quadmath}/atanh.cpp                   |   2 +-
 .../lib/quadmath}/ceil.cpp                    |   2 +-
 .../lib/quadmath}/complex-math.c              |   5 +-
 .../lib/quadmath}/complex-math.h              |   8 +-
 .../lib/quadmath}/cos.cpp                     |   2 +-
 .../lib/quadmath}/cosh.cpp                    |   2 +-
 .../lib/quadmath}/erf.cpp                     |   2 +-
 .../lib/quadmath}/erfc.cpp                    |   2 +-
 .../lib/quadmath}/exp.cpp                     |   2 +-
 .../lib/quadmath}/exponent.cpp                |   2 +-
 .../lib/quadmath}/floor.cpp                   |   2 +-
 .../lib/quadmath}/fma.cpp                     |   2 +-
 .../lib/quadmath}/fraction.cpp                |   2 +-
 .../lib/quadmath}/hypot.cpp                   |   2 +-
 .../lib/quadmath}/j0.cpp                      |   2 +-
 .../lib/quadmath}/j1.cpp                      |   2 +-
 .../lib/quadmath}/jn.cpp                      |   2 +-
 .../lib/quadmath}/lgamma.cpp                  |   2 +-
 .../lib/quadmath}/llround.cpp                 |   2 +-
 .../lib/quadmath}/log.cpp                     |   2 +-
 .../lib/quadmath}/log10.cpp                   |   2 +-
 .../lib/quadmath}/lround.cpp                  |   2 +-
 .../lib/quadmath}/math-entries.h              |  13 +-
 .../lib/quadmath}/mod-real.cpp                |   2 +-
 .../lib/quadmath}/modulo-real.cpp             |   2 +-
 .../lib/quadmath}/nearbyint.cpp               |   2 +-
 .../lib/quadmath}/nearest.cpp                 |   2 +-
 .../lib/quadmath}/norm2.cpp                   |   4 +-
 .../lib/quadmath}/numeric-template-specs.h    |  10 +-
 .../lib/quadmath}/pow.cpp                     |   2 +-
 .../lib/quadmath}/random.cpp                  |   4 +-
 .../lib/quadmath}/remainder.cpp               |   2 +-
 .../lib/quadmath}/round.cpp                   |   2 +-
 .../lib/quadmath}/rrspacing.cpp               |   2 +-
 .../lib/quadmath}/scale.cpp                   |   2 +-
 .../lib/quadmath}/set-exponent.cpp            |   2 +-
 .../lib/quadmath}/sin.cpp                     |   2 +-
 .../lib/quadmath}/sinh.cpp                    |   2 +-
 .../lib/quadmath}/spacing.cpp                 |   2 +-
 .../lib/quadmath}/sqrt.cpp                    |   2 +-
 .../lib/quadmath}/tan.cpp                     |   2 +-
 .../lib/quadmath}/tanh.cpp                    |   2 +-
 .../lib/quadmath}/tgamma.cpp                  |   2 +-
 .../lib/quadmath}/trunc.cpp                   |   2 +-
 .../lib/quadmath}/y0.cpp                      |   2 +-
 .../lib/quadmath}/y1.cpp                      |   2 +-
 .../lib/quadmath}/yn.cpp                      |   2 +-
 flang-rt/lib/runtime/CMakeLists.txt           | 215 +++++++++++++++
 .../lib}/runtime/ISO_Fortran_binding.cpp      |   8 +-
 .../lib}/runtime/ISO_Fortran_util.h           |  14 +-
 .../lib}/runtime/allocatable.cpp              |  14 +-
 .../lib}/runtime/allocator-registry.cpp       |   6 +-
 .../lib}/runtime/array-constructor.cpp        |  14 +-
 {flang => flang-rt/lib}/runtime/assign.cpp    |  16 +-
 {flang => flang-rt/lib}/runtime/buffer.cpp    |   4 +-
 {flang => flang-rt/lib}/runtime/character.cpp |   8 +-
 {flang => flang-rt/lib}/runtime/command.cpp   |  12 +-
 .../lib}/runtime/complex-powi.cpp             |  16 +-
 .../lib}/runtime/complex-reduction.c          |   5 +-
 .../lib}/runtime/complex-reduction.h          |  11 +-
 .../lib}/runtime/connection.cpp               |   8 +-
 {flang => flang-rt/lib}/runtime/copy.cpp      |   8 +-
 {flang => flang-rt/lib}/runtime/copy.h        |  10 +-
 .../lib}/runtime/derived-api.cpp              |  13 +-
 {flang => flang-rt/lib}/runtime/derived.cpp   |  14 +-
 .../lib}/runtime/descriptor-io.cpp            |   2 +-
 .../lib}/runtime/descriptor-io.h              |  18 +-
 .../lib}/runtime/descriptor.cpp               |  22 +-
 .../lib}/runtime/dot-product.cpp              |   8 +-
 .../lib}/runtime/edit-input.cpp               |   6 +-
 {flang => flang-rt/lib}/runtime/edit-input.h  |  12 +-
 .../lib}/runtime/edit-output.cpp              |   6 +-
 {flang => flang-rt/lib}/runtime/edit-output.h |  12 +-
 .../lib}/runtime/environment-default-list.h   |  11 +-
 .../lib}/runtime/environment.cpp              |   6 +-
 .../lib}/runtime/exceptions.cpp               |   4 +-
 {flang => flang-rt/lib}/runtime/execute.cpp   |  12 +-
 .../lib}/runtime/extensions.cpp               |   8 +-
 .../lib}/runtime/external-unit.cpp            |   8 +-
 {flang => flang-rt/lib}/runtime/extrema.cpp   |   4 +-
 {flang => flang-rt/lib}/runtime/file.cpp      |   8 +-
 {flang => flang-rt/lib}/runtime/findloc.cpp   |   4 +-
 {flang => flang-rt/lib}/runtime/format.cpp    |   4 +-
 {flang => flang-rt/lib}/runtime/inquiry.cpp   |   8 +-
 .../lib}/runtime/internal-unit.cpp            |   8 +-
 .../lib}/runtime/io-api-common.h              |  12 +-
 .../lib}/runtime/io-api-minimal.cpp           |  12 +-
 {flang => flang-rt/lib}/runtime/io-api.cpp    |  16 +-
 {flang => flang-rt/lib}/runtime/io-error.cpp  |   6 +-
 {flang => flang-rt/lib}/runtime/io-stmt.cpp   |  16 +-
 {flang => flang-rt/lib}/runtime/iostat.cpp    |   2 +-
 {flang => flang-rt/lib}/runtime/main.cpp      |   6 +-
 .../lib}/runtime/matmul-transpose.cpp         |   8 +-
 {flang => flang-rt/lib}/runtime/matmul.cpp    |   8 +-
 {flang => flang-rt/lib}/runtime/memory.cpp    |   8 +-
 .../lib}/runtime/misc-intrinsic.cpp           |   8 +-
 {flang => flang-rt/lib}/runtime/namelist.cpp  |   8 +-
 .../lib}/runtime/non-tbp-dio.cpp              |   6 +-
 {flang => flang-rt/lib}/runtime/numeric.cpp   |   8 +-
 {flang => flang-rt/lib}/runtime/pointer.cpp   |  16 +-
 {flang => flang-rt/lib}/runtime/product.cpp   |   4 +-
 .../lib}/runtime/pseudo-unit.cpp              |   6 +-
 {flang => flang-rt/lib}/runtime/ragged.cpp    |   4 +-
 {flang => flang-rt/lib}/runtime/random.cpp    |  10 +-
 {flang => flang-rt/lib}/runtime/reduce.cpp    |  10 +-
 {flang => flang-rt/lib}/runtime/reduction.cpp |   6 +-
 {flang => flang-rt/lib}/runtime/stack.h       |  12 +-
 {flang => flang-rt/lib}/runtime/stat.cpp      |  10 +-
 {flang => flang-rt/lib}/runtime/stop.cpp      |  10 +-
 {flang => flang-rt/lib}/runtime/sum.cpp       |   4 +-
 {flang => flang-rt/lib}/runtime/support.cpp   |   6 +-
 .../lib}/runtime/temporary-stack.cpp          |   8 +-
 .../lib}/runtime/terminator.cpp               |   4 +-
 .../lib}/runtime/time-intrinsic.cpp           |   8 +-
 {flang => flang-rt/lib}/runtime/tools.cpp     |   6 +-
 .../lib}/runtime/transformational.cpp         |   8 +-
 {flang => flang-rt/lib}/runtime/type-code.cpp |   4 +-
 {flang => flang-rt/lib}/runtime/type-info.cpp |  10 +-
 {flang => flang-rt/lib}/runtime/unit-map.cpp  |   2 +-
 {flang => flang-rt/lib}/runtime/unit-map.h    |  12 +-
 {flang => flang-rt/lib}/runtime/unit.cpp      |   8 +-
 {flang => flang-rt/lib}/runtime/unit.h        |  30 +-
 {flang => flang-rt/lib}/runtime/utf.cpp       |   4 +-
 flang-rt/test/CMakeLists.txt                  |  59 ++++
 .../test/Driver/ctofortran.f90                |  29 +-
 {flang => 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 => 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                      | 100 +++++++
 flang-rt/test/lit.site.cfg.py.in              |  19 ++
 flang-rt/unittests/CMakeLists.txt             | 105 +++++++
 flang-rt/unittests/Evaluate/CMakeLists.txt    |  21 ++
 .../Evaluate/ISO-Fortran-binding.cpp          |  10 +-
 .../unittests/Evaluate/reshape.cpp            |  10 +-
 .../unittests/Runtime/AccessTest.cpp          |   2 +-
 .../unittests/Runtime/Allocatable.cpp         |   4 +-
 .../unittests/Runtime/ArrayConstructor.cpp    |  10 +-
 .../unittests/Runtime/BufferTest.cpp          |   4 +-
 flang-rt/unittests/Runtime/CMakeLists.txt     |  48 ++++
 .../unittests/Runtime/CUDA/Allocatable.cpp    |  10 +-
 .../unittests/Runtime/CUDA/AllocatorCUF.cpp   |  10 +-
 .../unittests/Runtime/CUDA/CMakeLists.txt     |  18 ++
 .../unittests/Runtime/CUDA/Memory.cpp         |  11 +-
 .../unittests/Runtime/CharacterTest.cpp       |   4 +-
 .../unittests/Runtime/CommandTest.cpp         |   4 +-
 .../unittests/Runtime/Complex.cpp             |   3 +-
 .../unittests/Runtime/CrashHandlerFixture.cpp |   5 +-
 .../unittests/Runtime/CrashHandlerFixture.h   |   9 +-
 .../unittests/Runtime/Derived.cpp             |   6 +-
 .../unittests/Runtime/ExternalIOTest.cpp      |   4 +-
 .../unittests/Runtime/Format.cpp              |   8 +-
 .../unittests/Runtime/Inquiry.cpp             |   6 +-
 .../unittests/Runtime/ListInputTest.cpp       |   6 +-
 .../unittests/Runtime/LogicalFormatTest.cpp   |   4 +-
 .../unittests/Runtime/Matmul.cpp              |   8 +-
 .../unittests/Runtime/MatmulTranspose.cpp     |   8 +-
 .../unittests/Runtime/MiscIntrinsic.cpp       |   8 +-
 .../unittests/Runtime/Namelist.cpp            |   6 +-
 .../unittests/Runtime/Numeric.cpp             |   2 +-
 .../unittests/Runtime/NumericalFormatTest.cpp |   4 +-
 .../unittests/Runtime/Pointer.cpp             |   6 +-
 .../unittests/Runtime/Ragged.cpp              |   2 +-
 .../unittests/Runtime/Random.cpp              |   8 +-
 .../unittests/Runtime/Reduction.cpp           |   8 +-
 .../unittests/Runtime/RuntimeCrashTest.cpp    |   4 +-
 .../unittests/Runtime/Stop.cpp                |   5 +-
 .../unittests/Runtime/Support.cpp             |   6 +-
 .../unittests/Runtime/TemporaryStack.cpp      |   8 +-
 .../unittests/Runtime/Time.cpp                |   2 +-
 .../unittests/Runtime/Transformational.cpp    |   6 +-
 {flang => flang-rt}/unittests/Runtime/tools.h |  12 +-
 flang/CMakeLists.txt                          |  54 ++--
 flang/cmake/modules/FlangCommon.cmake         |  43 +++
 flang/docs/GettingStarted.md                  | 106 +++----
 flang/docs/ReleaseNotes.md                    |   6 +
 .../ExternalHelloWorld/CMakeLists.txt         |   2 +-
 flang/include/flang/Runtime/allocatable.h     |   2 +-
 .../include/flang/Runtime/descriptor-consts.h |   2 +
 flang/include/flang/Runtime/pointer.h         |   2 +-
 flang/runtime/CMakeLists.txt                  |  29 +-
 flang/runtime/CUDA/CMakeLists.txt             |   7 +-
 flang/runtime/Float128Math/CMakeLists.txt     |   1 +
 flang/test/lit.cfg.py                         |  20 --
 flang/test/lit.site.cfg.py.in                 |   3 -
 flang/unittests/CMakeLists.txt                |  10 +-
 flang/unittests/Runtime/CMakeLists.txt        |  60 ++--
 flang/unittests/Runtime/CUDA/CMakeLists.txt   |   6 +-
 llvm/CMakeLists.txt                           |   8 +-
 .../modules/LLVMExternalProjectUtils.cmake    |  16 +-
 llvm/projects/CMakeLists.txt                  |   4 +-
 llvm/runtimes/CMakeLists.txt                  |  25 +-
 runtimes/CMakeLists.txt                       |   2 +-
 255 files changed, 3015 insertions(+), 850 deletions(-)
 create mode 100644 flang-rt/.clang-format
 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
 rename flang/runtime/config.h.cmake => flang-rt/cmake/config.h.cmake.in (54%)
 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/examples/CMakeLists.txt
 create mode 100644 flang-rt/examples/ExternalHelloWorld/CMakeLists.txt
 rename {flang => flang-rt}/examples/ExternalHelloWorld/external-hello.cpp (80%)
 rename {flang/include/flang/Runtime => flang-rt/include/flang-rt/runtime}/allocator-registry.h (87%)
 rename {flang/include/flang/Runtime => flang-rt/include/flang-rt/runtime}/array-constructor.h (89%)
 rename {flang => flang-rt/include/flang-rt}/runtime/assign-impl.h (85%)
 rename {flang => flang-rt/include/flang-rt}/runtime/buffer.h (97%)
 rename {flang => flang-rt/include/flang-rt}/runtime/connection.h (96%)
 rename {flang => flang-rt/include/flang-rt}/runtime/derived.h (90%)
 rename {flang/include/flang/Runtime => flang-rt/include/flang-rt/runtime}/descriptor.h (98%)
 rename {flang => flang-rt/include/flang-rt}/runtime/emit-encoded.h (94%)
 rename {flang => flang-rt/include/flang-rt}/runtime/environment.h (91%)
 rename {flang => flang-rt/include/flang-rt}/runtime/file.h (95%)
 rename {flang => flang-rt/include/flang-rt}/runtime/format-implementation.h (98%)
 rename {flang => flang-rt/include/flang-rt}/runtime/format.h (97%)
 rename {flang => flang-rt/include/flang-rt}/runtime/internal-unit.h (89%)
 rename {flang => flang-rt/include/flang-rt}/runtime/io-error.h (92%)
 rename {flang => flang-rt/include/flang-rt}/runtime/io-stmt.h (99%)
 rename {flang => flang-rt/include/flang-rt}/runtime/lock.h (94%)
 rename {flang/include/flang/Runtime => flang-rt/include/flang-rt/runtime}/memory.h (96%)
 rename {flang => flang-rt/include/flang-rt}/runtime/namelist.h (91%)
 rename {flang => flang-rt/include/flang-rt}/runtime/non-tbp-dio.h (91%)
 rename {flang => flang-rt/include/flang-rt}/runtime/numeric-templates.h (98%)
 rename {flang => flang-rt/include/flang-rt}/runtime/random-templates.h (93%)
 rename {flang => flang-rt/include/flang-rt}/runtime/reduction-templates.h (98%)
 rename {flang => flang-rt/include/flang-rt}/runtime/stat.h (93%)
 rename {flang => flang-rt/include/flang-rt}/runtime/terminator.h (95%)
 rename {flang => flang-rt/include/flang-rt}/runtime/tools.h (98%)
 rename {flang/include/flang/Runtime => flang-rt/include/flang-rt/runtime}/type-code.h (93%)
 rename {flang => flang-rt/include/flang-rt}/runtime/type-info.h (98%)
 rename {flang => flang-rt/include/flang-rt}/runtime/utf.h (94%)
 create mode 100644 flang-rt/lib/CMakeLists.txt
 create mode 100644 flang-rt/lib/Testing/CMakeLists.txt
 create mode 100644 flang-rt/lib/cuda/CMakeLists.txt
 rename {flang/runtime/CUDA => flang-rt/lib/cuda}/allocatable.cpp (94%)
 rename {flang/runtime/CUDA => flang-rt/lib/cuda}/allocator.cpp (86%)
 rename {flang/runtime/CUDA => flang-rt/lib/cuda}/descriptor.cpp (91%)
 rename {flang/runtime/CUDA => flang-rt/lib/cuda}/init.cpp (81%)
 rename {flang/runtime/CUDA => flang-rt/lib/cuda}/kernel.cpp (98%)
 rename {flang/runtime/CUDA => flang-rt/lib/cuda}/memmove-function.cpp (90%)
 rename {flang/runtime/CUDA => flang-rt/lib/cuda}/memory.cpp (96%)
 rename {flang/runtime/CUDA => flang-rt/lib/cuda}/pointer.cpp (92%)
 rename {flang/runtime/CUDA => flang-rt/lib/cuda}/registration.cpp (92%)
 create mode 100644 flang-rt/lib/quadmath/CMakeLists.txt
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/acos.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/acosh.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/asin.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/asinh.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/atan.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/atan2.cpp (88%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/atanh.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/ceil.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/complex-math.c (92%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/complex-math.h (88%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/cos.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/cosh.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/erf.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/erfc.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/exp.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/exponent.cpp (90%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/floor.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/fma.cpp (89%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/fraction.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/hypot.cpp (88%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/j0.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/j1.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/jn.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/lgamma.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/llround.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/log.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/log10.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/lround.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/math-entries.h (96%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/mod-real.cpp (88%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/modulo-real.cpp (89%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/nearbyint.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/nearest.cpp (88%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/norm2.cpp (89%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/numeric-template-specs.h (82%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/pow.cpp (88%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/random.cpp (83%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/remainder.cpp (88%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/round.cpp (89%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/rrspacing.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/scale.cpp (90%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/set-exponent.cpp (88%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/sin.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/sinh.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/spacing.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/sqrt.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/tan.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/tanh.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/tgamma.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/trunc.cpp (89%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/y0.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/y1.cpp (87%)
 rename {flang/runtime/Float128Math => flang-rt/lib/quadmath}/yn.cpp (87%)
 create mode 100644 flang-rt/lib/runtime/CMakeLists.txt
 rename {flang => flang-rt/lib}/runtime/ISO_Fortran_binding.cpp (97%)
 rename {flang => flang-rt/lib}/runtime/ISO_Fortran_util.h (90%)
 rename {flang => flang-rt/lib}/runtime/allocatable.cpp (96%)
 rename {flang => flang-rt/lib}/runtime/allocator-registry.cpp (87%)
 rename {flang => flang-rt/lib}/runtime/array-constructor.cpp (96%)
 rename {flang => flang-rt/lib}/runtime/assign.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/buffer.cpp (88%)
 rename {flang => flang-rt/lib}/runtime/character.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/command.cpp (96%)
 rename {flang => flang-rt/lib}/runtime/complex-powi.cpp (92%)
 rename {flang => flang-rt/lib}/runtime/complex-reduction.c (97%)
 rename {flang => flang-rt/lib}/runtime/complex-reduction.h (96%)
 rename {flang => flang-rt/lib}/runtime/connection.cpp (90%)
 rename {flang => flang-rt/lib}/runtime/copy.cpp (97%)
 rename {flang => flang-rt/lib}/runtime/copy.h (78%)
 rename {flang => flang-rt/lib}/runtime/derived-api.cpp (95%)
 rename {flang => flang-rt/lib}/runtime/derived.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/descriptor-io.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/descriptor-io.h (98%)
 rename {flang => flang-rt/lib}/runtime/descriptor.cpp (94%)
 rename {flang => flang-rt/lib}/runtime/dot-product.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/edit-input.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/edit-input.h (87%)
 rename {flang => flang-rt/lib}/runtime/edit-output.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/edit-output.h (95%)
 rename {flang => flang-rt/lib}/runtime/environment-default-list.h (70%)
 rename {flang => flang-rt/lib}/runtime/environment.cpp (96%)
 rename {flang => flang-rt/lib}/runtime/exceptions.cpp (97%)
 rename {flang => flang-rt/lib}/runtime/execute.cpp (97%)
 rename {flang => flang-rt/lib}/runtime/extensions.cpp (97%)
 rename {flang => flang-rt/lib}/runtime/external-unit.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/extrema.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/file.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/findloc.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/format.cpp (87%)
 rename {flang => flang-rt/lib}/runtime/inquiry.cpp (94%)
 rename {flang => flang-rt/lib}/runtime/internal-unit.cpp (96%)
 rename {flang => flang-rt/lib}/runtime/io-api-common.h (92%)
 rename {flang => flang-rt/lib}/runtime/io-api-minimal.cpp (94%)
 rename {flang => flang-rt/lib}/runtime/io-api.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/io-error.cpp (96%)
 rename {flang => flang-rt/lib}/runtime/io-stmt.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/iostat.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/main.cpp (89%)
 rename {flang => flang-rt/lib}/runtime/matmul-transpose.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/matmul.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/memory.cpp (85%)
 rename {flang => flang-rt/lib}/runtime/misc-intrinsic.cpp (95%)
 rename {flang => flang-rt/lib}/runtime/namelist.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/non-tbp-dio.cpp (86%)
 rename {flang => flang-rt/lib}/runtime/numeric.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/pointer.cpp (96%)
 rename {flang => flang-rt/lib}/runtime/product.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/pseudo-unit.cpp (97%)
 rename {flang => flang-rt/lib}/runtime/ragged.cpp (96%)
 rename {flang => flang-rt/lib}/runtime/random.cpp (96%)
 rename {flang => flang-rt/lib}/runtime/reduce.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/reduction.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/stack.h (93%)
 rename {flang => flang-rt/lib}/runtime/stat.cpp (92%)
 rename {flang => flang-rt/lib}/runtime/stop.cpp (95%)
 rename {flang => flang-rt/lib}/runtime/sum.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/support.cpp (90%)
 rename {flang => flang-rt/lib}/runtime/temporary-stack.cpp (97%)
 rename {flang => flang-rt/lib}/runtime/terminator.cpp (95%)
 rename {flang => flang-rt/lib}/runtime/time-intrinsic.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/tools.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/transformational.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/type-code.cpp (98%)
 rename {flang => flang-rt/lib}/runtime/type-info.cpp (97%)
 rename {flang => flang-rt/lib}/runtime/unit-map.cpp (97%)
 rename {flang => flang-rt/lib}/runtime/unit-map.h (92%)
 rename {flang => flang-rt/lib}/runtime/unit.cpp (99%)
 rename {flang => flang-rt/lib}/runtime/unit.h (95%)
 rename {flang => flang-rt/lib}/runtime/utf.cpp (97%)
 create mode 100644 flang-rt/test/CMakeLists.txt
 rename {flang => flang-rt}/test/Driver/ctofortran.f90 (69%)
 rename {flang => flang-rt}/test/Driver/exec.f90 (50%)
 create mode 100644 flang-rt/test/NonGtestUnit/lit.cfg.py
 create mode 100644 flang-rt/test/NonGtestUnit/lit.site.cfg.py.in
 rename {flang => flang-rt}/test/Runtime/no-cpp-dep.c (91%)
 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
 rename {flang => flang-rt}/unittests/Evaluate/ISO-Fortran-binding.cpp (98%)
 rename {flang => flang-rt}/unittests/Evaluate/reshape.cpp (88%)
 rename {flang => flang-rt}/unittests/Runtime/AccessTest.cpp (99%)
 rename {flang => flang-rt}/unittests/Runtime/Allocatable.cpp (97%)
 rename {flang => flang-rt}/unittests/Runtime/ArrayConstructor.cpp (96%)
 rename {flang => flang-rt}/unittests/Runtime/BufferTest.cpp (97%)
 create mode 100644 flang-rt/unittests/Runtime/CMakeLists.txt
 rename {flang => flang-rt}/unittests/Runtime/CUDA/Allocatable.cpp (90%)
 rename {flang => flang-rt}/unittests/Runtime/CUDA/AllocatorCUF.cpp (92%)
 create mode 100644 flang-rt/unittests/Runtime/CUDA/CMakeLists.txt
 rename {flang => flang-rt}/unittests/Runtime/CUDA/Memory.cpp (93%)
 rename {flang => flang-rt}/unittests/Runtime/CharacterTest.cpp (99%)
 rename {flang => flang-rt}/unittests/Runtime/CommandTest.cpp (99%)
 rename {flang => flang-rt}/unittests/Runtime/Complex.cpp (98%)
 rename {flang => flang-rt}/unittests/Runtime/CrashHandlerFixture.cpp (92%)
 rename {flang => flang-rt}/unittests/Runtime/CrashHandlerFixture.h (70%)
 rename {flang => flang-rt}/unittests/Runtime/Derived.cpp (93%)
 rename {flang => flang-rt}/unittests/Runtime/ExternalIOTest.cpp (99%)
 rename {flang => flang-rt}/unittests/Runtime/Format.cpp (96%)
 rename {flang => flang-rt}/unittests/Runtime/Inquiry.cpp (97%)
 rename {flang => flang-rt}/unittests/Runtime/ListInputTest.cpp (98%)
 rename {flang => flang-rt}/unittests/Runtime/LogicalFormatTest.cpp (94%)
 rename {flang => flang-rt}/unittests/Runtime/Matmul.cpp (98%)
 rename {flang => flang-rt}/unittests/Runtime/MatmulTranspose.cpp (98%)
 rename {flang => flang-rt}/unittests/Runtime/MiscIntrinsic.cpp (96%)
 rename {flang => flang-rt}/unittests/Runtime/Namelist.cpp (99%)
 rename {flang => flang-rt}/unittests/Runtime/Numeric.cpp (99%)
 rename {flang => flang-rt}/unittests/Runtime/NumericalFormatTest.cpp (99%)
 rename {flang => flang-rt}/unittests/Runtime/Pointer.cpp (96%)
 rename {flang => flang-rt}/unittests/Runtime/Ragged.cpp (94%)
 rename {flang => flang-rt}/unittests/Runtime/Random.cpp (92%)
 rename {flang => flang-rt}/unittests/Runtime/Reduction.cpp (99%)
 rename {flang => flang-rt}/unittests/Runtime/RuntimeCrashTest.cpp (98%)
 rename {flang => flang-rt}/unittests/Runtime/Stop.cpp (96%)
 rename {flang => flang-rt}/unittests/Runtime/Support.cpp (96%)
 rename {flang => flang-rt}/unittests/Runtime/TemporaryStack.cpp (98%)
 rename {flang => flang-rt}/unittests/Runtime/Time.cpp (98%)
 rename {flang => flang-rt}/unittests/Runtime/Transformational.cpp (99%)
 rename {flang => flang-rt}/unittests/Runtime/tools.h (85%)
 create mode 100644 flang/cmake/modules/FlangCommon.cmake

diff --git a/flang-rt/.clang-format b/flang-rt/.clang-format
new file mode 100644
index 0000000000000..23f4c5ae2dcf2
--- /dev/null
+++ b/flang-rt/.clang-format
@@ -0,0 +1,25 @@
+---
+# See: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+BasedOnStyle: LLVM
+AlignAfterOpenBracket: DontAlign
+AlignEscapedNewlines: DontAlign
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignOperands: false
+AlignTrailingComments: false
+IncludeCategories:
+  - Regex:           '^<'
+    Priority:        6
+  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
+    Priority:        5
+  - Regex:           '^"flang/'     # Headers shared with Flang
+    Priority:        4
+  - Regex:           '^"flang-rt/'  # Public Flang-RT headers
+    Priority:        3
+  - Regex:           '^[^/]*$'      # Private headers from same library
+    Priority:        1
+  - Regex:           '.*'           # Private headers from sibling libraries
+    Priority:        2
+...
+
+# vim:set filetype=yaml:
diff --git a/flang-rt/.clang-tidy b/flang-rt/.clang-tidy
new file mode 100644
index 0000000000000..ee3a0ab2201bf
--- /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 0000000000000..df35e24ec28a7
--- /dev/null
+++ b/flang-rt/CMakeLists.txt
@@ -0,0 +1,261 @@
+#===-- 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_INCLUDE_CUF "Build the CUDA Fortran runtime (libflang_rt.cuda.a)" OFF)
+if (FLANG_RT_INCLUDE_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)
+
+# Check whether -fno-lto is supported.
+check_cxx_compiler_flag(-fno-lto FLANG_RT_HAS_FNO_LTO_FLAG)
+
+
+# function checks
+find_package(Backtrace)
+set(HAVE_BACKTRACE ${Backtrace_FOUND})
+set(BACKTRACE_HEADER ${Backtrace_HEADER})
+
+
+#####################
+# 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 (LLVM_INCLUDE_EXAMPLES)
+  add_subdirectory(examples)
+endif ()
+
+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 0000000000000..649243aa1e8fe
--- /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 0000000000000..53bb2e7fbc764
--- /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 0000000000000..aba64e2453872
--- /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_INCLUDE_CUF` (bool, default: `OFF`)
+
+   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)
+   (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/runtime/config.h.cmake b/flang-rt/cmake/config.h.cmake.in
similarity index 54%
rename from flang/runtime/config.h.cmake
rename to flang-rt/cmake/config.h.cmake.in
index a2271be77b8c6..84c234d86bae6 100644
--- a/flang/runtime/config.h.cmake
+++ b/flang-rt/cmake/config.h.cmake.in
@@ -1,3 +1,11 @@
+/*===-- cmake/config.cmake.in ---------------------------------------*- C -*-===
+ *
+ * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+ * See https://llvm.org/LICENSE.txt for license information.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ *
+ *===----------------------------------------------------------------------===*/
+
 #ifndef FORTRAN_RUNTIME_CONFIG_H
 #define FORTRAN_RUNTIME_CONFIG_H
 
diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
new file mode 100644
index 0000000000000..630aeb3c65005
--- /dev/null
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -0,0 +1,199 @@
+#===-- 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 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 LINK_LIBRARIES 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} 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} PUBLIC "${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 ()
+
+  # When building the flang runtime if LTO is enabled the archive file
+  # contains LLVM IR rather than object code. Currently flang is not
+  # LTO aware so cannot link this file to compiled Fortran code.
+  if (FLANG_RT_HAS_FNO_LTO_FLAG)
+    target_compile_options(${name} PRIVATE -fno-lto)
+  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 (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")
+  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 0000000000000..4e4bd60c63545
--- /dev/null
+++ b/flang-rt/cmake/modules/AddFlangRTOffload.cmake
@@ -0,0 +1,100 @@
+#===-- 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.
+# 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 0000000000000..426a5e8e801f3
--- /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/examples/CMakeLists.txt b/flang-rt/examples/CMakeLists.txt
new file mode 100644
index 0000000000000..f45a95d18b641
--- /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 0000000000000..4fd04f8f2769a
--- /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 4991bf9eba999..7c8a12476295c 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/include/flang/Runtime/allocator-registry.h b/flang-rt/include/flang-rt/runtime/allocator-registry.h
similarity index 87%
rename from flang/include/flang/Runtime/allocator-registry.h
rename to flang-rt/include/flang-rt/runtime/allocator-registry.h
index 29302c5d825bc..1a59ec8b1ef5b 100644
--- a/flang/include/flang/Runtime/allocator-registry.h
+++ b/flang-rt/include/flang-rt/runtime/allocator-registry.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_ALLOCATOR_REGISTRY_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_ALLOCATOR_REGISTRY_H_
+#endif // FLANG_RT_RUNTIME_ALLOCATOR_REGISTRY_H_
diff --git a/flang/include/flang/Runtime/array-constructor.h b/flang-rt/include/flang-rt/runtime/array-constructor.h
similarity index 89%
rename from flang/include/flang/Runtime/array-constructor.h
rename to flang-rt/include/flang-rt/runtime/array-constructor.h
index 2f6aaae17c650..9c037177161c0 100644
--- a/flang/include/flang/Runtime/array-constructor.h
+++ b/flang-rt/include/flang-rt/runtime/array-constructor.h
@@ -1,4 +1,4 @@
-//===-- include/flang/Runtime/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,11 +9,11 @@
 // External APIs to create temporary storage for array constructors when their
 // final extents or length parameters cannot be pre-computed.
 
-#ifndef FORTRAN_RUNTIME_ARRAYCONSTRUCTOR_H_
-#define FORTRAN_RUNTIME_ARRAYCONSTRUCTOR_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"
-#include "flang/Runtime/descriptor.h"
 #include "flang/Runtime/entry-names.h"
 #include <cstdint>
 
@@ -54,4 +54,4 @@ static_assert(alignof(Fortran::runtime::ArrayConstructorVector) <=
     "MaxArrayConstructorVectorAlignInBytes");
 
 } // namespace Fortran::runtime
-#endif // FORTRAN_RUNTIME_ARRAYCONSTRUCTOR_H_
+#endif // FLANG_RT_RUNTIME_ARRAY_CONSTRUCTOR_H_
diff --git a/flang/runtime/assign-impl.h b/flang-rt/include/flang-rt/runtime/assign-impl.h
similarity index 85%
rename from flang/runtime/assign-impl.h
rename to flang-rt/include/flang-rt/runtime/assign-impl.h
index aaa320ef7f959..cc931f6713cd8 100644
--- a/flang/runtime/assign-impl.h
+++ b/flang-rt/include/flang-rt/runtime/assign-impl.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_ASSIGN_IMPL_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_ASSIGN_IMPL_H_
+#endif // FLANG_RT_RUNTIME_ASSIGN_IMPL_H_
diff --git a/flang/runtime/buffer.h b/flang-rt/include/flang-rt/runtime/buffer.h
similarity index 97%
rename from flang/runtime/buffer.h
rename to flang-rt/include/flang-rt/runtime/buffer.h
index 41a1abb1b2d90..b5a9ce9e35e91 100644
--- a/flang/runtime/buffer.h
+++ b/flang-rt/include/flang-rt/runtime/buffer.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,12 +8,12 @@
 
 // External file buffering
 
-#ifndef FORTRAN_RUNTIME_BUFFER_H_
-#define FORTRAN_RUNTIME_BUFFER_H_
+#ifndef FLANG_RT_RUNTIME_BUFFER_H_
+#define FLANG_RT_RUNTIME_BUFFER_H_
 
 #include "io-error.h"
+#include "memory.h"
 #include "flang/Runtime/freestanding-tools.h"
-#include "flang/Runtime/memory.h"
 #include <algorithm>
 #include <cinttypes>
 #include <cstring>
@@ -221,4 +221,4 @@ template <typename STORE, std::size_t minBuffer = 65536> class FileFrame {
   bool dirty_{false};
 };
 } // namespace Fortran::runtime::io
-#endif // FORTRAN_RUNTIME_BUFFER_H_
+#endif // FLANG_RT_RUNTIME_BUFFER_H_
diff --git a/flang/runtime/connection.h b/flang-rt/include/flang-rt/runtime/connection.h
similarity index 96%
rename from flang/runtime/connection.h
rename to flang-rt/include/flang-rt/runtime/connection.h
index 6f1ea90a160e5..03d9658e7067b 100644
--- a/flang/runtime/connection.h
+++ b/flang-rt/include/flang-rt/runtime/connection.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_IO_CONNECTION_H_
-#define FORTRAN_RUNTIME_IO_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 // FORTRAN_RUNTIME_IO_CONNECTION_H_
+#endif // FLANG_RT_RUNTIME_CONNECTION_H_
diff --git a/flang/runtime/derived.h b/flang-rt/include/flang-rt/runtime/derived.h
similarity index 90%
rename from flang/runtime/derived.h
rename to flang-rt/include/flang-rt/runtime/derived.h
index f5a1e219b848c..ac6962c57168c 100644
--- a/flang/runtime/derived.h
+++ b/flang-rt/include/flang-rt/runtime/derived.h
@@ -1,4 +1,4 @@
-//===-- runtime/derived.h -------------------------------------------------===//
+//===-- 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 FORTRAN_RUNTIME_DERIVED_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_DERIVED_H_
+#endif // FLANG_RT_RUNTIME_DERIVED_H_
diff --git a/flang/include/flang/Runtime/descriptor.h b/flang-rt/include/flang-rt/runtime/descriptor.h
similarity index 98%
rename from flang/include/flang/Runtime/descriptor.h
rename to flang-rt/include/flang-rt/runtime/descriptor.h
index 628ac8c927a51..19e1a0bf6a1dd 100644
--- a/flang/include/flang/Runtime/descriptor.h
+++ b/flang-rt/include/flang-rt/runtime/descriptor.h
@@ -1,4 +1,4 @@
-//===-- include/flang/Runtime/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 FORTRAN_RUNTIME_DESCRIPTOR_H_
-#define FORTRAN_RUNTIME_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,
@@ -18,10 +18,10 @@
 // User C code is welcome to depend on that ISO_Fortran_binding.h file,
 // but should never reference this internal header.
 
+#include "memory.h"
+#include "type-code.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
 #include "flang/Runtime/descriptor-consts.h"
-#include "flang/Runtime/memory.h"
-#include "flang/Runtime/type-code.h"
 #include <algorithm>
 #include <cassert>
 #include <cinttypes>
@@ -482,4 +482,4 @@ class alignas(Descriptor) StaticDescriptor {
 };
 
 } // namespace Fortran::runtime
-#endif // FORTRAN_RUNTIME_DESCRIPTOR_H_
+#endif // FLANG_RT_RUNTIME_DESCRIPTOR_H_
diff --git a/flang/runtime/emit-encoded.h b/flang-rt/include/flang-rt/runtime/emit-encoded.h
similarity index 94%
rename from flang/runtime/emit-encoded.h
rename to flang-rt/include/flang-rt/runtime/emit-encoded.h
index 4b5e390078835..d99f56b29558e 100644
--- a/flang/runtime/emit-encoded.h
+++ b/flang-rt/include/flang-rt/runtime/emit-encoded.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_EMIT_ENCODED_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_EMIT_ENCODED_H_
+#endif // FLANG_RT_RUNTIME_EMIT_ENCODED_H_
diff --git a/flang/runtime/environment.h b/flang-rt/include/flang-rt/runtime/environment.h
similarity index 91%
rename from flang/runtime/environment.h
rename to flang-rt/include/flang-rt/runtime/environment.h
index 500aa925a625b..142add432b5f7 100644
--- a/flang/runtime/environment.h
+++ b/flang-rt/include/flang-rt/runtime/environment.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_ENVIRONMENT_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_ENVIRONMENT_H_
+#endif // FLANG_RT_RUNTIME_ENVIRONMENT_H_
diff --git a/flang/runtime/file.h b/flang-rt/include/flang-rt/runtime/file.h
similarity index 95%
rename from flang/runtime/file.h
rename to flang-rt/include/flang-rt/runtime/file.h
index c06acbb9904cc..3bba29722b3b8 100644
--- a/flang/runtime/file.h
+++ b/flang-rt/include/flang-rt/runtime/file.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,12 +8,12 @@
 
 // Raw system I/O wrappers
 
-#ifndef FORTRAN_RUNTIME_FILE_H_
-#define FORTRAN_RUNTIME_FILE_H_
+#ifndef FLANG_RT_RUNTIME_FILE_H_
+#define FLANG_RT_RUNTIME_FILE_H_
 
 #include "io-error.h"
+#include "memory.h"
 #include "flang/Common/optional.h"
-#include "flang/Runtime/memory.h"
 #include <cinttypes>
 
 namespace Fortran::runtime::io {
@@ -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 // FORTRAN_RUNTIME_FILE_H_
+#endif // FLANG_RT_RUNTIME_FILE_H_
diff --git a/flang/runtime/format-implementation.h b/flang-rt/include/flang-rt/runtime/format-implementation.h
similarity index 98%
rename from flang/runtime/format-implementation.h
rename to flang-rt/include/flang-rt/runtime/format-implementation.h
index 46204ca927c13..8f4eb1161dd14 100644
--- a/flang/runtime/format-implementation.h
+++ b/flang-rt/include/flang-rt/runtime/format-implementation.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_FORMAT_IMPLEMENTATION_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_FORMAT_IMPLEMENTATION_H_
+#endif // FLANG_RT_RUNTIME_FORMAT_IMPLEMENTATION_H_
diff --git a/flang/runtime/format.h b/flang-rt/include/flang-rt/runtime/format.h
similarity index 97%
rename from flang/runtime/format.h
rename to flang-rt/include/flang-rt/runtime/format.h
index 815bf70685e64..b169d63fa4999 100644
--- a/flang/runtime/format.h
+++ b/flang-rt/include/flang-rt/runtime/format.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_FORMAT_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_FORMAT_H_
+#endif // FLANG_RT_RUNTIME_FORMAT_H_
diff --git a/flang/runtime/internal-unit.h b/flang-rt/include/flang-rt/runtime/internal-unit.h
similarity index 89%
rename from flang/runtime/internal-unit.h
rename to flang-rt/include/flang-rt/runtime/internal-unit.h
index a0ee6353eeda3..429d3489e0112 100644
--- a/flang/runtime/internal-unit.h
+++ b/flang-rt/include/flang-rt/runtime/internal-unit.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,11 +8,11 @@
 
 // Fortran internal I/O "units"
 
-#ifndef FORTRAN_RUNTIME_IO_INTERNAL_UNIT_H_
-#define FORTRAN_RUNTIME_IO_INTERNAL_UNIT_H_
+#ifndef FLANG_RT_RUNTIME_INTERNAL_UNIT_H_
+#define FLANG_RT_RUNTIME_INTERNAL_UNIT_H_
 
 #include "connection.h"
-#include "flang/Runtime/descriptor.h"
+#include "descriptor.h"
 #include <cinttypes>
 #include <type_traits>
 
@@ -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 // FORTRAN_RUNTIME_IO_INTERNAL_UNIT_H_
+#endif // FLANG_RT_RUNTIME_INTERNAL_UNIT_H_
diff --git a/flang/runtime/io-error.h b/flang-rt/include/flang-rt/runtime/io-error.h
similarity index 92%
rename from flang/runtime/io-error.h
rename to flang-rt/include/flang-rt/runtime/io-error.h
index 39a343c8e0a51..1cef6a208f374 100644
--- a/flang/runtime/io-error.h
+++ b/flang-rt/include/flang-rt/runtime/io-error.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,12 +12,12 @@
 // IOSTAT error codes are raw errno values augmented with values for
 // Fortran-specific errors.
 
-#ifndef FORTRAN_RUNTIME_IO_ERROR_H_
-#define FORTRAN_RUNTIME_IO_ERROR_H_
+#ifndef FLANG_RT_RUNTIME_IO_ERROR_H_
+#define FLANG_RT_RUNTIME_IO_ERROR_H_
 
+#include "memory.h"
 #include "terminator.h"
 #include "flang/Runtime/iostat.h"
-#include "flang/Runtime/memory.h"
 #include <cinttypes>
 
 namespace Fortran::runtime::io {
@@ -80,4 +80,4 @@ class IoErrorHandler : public Terminator {
 };
 
 } // namespace Fortran::runtime::io
-#endif // FORTRAN_RUNTIME_IO_ERROR_H_
+#endif // FLANG_RT_RUNTIME_IO_ERROR_H_
diff --git a/flang/runtime/io-stmt.h b/flang-rt/include/flang-rt/runtime/io-stmt.h
similarity index 99%
rename from flang/runtime/io-stmt.h
rename to flang-rt/include/flang-rt/runtime/io-stmt.h
index 1f1419b249e5e..a364ddfd9b3c7 100644
--- a/flang/runtime/io-stmt.h
+++ b/flang-rt/include/flang-rt/runtime/io-stmt.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,10 +8,11 @@
 
 // Representations of the state of an I/O statement in progress
 
-#ifndef FORTRAN_RUNTIME_IO_STMT_H_
-#define FORTRAN_RUNTIME_IO_STMT_H_
+#ifndef FLANG_RT_RUNTIME_IO_STMT_H_
+#define FLANG_RT_RUNTIME_IO_STMT_H_
 
 #include "connection.h"
+#include "descriptor.h"
 #include "file.h"
 #include "format.h"
 #include "internal-unit.h"
@@ -19,7 +20,6 @@
 #include "flang/Common/optional.h"
 #include "flang/Common/reference-wrapper.h"
 #include "flang/Common/visit.h"
-#include "flang/Runtime/descriptor.h"
 #include "flang/Runtime/io-api.h"
 #include <flang/Common/variant.h>
 #include <functional>
@@ -791,4 +791,4 @@ class ErroneousIoStatementState : public IoStatementBase {
 };
 
 } // namespace Fortran::runtime::io
-#endif // FORTRAN_RUNTIME_IO_STMT_H_
+#endif // FLANG_RT_RUNTIME_IO_STMT_H_
diff --git a/flang/runtime/lock.h b/flang-rt/include/flang-rt/runtime/lock.h
similarity index 94%
rename from flang/runtime/lock.h
rename to flang-rt/include/flang-rt/runtime/lock.h
index 46ca28703a45b..7c88534245733 100644
--- a/flang/runtime/lock.h
+++ b/flang-rt/include/flang-rt/runtime/lock.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_LOCK_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_LOCK_H_
+#endif // FLANG_RT_RUNTIME_LOCK_H_
diff --git a/flang/include/flang/Runtime/memory.h b/flang-rt/include/flang-rt/runtime/memory.h
similarity index 96%
rename from flang/include/flang/Runtime/memory.h
rename to flang-rt/include/flang-rt/runtime/memory.h
index 98412a989f890..93b477afa9814 100644
--- a/flang/include/flang/Runtime/memory.h
+++ b/flang-rt/include/flang-rt/runtime/memory.h
@@ -1,4 +1,4 @@
-//===-- include/flang/Runtime/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 FORTRAN_RUNTIME_MEMORY_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_MEMORY_H_
+#endif // FLANG_RT_RUNTIME_MEMORY_H_
diff --git a/flang/runtime/namelist.h b/flang-rt/include/flang-rt/runtime/namelist.h
similarity index 91%
rename from flang/runtime/namelist.h
rename to flang-rt/include/flang-rt/runtime/namelist.h
index 25216a75e9367..17d7bf310cc96 100644
--- a/flang/runtime/namelist.h
+++ b/flang-rt/include/flang-rt/runtime/namelist.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_NAMELIST_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_NAMELIST_H_
+#endif // FLANG_RT_RUNTIME_NAMELIST_H_
diff --git a/flang/runtime/non-tbp-dio.h b/flang-rt/include/flang-rt/runtime/non-tbp-dio.h
similarity index 91%
rename from flang/runtime/non-tbp-dio.h
rename to flang-rt/include/flang-rt/runtime/non-tbp-dio.h
index 8429d790fea57..2bbbfa7f97f79 100644
--- a/flang/runtime/non-tbp-dio.h
+++ b/flang-rt/include/flang-rt/runtime/non-tbp-dio.h
@@ -1,4 +1,4 @@
-//===-- flang/runtime/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 FORTRAN_RUNTIME_NON_TBP_DIO_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_NON_TBP_DIO_H_
+#endif // FLANG_RT_RUNTIME_NON_TBP_DIO_H_
diff --git a/flang/runtime/numeric-templates.h b/flang-rt/include/flang-rt/runtime/numeric-templates.h
similarity index 98%
rename from flang/runtime/numeric-templates.h
rename to flang-rt/include/flang-rt/runtime/numeric-templates.h
index fbb371bffc27a..1253d56f3c0fd 100644
--- a/flang/runtime/numeric-templates.h
+++ b/flang-rt/include/flang-rt/runtime/numeric-templates.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_NUMERIC_TEMPLATES_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_NUMERIC_TEMPLATES_H_
+#endif // FLANG_RT_RUNTIME_NUMERIC_TEMPLATES_H_
diff --git a/flang/runtime/random-templates.h b/flang-rt/include/flang-rt/runtime/random-templates.h
similarity index 93%
rename from flang/runtime/random-templates.h
rename to flang-rt/include/flang-rt/runtime/random-templates.h
index 3885941704d4a..895c5ad4fc8bb 100644
--- a/flang/runtime/random-templates.h
+++ b/flang-rt/include/flang-rt/runtime/random-templates.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,13 +6,13 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FORTRAN_RUNTIME_RANDOM_TEMPLATES_H_
-#define FORTRAN_RUNTIME_RANDOM_TEMPLATES_H_
+#ifndef FLANG_RT_RUNTIME_RANDOM_TEMPLATES_H_
+#define FLANG_RT_RUNTIME_RANDOM_TEMPLATES_H_
 
+#include "descriptor.h"
 #include "lock.h"
 #include "numeric-templates.h"
 #include "flang/Common/optional.h"
-#include "flang/Runtime/descriptor.h"
 #include <algorithm>
 #include <random>
 
@@ -108,4 +108,4 @@ inline void GenerateUnsigned(const Descriptor &harvest) {
 
 } // namespace Fortran::runtime::random
 
-#endif // FORTRAN_RUNTIME_RANDOM_TEMPLATES_H_
+#endif // FLANG_RT_RUNTIME_RANDOM_TEMPLATES_H_
diff --git a/flang/runtime/reduction-templates.h b/flang-rt/include/flang-rt/runtime/reduction-templates.h
similarity index 98%
rename from flang/runtime/reduction-templates.h
rename to flang-rt/include/flang-rt/runtime/reduction-templates.h
index b20b03655c3d0..8c6f838b8dadf 100644
--- a/flang/runtime/reduction-templates.h
+++ b/flang-rt/include/flang-rt/runtime/reduction-templates.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,14 +18,14 @@
 // * Character-valued reductions (MAXVAL & MINVAL) return arbitrary
 //   length results, dynamically allocated in a caller-supplied descriptor
 
-#ifndef FORTRAN_RUNTIME_REDUCTION_TEMPLATES_H_
-#define FORTRAN_RUNTIME_REDUCTION_TEMPLATES_H_
+#ifndef FLANG_RT_RUNTIME_REDUCTION_TEMPLATES_H_
+#define FLANG_RT_RUNTIME_REDUCTION_TEMPLATES_H_
 
+#include "descriptor.h"
 #include "numeric-templates.h"
 #include "terminator.h"
 #include "tools.h"
 #include "flang/Runtime/cpp-type.h"
-#include "flang/Runtime/descriptor.h"
 #include <algorithm>
 
 namespace Fortran::runtime {
@@ -416,4 +416,4 @@ template <int KIND> struct Norm2Helper {
 };
 
 } // namespace Fortran::runtime
-#endif // FORTRAN_RUNTIME_REDUCTION_TEMPLATES_H_
+#endif // FLANG_RT_RUNTIME_REDUCTION_TEMPLATES_H_
diff --git a/flang/runtime/stat.h b/flang-rt/include/flang-rt/runtime/stat.h
similarity index 93%
rename from flang/runtime/stat.h
rename to flang-rt/include/flang-rt/runtime/stat.h
index 572cb6d10b489..070d0bf8673fb 100644
--- a/flang/runtime/stat.h
+++ b/flang-rt/include/flang-rt/runtime/stat.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_STAT_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_STAT_H
+#endif // FLANG_RT_RUNTIME_STAT_H_
diff --git a/flang/runtime/terminator.h b/flang-rt/include/flang-rt/runtime/terminator.h
similarity index 95%
rename from flang/runtime/terminator.h
rename to flang-rt/include/flang-rt/runtime/terminator.h
index 609f059d6e092..4815f0674c849 100644
--- a/flang/runtime/terminator.h
+++ b/flang-rt/include/flang-rt/runtime/terminator.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_TERMINATOR_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_TERMINATOR_H_
+#endif // FLANG_RT_RUNTIME_TERMINATOR_H_
diff --git a/flang/runtime/tools.h b/flang-rt/include/flang-rt/runtime/tools.h
similarity index 98%
rename from flang/runtime/tools.h
rename to flang-rt/include/flang-rt/runtime/tools.h
index 75544098d47ab..91a026bf2ac14 100644
--- a/flang/runtime/tools.h
+++ b/flang-rt/include/flang-rt/runtime/tools.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,16 +6,16 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FORTRAN_RUNTIME_TOOLS_H_
-#define FORTRAN_RUNTIME_TOOLS_H_
+#ifndef FLANG_RT_RUNTIME_TOOLS_H_
+#define FLANG_RT_RUNTIME_TOOLS_H_
 
+#include "descriptor.h"
+#include "memory.h"
 #include "stat.h"
 #include "terminator.h"
 #include "flang/Common/optional.h"
 #include "flang/Runtime/cpp-type.h"
-#include "flang/Runtime/descriptor.h"
 #include "flang/Runtime/freestanding-tools.h"
-#include "flang/Runtime/memory.h"
 #include <cstring>
 #include <functional>
 #include <map>
@@ -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 flang_rt.quadmath
+      // generated a call to an entry in the libflang_rt.quadmath
       // library.
       if constexpr (!NEEDSMATH) {
         return FUNC<16>{}(std::forward<A>(x)...);
@@ -570,4 +570,4 @@ RT_API_ATTRS void CreatePartialReductionResult(Descriptor &result,
     const char *intrinsic, TypeCode);
 
 } // namespace Fortran::runtime
-#endif // FORTRAN_RUNTIME_TOOLS_H_
+#endif // FLANG_RT_RUNTIME_TOOLS_H_
diff --git a/flang/include/flang/Runtime/type-code.h b/flang-rt/include/flang-rt/runtime/type-code.h
similarity index 93%
rename from flang/include/flang/Runtime/type-code.h
rename to flang-rt/include/flang-rt/runtime/type-code.h
index ae854ed2145e4..9416a2816fd43 100644
--- a/flang/include/flang/Runtime/type-code.h
+++ b/flang-rt/include/flang-rt/runtime/type-code.h
@@ -1,4 +1,4 @@
-//===-- include/flang/Runtime/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 FORTRAN_RUNTIME_TYPE_CODE_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_TYPE_CODE_H_
+#endif // FLANG_RT_RUNTIME_TYPE_CODE_H_
diff --git a/flang/runtime/type-info.h b/flang-rt/include/flang-rt/runtime/type-info.h
similarity index 98%
rename from flang/runtime/type-info.h
rename to flang-rt/include/flang-rt/runtime/type-info.h
index 32403b1db5169..9891fcecdcb25 100644
--- a/flang/runtime/type-info.h
+++ b/flang-rt/include/flang-rt/runtime/type-info.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,17 +6,17 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FORTRAN_RUNTIME_TYPE_INFO_H_
-#define FORTRAN_RUNTIME_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.
 
+#include "descriptor.h"
 #include "terminator.h"
 #include "flang/Common/Fortran-consts.h"
 #include "flang/Common/bit-population-count.h"
 #include "flang/Common/optional.h"
-#include "flang/Runtime/descriptor.h"
 #include <cinttypes>
 #include <memory>
 
@@ -321,4 +321,4 @@ class DerivedType {
 };
 
 } // namespace Fortran::runtime::typeInfo
-#endif // FORTRAN_RUNTIME_TYPE_INFO_H_
+#endif // FLANG_RT_RUNTIME_TYPE_INFO_H_
diff --git a/flang/runtime/utf.h b/flang-rt/include/flang-rt/runtime/utf.h
similarity index 94%
rename from flang/runtime/utf.h
rename to flang-rt/include/flang-rt/runtime/utf.h
index 10c2d61484217..b5add823124fc 100644
--- a/flang/runtime/utf.h
+++ b/flang-rt/include/flang-rt/runtime/utf.h
@@ -1,4 +1,4 @@
-//===-- runtime/utf.h -----------------------------------------------------===//
+//===-- 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 FORTRAN_RUNTIME_UTF_H_
-#define FORTRAN_RUNTIME_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 // FORTRAN_RUNTIME_UTF_H_
+#endif // FLANG_RT_RUNTIME_UTF_H_
diff --git a/flang-rt/lib/CMakeLists.txt b/flang-rt/lib/CMakeLists.txt
new file mode 100644
index 0000000000000..aee51dcc9fa24
--- /dev/null
+++ b/flang-rt/lib/CMakeLists.txt
@@ -0,0 +1,17 @@
+#===-- 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(quadmath)
+add_subdirectory(runtime)
+if (FLANG_RT_INCLUDE_CUF)
+  add_subdirectory(cuda)
+endif()
+
+if (FLANG_RT_INCLUDE_TESTS)
+  add_subdirectory(Testing)
+endif ()
diff --git a/flang-rt/lib/Testing/CMakeLists.txt b/flang-rt/lib/Testing/CMakeLists.txt
new file mode 100644
index 0000000000000..19c20ad44c025
--- /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/cuda/CMakeLists.txt b/flang-rt/lib/cuda/CMakeLists.txt
new file mode 100644
index 0000000000000..d5ca354c1029f
--- /dev/null
+++ b/flang-rt/lib/cuda/CMakeLists.txt
@@ -0,0 +1,34 @@
+#===-- 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.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+#===------------------------------------------------------------------------===#
+
+
+add_flangrt_library(flang_rt.cuda STATIC
+  allocatable.cpp
+  allocator.cpp
+  descriptor.cpp
+  init.cpp
+  kernel.cpp
+  memmove-function.cpp
+  memory.cpp
+  registration.cpp
+
+  # 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 "flang_rt.cuda_${CUDAToolkit_VERSION_MAJOR}"
+
+  INCLUDE_DIRECTORIES
+    PRIVATE ${CUDAToolkit_INCLUDE_DIRS}
+)
+
+target_link_libraries(flang_rt.cuda
+  PUBLIC
+  flang_rt.runtime
+  CUDA::cudart_static
+)
diff --git a/flang/runtime/CUDA/allocatable.cpp b/flang-rt/lib/cuda/allocatable.cpp
similarity index 94%
rename from flang/runtime/CUDA/allocatable.cpp
rename to flang-rt/lib/cuda/allocatable.cpp
index 6df3b06793b3e..b773e802c90ff 100644
--- a/flang/runtime/CUDA/allocatable.cpp
+++ b/flang-rt/lib/cuda/allocatable.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/CUDA/allocatable.cpp --------------------------------------===//
+//===-- 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,9 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/allocatable.h"
-#include "../assign-impl.h"
-#include "../stat.h"
-#include "../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/runtime/CUDA/allocator.cpp b/flang-rt/lib/cuda/allocator.cpp
similarity index 86%
rename from flang/runtime/CUDA/allocator.cpp
rename to flang-rt/lib/cuda/allocator.cpp
index 368c1124ef70a..4199bf04b33f0 100644
--- a/flang/runtime/CUDA/allocator.cpp
+++ b/flang-rt/lib/cuda/allocator.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/CUDA/allocator.cpp ----------------------------------------===//
+//===-- 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.
@@ -7,13 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/allocator.h"
-#include "../derived.h"
-#include "../stat.h"
-#include "../terminator.h"
-#include "../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/type-info.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
 #include "flang/Runtime/CUDA/common.h"
-#include "flang/Runtime/allocator-registry.h"
 #include "flang/Support/Fortran.h"
 
 #include "cuda_runtime.h"
diff --git a/flang/runtime/CUDA/descriptor.cpp b/flang-rt/lib/cuda/descriptor.cpp
similarity index 91%
rename from flang/runtime/CUDA/descriptor.cpp
rename to flang-rt/lib/cuda/descriptor.cpp
index 947eeb66aa3d6..d44ab2e45d2a8 100644
--- a/flang/runtime/CUDA/descriptor.cpp
+++ b/flang-rt/lib/cuda/descriptor.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/CUDA/descriptor.cpp ---------------------------------------===//
+//===-- 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,10 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/descriptor.h"
-#include "../terminator.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/allocator.h"
 #include "flang/Runtime/CUDA/common.h"
-#include "flang/Runtime/descriptor.h"
 
 #include "cuda_runtime.h"
 
diff --git a/flang/runtime/CUDA/init.cpp b/flang-rt/lib/cuda/init.cpp
similarity index 81%
rename from flang/runtime/CUDA/init.cpp
rename to flang-rt/lib/cuda/init.cpp
index 2bffce842b952..d79bffc32424d 100644
--- a/flang/runtime/CUDA/init.cpp
+++ b/flang-rt/lib/cuda/init.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/CUDA/init.cpp ---------------------------------------------===//
+//===-- 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 "../environment.h"
-#include "../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/runtime/CUDA/kernel.cpp b/flang-rt/lib/cuda/kernel.cpp
similarity index 98%
rename from flang/runtime/CUDA/kernel.cpp
rename to flang-rt/lib/cuda/kernel.cpp
index 02d89fb8423a5..75eb639817b9a 100644
--- a/flang/runtime/CUDA/kernel.cpp
+++ b/flang-rt/lib/cuda/kernel.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/CUDA/kernel.cpp -------------------------------------------===//
+//===-- 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 "../terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 
 #include "cuda_runtime.h"
diff --git a/flang/runtime/CUDA/memmove-function.cpp b/flang-rt/lib/cuda/memmove-function.cpp
similarity index 90%
rename from flang/runtime/CUDA/memmove-function.cpp
rename to flang-rt/lib/cuda/memmove-function.cpp
index 3ba9fa7e0f7f7..a7eb0cf1a3e7a 100644
--- a/flang/runtime/CUDA/memmove-function.cpp
+++ b/flang-rt/lib/cuda/memmove-function.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/CUDA/memmove-function.cpp ---------------------------------===//
+//===-- 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 "../terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 
 #include "cuda_runtime.h"
diff --git a/flang/runtime/CUDA/memory.cpp b/flang-rt/lib/cuda/memory.cpp
similarity index 96%
rename from flang/runtime/CUDA/memory.cpp
rename to flang-rt/lib/cuda/memory.cpp
index 0bbb493d2db91..7ead0dd35e1d9 100644
--- a/flang/runtime/CUDA/memory.cpp
+++ b/flang-rt/lib/cuda/memory.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/CUDA/memory.cpp -------------------------------------------===//
+//===-- 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 "../assign-impl.h"
-#include "../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/runtime/CUDA/pointer.cpp b/flang-rt/lib/cuda/pointer.cpp
similarity index 92%
rename from flang/runtime/CUDA/pointer.cpp
rename to flang-rt/lib/cuda/pointer.cpp
index d3ebe97b4e4ac..c2559ecb9a6f2 100644
--- a/flang/runtime/CUDA/pointer.cpp
+++ b/flang-rt/lib/cuda/pointer.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/CUDA/pointer.cpp ------------------------------------------===//
+//===-- 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,9 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/pointer.h"
-#include "../assign-impl.h"
-#include "../stat.h"
-#include "../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/runtime/CUDA/registration.cpp b/flang-rt/lib/cuda/registration.cpp
similarity index 92%
rename from flang/runtime/CUDA/registration.cpp
rename to flang-rt/lib/cuda/registration.cpp
index b7b6ef389bffb..60b0e491b6ffd 100644
--- a/flang/runtime/CUDA/registration.cpp
+++ b/flang-rt/lib/cuda/registration.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/CUDA/registration.cpp -------------------------------------===//
+//===-- 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 "../terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/common.h"
 
 #include "cuda_runtime.h"
diff --git a/flang-rt/lib/quadmath/CMakeLists.txt b/flang-rt/lib/quadmath/CMakeLists.txt
new file mode 100644
index 0000000000000..4f113216b42c8
--- /dev/null
+++ b/flang-rt/lib/quadmath/CMakeLists.txt
@@ -0,0 +1,136 @@
+#===-- 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.
+# 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 libflang_rt.quadmath 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 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)
+  else ()
+    add_flangrt_library(flang_rt.quadmath STATIC INSTALL_WITH_TOOLCHAIN
+      ${sources})
+    target_include_directories(flang_rt.quadmath 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/runtime/Float128Math/acos.cpp b/flang-rt/lib/quadmath/acos.cpp
similarity index 87%
rename from flang/runtime/Float128Math/acos.cpp
rename to flang-rt/lib/quadmath/acos.cpp
index d9b4950aa1e35..d094121f0f678 100644
--- a/flang/runtime/Float128Math/acos.cpp
+++ b/flang-rt/lib/quadmath/acos.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/acos.cpp -------------------------------------===//
+//===-- 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/runtime/Float128Math/acosh.cpp b/flang-rt/lib/quadmath/acosh.cpp
similarity index 87%
rename from flang/runtime/Float128Math/acosh.cpp
rename to flang-rt/lib/quadmath/acosh.cpp
index c572673ef55e6..968aa489d15a2 100644
--- a/flang/runtime/Float128Math/acosh.cpp
+++ b/flang-rt/lib/quadmath/acosh.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/acosh.cpp ------------------------------------===//
+//===-- 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/runtime/Float128Math/asin.cpp b/flang-rt/lib/quadmath/asin.cpp
similarity index 87%
rename from flang/runtime/Float128Math/asin.cpp
rename to flang-rt/lib/quadmath/asin.cpp
index 57b6ffd967360..c3345cd3d748a 100644
--- a/flang/runtime/Float128Math/asin.cpp
+++ b/flang-rt/lib/quadmath/asin.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/asin.cpp -------------------------------------===//
+//===-- 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/runtime/Float128Math/asinh.cpp b/flang-rt/lib/quadmath/asinh.cpp
similarity index 87%
rename from flang/runtime/Float128Math/asinh.cpp
rename to flang-rt/lib/quadmath/asinh.cpp
index 03dded722b254..1023b678b6131 100644
--- a/flang/runtime/Float128Math/asinh.cpp
+++ b/flang-rt/lib/quadmath/asinh.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/asinh.cpp ------------------------------------===//
+//===-- 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/runtime/Float128Math/atan.cpp b/flang-rt/lib/quadmath/atan.cpp
similarity index 87%
rename from flang/runtime/Float128Math/atan.cpp
rename to flang-rt/lib/quadmath/atan.cpp
index 19c86cae8867b..6379df3275c03 100644
--- a/flang/runtime/Float128Math/atan.cpp
+++ b/flang-rt/lib/quadmath/atan.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/atan.cpp -------------------------------------===//
+//===-- 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/runtime/Float128Math/atan2.cpp b/flang-rt/lib/quadmath/atan2.cpp
similarity index 88%
rename from flang/runtime/Float128Math/atan2.cpp
rename to flang-rt/lib/quadmath/atan2.cpp
index 09d666ae14304..7527b224cb3a5 100644
--- a/flang/runtime/Float128Math/atan2.cpp
+++ b/flang-rt/lib/quadmath/atan2.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/atan2.cpp ------------------------------------===//
+//===-- 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/runtime/Float128Math/atanh.cpp b/flang-rt/lib/quadmath/atanh.cpp
similarity index 87%
rename from flang/runtime/Float128Math/atanh.cpp
rename to flang-rt/lib/quadmath/atanh.cpp
index 442d9beafae47..c7455fcb7ca67 100644
--- a/flang/runtime/Float128Math/atanh.cpp
+++ b/flang-rt/lib/quadmath/atanh.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/atanh.cpp ------------------------------------===//
+//===-- 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/runtime/Float128Math/ceil.cpp b/flang-rt/lib/quadmath/ceil.cpp
similarity index 87%
rename from flang/runtime/Float128Math/ceil.cpp
rename to flang-rt/lib/quadmath/ceil.cpp
index 48e20b2b41577..03a98bedfdc03 100644
--- a/flang/runtime/Float128Math/ceil.cpp
+++ b/flang-rt/lib/quadmath/ceil.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/ceil.cpp -------------------------------------===//
+//===-- 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/runtime/Float128Math/complex-math.c b/flang-rt/lib/quadmath/complex-math.c
similarity index 92%
rename from flang/runtime/Float128Math/complex-math.c
rename to flang-rt/lib/quadmath/complex-math.c
index a7e32f3ef755b..e485fd7eb5dbb 100644
--- a/flang/runtime/Float128Math/complex-math.c
+++ b/flang-rt/lib/quadmath/complex-math.c
@@ -1,11 +1,10 @@
-/*===-- runtime/Float128Math/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.
  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  *
- * ===-----------------------------------------------------------------------===
- */
+ *===----------------------------------------------------------------------===*/
 
 #include "complex-math.h"
 
diff --git a/flang/runtime/Float128Math/complex-math.h b/flang-rt/lib/quadmath/complex-math.h
similarity index 88%
rename from flang/runtime/Float128Math/complex-math.h
rename to flang-rt/lib/quadmath/complex-math.h
index cf5e980a39b6b..424ed84da4e01 100644
--- a/flang/runtime/Float128Math/complex-math.h
+++ b/flang-rt/lib/quadmath/complex-math.h
@@ -1,4 +1,4 @@
-/*===-- runtime/Float128Math/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 FORTRAN_RUNTIME_FLOAT128MATH_COMPLEX_MATH_H_
-#define FORTRAN_RUNTIME_FLOAT128MATH_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 /* FORTRAN_RUNTIME_FLOAT128MATH_COMPLEX_MATH_H_ */
+#endif /* FLANG_RT_QUADMATH_COMPLEX_MATH_H_ */
diff --git a/flang/runtime/Float128Math/cos.cpp b/flang-rt/lib/quadmath/cos.cpp
similarity index 87%
rename from flang/runtime/Float128Math/cos.cpp
rename to flang-rt/lib/quadmath/cos.cpp
index d1b3e0e736ca3..1116080c53d2a 100644
--- a/flang/runtime/Float128Math/cos.cpp
+++ b/flang-rt/lib/quadmath/cos.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/cos.cpp --------------------------------------===//
+//===-- 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/runtime/Float128Math/cosh.cpp b/flang-rt/lib/quadmath/cosh.cpp
similarity index 87%
rename from flang/runtime/Float128Math/cosh.cpp
rename to flang-rt/lib/quadmath/cosh.cpp
index 9fe5b61d8f95f..dd5978e5e5f08 100644
--- a/flang/runtime/Float128Math/cosh.cpp
+++ b/flang-rt/lib/quadmath/cosh.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/cosh.cpp -------------------------------------===//
+//===-- 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/runtime/Float128Math/erf.cpp b/flang-rt/lib/quadmath/erf.cpp
similarity index 87%
rename from flang/runtime/Float128Math/erf.cpp
rename to flang-rt/lib/quadmath/erf.cpp
index 2a553bd395e88..0021b7900f6a1 100644
--- a/flang/runtime/Float128Math/erf.cpp
+++ b/flang-rt/lib/quadmath/erf.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/erf.cpp --------------------------------------===//
+//===-- 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/runtime/Float128Math/erfc.cpp b/flang-rt/lib/quadmath/erfc.cpp
similarity index 87%
rename from flang/runtime/Float128Math/erfc.cpp
rename to flang-rt/lib/quadmath/erfc.cpp
index 2435ed2786cb3..5b80fb475b3fc 100644
--- a/flang/runtime/Float128Math/erfc.cpp
+++ b/flang-rt/lib/quadmath/erfc.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/erfc.cpp -------------------------------------===//
+//===-- 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/runtime/Float128Math/exp.cpp b/flang-rt/lib/quadmath/exp.cpp
similarity index 87%
rename from flang/runtime/Float128Math/exp.cpp
rename to flang-rt/lib/quadmath/exp.cpp
index 5ca87d9dd25d2..94e444c3b00c7 100644
--- a/flang/runtime/Float128Math/exp.cpp
+++ b/flang-rt/lib/quadmath/exp.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/exp.cpp --------------------------------------===//
+//===-- 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/runtime/Float128Math/exponent.cpp b/flang-rt/lib/quadmath/exponent.cpp
similarity index 90%
rename from flang/runtime/Float128Math/exponent.cpp
rename to flang-rt/lib/quadmath/exponent.cpp
index 237cde34e8691..0d2fa6478cca8 100644
--- a/flang/runtime/Float128Math/exponent.cpp
+++ b/flang-rt/lib/quadmath/exponent.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/exponent.cpp ---------------------------------===//
+//===-- 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/runtime/Float128Math/floor.cpp b/flang-rt/lib/quadmath/floor.cpp
similarity index 87%
rename from flang/runtime/Float128Math/floor.cpp
rename to flang-rt/lib/quadmath/floor.cpp
index 28f9c7b55dd51..e5dfb33db82ce 100644
--- a/flang/runtime/Float128Math/floor.cpp
+++ b/flang-rt/lib/quadmath/floor.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/floor.cpp ------------------------------------===//
+//===-- 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/runtime/Float128Math/fma.cpp b/flang-rt/lib/quadmath/fma.cpp
similarity index 89%
rename from flang/runtime/Float128Math/fma.cpp
rename to flang-rt/lib/quadmath/fma.cpp
index 87176c25dd604..910303af32339 100644
--- a/flang/runtime/Float128Math/fma.cpp
+++ b/flang-rt/lib/quadmath/fma.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/fma.cpp --------------------------------------===//
+//===-- 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/runtime/Float128Math/fraction.cpp b/flang-rt/lib/quadmath/fraction.cpp
similarity index 87%
rename from flang/runtime/Float128Math/fraction.cpp
rename to flang-rt/lib/quadmath/fraction.cpp
index 45ec12cd77518..a9927666a7b00 100644
--- a/flang/runtime/Float128Math/fraction.cpp
+++ b/flang-rt/lib/quadmath/fraction.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/fraction.cpp ---------------------------------===//
+//===-- 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/runtime/Float128Math/hypot.cpp b/flang-rt/lib/quadmath/hypot.cpp
similarity index 88%
rename from flang/runtime/Float128Math/hypot.cpp
rename to flang-rt/lib/quadmath/hypot.cpp
index 03049b06d3a24..3090d0b2aff74 100644
--- a/flang/runtime/Float128Math/hypot.cpp
+++ b/flang-rt/lib/quadmath/hypot.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/hypot.cpp ------------------------------------===//
+//===-- 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/runtime/Float128Math/j0.cpp b/flang-rt/lib/quadmath/j0.cpp
similarity index 87%
rename from flang/runtime/Float128Math/j0.cpp
rename to flang-rt/lib/quadmath/j0.cpp
index 7207cbe1a92e7..06df1c2aca452 100644
--- a/flang/runtime/Float128Math/j0.cpp
+++ b/flang-rt/lib/quadmath/j0.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/j0.cpp ---------------------------------------===//
+//===-- 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/runtime/Float128Math/j1.cpp b/flang-rt/lib/quadmath/j1.cpp
similarity index 87%
rename from flang/runtime/Float128Math/j1.cpp
rename to flang-rt/lib/quadmath/j1.cpp
index 9e49bcbc32ca4..d8a1f123b95e6 100644
--- a/flang/runtime/Float128Math/j1.cpp
+++ b/flang-rt/lib/quadmath/j1.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/j1.cpp ---------------------------------------===//
+//===-- 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/runtime/Float128Math/jn.cpp b/flang-rt/lib/quadmath/jn.cpp
similarity index 87%
rename from flang/runtime/Float128Math/jn.cpp
rename to flang-rt/lib/quadmath/jn.cpp
index 37e5f428e5e26..a53e305bb8746 100644
--- a/flang/runtime/Float128Math/jn.cpp
+++ b/flang-rt/lib/quadmath/jn.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/jn.cpp ---------------------------------------===//
+//===-- 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/runtime/Float128Math/lgamma.cpp b/flang-rt/lib/quadmath/lgamma.cpp
similarity index 87%
rename from flang/runtime/Float128Math/lgamma.cpp
rename to flang-rt/lib/quadmath/lgamma.cpp
index 54d0dd8083868..b96dff1d0d72a 100644
--- a/flang/runtime/Float128Math/lgamma.cpp
+++ b/flang-rt/lib/quadmath/lgamma.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/lgamma.cpp -----------------------------------===//
+//===-- 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/runtime/Float128Math/llround.cpp b/flang-rt/lib/quadmath/llround.cpp
similarity index 87%
rename from flang/runtime/Float128Math/llround.cpp
rename to flang-rt/lib/quadmath/llround.cpp
index f0c53ccdf66fd..8f2913d390431 100644
--- a/flang/runtime/Float128Math/llround.cpp
+++ b/flang-rt/lib/quadmath/llround.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/llround.cpp ----------------------------------===//
+//===-- 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/runtime/Float128Math/log.cpp b/flang-rt/lib/quadmath/log.cpp
similarity index 87%
rename from flang/runtime/Float128Math/log.cpp
rename to flang-rt/lib/quadmath/log.cpp
index 28fec1958f10b..0c489c922a3fc 100644
--- a/flang/runtime/Float128Math/log.cpp
+++ b/flang-rt/lib/quadmath/log.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/log.cpp --------------------------------------===//
+//===-- 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/runtime/Float128Math/log10.cpp b/flang-rt/lib/quadmath/log10.cpp
similarity index 87%
rename from flang/runtime/Float128Math/log10.cpp
rename to flang-rt/lib/quadmath/log10.cpp
index f844d508f8d3b..a2f222e15a147 100644
--- a/flang/runtime/Float128Math/log10.cpp
+++ b/flang-rt/lib/quadmath/log10.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/log10.cpp ------------------------------------===//
+//===-- 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/runtime/Float128Math/lround.cpp b/flang-rt/lib/quadmath/lround.cpp
similarity index 87%
rename from flang/runtime/Float128Math/lround.cpp
rename to flang-rt/lib/quadmath/lround.cpp
index 8c2d3315c62a7..539ee107a3881 100644
--- a/flang/runtime/Float128Math/lround.cpp
+++ b/flang-rt/lib/quadmath/lround.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/lround.cpp -----------------------------------===//
+//===-- 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/runtime/Float128Math/math-entries.h b/flang-rt/lib/quadmath/math-entries.h
similarity index 96%
rename from flang/runtime/Float128Math/math-entries.h
rename to flang-rt/lib/quadmath/math-entries.h
index a94503fe8e67a..6e47f32cc8a43 100644
--- a/flang/runtime/Float128Math/math-entries.h
+++ b/flang-rt/lib/quadmath/math-entries.h
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/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,10 +6,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FORTRAN_RUNTIME_FLOAT128MATH_MATH_ENTRIES_H_
-#define FORTRAN_RUNTIME_FLOAT128MATH_MATH_ENTRIES_H_
-#include "terminator.h"
-#include "tools.h"
+#ifndef FLANG_RT_QUADMATH_MATH_ENTRIES_H_
+#define FLANG_RT_QUADMATH_MATH_ENTRIES_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>
@@ -231,4 +232,4 @@ DEFINE_SIMPLE_ALIAS(Yn, ynl)
 
 } // namespace Fortran::runtime
 
-#endif // FORTRAN_RUNTIME_FLOAT128MATH_MATH_ENTRIES_H_
+#endif // FLANG_RT_QUADMATH_MATH_ENTRIES_H_
diff --git a/flang/runtime/Float128Math/mod-real.cpp b/flang-rt/lib/quadmath/mod-real.cpp
similarity index 88%
rename from flang/runtime/Float128Math/mod-real.cpp
rename to flang-rt/lib/quadmath/mod-real.cpp
index e831c2df4abc1..0230964e3ddc2 100644
--- a/flang/runtime/Float128Math/mod-real.cpp
+++ b/flang-rt/lib/quadmath/mod-real.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/mod-real.cpp ---------------------------------===//
+//===-- 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/runtime/Float128Math/modulo-real.cpp b/flang-rt/lib/quadmath/modulo-real.cpp
similarity index 89%
rename from flang/runtime/Float128Math/modulo-real.cpp
rename to flang-rt/lib/quadmath/modulo-real.cpp
index 88729da7e3987..0f28747b86985 100644
--- a/flang/runtime/Float128Math/modulo-real.cpp
+++ b/flang-rt/lib/quadmath/modulo-real.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/modulo-real.cpp ------------------------------===//
+//===-- 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/runtime/Float128Math/nearbyint.cpp b/flang-rt/lib/quadmath/nearbyint.cpp
similarity index 87%
rename from flang/runtime/Float128Math/nearbyint.cpp
rename to flang-rt/lib/quadmath/nearbyint.cpp
index 9eecb0c5f3e2f..3811fc53d1d82 100644
--- a/flang/runtime/Float128Math/nearbyint.cpp
+++ b/flang-rt/lib/quadmath/nearbyint.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/nearbyint.cpp --------------------------------===//
+//===-- 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/runtime/Float128Math/nearest.cpp b/flang-rt/lib/quadmath/nearest.cpp
similarity index 88%
rename from flang/runtime/Float128Math/nearest.cpp
rename to flang-rt/lib/quadmath/nearest.cpp
index 50f6e7ea75a60..8c1969a7b596c 100644
--- a/flang/runtime/Float128Math/nearest.cpp
+++ b/flang-rt/lib/quadmath/nearest.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/nearest.cpp ----------------------------------===//
+//===-- 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/runtime/Float128Math/norm2.cpp b/flang-rt/lib/quadmath/norm2.cpp
similarity index 89%
rename from flang/runtime/Float128Math/norm2.cpp
rename to flang-rt/lib/quadmath/norm2.cpp
index 18e9c8cc8a2b9..e98f4007737d1 100644
--- a/flang/runtime/Float128Math/norm2.cpp
+++ b/flang-rt/lib/quadmath/norm2.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/norm2.cpp ------------------------------------===//
+//===-- 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 "reduction-templates.h"
+#include "flang-rt/runtime/reduction-templates.h"
 
 namespace Fortran::runtime {
 extern "C" {
diff --git a/flang/runtime/Float128Math/numeric-template-specs.h b/flang-rt/lib/quadmath/numeric-template-specs.h
similarity index 82%
rename from flang/runtime/Float128Math/numeric-template-specs.h
rename to flang-rt/lib/quadmath/numeric-template-specs.h
index a0a77230c3e9e..e215ad70eca14 100644
--- a/flang/runtime/Float128Math/numeric-template-specs.h
+++ b/flang-rt/lib/quadmath/numeric-template-specs.h
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/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 FORTRAN_RUNTIME_FLOAT128MATH_NUMERIC_TEMPLATE_SPECS_H_
-#define FORTRAN_RUNTIME_FLOAT128MATH_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 "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 // FORTRAN_RUNTIME_FLOAT128MATH_NUMERIC_TEMPLATE_SPECS_H_
+#endif // FLANG_RT_QUADMATH_NUMERIC_TEMPLATE_SPECS_H_
diff --git a/flang/runtime/Float128Math/pow.cpp b/flang-rt/lib/quadmath/pow.cpp
similarity index 88%
rename from flang/runtime/Float128Math/pow.cpp
rename to flang-rt/lib/quadmath/pow.cpp
index 99aae04c65ecb..29c0536254658 100644
--- a/flang/runtime/Float128Math/pow.cpp
+++ b/flang-rt/lib/quadmath/pow.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/pow.cpp --------------------------------------===//
+//===-- 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/runtime/Float128Math/random.cpp b/flang-rt/lib/quadmath/random.cpp
similarity index 83%
rename from flang/runtime/Float128Math/random.cpp
rename to flang-rt/lib/quadmath/random.cpp
index 93c5c14cee37d..a6d22733ebce4 100644
--- a/flang/runtime/Float128Math/random.cpp
+++ b/flang-rt/lib/quadmath/random.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/random.cpp -----------------------------------===//
+//===-- 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 "random-templates.h"
+#include "flang-rt/runtime/random-templates.h"
 
 using namespace Fortran::runtime::random;
 extern "C" {
diff --git a/flang/runtime/Float128Math/remainder.cpp b/flang-rt/lib/quadmath/remainder.cpp
similarity index 88%
rename from flang/runtime/Float128Math/remainder.cpp
rename to flang-rt/lib/quadmath/remainder.cpp
index e5c2793dab71a..4b68cdd6ac9de 100644
--- a/flang/runtime/Float128Math/remainder.cpp
+++ b/flang-rt/lib/quadmath/remainder.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/remainder.cpp --------------------------------===//
+//===-- 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/runtime/Float128Math/round.cpp b/flang-rt/lib/quadmath/round.cpp
similarity index 89%
rename from flang/runtime/Float128Math/round.cpp
rename to flang-rt/lib/quadmath/round.cpp
index e79ce30536b3b..844338f5e6413 100644
--- a/flang/runtime/Float128Math/round.cpp
+++ b/flang-rt/lib/quadmath/round.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/round.cpp ------------------------------------===//
+//===-- 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/runtime/Float128Math/rrspacing.cpp b/flang-rt/lib/quadmath/rrspacing.cpp
similarity index 87%
rename from flang/runtime/Float128Math/rrspacing.cpp
rename to flang-rt/lib/quadmath/rrspacing.cpp
index 04cefc049bbee..e8613f4d7d7e2 100644
--- a/flang/runtime/Float128Math/rrspacing.cpp
+++ b/flang-rt/lib/quadmath/rrspacing.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/rrspacing.cpp --------------------------------===//
+//===-- 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/runtime/Float128Math/scale.cpp b/flang-rt/lib/quadmath/scale.cpp
similarity index 90%
rename from flang/runtime/Float128Math/scale.cpp
rename to flang-rt/lib/quadmath/scale.cpp
index 6b083afbdf4d1..3d919f85a4487 100644
--- a/flang/runtime/Float128Math/scale.cpp
+++ b/flang-rt/lib/quadmath/scale.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/scale.cpp ------------------------------------===//
+//===-- 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/runtime/Float128Math/set-exponent.cpp b/flang-rt/lib/quadmath/set-exponent.cpp
similarity index 88%
rename from flang/runtime/Float128Math/set-exponent.cpp
rename to flang-rt/lib/quadmath/set-exponent.cpp
index 63c5b325085fb..d6b582e7c4f38 100644
--- a/flang/runtime/Float128Math/set-exponent.cpp
+++ b/flang-rt/lib/quadmath/set-exponent.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/set-exponent.cpp -----------------------------===//
+//===-- 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/runtime/Float128Math/sin.cpp b/flang-rt/lib/quadmath/sin.cpp
similarity index 87%
rename from flang/runtime/Float128Math/sin.cpp
rename to flang-rt/lib/quadmath/sin.cpp
index 99fa3e493e694..dcff2f9ce02ca 100644
--- a/flang/runtime/Float128Math/sin.cpp
+++ b/flang-rt/lib/quadmath/sin.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/sin.cpp --------------------------------------===//
+//===-- 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/runtime/Float128Math/sinh.cpp b/flang-rt/lib/quadmath/sinh.cpp
similarity index 87%
rename from flang/runtime/Float128Math/sinh.cpp
rename to flang-rt/lib/quadmath/sinh.cpp
index b6cd96963612e..3ab7280f705a6 100644
--- a/flang/runtime/Float128Math/sinh.cpp
+++ b/flang-rt/lib/quadmath/sinh.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/sinh.cpp -------------------------------------===//
+//===-- 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/runtime/Float128Math/spacing.cpp b/flang-rt/lib/quadmath/spacing.cpp
similarity index 87%
rename from flang/runtime/Float128Math/spacing.cpp
rename to flang-rt/lib/quadmath/spacing.cpp
index fc6aa2c4ec2d8..1d7ecdb4852d2 100644
--- a/flang/runtime/Float128Math/spacing.cpp
+++ b/flang-rt/lib/quadmath/spacing.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/spacing.cpp ----------------------------------===//
+//===-- 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/runtime/Float128Math/sqrt.cpp b/flang-rt/lib/quadmath/sqrt.cpp
similarity index 87%
rename from flang/runtime/Float128Math/sqrt.cpp
rename to flang-rt/lib/quadmath/sqrt.cpp
index 871c66e007984..6e0d11a6697f0 100644
--- a/flang/runtime/Float128Math/sqrt.cpp
+++ b/flang-rt/lib/quadmath/sqrt.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/sqrt.cpp -------------------------------------===//
+//===-- 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/runtime/Float128Math/tan.cpp b/flang-rt/lib/quadmath/tan.cpp
similarity index 87%
rename from flang/runtime/Float128Math/tan.cpp
rename to flang-rt/lib/quadmath/tan.cpp
index 2d6f448ba8955..6f09b93060228 100644
--- a/flang/runtime/Float128Math/tan.cpp
+++ b/flang-rt/lib/quadmath/tan.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/tan.cpp --------------------------------------===//
+//===-- 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/runtime/Float128Math/tanh.cpp b/flang-rt/lib/quadmath/tanh.cpp
similarity index 87%
rename from flang/runtime/Float128Math/tanh.cpp
rename to flang-rt/lib/quadmath/tanh.cpp
index f6321f4819191..214a18d5c3778 100644
--- a/flang/runtime/Float128Math/tanh.cpp
+++ b/flang-rt/lib/quadmath/tanh.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/tanh.cpp -------------------------------------===//
+//===-- 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/runtime/Float128Math/tgamma.cpp b/flang-rt/lib/quadmath/tgamma.cpp
similarity index 87%
rename from flang/runtime/Float128Math/tgamma.cpp
rename to flang-rt/lib/quadmath/tgamma.cpp
index 98fd792a63330..2b05a60dcaabb 100644
--- a/flang/runtime/Float128Math/tgamma.cpp
+++ b/flang-rt/lib/quadmath/tgamma.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/tgamma.cpp -----------------------------------===//
+//===-- 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/runtime/Float128Math/trunc.cpp b/flang-rt/lib/quadmath/trunc.cpp
similarity index 89%
rename from flang/runtime/Float128Math/trunc.cpp
rename to flang-rt/lib/quadmath/trunc.cpp
index 54fa33176813c..cd7c27b569fc3 100644
--- a/flang/runtime/Float128Math/trunc.cpp
+++ b/flang-rt/lib/quadmath/trunc.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/trunc.cpp ------------------------------------===//
+//===-- 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/runtime/Float128Math/y0.cpp b/flang-rt/lib/quadmath/y0.cpp
similarity index 87%
rename from flang/runtime/Float128Math/y0.cpp
rename to flang-rt/lib/quadmath/y0.cpp
index 0b3059b4cfe25..9db04277660ad 100644
--- a/flang/runtime/Float128Math/y0.cpp
+++ b/flang-rt/lib/quadmath/y0.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/y0.cpp ---------------------------------------===//
+//===-- 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/runtime/Float128Math/y1.cpp b/flang-rt/lib/quadmath/y1.cpp
similarity index 87%
rename from flang/runtime/Float128Math/y1.cpp
rename to flang-rt/lib/quadmath/y1.cpp
index cb39d87034dc7..92e658195f3d9 100644
--- a/flang/runtime/Float128Math/y1.cpp
+++ b/flang-rt/lib/quadmath/y1.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/y1.cpp ---------------------------------------===//
+//===-- 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/runtime/Float128Math/yn.cpp b/flang-rt/lib/quadmath/yn.cpp
similarity index 87%
rename from flang/runtime/Float128Math/yn.cpp
rename to flang-rt/lib/quadmath/yn.cpp
index bef8f9457df2f..20c0bc9d5218e 100644
--- a/flang/runtime/Float128Math/yn.cpp
+++ b/flang-rt/lib/quadmath/yn.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/Float128Math/yn.cpp ---------------------------------------===//
+//===-- 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/runtime/CMakeLists.txt b/flang-rt/lib/runtime/CMakeLists.txt
new file mode 100644
index 0000000000000..0afcbf2783533
--- /dev/null
+++ b/flang-rt/lib/runtime/CMakeLists.txt
@@ -0,0 +1,215 @@
+#===-- 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.
+# 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 flang_rt.quadmath
+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.runtime STATIC
+    ${sources}
+    LINK_LIBRARIES ${Backtrace_LIBRARY}
+    INSTALL_WITH_TOOLCHAIN
+    ADDITIONAL_HEADERS ${public_headers} ${private_headers}
+  )
+
+  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.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.runtime)
+  set_target_properties(flang_rt.runtime PROPERTIES FOLDER "Flang-RT/Meta")
+
+  function (add_win_flangrt_runtime libtype suffix msvc_lib)
+    set(name "flang_rt.runtime.${suffix}")
+    add_flangrt_library(${name} ${libtype}
+        ${sources}
+        ${ARGN}
+        LINK_LIBRARIES ${Backtrace_LIBRARY}
+        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.runtime ${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_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
+  # 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_runtime(STATIC unittest "" EXCLUDE_FROM_ALL)
+  else ()
+    string(TOLOWER ${CMAKE_BUILD_TYPE} build_type)
+    if (build_type STREQUAL "debug")
+      add_library(flang_rt.runtime.unittest ALIAS flang_rt.runtime.dynamic_dbg)
+    else ()
+      add_library(flang_rt.runtime.unittest ALIAS flang_rt.runtime.dynamic)
+    endif ()
+  endif ()
+endif()
diff --git a/flang/runtime/ISO_Fortran_binding.cpp b/flang-rt/lib/runtime/ISO_Fortran_binding.cpp
similarity index 97%
rename from flang/runtime/ISO_Fortran_binding.cpp
rename to flang-rt/lib/runtime/ISO_Fortran_binding.cpp
index 64e239f498230..a5f8b357ae0b8 100644
--- a/flang/runtime/ISO_Fortran_binding.cpp
+++ b/flang-rt/lib/runtime/ISO_Fortran_binding.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/ISO_Fortran_binding.cpp -----------------------------------===//
+//===-- 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,11 +10,11 @@
 // as specified in section 18.5.5 of Fortran 2018.
 
 #include "ISO_Fortran_util.h"
-#include "terminator.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/descriptor.h"
 #include "flang/Runtime/pointer.h"
-#include "flang/Runtime/type-code.h"
 #include <cstdlib>
 
 namespace Fortran::ISO {
diff --git a/flang/runtime/ISO_Fortran_util.h b/flang-rt/lib/runtime/ISO_Fortran_util.h
similarity index 90%
rename from flang/runtime/ISO_Fortran_util.h
rename to flang-rt/lib/runtime/ISO_Fortran_util.h
index aca9aee8c5718..9bbc03eefc490 100644
--- a/flang/runtime/ISO_Fortran_util.h
+++ b/flang-rt/lib/runtime/ISO_Fortran_util.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,15 +6,15 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FORTRAN_RUNTIME_ISO_FORTRAN_UTIL_H_
-#define FORTRAN_RUNTIME_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 "terminator.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/descriptor.h"
-#include "flang/Runtime/type-code.h"
 #include <cstdlib>
 
 namespace Fortran::ISO {
@@ -99,4 +99,4 @@ static inline RT_API_ATTRS void EstablishDescriptor(CFI_cdesc_t *descriptor,
   }
 }
 } // namespace Fortran::ISO
-#endif // FORTRAN_RUNTIME_ISO_FORTRAN_UTIL_H_
+#endif // FLANG_RT_RUNTIME_ISO_FORTRAN_UTIL_H_
diff --git a/flang/runtime/allocatable.cpp b/flang-rt/lib/runtime/allocatable.cpp
similarity index 96%
rename from flang/runtime/allocatable.cpp
rename to flang-rt/lib/runtime/allocatable.cpp
index 686114bf86eaf..a51816129199a 100644
--- a/flang/runtime/allocatable.cpp
+++ b/flang-rt/lib/runtime/allocatable.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/allocatable.cpp -------------------------------------------===//
+//===-- 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,14 +7,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/allocatable.h"
-#include "assign-impl.h"
-#include "derived.h"
-#include "stat.h"
-#include "terminator.h"
-#include "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"
-#include "flang/Runtime/descriptor.h"
 
 namespace Fortran::runtime {
 extern "C" {
diff --git a/flang/runtime/allocator-registry.cpp b/flang-rt/lib/runtime/allocator-registry.cpp
similarity index 87%
rename from flang/runtime/allocator-registry.cpp
rename to flang-rt/lib/runtime/allocator-registry.cpp
index f5670331d6dbe..f8a8daaf8e748 100644
--- a/flang/runtime/allocator-registry.cpp
+++ b/flang-rt/lib/runtime/allocator-registry.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/allocator-registry.cpp ------------------------------------===//
+//===-- 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/Runtime/allocator-registry.h"
-#include "terminator.h"
+#include "flang-rt/runtime/allocator-registry.h"
+#include "flang-rt/runtime/terminator.h"
 
 namespace Fortran::runtime {
 
diff --git a/flang/runtime/array-constructor.cpp b/flang-rt/lib/runtime/array-constructor.cpp
similarity index 96%
rename from flang/runtime/array-constructor.cpp
rename to flang-rt/lib/runtime/array-constructor.cpp
index c6953167f5fb2..7e267e714927f 100644
--- a/flang/runtime/array-constructor.cpp
+++ b/flang-rt/lib/runtime/array-constructor.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/array-constructor.cpp -------------------------------------===//
+//===-- 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,14 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang/Runtime/array-constructor.h"
-#include "derived.h"
-#include "terminator.h"
-#include "tools.h"
-#include "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"
-#include "flang/Runtime/descriptor.h"
 
 namespace Fortran::runtime {
 
diff --git a/flang/runtime/assign.cpp b/flang-rt/lib/runtime/assign.cpp
similarity index 98%
rename from flang/runtime/assign.cpp
rename to flang-rt/lib/runtime/assign.cpp
index 8f0efaa376c19..a1f3715f278c1 100644
--- a/flang/runtime/assign.cpp
+++ b/flang-rt/lib/runtime/assign.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/assign.cpp ------------------------------------------------===//
+//===-- 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 "assign-impl.h"
-#include "derived.h"
-#include "stat.h"
-#include "terminator.h"
-#include "tools.h"
-#include "type-info.h"
-#include "flang/Runtime/descriptor.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/runtime/buffer.cpp b/flang-rt/lib/runtime/buffer.cpp
similarity index 88%
rename from flang/runtime/buffer.cpp
rename to flang-rt/lib/runtime/buffer.cpp
index 7b4869d69c2e5..4cf85e13b6d36 100644
--- a/flang/runtime/buffer.cpp
+++ b/flang-rt/lib/runtime/buffer.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/buffer.cpp ------------------------------------------------===//
+//===-- 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 "buffer.h"
+#include "flang-rt/runtime/buffer.h"
 #include <algorithm>
 
 namespace Fortran::runtime::io {
diff --git a/flang/runtime/character.cpp b/flang-rt/lib/runtime/character.cpp
similarity index 99%
rename from flang/runtime/character.cpp
rename to flang-rt/lib/runtime/character.cpp
index 5049247397eb3..10cf27c37c4d8 100644
--- a/flang/runtime/character.cpp
+++ b/flang-rt/lib/runtime/character.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/character.cpp ---------------------------------------------===//
+//===-- 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,13 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/character.h"
-#include "terminator.h"
-#include "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"
 #include "flang/Runtime/cpp-type.h"
-#include "flang/Runtime/descriptor.h"
 #include <algorithm>
 #include <cstring>
 
diff --git a/flang/runtime/command.cpp b/flang-rt/lib/runtime/command.cpp
similarity index 96%
rename from flang/runtime/command.cpp
rename to flang-rt/lib/runtime/command.cpp
index a555e26f96a66..8a5a61ac1ad44 100644
--- a/flang/runtime/command.cpp
+++ b/flang-rt/lib/runtime/command.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/command.cpp -----------------------------------------------===//
+//===-- 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 "environment.h"
-#include "stat.h"
-#include "terminator.h"
-#include "tools.h"
-#include "flang/Runtime/descriptor.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/runtime/complex-powi.cpp b/flang-rt/lib/runtime/complex-powi.cpp
similarity index 92%
rename from flang/runtime/complex-powi.cpp
rename to flang-rt/lib/runtime/complex-powi.cpp
index 62f73e037b76f..a561d114591cf 100644
--- a/flang/runtime/complex-powi.cpp
+++ b/flang-rt/lib/runtime/complex-powi.cpp
@@ -1,11 +1,11 @@
-/*===-- flang/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.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- *
- * ===-----------------------------------------------------------------------===
- */
+//===-- 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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
 #include "flang/Common/float128.h"
 #include "flang/Runtime/cpp-type.h"
 #include "flang/Runtime/entry-names.h"
diff --git a/flang/runtime/complex-reduction.c b/flang-rt/lib/runtime/complex-reduction.c
similarity index 97%
rename from flang/runtime/complex-reduction.c
rename to flang-rt/lib/runtime/complex-reduction.c
index de1ff3d683084..967f26c05e702 100644
--- a/flang/runtime/complex-reduction.c
+++ b/flang-rt/lib/runtime/complex-reduction.c
@@ -1,11 +1,10 @@
-/*===-- flang/runtime/complex-reduction.c ---------------------------*- C -*-===
+/*===-- lib/flang_rt/complex-reduction.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.
  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  *
- * ===-----------------------------------------------------------------------===
- */
+ *===----------------------------------------------------------------------===*/
 
 #include "complex-reduction.h"
 #include <float.h>
diff --git a/flang/runtime/complex-reduction.h b/flang-rt/lib/runtime/complex-reduction.h
similarity index 96%
rename from flang/runtime/complex-reduction.h
rename to flang-rt/lib/runtime/complex-reduction.h
index 5ff2a828fb3ec..44c52fb02fa43 100644
--- a/flang/runtime/complex-reduction.h
+++ b/flang-rt/lib/runtime/complex-reduction.h
@@ -1,19 +1,18 @@
-/*===-- flang/runtime/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.
  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  *
- * ===-----------------------------------------------------------------------===
- */
+ *===----------------------------------------------------------------------===*/
 
 /* Wraps the C++-coded complex-valued SUM and PRODUCT reductions with
  * C-coded wrapper functions returning _Complex values, to avoid problems
  * with C++ build compilers that don't support C's _Complex.
  */
 
-#ifndef FORTRAN_RUNTIME_COMPLEX_REDUCTION_H_
-#define FORTRAN_RUNTIME_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"
@@ -156,4 +155,4 @@ void RTNAME(ReduceComplex16DimValue)(
     REDUCE_DIM_ARGS(CFloat128ComplexType, CFloat128ComplexType_value_op));
 #endif
 
-#endif // FORTRAN_RUNTIME_COMPLEX_REDUCTION_H_
+#endif // FLANG_RT_RUNTIME_COMPLEX_REDUCTION_H_
diff --git a/flang/runtime/connection.cpp b/flang-rt/lib/runtime/connection.cpp
similarity index 90%
rename from flang/runtime/connection.cpp
rename to flang-rt/lib/runtime/connection.cpp
index f24f0e832eb48..2f01dbbb95920 100644
--- a/flang/runtime/connection.cpp
+++ b/flang-rt/lib/runtime/connection.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/connection.cpp --------------------------------------------===//
+//===-- 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 "connection.h"
-#include "environment.h"
-#include "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/runtime/copy.cpp b/flang-rt/lib/runtime/copy.cpp
similarity index 97%
rename from flang/runtime/copy.cpp
rename to flang-rt/lib/runtime/copy.cpp
index b20f68f019498..5956642dd7258 100644
--- a/flang/runtime/copy.cpp
+++ b/flang-rt/lib/runtime/copy.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/copy.cpp -------------------------------------------------===//
+//===-- 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,10 +8,10 @@
 
 #include "copy.h"
 #include "stack.h"
-#include "terminator.h"
-#include "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 "flang/Runtime/descriptor.h"
 #include <cstring>
 
 namespace Fortran::runtime {
diff --git a/flang/runtime/copy.h b/flang-rt/lib/runtime/copy.h
similarity index 78%
rename from flang/runtime/copy.h
rename to flang-rt/lib/runtime/copy.h
index 542660530bfb6..836c9d4a1ef89 100644
--- a/flang/runtime/copy.h
+++ b/flang-rt/lib/runtime/copy.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_COPY_H_
-#define FORTRAN_RUNTIME_COPY_H_
+#ifndef FLANG_RT_RUNTIME_COPY_H_
+#define FLANG_RT_RUNTIME_COPY_H_
 
-#include "flang/Runtime/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 // FORTRAN_RUNTIME_COPY_H_
+#endif // FLANG_RT_RUNTIME_COPY_H_
diff --git a/flang/runtime/derived-api.cpp b/flang-rt/lib/runtime/derived-api.cpp
similarity index 95%
rename from flang/runtime/derived-api.cpp
rename to flang-rt/lib/runtime/derived-api.cpp
index c8ffd8e3bb67c..884fa8ee7d095 100644
--- a/flang/runtime/derived-api.cpp
+++ b/flang-rt/lib/runtime/derived-api.cpp
@@ -1,5 +1,4 @@
-//===-- runtime/derived-api.cpp
-//-----------------------------------------------===//
+//===-- 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.
@@ -8,11 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/derived-api.h"
-#include "derived.h"
-#include "terminator.h"
-#include "tools.h"
-#include "type-info.h"
-#include "flang/Runtime/descriptor.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/runtime/derived.cpp b/flang-rt/lib/runtime/derived.cpp
similarity index 98%
rename from flang/runtime/derived.cpp
rename to flang-rt/lib/runtime/derived.cpp
index 10813c62e5da1..87e4b29d08c28 100644
--- a/flang/runtime/derived.cpp
+++ b/flang-rt/lib/runtime/derived.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/derived.cpp -----------------------------------------------===//
+//===-- 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 "derived.h"
-#include "stat.h"
-#include "terminator.h"
-#include "tools.h"
-#include "type-info.h"
-#include "flang/Runtime/descriptor.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/runtime/descriptor-io.cpp b/flang-rt/lib/runtime/descriptor-io.cpp
similarity index 98%
rename from flang/runtime/descriptor-io.cpp
rename to flang-rt/lib/runtime/descriptor-io.cpp
index 380ad425d925f..3db1455af52fe 100644
--- a/flang/runtime/descriptor-io.cpp
+++ b/flang-rt/lib/runtime/descriptor-io.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/descriptor-io.cpp -----------------------------------------===//
+//===-- 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/runtime/descriptor-io.h b/flang-rt/lib/runtime/descriptor-io.h
similarity index 98%
rename from flang/runtime/descriptor-io.h
rename to flang-rt/lib/runtime/descriptor-io.h
index 1034958bf654a..dd399164325cb 100644
--- a/flang/runtime/descriptor-io.h
+++ b/flang-rt/lib/runtime/descriptor-io.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_DESCRIPTOR_IO_H_
-#define FORTRAN_RUNTIME_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;
@@ -16,15 +16,15 @@
 
 #include "edit-input.h"
 #include "edit-output.h"
-#include "io-stmt.h"
-#include "namelist.h"
-#include "terminator.h"
-#include "type-info.h"
 #include "unit.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"
-#include "flang/Runtime/descriptor.h"
 
 namespace Fortran::runtime::io::descr {
 template <typename A>
@@ -626,4 +626,4 @@ static RT_API_ATTRS bool DescriptorIO(IoStatementState &io,
   return false;
 }
 } // namespace Fortran::runtime::io::descr
-#endif // FORTRAN_RUNTIME_DESCRIPTOR_IO_H_
+#endif // FLANG_RT_RUNTIME_DESCRIPTOR_IO_H_
diff --git a/flang/runtime/descriptor.cpp b/flang-rt/lib/runtime/descriptor.cpp
similarity index 94%
rename from flang/runtime/descriptor.cpp
rename to flang-rt/lib/runtime/descriptor.cpp
index 32f43e89dc7a3..8241a34a4990c 100644
--- a/flang/runtime/descriptor.cpp
+++ b/flang-rt/lib/runtime/descriptor.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/descriptor.cpp --------------------------------------------===//
+//===-- 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/Runtime/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "ISO_Fortran_util.h"
-#include "derived.h"
 #include "memory.h"
-#include "stat.h"
-#include "terminator.h"
-#include "tools.h"
-#include "type-info.h"
-#include "flang/Runtime/allocator-registry.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>
@@ -141,8 +141,10 @@ RT_API_ATTRS OwningPtr<Descriptor> Descriptor::Create(
 
 RT_API_ATTRS std::size_t Descriptor::SizeInBytes() const {
   const DescriptorAddendum *addendum{Addendum()};
-  return sizeof *this - sizeof(Dimension) + raw_.rank * sizeof(Dimension) +
-      (addendum ? addendum->SizeInBytes() : 0);
+  std::size_t bytes{ sizeof *this - sizeof(Dimension) + raw_.rank * sizeof(Dimension) +
+      (addendum ? addendum->SizeInBytes() : 0)};
+  assert (bytes <= MaxDescriptorSizeInBytes(raw_.rank,addendum) && "Descriptor must fit compiler-allocated space");
+  return bytes;
 }
 
 RT_API_ATTRS std::size_t Descriptor::Elements() const {
diff --git a/flang/runtime/dot-product.cpp b/flang-rt/lib/runtime/dot-product.cpp
similarity index 98%
rename from flang/runtime/dot-product.cpp
rename to flang-rt/lib/runtime/dot-product.cpp
index 712497a3a50ac..20612f1876c15 100644
--- a/flang/runtime/dot-product.cpp
+++ b/flang-rt/lib/runtime/dot-product.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/dot-product.cpp -------------------------------------------===//
+//===-- 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,11 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "float.h"
-#include "terminator.h"
-#include "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/descriptor.h"
 #include "flang/Runtime/reduction.h"
 #include <cfloat>
 #include <cinttypes>
diff --git a/flang/runtime/edit-input.cpp b/flang-rt/lib/runtime/edit-input.cpp
similarity index 99%
rename from flang/runtime/edit-input.cpp
rename to flang-rt/lib/runtime/edit-input.cpp
index 317f0b676bd21..99a266648f95c 100644
--- a/flang/runtime/edit-input.cpp
+++ b/flang-rt/lib/runtime/edit-input.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/edit-input.cpp --------------------------------------------===//
+//===-- 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 "namelist.h"
-#include "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/runtime/edit-input.h b/flang-rt/lib/runtime/edit-input.h
similarity index 87%
rename from flang/runtime/edit-input.h
rename to flang-rt/lib/runtime/edit-input.h
index 55a7a45578171..686cd461b3e34 100644
--- a/flang/runtime/edit-input.h
+++ b/flang-rt/lib/runtime/edit-input.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_EDIT_INPUT_H_
-#define FORTRAN_RUNTIME_EDIT_INPUT_H_
+#ifndef FLANG_RT_RUNTIME_EDIT_INPUT_H_
+#define FLANG_RT_RUNTIME_EDIT_INPUT_H_
 
-#include "format.h"
-#include "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 // FORTRAN_RUNTIME_EDIT_INPUT_H_
+#endif // FLANG_RT_RUNTIME_EDIT_INPUT_H_
diff --git a/flang/runtime/edit-output.cpp b/flang-rt/lib/runtime/edit-output.cpp
similarity index 99%
rename from flang/runtime/edit-output.cpp
rename to flang-rt/lib/runtime/edit-output.cpp
index 9db9a3d4a511b..36bbc638ff5fc 100644
--- a/flang/runtime/edit-output.cpp
+++ b/flang-rt/lib/runtime/edit-output.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/edit-output.cpp -------------------------------------------===//
+//===-- 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 "emit-encoded.h"
-#include "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/runtime/edit-output.h b/flang-rt/lib/runtime/edit-output.h
similarity index 95%
rename from flang/runtime/edit-output.h
rename to flang-rt/lib/runtime/edit-output.h
index 42cc993f98cc1..51a47405e49e4 100644
--- a/flang/runtime/edit-output.h
+++ b/flang-rt/lib/runtime/edit-output.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_EDIT_OUTPUT_H_
-#define FORTRAN_RUNTIME_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 "format.h"
-#include "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 // FORTRAN_RUNTIME_EDIT_OUTPUT_H_
+#endif // FLANG_RT_RUNTIME_EDIT_OUTPUT_H_
diff --git a/flang/runtime/environment-default-list.h b/flang-rt/lib/runtime/environment-default-list.h
similarity index 70%
rename from flang/runtime/environment-default-list.h
rename to flang-rt/lib/runtime/environment-default-list.h
index 4da261b10b9a8..76c0955bcce6d 100644
--- a/flang/runtime/environment-default-list.h
+++ b/flang-rt/lib/runtime/environment-default-list.h
@@ -1,14 +1,13 @@
-/*===-- runtime/environment-default-list.h --------------------------*- C -*-===
+/*===-- lib/flang_rt/environment-default-list.h ---------------------*- C -*-===
  *
  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  * See https://llvm.org/LICENSE.txt for license information.
  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  *
- * ===-----------------------------------------------------------------------===
- */
+ *===----------------------------------------------------------------------===*/
 
-#ifndef FORTRAN_RUNTIME_ENVIRONMENT_DEFAULT_LIST_H_
-#define FORTRAN_RUNTIME_ENVIRONMENT_DEFAULT_LIST_H_
+#ifndef FLANG_RT_ENVIRONMENT_DEFAULT_LIST_H_
+#define FLANG_RT_ENVIRONMENT_DEFAULT_LIST_H_
 
 /* Try to maintain C compatibility to make it easier to both define environment
  * defaults in non-Fortran main programs as well as pass through the environment
@@ -28,4 +27,4 @@ struct EnvironmentDefaultList {
   const struct EnvironmentDefaultItem *item;
 };
 
-#endif /* FORTRAN_RUNTIME_ENVIRONMENT_DEFAULT_LIST_H_ */
+#endif /* FLANG_RT_ENVIRONMENT_DEFAULT_LIST_H_ */
diff --git a/flang/runtime/environment.cpp b/flang-rt/lib/runtime/environment.cpp
similarity index 96%
rename from flang/runtime/environment.cpp
rename to flang-rt/lib/runtime/environment.cpp
index 678d8745c9fd7..15380ba148df5 100644
--- a/flang/runtime/environment.cpp
+++ b/flang-rt/lib/runtime/environment.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/environment.cpp -------------------------------------------===//
+//===-- 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 "environment.h"
+#include "flang-rt/runtime/environment.h"
 #include "environment-default-list.h"
 #include "memory.h"
-#include "tools.h"
+#include "flang-rt/runtime/tools.h"
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
diff --git a/flang/runtime/exceptions.cpp b/flang-rt/lib/runtime/exceptions.cpp
similarity index 97%
rename from flang/runtime/exceptions.cpp
rename to flang-rt/lib/runtime/exceptions.cpp
index 344e7216cfaae..d25a67c8e9cb5 100644
--- a/flang/runtime/exceptions.cpp
+++ b/flang-rt/lib/runtime/exceptions.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/exceptions.cpp --------------------------------------===//
+//===-- 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 "terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include <cfenv>
 #if defined(__aarch64__) && defined(__GLIBC__)
 #include <fpu_control.h>
diff --git a/flang/runtime/execute.cpp b/flang-rt/lib/runtime/execute.cpp
similarity index 97%
rename from flang/runtime/execute.cpp
rename to flang-rt/lib/runtime/execute.cpp
index c7f8f386d81f4..f180da846a32c 100644
--- a/flang/runtime/execute.cpp
+++ b/flang-rt/lib/runtime/execute.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/execute.cpp -----------------------------------------------===//
+//===-- 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 "environment.h"
-#include "stat.h"
-#include "terminator.h"
-#include "tools.h"
-#include "flang/Runtime/descriptor.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/runtime/extensions.cpp b/flang-rt/lib/runtime/extensions.cpp
similarity index 97%
rename from flang/runtime/extensions.cpp
rename to flang-rt/lib/runtime/extensions.cpp
index ac19ba7b31d4c..75195c33a6c21 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang-rt/lib/runtime/extensions.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/extensions.cpp --------------------------------------------===//
+//===-- 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,10 +10,10 @@
 // extensions that will eventually be implemented in Fortran.
 
 #include "flang/Runtime/extensions.h"
-#include "terminator.h"
-#include "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/descriptor.h"
 #include "flang/Runtime/entry-names.h"
 #include "flang/Runtime/io-api.h"
 #include <chrono>
diff --git a/flang/runtime/external-unit.cpp b/flang-rt/lib/runtime/external-unit.cpp
similarity index 98%
rename from flang/runtime/external-unit.cpp
rename to flang-rt/lib/runtime/external-unit.cpp
index d17a92622f844..b8004d6315994 100644
--- a/flang/runtime/external-unit.cpp
+++ b/flang-rt/lib/runtime/external-unit.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/external-unit.cpp -----------------------------------------===//
+//===-- 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.
@@ -10,11 +10,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "io-error.h"
-#include "lock.h"
-#include "tools.h"
 #include "unit-map.h"
 #include "unit.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/runtime/extrema.cpp b/flang-rt/lib/runtime/extrema.cpp
similarity index 99%
rename from flang/runtime/extrema.cpp
rename to flang-rt/lib/runtime/extrema.cpp
index 7ecdf4b91702e..3d84daa380441 100644
--- a/flang/runtime/extrema.cpp
+++ b/flang-rt/lib/runtime/extrema.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/extrema.cpp -----------------------------------------------===//
+//===-- 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 "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/runtime/file.cpp b/flang-rt/lib/runtime/file.cpp
similarity index 98%
rename from flang/runtime/file.cpp
rename to flang-rt/lib/runtime/file.cpp
index 9e077b8cea44b..16e73db488727 100644
--- a/flang/runtime/file.cpp
+++ b/flang-rt/lib/runtime/file.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/file.cpp --------------------------------------------------===//
+//===-- 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,10 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "file.h"
-#include "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 "flang/Runtime/memory.h"
 #include <algorithm>
 #include <cerrno>
 #include <cstring>
diff --git a/flang/runtime/findloc.cpp b/flang-rt/lib/runtime/findloc.cpp
similarity index 99%
rename from flang/runtime/findloc.cpp
rename to flang-rt/lib/runtime/findloc.cpp
index b9b1d7f7ab689..95986aefb86a4 100644
--- a/flang/runtime/findloc.cpp
+++ b/flang-rt/lib/runtime/findloc.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/findloc.cpp -----------------------------------------------===//
+//===-- 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 "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/runtime/format.cpp b/flang-rt/lib/runtime/format.cpp
similarity index 87%
rename from flang/runtime/format.cpp
rename to flang-rt/lib/runtime/format.cpp
index 433acce4b7373..ee0059f5f0729 100644
--- a/flang/runtime/format.cpp
+++ b/flang-rt/lib/runtime/format.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/format.cpp ------------------------------------------------===//
+//===-- 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 "format-implementation.h"
+#include "flang-rt/runtime/format-implementation.h"
 
 namespace Fortran::runtime::io {
 RT_OFFLOAD_API_GROUP_BEGIN
diff --git a/flang/runtime/inquiry.cpp b/flang-rt/lib/runtime/inquiry.cpp
similarity index 94%
rename from flang/runtime/inquiry.cpp
rename to flang-rt/lib/runtime/inquiry.cpp
index 9fbcaa96fa3c4..b6a7fce7a1e78 100644
--- a/flang/runtime/inquiry.cpp
+++ b/flang-rt/lib/runtime/inquiry.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/inquiry.cpp --------------------------------------===//
+//===-- 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 "terminator.h"
-#include "tools.h"
-#include "flang/Runtime/descriptor.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/runtime/internal-unit.cpp b/flang-rt/lib/runtime/internal-unit.cpp
similarity index 96%
rename from flang/runtime/internal-unit.cpp
rename to flang-rt/lib/runtime/internal-unit.cpp
index f8f3877efb20e..e344b01e8b34e 100644
--- a/flang/runtime/internal-unit.cpp
+++ b/flang-rt/lib/runtime/internal-unit.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/internal-unit.cpp -----------------------------------------===//
+//===-- 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 "internal-unit.h"
-#include "io-error.h"
-#include "flang/Runtime/descriptor.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/runtime/io-api-common.h b/flang-rt/lib/runtime/io-api-common.h
similarity index 92%
rename from flang/runtime/io-api-common.h
rename to flang-rt/lib/runtime/io-api-common.h
index c7b86cab73a52..b91ff9ff16863 100644
--- a/flang/runtime/io-api-common.h
+++ b/flang-rt/lib/runtime/io-api-common.h
@@ -1,4 +1,4 @@
-//===-- runtime/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_RUNTIME_IO_API_COMMON_H_
-#define FLANG_RUNTIME_IO_API_COMMON_H_
+#ifndef FLANG_RT_RUNTIME_IO_API_COMMON_H_
+#define FLANG_RT_RUNTIME_IO_API_COMMON_H_
 
-#include "io-stmt.h"
-#include "terminator.h"
 #include "unit.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_RUNTIME_IO_API_COMMON_H_
+#endif // FLANG_RT_RUNTIME_IO_API_COMMON_H_
diff --git a/flang/runtime/io-api-minimal.cpp b/flang-rt/lib/runtime/io-api-minimal.cpp
similarity index 94%
rename from flang/runtime/io-api-minimal.cpp
rename to flang-rt/lib/runtime/io-api-minimal.cpp
index 68768427be0c2..c706a3aa239a5 100644
--- a/flang/runtime/io-api-minimal.cpp
+++ b/flang-rt/lib/runtime/io-api-minimal.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/io-api-minimal.cpp ----------------------------------------===//
+//===-- 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.
@@ -10,12 +10,12 @@
 // list-directed output (PRINT *) of intrinsic types.
 
 #include "edit-output.h"
-#include "format.h"
 #include "io-api-common.h"
-#include "io-stmt.h"
-#include "terminator.h"
-#include "tools.h"
 #include "unit.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 {
@@ -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/runtime/io-api.cpp b/flang-rt/lib/runtime/io-api.cpp
similarity index 99%
rename from flang/runtime/io-api.cpp
rename to flang-rt/lib/runtime/io-api.cpp
index dc3f6f87fc21b..0355734c67fcd 100644
--- a/flang/runtime/io-api.cpp
+++ b/flang-rt/lib/runtime/io-api.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/io-api.cpp ------------------------------------------------===//
+//===-- 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.
@@ -17,16 +17,16 @@
 #include "descriptor-io.h"
 #include "edit-input.h"
 #include "edit-output.h"
-#include "environment.h"
-#include "format.h"
 #include "io-api-common.h"
-#include "io-stmt.h"
-#include "terminator.h"
-#include "tools.h"
 #include "unit.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 "flang/Runtime/descriptor.h"
-#include "flang/Runtime/memory.h"
 #include <cstdlib>
 #include <memory>
 
diff --git a/flang/runtime/io-error.cpp b/flang-rt/lib/runtime/io-error.cpp
similarity index 96%
rename from flang/runtime/io-error.cpp
rename to flang-rt/lib/runtime/io-error.cpp
index 37909e8e6dad2..b350fb66fc25b 100644
--- a/flang/runtime/io-error.cpp
+++ b/flang-rt/lib/runtime/io-error.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/io-error.cpp ----------------------------------------------===//
+//===-- 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 "io-error.h"
+#include "flang-rt/runtime/io-error.h"
 #include "config.h"
-#include "tools.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Runtime/magic-numbers.h"
 #include <cerrno>
 #include <cstdarg>
diff --git a/flang/runtime/io-stmt.cpp b/flang-rt/lib/runtime/io-stmt.cpp
similarity index 99%
rename from flang/runtime/io-stmt.cpp
rename to flang-rt/lib/runtime/io-stmt.cpp
index f24eb929ce748..b0823ffd9e703 100644
--- a/flang/runtime/io-stmt.cpp
+++ b/flang-rt/lib/runtime/io-stmt.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/io-stmt.cpp -----------------------------------------------===//
+//===-- 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 "io-stmt.h"
-#include "connection.h"
-#include "emit-encoded.h"
-#include "format.h"
-#include "tools.h"
+#include "flang-rt/runtime/io-stmt.h"
 #include "unit.h"
-#include "utf.h"
-#include "flang/Runtime/memory.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/runtime/iostat.cpp b/flang-rt/lib/runtime/iostat.cpp
similarity index 98%
rename from flang/runtime/iostat.cpp
rename to flang-rt/lib/runtime/iostat.cpp
index 39e224cb01286..0f8bfb884e544 100644
--- a/flang/runtime/iostat.cpp
+++ b/flang-rt/lib/runtime/iostat.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/iostat.cpp ------------------------------------------------===//
+//===-- 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/runtime/main.cpp b/flang-rt/lib/runtime/main.cpp
similarity index 89%
rename from flang/runtime/main.cpp
rename to flang-rt/lib/runtime/main.cpp
index 96454989581b7..b3f066cda3732 100644
--- a/flang/runtime/main.cpp
+++ b/flang-rt/lib/runtime/main.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/main.cpp --------------------------------------------------===//
+//===-- 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 "environment.h"
-#include "terminator.h"
+#include "flang-rt/runtime/environment.h"
+#include "flang-rt/runtime/terminator.h"
 #include <cfenv>
 #include <cstdio>
 #include <cstdlib>
diff --git a/flang/runtime/matmul-transpose.cpp b/flang-rt/lib/runtime/matmul-transpose.cpp
similarity index 98%
rename from flang/runtime/matmul-transpose.cpp
rename to flang-rt/lib/runtime/matmul-transpose.cpp
index bafa05056bebc..e20abbdddcd30 100644
--- a/flang/runtime/matmul-transpose.cpp
+++ b/flang-rt/lib/runtime/matmul-transpose.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/matmul-transpose.cpp --------------------------------------===//
+//===-- 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,12 +21,12 @@
 // to use the faster BLAS routines.
 
 #include "flang/Runtime/matmul-transpose.h"
-#include "terminator.h"
-#include "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"
-#include "flang/Runtime/descriptor.h"
 #include <cstring>
 
 namespace {
diff --git a/flang/runtime/matmul.cpp b/flang-rt/lib/runtime/matmul.cpp
similarity index 98%
rename from flang/runtime/matmul.cpp
rename to flang-rt/lib/runtime/matmul.cpp
index f72601073a600..f14cea922d21e 100644
--- a/flang/runtime/matmul.cpp
+++ b/flang-rt/lib/runtime/matmul.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/matmul.cpp ------------------------------------------------===//
+//===-- 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,12 +20,12 @@
 // Places where BLAS routines could be called are marked as TODO items.
 
 #include "flang/Runtime/matmul.h"
-#include "terminator.h"
-#include "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"
-#include "flang/Runtime/descriptor.h"
 #include <cstring>
 
 namespace {
diff --git a/flang/runtime/memory.cpp b/flang-rt/lib/runtime/memory.cpp
similarity index 85%
rename from flang/runtime/memory.cpp
rename to flang-rt/lib/runtime/memory.cpp
index c7068ad6479a1..79c7e33777569 100644
--- a/flang/runtime/memory.cpp
+++ b/flang-rt/lib/runtime/memory.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/memory.cpp ------------------------------------------------===//
+//===-- 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/Runtime/memory.h"
-#include "terminator.h"
-#include "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/runtime/misc-intrinsic.cpp b/flang-rt/lib/runtime/misc-intrinsic.cpp
similarity index 95%
rename from flang/runtime/misc-intrinsic.cpp
rename to flang-rt/lib/runtime/misc-intrinsic.cpp
index f7d893829fc0d..b7335e9f6799e 100644
--- a/flang/runtime/misc-intrinsic.cpp
+++ b/flang-rt/lib/runtime/misc-intrinsic.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/misc-intrinsic.cpp ----------------------------------------===//
+//===-- 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,10 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/misc-intrinsic.h"
-#include "terminator.h"
-#include "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/descriptor.h"
 #include <algorithm>
 #include <cstdio>
 #include <cstring>
diff --git a/flang/runtime/namelist.cpp b/flang-rt/lib/runtime/namelist.cpp
similarity index 99%
rename from flang/runtime/namelist.cpp
rename to flang-rt/lib/runtime/namelist.cpp
index af092de70f781..b0cf2180fc6d4 100644
--- a/flang/runtime/namelist.cpp
+++ b/flang-rt/lib/runtime/namelist.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/namelist.cpp ----------------------------------------------===//
+//===-- 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 "namelist.h"
+#include "flang-rt/runtime/namelist.h"
 #include "descriptor-io.h"
-#include "emit-encoded.h"
-#include "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/runtime/non-tbp-dio.cpp b/flang-rt/lib/runtime/non-tbp-dio.cpp
similarity index 86%
rename from flang/runtime/non-tbp-dio.cpp
rename to flang-rt/lib/runtime/non-tbp-dio.cpp
index 9419adb7631cc..72101b06e0c6e 100644
--- a/flang/runtime/non-tbp-dio.cpp
+++ b/flang-rt/lib/runtime/non-tbp-dio.cpp
@@ -1,4 +1,4 @@
-//===-- flang/runtime/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 "non-tbp-dio.h"
-#include "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/runtime/numeric.cpp b/flang-rt/lib/runtime/numeric.cpp
similarity index 99%
rename from flang/runtime/numeric.cpp
rename to flang-rt/lib/runtime/numeric.cpp
index 45fb56348fd44..37638765dc650 100644
--- a/flang/runtime/numeric.cpp
+++ b/flang-rt/lib/runtime/numeric.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/numeric.cpp -----------------------------------------------===//
+//===-- 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 "numeric-templates.h"
-#include "terminator.h"
-#include "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/runtime/pointer.cpp b/flang-rt/lib/runtime/pointer.cpp
similarity index 96%
rename from flang/runtime/pointer.cpp
rename to flang-rt/lib/runtime/pointer.cpp
index 2979181ddd61b..93ba82e0583c0 100644
--- a/flang/runtime/pointer.cpp
+++ b/flang-rt/lib/runtime/pointer.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/pointer.cpp -----------------------------------------------===//
+//===-- 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 "assign-impl.h"
-#include "derived.h"
-#include "environment.h"
-#include "stat.h"
-#include "terminator.h"
-#include "tools.h"
-#include "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/runtime/product.cpp b/flang-rt/lib/runtime/product.cpp
similarity index 98%
rename from flang/runtime/product.cpp
rename to flang-rt/lib/runtime/product.cpp
index 293ffd301ba2e..02fdc2bfcd576 100644
--- a/flang/runtime/product.cpp
+++ b/flang-rt/lib/runtime/product.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/product.cpp -----------------------------------------------===//
+//===-- 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 "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/runtime/pseudo-unit.cpp b/flang-rt/lib/runtime/pseudo-unit.cpp
similarity index 97%
rename from flang/runtime/pseudo-unit.cpp
rename to flang-rt/lib/runtime/pseudo-unit.cpp
index 526afd11d916e..7e1f3bc86b294 100644
--- a/flang/runtime/pseudo-unit.cpp
+++ b/flang-rt/lib/runtime/pseudo-unit.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/pseudo-unit.cpp -------------------------------------------===//
+//===-- 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.
@@ -11,9 +11,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "io-error.h"
-#include "tools.h"
 #include "unit.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/runtime/ragged.cpp b/flang-rt/lib/runtime/ragged.cpp
similarity index 96%
rename from flang/runtime/ragged.cpp
rename to flang-rt/lib/runtime/ragged.cpp
index a4d9e541ba531..dddc3ccdfd858 100644
--- a/flang/runtime/ragged.cpp
+++ b/flang-rt/lib/runtime/ragged.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/ragged.cpp ------------------------------------------------===//
+//===-- 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 "tools.h"
+#include "flang-rt/runtime/tools.h"
 #include <cstdlib>
 
 namespace Fortran::runtime {
diff --git a/flang/runtime/random.cpp b/flang-rt/lib/runtime/random.cpp
similarity index 96%
rename from flang/runtime/random.cpp
rename to flang-rt/lib/runtime/random.cpp
index 8f158007a5a65..dc74f2725ed51 100644
--- a/flang/runtime/random.cpp
+++ b/flang-rt/lib/runtime/random.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/random.cpp ------------------------------------------------===//
+//===-- 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,14 +10,14 @@
 // RANDOM_SEED.
 
 #include "flang/Runtime/random.h"
-#include "lock.h"
-#include "random-templates.h"
-#include "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"
 #include "flang/Runtime/cpp-type.h"
-#include "flang/Runtime/descriptor.h"
 #include <cmath>
 #include <cstdint>
 #include <limits>
diff --git a/flang/runtime/reduce.cpp b/flang-rt/lib/runtime/reduce.cpp
similarity index 99%
rename from flang/runtime/reduce.cpp
rename to flang-rt/lib/runtime/reduce.cpp
index 6c42c5ef50e4f..3c5e815e32d2b 100644
--- a/flang/runtime/reduce.cpp
+++ b/flang-rt/lib/runtime/reduce.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/reduce.cpp ------------------------------------------------===//
+//===-- 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 "reduction-templates.h"
-#include "terminator.h"
-#include "tools.h"
-#include "flang/Runtime/descriptor.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/runtime/reduction.cpp b/flang-rt/lib/runtime/reduction.cpp
similarity index 98%
rename from flang/runtime/reduction.cpp
rename to flang-rt/lib/runtime/reduction.cpp
index a8fcde7b3a166..5e45870489479 100644
--- a/flang/runtime/reduction.cpp
+++ b/flang-rt/lib/runtime/reduction.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/reduction.cpp ---------------------------------------------===//
+//===-- 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 "reduction-templates.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/reduction-templates.h"
 #include <cinttypes>
 
 namespace Fortran::runtime {
diff --git a/flang/runtime/stack.h b/flang-rt/lib/runtime/stack.h
similarity index 93%
rename from flang/runtime/stack.h
rename to flang-rt/lib/runtime/stack.h
index b6e6edb595e9a..38364ff541f16 100644
--- a/flang/runtime/stack.h
+++ b/flang-rt/lib/runtime/stack.h
@@ -1,4 +1,4 @@
-//===-- runtime/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 FORTRAN_RUNTIME_STACK_H
-#define FORTRAN_RUNTIME_STACK_H
+#ifndef FLANG_RT_RUNTIME_STACK_H_
+#define FLANG_RT_RUNTIME_STACK_H_
 
-#include "terminator.h"
-#include "flang/Runtime/memory.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 // FORTRAN_RUNTIME_STACK_H
+#endif // FLANG_RT_RUNTIME_STACK_H_
diff --git a/flang/runtime/stat.cpp b/flang-rt/lib/runtime/stat.cpp
similarity index 92%
rename from flang/runtime/stat.cpp
rename to flang-rt/lib/runtime/stat.cpp
index 525a4e36cdc77..322b7282b7024 100644
--- a/flang/runtime/stat.cpp
+++ b/flang-rt/lib/runtime/stat.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/stat.cpp --------------------------------------------------===//
+//===-- 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 "stat.h"
-#include "terminator.h"
-#include "tools.h"
-#include "flang/Runtime/descriptor.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/runtime/stop.cpp b/flang-rt/lib/runtime/stop.cpp
similarity index 95%
rename from flang/runtime/stop.cpp
rename to flang-rt/lib/runtime/stop.cpp
index f8c180e0aaffa..1d70a137377aa 100644
--- a/flang/runtime/stop.cpp
+++ b/flang-rt/lib/runtime/stop.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/stop.cpp --------------------------------------------------===//
+//===-- 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.
@@ -8,11 +8,11 @@
 
 #include "flang/Runtime/stop.h"
 #include "config.h"
-#include "environment.h"
-#include "file.h"
-#include "io-error.h"
-#include "terminator.h"
 #include "unit.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/runtime/sum.cpp b/flang-rt/lib/runtime/sum.cpp
similarity index 98%
rename from flang/runtime/sum.cpp
rename to flang-rt/lib/runtime/sum.cpp
index 3cb7a2b285779..a76e228f18a4e 100644
--- a/flang/runtime/sum.cpp
+++ b/flang-rt/lib/runtime/sum.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/sum.cpp ---------------------------------------------------===//
+//===-- 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 "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/runtime/support.cpp b/flang-rt/lib/runtime/support.cpp
similarity index 90%
rename from flang/runtime/support.cpp
rename to flang-rt/lib/runtime/support.cpp
index a607120256d9d..5a2b0c920aa80 100644
--- a/flang/runtime/support.cpp
+++ b/flang-rt/lib/runtime/support.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/support.cpp -----------------------------------------------===//
+//===-- 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 "type-info.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/type-info.h"
 
 namespace Fortran::runtime {
 extern "C" {
diff --git a/flang/runtime/temporary-stack.cpp b/flang-rt/lib/runtime/temporary-stack.cpp
similarity index 97%
rename from flang/runtime/temporary-stack.cpp
rename to flang-rt/lib/runtime/temporary-stack.cpp
index 93340266b1b44..ea89d0c17bb6b 100644
--- a/flang/runtime/temporary-stack.cpp
+++ b/flang-rt/lib/runtime/temporary-stack.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/temporary-stack.cpp ---------------------------------------===//
+//===-- 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,11 +10,11 @@
 // temporaries. For use in HLFIR lowering.
 
 #include "flang/Runtime/temporary-stack.h"
-#include "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"
-#include "flang/Runtime/descriptor.h"
-#include "flang/Runtime/memory.h"
 
 namespace {
 
diff --git a/flang/runtime/terminator.cpp b/flang-rt/lib/runtime/terminator.cpp
similarity index 95%
rename from flang/runtime/terminator.cpp
rename to flang-rt/lib/runtime/terminator.cpp
index bab9edc64fa35..8a57ba06b1304 100644
--- a/flang/runtime/terminator.cpp
+++ b/flang-rt/lib/runtime/terminator.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/terminate.cpp ---------------------------------------------===//
+//===-- 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 "terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include <cstdio>
 #include <cstdlib>
 
diff --git a/flang/runtime/time-intrinsic.cpp b/flang-rt/lib/runtime/time-intrinsic.cpp
similarity index 98%
rename from flang/runtime/time-intrinsic.cpp
rename to flang-rt/lib/runtime/time-intrinsic.cpp
index 942604a92aaad..69c344f5d24bc 100644
--- a/flang/runtime/time-intrinsic.cpp
+++ b/flang-rt/lib/runtime/time-intrinsic.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/time-intrinsic.cpp ----------------------------------------===//
+//===-- 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,10 +9,10 @@
 // Implements time-related intrinsic subroutines.
 
 #include "flang/Runtime/time-intrinsic.h"
-#include "terminator.h"
-#include "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 "flang/Runtime/descriptor.h"
 #include <algorithm>
 #include <cstdint>
 #include <cstdio>
diff --git a/flang/runtime/tools.cpp b/flang-rt/lib/runtime/tools.cpp
similarity index 98%
rename from flang/runtime/tools.cpp
rename to flang-rt/lib/runtime/tools.cpp
index 73d6c2cf7e1d2..b9d9ca4fc378c 100644
--- a/flang/runtime/tools.cpp
+++ b/flang-rt/lib/runtime/tools.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/tools.cpp -------------------------------------------------===//
+//===-- 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 "tools.h"
-#include "terminator.h"
+#include "flang-rt/runtime/tools.h"
+#include "flang-rt/runtime/terminator.h"
 #include <algorithm>
 #include <cstdint>
 #include <cstdlib>
diff --git a/flang/runtime/transformational.cpp b/flang-rt/lib/runtime/transformational.cpp
similarity index 99%
rename from flang/runtime/transformational.cpp
rename to flang-rt/lib/runtime/transformational.cpp
index ab303bdef9b1d..eb694a9f2c833 100644
--- a/flang/runtime/transformational.cpp
+++ b/flang-rt/lib/runtime/transformational.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/transformational.cpp --------------------------------------===//
+//===-- 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,10 +18,10 @@
 
 #include "flang/Runtime/transformational.h"
 #include "copy.h"
-#include "terminator.h"
-#include "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/descriptor.h"
 
 namespace Fortran::runtime {
 
diff --git a/flang/runtime/type-code.cpp b/flang-rt/lib/runtime/type-code.cpp
similarity index 98%
rename from flang/runtime/type-code.cpp
rename to flang-rt/lib/runtime/type-code.cpp
index d6948983bfe9f..8cfec9a4ec2fb 100644
--- a/flang/runtime/type-code.cpp
+++ b/flang-rt/lib/runtime/type-code.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/type-code.cpp ---------------------------------------------===//
+//===-- 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/Runtime/type-code.h"
+#include "flang-rt/runtime/type-code.h"
 
 namespace Fortran::runtime {
 
diff --git a/flang/runtime/type-info.cpp b/flang-rt/lib/runtime/type-info.cpp
similarity index 97%
rename from flang/runtime/type-info.cpp
rename to flang-rt/lib/runtime/type-info.cpp
index d4daa72aee6a1..82182696d70c6 100644
--- a/flang/runtime/type-info.cpp
+++ b/flang-rt/lib/runtime/type-info.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/type-info.cpp ---------------------------------------------===//
+//===-- 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 "type-info.h"
-#include "terminator.h"
-#include "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 {
@@ -86,7 +86,7 @@ RT_API_ATTRS std::size_t Component::SizeInBytes(
   } else if (category() == TypeCategory::Derived) {
     const DerivedType *type{derivedType()};
     return Descriptor::SizeInBytes(
-        rank_, true, type ? type->LenParameters() : 0);
+         rank_, true, type ? type->LenParameters() : 0);
   } else {
     return Descriptor::SizeInBytes(rank_);
   }
diff --git a/flang/runtime/unit-map.cpp b/flang-rt/lib/runtime/unit-map.cpp
similarity index 97%
rename from flang/runtime/unit-map.cpp
rename to flang-rt/lib/runtime/unit-map.cpp
index 684a9b9e20b97..41a03f3319d64 100644
--- a/flang/runtime/unit-map.cpp
+++ b/flang-rt/lib/runtime/unit-map.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/unit-map.cpp ----------------------------------------------===//
+//===-- 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/runtime/unit-map.h b/flang-rt/lib/runtime/unit-map.h
similarity index 92%
rename from flang/runtime/unit-map.h
rename to flang-rt/lib/runtime/unit-map.h
index 6f1e01bb1e64a..fa61288a1e18d 100644
--- a/flang/runtime/unit-map.h
+++ b/flang-rt/lib/runtime/unit-map.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,13 +9,13 @@
 // Maps Fortran unit numbers to their ExternalFileUnit instances.
 // A simple hash table with forward-linked chains per bucket.
 
-#ifndef FORTRAN_RUNTIME_UNIT_MAP_H_
-#define FORTRAN_RUNTIME_UNIT_MAP_H_
+#ifndef FLANG_RT_RUNTIME_UNIT_MAP_H_
+#define FLANG_RT_RUNTIME_UNIT_MAP_H_
 
-#include "lock.h"
 #include "unit.h"
+#include "flang-rt/runtime/lock.h"
+#include "flang-rt/runtime/memory.h"
 #include "flang/Common/fast-int-set.h"
-#include "flang/Runtime/memory.h"
 #include <cstdint>
 #include <cstdlib>
 
@@ -100,4 +100,4 @@ class UnitMap {
   int emergencyNewUnit_{maxNewUnits_}; // not recycled
 };
 } // namespace Fortran::runtime::io
-#endif // FORTRAN_RUNTIME_UNIT_MAP_H_
+#endif // FLANG_RT_RUNTIME_UNIT_MAP_H_
diff --git a/flang/runtime/unit.cpp b/flang-rt/lib/runtime/unit.cpp
similarity index 99%
rename from flang/runtime/unit.cpp
rename to flang-rt/lib/runtime/unit.cpp
index 4aee8397d477e..1d4d54ae01956 100644
--- a/flang/runtime/unit.cpp
+++ b/flang-rt/lib/runtime/unit.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/unit.cpp --------------------------------------------------===//
+//===-- 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 "io-error.h"
-#include "lock.h"
-#include "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/runtime/unit.h b/flang-rt/lib/runtime/unit.h
similarity index 95%
rename from flang/runtime/unit.h
rename to flang-rt/lib/runtime/unit.h
index a3ea268681680..eb762a2d3b235 100644
--- a/flang/runtime/unit.h
+++ b/flang-rt/lib/runtime/unit.h
@@ -1,4 +1,4 @@
-//===-- runtime/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,21 +8,21 @@
 
 // Fortran external I/O units
 
-#ifndef FORTRAN_RUNTIME_IO_UNIT_H_
-#define FORTRAN_RUNTIME_IO_UNIT_H_
-
-#include "buffer.h"
-#include "connection.h"
-#include "environment.h"
-#include "file.h"
-#include "format.h"
-#include "io-error.h"
-#include "io-stmt.h"
-#include "lock.h"
-#include "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 "flang/Runtime/memory.h"
 #include <cstdlib>
 #include <cstring>
 #include <flang/Common/variant.h>
@@ -291,4 +291,4 @@ class ChildIo {
 };
 
 } // namespace Fortran::runtime::io
-#endif // FORTRAN_RUNTIME_IO_UNIT_H_
+#endif // FLANG_RT_RUNTIME_UNIT_H_
diff --git a/flang/runtime/utf.cpp b/flang-rt/lib/runtime/utf.cpp
similarity index 97%
rename from flang/runtime/utf.cpp
rename to flang-rt/lib/runtime/utf.cpp
index b09819cb2f736..ef9df49f24f66 100644
--- a/flang/runtime/utf.cpp
+++ b/flang-rt/lib/runtime/utf.cpp
@@ -1,4 +1,4 @@
-//===-- runtime/utf.cpp ---------------------------------------------------===//
+//===-- 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 "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
new file mode 100644
index 0000000000000..f5f7b8832d381
--- /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.runtime
+    flang_rt.runtime.unittest
+  )
+
+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/test/Driver/ctofortran.f90 b/flang-rt/test/Driver/ctofortran.f90
similarity index 69%
rename from flang/test/Driver/ctofortran.f90
rename to flang-rt/test/Driver/ctofortran.f90
index 10c7adaccc958..e385e7974cdc1 100644
--- a/flang/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/test/Driver/exec.f90 b/flang-rt/test/Driver/exec.f90
similarity index 50%
rename from flang/test/Driver/exec.f90
rename to flang-rt/test/Driver/exec.f90
index 9ca91ee24011c..5a81a1e4c3e45 100644
--- a/flang/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 0000000000000..4bee709b78f43
--- /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 0000000000000..8244b5b62d3aa
--- /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/test/Runtime/no-cpp-dep.c b/flang-rt/test/Runtime/no-cpp-dep.c
similarity index 91%
rename from flang/test/Runtime/no-cpp-dep.c
rename to flang-rt/test/Runtime/no-cpp-dep.c
index 4fcf8f9d478d8..c98678b69aa14 100644
--- a/flang/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.runtime.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.runtime -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 0000000000000..516bc653f413f
--- /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 0000000000000..ed6dea07bcdde
--- /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 0000000000000..652da31e6438f
--- /dev/null
+++ b/flang-rt/test/lit.cfg.py
@@ -0,0 +1,100 @@
+# -*- 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.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.
+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 0000000000000..662d076b1fe24
--- /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 0000000000000..e2a50d8b40a9d
--- /dev/null
+++ b/flang-rt/unittests/CMakeLists.txt
@@ -0,0 +1,105 @@
+#===-- 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_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_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 0000000000000..526ec234d57da
--- /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.runtime.unittest
+)
+
+add_flangrt_nongtest_unittest(ISO-Fortran-binding
+  ISO-Fortran-binding.cpp
+
+  LINK_LIBS
+    flang_rt.runtime.unittest
+)
diff --git a/flang/unittests/Evaluate/ISO-Fortran-binding.cpp b/flang-rt/unittests/Evaluate/ISO-Fortran-binding.cpp
similarity index 98%
rename from flang/unittests/Evaluate/ISO-Fortran-binding.cpp
rename to flang-rt/unittests/Evaluate/ISO-Fortran-binding.cpp
index 2884686b3f26e..8c0a6f29b6967 100644
--- a/flang/unittests/Evaluate/ISO-Fortran-binding.cpp
+++ b/flang-rt/unittests/Evaluate/ISO-Fortran-binding.cpp
@@ -1,5 +1,13 @@
+//===-- unittests/Evaluate/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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
-#include "flang/Runtime/descriptor.h"
 #include "flang/Testing/testing.h"
 #include "llvm/Support/raw_ostream.h"
 #include <type_traits>
diff --git a/flang/unittests/Evaluate/reshape.cpp b/flang-rt/unittests/Evaluate/reshape.cpp
similarity index 88%
rename from flang/unittests/Evaluate/reshape.cpp
rename to flang-rt/unittests/Evaluate/reshape.cpp
index 16cba15dcef60..2abe46c0969fb 100644
--- a/flang/unittests/Evaluate/reshape.cpp
+++ b/flang-rt/unittests/Evaluate/reshape.cpp
@@ -1,4 +1,12 @@
-#include "flang/Runtime/descriptor.h"
+//===-- unittests/Evaluate/reshape.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-rt/runtime/descriptor.h"
 #include "flang/Runtime/transformational.h"
 #include "flang/Testing/testing.h"
 #include <cinttypes>
diff --git a/flang/unittests/Runtime/AccessTest.cpp b/flang-rt/unittests/Runtime/AccessTest.cpp
similarity index 99%
rename from flang/unittests/Runtime/AccessTest.cpp
rename to flang-rt/unittests/Runtime/AccessTest.cpp
index c2a2d7d398220..d431d0d19bd61 100644
--- a/flang/unittests/Runtime/AccessTest.cpp
+++ b/flang-rt/unittests/Runtime/AccessTest.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/AccessTest.cpp ----------------------------===//
+//===-- unittests/Runtime/AccessTest.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/unittests/Runtime/Allocatable.cpp b/flang-rt/unittests/Runtime/Allocatable.cpp
similarity index 97%
rename from flang/unittests/Runtime/Allocatable.cpp
rename to flang-rt/unittests/Runtime/Allocatable.cpp
index f15f26bfd9c57..4702f48e0f440 100644
--- a/flang/unittests/Runtime/Allocatable.cpp
+++ b/flang-rt/unittests/Runtime/Allocatable.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Allocatable.cpp--------- ---------*- C++-*-===//
+//===-- unittests/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.
@@ -8,7 +8,7 @@
 
 #include "flang/Runtime/allocatable.h"
 #include "gtest/gtest.h"
-#include "tools.h"
+#include "flang-rt/runtime/tools.h"
 
 using namespace Fortran::runtime;
 
diff --git a/flang/unittests/Runtime/ArrayConstructor.cpp b/flang-rt/unittests/Runtime/ArrayConstructor.cpp
similarity index 96%
rename from flang/unittests/Runtime/ArrayConstructor.cpp
rename to flang-rt/unittests/Runtime/ArrayConstructor.cpp
index 53774a0eea07d..5f791e7f4a7c3 100644
--- a/flang/unittests/Runtime/ArrayConstructor.cpp
+++ b/flang-rt/unittests/Runtime/ArrayConstructor.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/ArrayConstructor.cpp-------------*- C++ -*-===//
+//===-- unittests/Runtime/ArrayConstructor.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,13 +6,13 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "gtest/gtest.h"
 #include "tools.h"
+#include "gtest/gtest.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/array-constructor.h"
 #include "flang/Runtime/cpp-type.h"
-#include "flang/Runtime/descriptor.h"
-#include "flang/Runtime/type-code.h"
 
 #include <memory>
 
diff --git a/flang/unittests/Runtime/BufferTest.cpp b/flang-rt/unittests/Runtime/BufferTest.cpp
similarity index 97%
rename from flang/unittests/Runtime/BufferTest.cpp
rename to flang-rt/unittests/Runtime/BufferTest.cpp
index 0632324b25d22..f4b9e901d6fcb 100644
--- a/flang/unittests/Runtime/BufferTest.cpp
+++ b/flang-rt/unittests/Runtime/BufferTest.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/BufferTest.cpp ------------------*- C++ -*-===//
+//===-- unittests/Runtime/BufferTest.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 "../../runtime/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
new file mode 100644
index 0000000000000..61d0aba93b14b
--- /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.runtime.unittest
+)
+target_compile_definitions(RuntimeTests PRIVATE NOT_EXE="${LLVM_TOOLS_DIR}/not${CMAKE_EXECUTABLE_SUFFIX}")
+
+if (FLANG_RT_INCLUDE_CUF)
+  add_subdirectory(CUDA)
+endif ()
diff --git a/flang/unittests/Runtime/CUDA/Allocatable.cpp b/flang-rt/unittests/Runtime/CUDA/Allocatable.cpp
similarity index 90%
rename from flang/unittests/Runtime/CUDA/Allocatable.cpp
rename to flang-rt/unittests/Runtime/CUDA/Allocatable.cpp
index bdfa8f5cc3213..3f759a69c0388 100644
--- a/flang/unittests/Runtime/CUDA/Allocatable.cpp
+++ b/flang-rt/unittests/Runtime/CUDA/Allocatable.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Allocatable.cpp ------------------*- C++-*-===//
+//===-- unittests/Runtime/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,16 +7,16 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/allocatable.h"
+#include "cuda_runtime.h"
 #include "gtest/gtest.h"
-#include "../../../runtime/terminator.h"
+#include "flang-rt/runtime/allocator-registry.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/allocator.h"
 #include "flang/Runtime/CUDA/common.h"
 #include "flang/Runtime/CUDA/descriptor.h"
-#include "flang/Runtime/allocator-registry.h"
 #include "flang/Support/Fortran.h"
 
-#include "cuda_runtime.h"
-
 using namespace Fortran::runtime;
 using namespace Fortran::runtime::cuda;
 
diff --git a/flang/unittests/Runtime/CUDA/AllocatorCUF.cpp b/flang-rt/unittests/Runtime/CUDA/AllocatorCUF.cpp
similarity index 92%
rename from flang/unittests/Runtime/CUDA/AllocatorCUF.cpp
rename to flang-rt/unittests/Runtime/CUDA/AllocatorCUF.cpp
index 5ec122e4c5777..9bda3270fe8a1 100644
--- a/flang/unittests/Runtime/CUDA/AllocatorCUF.cpp
+++ b/flang-rt/unittests/Runtime/CUDA/AllocatorCUF.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/AllocatableCUF.cpp ---------------*- C++-*-===//
+//===-- unittests/Runtime/CUDA/AllocatorCUF.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,16 +6,16 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "cuda_runtime.h"
 #include "gtest/gtest.h"
-#include "../../../runtime/terminator.h"
+#include "flang-rt/runtime/allocator-registry.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/allocator.h"
 #include "flang/Runtime/CUDA/descriptor.h"
 #include "flang/Runtime/allocatable.h"
-#include "flang/Runtime/allocator-registry.h"
 #include "flang/Support/Fortran.h"
 
-#include "cuda_runtime.h"
-
 using namespace Fortran::runtime;
 using namespace Fortran::runtime::cuda;
 
diff --git a/flang-rt/unittests/Runtime/CUDA/CMakeLists.txt b/flang-rt/unittests/Runtime/CUDA/CMakeLists.txt
new file mode 100644
index 0000000000000..cd69a6f472873
--- /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
+  flang_rt.cuda
+)
diff --git a/flang/unittests/Runtime/CUDA/Memory.cpp b/flang-rt/unittests/Runtime/CUDA/Memory.cpp
similarity index 93%
rename from flang/unittests/Runtime/CUDA/Memory.cpp
rename to flang-rt/unittests/Runtime/CUDA/Memory.cpp
index 61c9d1131ee56..37ae59ec238c8 100644
--- a/flang/unittests/Runtime/CUDA/Memory.cpp
+++ b/flang-rt/unittests/Runtime/CUDA/Memory.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Memory.cpp -----------------------*- C++-*-===//
+//===-- unittests/Runtime/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,18 +7,17 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/CUDA/memory.h"
-#include "gtest/gtest.h"
-#include "../../../runtime/terminator.h"
+#include "cuda_runtime.h"
 #include "../tools.h"
+#include "gtest/gtest.h"
+#include "flang-rt/runtime/allocator-registry.h"
+#include "flang-rt/runtime/terminator.h"
 #include "flang/Runtime/CUDA/allocator.h"
 #include "flang/Runtime/CUDA/common.h"
 #include "flang/Runtime/CUDA/descriptor.h"
 #include "flang/Runtime/allocatable.h"
-#include "flang/Runtime/allocator-registry.h"
 #include "flang/Support/Fortran.h"
 
-#include "cuda_runtime.h"
-
 using namespace Fortran::runtime;
 using namespace Fortran::runtime::cuda;
 
diff --git a/flang/unittests/Runtime/CharacterTest.cpp b/flang-rt/unittests/Runtime/CharacterTest.cpp
similarity index 99%
rename from flang/unittests/Runtime/CharacterTest.cpp
rename to flang-rt/unittests/Runtime/CharacterTest.cpp
index d462c9120fd8c..83ec9b36d9b0c 100644
--- a/flang/unittests/Runtime/CharacterTest.cpp
+++ b/flang-rt/unittests/Runtime/CharacterTest.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/CharacterTest.cpp ---------------*- C++ -*-===//
+//===-- unittests/Runtime/CharacterTest.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,7 +11,7 @@
 
 #include "flang/Runtime/character.h"
 #include "gtest/gtest.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include <cstring>
 #include <functional>
 #include <tuple>
diff --git a/flang/unittests/Runtime/CommandTest.cpp b/flang-rt/unittests/Runtime/CommandTest.cpp
similarity index 99%
rename from flang/unittests/Runtime/CommandTest.cpp
rename to flang-rt/unittests/Runtime/CommandTest.cpp
index ecb325330f1ad..72fe7629dbbb8 100644
--- a/flang/unittests/Runtime/CommandTest.cpp
+++ b/flang-rt/unittests/Runtime/CommandTest.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/CommandTest.cpp ---------------------------===//
+//===-- unittests/Runtime/CommandTest.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 @@
 #include "flang/Runtime/command.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
-#include "flang/Runtime/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/unittests/Runtime/Complex.cpp b/flang-rt/unittests/Runtime/Complex.cpp
similarity index 98%
rename from flang/unittests/Runtime/Complex.cpp
rename to flang-rt/unittests/Runtime/Complex.cpp
index d714da24dc4e5..d7e5f55414fe2 100644
--- a/flang/unittests/Runtime/Complex.cpp
+++ b/flang-rt/unittests/Runtime/Complex.cpp
@@ -1,10 +1,11 @@
-//===-- flang/unittests/Runtime/Complex.cpp ---------------------*- C++ -*-===//
+//===-- unittests/Runtime/Complex.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 "gmock/gmock.h"
 #include "gtest/gtest-matchers.h"
 #include <limits>
diff --git a/flang/unittests/Runtime/CrashHandlerFixture.cpp b/flang-rt/unittests/Runtime/CrashHandlerFixture.cpp
similarity index 92%
rename from flang/unittests/Runtime/CrashHandlerFixture.cpp
rename to flang-rt/unittests/Runtime/CrashHandlerFixture.cpp
index 811603337e660..8213edd1f9225 100644
--- a/flang/unittests/Runtime/CrashHandlerFixture.cpp
+++ b/flang-rt/unittests/Runtime/CrashHandlerFixture.cpp
@@ -1,12 +1,13 @@
-//===-- flang/unittests/Runtime/CrashHandlerFixture.cpp ---------*- C++ -*-===//
+//===-- unittests/Runtime/CrashHandlerFixture.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 "CrashHandlerFixture.h"
-#include "../../runtime/terminator.h"
+#include "flang-rt/runtime/terminator.h"
 #include <cstdarg>
 #include <cstdlib>
 
diff --git a/flang/unittests/Runtime/CrashHandlerFixture.h b/flang-rt/unittests/Runtime/CrashHandlerFixture.h
similarity index 70%
rename from flang/unittests/Runtime/CrashHandlerFixture.h
rename to flang-rt/unittests/Runtime/CrashHandlerFixture.h
index fe0ee0da5204e..74531d1e728f4 100644
--- a/flang/unittests/Runtime/CrashHandlerFixture.h
+++ b/flang-rt/unittests/Runtime/CrashHandlerFixture.h
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/CrashHandlerFixture.h -----------*- C++ -*-===//
+//===-- unittests/Runtime/CrashHandlerFixture.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,12 +10,13 @@
 /// with expected message.
 //
 //===----------------------------------------------------------------------===//
-#ifndef LLVM_FLANG_UNITTESTS_RUNTIMEGTEST_CRASHHANDLERFIXTURE_H
-#define LLVM_FLANG_UNITTESTS_RUNTIMEGTEST_CRASHHANDLERFIXTURE_H
+
+#ifndef FLANG_RT_UNITTESTS_RUNTIME_CRASHHANDLERFIXTURE_H_
+#define FLANG_RT_UNITTESTS_RUNTIME_CRASHHANDLERFIXTURE_H_
 #include <gtest/gtest.h>
 
 struct CrashHandlerFixture : testing::Test {
   void SetUp();
 };
 
-#endif
+#endif /* FLANG_RT_UNITTESTS_RUNTIME_CRASHHANDLERFIXTURE_H_ */
diff --git a/flang/unittests/Runtime/Derived.cpp b/flang-rt/unittests/Runtime/Derived.cpp
similarity index 93%
rename from flang/unittests/Runtime/Derived.cpp
rename to flang-rt/unittests/Runtime/Derived.cpp
index 019d5e8309e4a..3196ba796ad19 100644
--- a/flang/unittests/Runtime/Derived.cpp
+++ b/flang-rt/unittests/Runtime/Derived.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Pointer.cpp--------- -------------*- C++-*-===//
+//===-- unittests/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.
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "gtest/gtest.h"
-#include "tools.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/tools.h"
 #include "flang/Runtime/derived-api.h"
-#include "flang/Runtime/descriptor.h"
 
 using namespace Fortran::runtime;
 
diff --git a/flang/unittests/Runtime/ExternalIOTest.cpp b/flang-rt/unittests/Runtime/ExternalIOTest.cpp
similarity index 99%
rename from flang/unittests/Runtime/ExternalIOTest.cpp
rename to flang-rt/unittests/Runtime/ExternalIOTest.cpp
index b9407b5e7a591..c83535ca82bd3 100644
--- a/flang/unittests/Runtime/ExternalIOTest.cpp
+++ b/flang-rt/unittests/Runtime/ExternalIOTest.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/RuntimeGTest/ExternalIOTest.cpp ---------*- C++ -*-===//
+//===-- unittests/Runtime/ExternalIOTest.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 @@
 
 #include "CrashHandlerFixture.h"
 #include "gtest/gtest.h"
-#include "flang/Runtime/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/unittests/Runtime/Format.cpp b/flang-rt/unittests/Runtime/Format.cpp
similarity index 96%
rename from flang/unittests/Runtime/Format.cpp
rename to flang-rt/unittests/Runtime/Format.cpp
index 01803c628de26..fe7403f26700b 100644
--- a/flang/unittests/Runtime/Format.cpp
+++ b/flang-rt/unittests/Runtime/Format.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Format.cpp ----------------------*- C++ -*-===//
+//===-- unittests/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.
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "CrashHandlerFixture.h"
-#include "../runtime/connection.h"
-#include "../runtime/format-implementation.h"
-#include "../runtime/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/unittests/Runtime/Inquiry.cpp b/flang-rt/unittests/Runtime/Inquiry.cpp
similarity index 97%
rename from flang/unittests/Runtime/Inquiry.cpp
rename to flang-rt/unittests/Runtime/Inquiry.cpp
index 3b523e992a317..e79c037864d9c 100644
--- a/flang/unittests/Runtime/Inquiry.cpp
+++ b/flang-rt/unittests/Runtime/Inquiry.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Inquiry.cpp -------------------------------===//
+//===-- unittests/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.
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/inquiry.h"
-#include "gtest/gtest.h"
 #include "tools.h"
-#include "flang/Runtime/type-code.h"
+#include "gtest/gtest.h"
+#include "flang-rt/runtime/type-code.h"
 
 using namespace Fortran::runtime;
 using Fortran::common::TypeCategory;
diff --git a/flang/unittests/Runtime/ListInputTest.cpp b/flang-rt/unittests/Runtime/ListInputTest.cpp
similarity index 98%
rename from flang/unittests/Runtime/ListInputTest.cpp
rename to flang-rt/unittests/Runtime/ListInputTest.cpp
index 38c758b7ef966..310c41a5c3fa5 100644
--- a/flang/unittests/Runtime/ListInputTest.cpp
+++ b/flang-rt/unittests/Runtime/ListInputTest.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/ListInputTest.cpp ---------------*- C++ -*-===//
+//===-- unittests/Runtime/ListInputTest.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 "CrashHandlerFixture.h"
-#include "../../runtime/io-error.h"
-#include "flang/Runtime/descriptor.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/unittests/Runtime/LogicalFormatTest.cpp b/flang-rt/unittests/Runtime/LogicalFormatTest.cpp
similarity index 94%
rename from flang/unittests/Runtime/LogicalFormatTest.cpp
rename to flang-rt/unittests/Runtime/LogicalFormatTest.cpp
index 26c9374be1338..bc933292181c1 100644
--- a/flang/unittests/Runtime/LogicalFormatTest.cpp
+++ b/flang-rt/unittests/Runtime/LogicalFormatTest.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/LogicalFormatTest.cpp -----------*- C++ -*-===//
+//===-- unittests/Runtime/LogicalFormatTest.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 "CrashHandlerFixture.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Runtime/io-api-consts.h"
 #include <algorithm>
 #include <array>
diff --git a/flang/unittests/Runtime/Matmul.cpp b/flang-rt/unittests/Runtime/Matmul.cpp
similarity index 98%
rename from flang/unittests/Runtime/Matmul.cpp
rename to flang-rt/unittests/Runtime/Matmul.cpp
index c3fed9b972df2..1b716fd01fd42 100644
--- a/flang/unittests/Runtime/Matmul.cpp
+++ b/flang-rt/unittests/Runtime/Matmul.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Matmul.cpp--------- -------------*- C++ -*-===//
+//===-- unittests/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.
@@ -7,12 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/matmul.h"
-#include "gtest/gtest.h"
 #include "tools.h"
+#include "gtest/gtest.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/descriptor.h"
-#include "flang/Runtime/type-code.h"
 
 using namespace Fortran::runtime;
 using Fortran::common::TypeCategory;
diff --git a/flang/unittests/Runtime/MatmulTranspose.cpp b/flang-rt/unittests/Runtime/MatmulTranspose.cpp
similarity index 98%
rename from flang/unittests/Runtime/MatmulTranspose.cpp
rename to flang-rt/unittests/Runtime/MatmulTranspose.cpp
index c582e945dc7c9..cc14ab755505a 100644
--- a/flang/unittests/Runtime/MatmulTranspose.cpp
+++ b/flang-rt/unittests/Runtime/MatmulTranspose.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/MatmulTranspose.cpp -------------*- C++ -*-===//
+//===-- unittests/Runtime/MatmulTranspose.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,13 +6,13 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "gtest/gtest.h"
 #include "tools.h"
+#include "gtest/gtest.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/descriptor.h"
 #include "flang/Runtime/matmul-transpose.h"
-#include "flang/Runtime/type-code.h"
 
 using namespace Fortran::runtime;
 using Fortran::common::TypeCategory;
diff --git a/flang/unittests/Runtime/MiscIntrinsic.cpp b/flang-rt/unittests/Runtime/MiscIntrinsic.cpp
similarity index 96%
rename from flang/unittests/Runtime/MiscIntrinsic.cpp
rename to flang-rt/unittests/Runtime/MiscIntrinsic.cpp
index 7e19ed250bdc0..c6783381bfc35 100644
--- a/flang/unittests/Runtime/MiscIntrinsic.cpp
+++ b/flang-rt/unittests/Runtime/MiscIntrinsic.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/MiscIntrinsic.cpp ---------------*- C++ -*-===//
+//===-- unittests/Runtime/MiscIntrinsic.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 "gtest/gtest.h"
 #include "tools.h"
-#include "flang/Runtime//misc-intrinsic.h"
+#include "gtest/gtest.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Runtime/allocatable.h"
 #include "flang/Runtime/cpp-type.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang/Runtime/misc-intrinsic.h"
 
 using namespace Fortran::runtime;
 
diff --git a/flang/unittests/Runtime/Namelist.cpp b/flang-rt/unittests/Runtime/Namelist.cpp
similarity index 99%
rename from flang/unittests/Runtime/Namelist.cpp
rename to flang-rt/unittests/Runtime/Namelist.cpp
index 0a28f3590b86e..040dedb8cd47c 100644
--- a/flang/unittests/Runtime/Namelist.cpp
+++ b/flang-rt/unittests/Runtime/Namelist.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Namelist.cpp --------------------*- C++ -*-===//
+//===-- unittests/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 "../../runtime/namelist.h"
+#include "flang-rt/runtime/namelist.h"
 #include "CrashHandlerFixture.h"
 #include "tools.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Runtime/io-api-consts.h"
 #include <algorithm>
 #include <cinttypes>
diff --git a/flang/unittests/Runtime/Numeric.cpp b/flang-rt/unittests/Runtime/Numeric.cpp
similarity index 99%
rename from flang/unittests/Runtime/Numeric.cpp
rename to flang-rt/unittests/Runtime/Numeric.cpp
index 29ebbc8ad7aa7..4baad3fc9ad0e 100644
--- a/flang/unittests/Runtime/Numeric.cpp
+++ b/flang-rt/unittests/Runtime/Numeric.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Numeric.cpp ---------------------*- C++ -*-===//
+//===-- unittests/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.
diff --git a/flang/unittests/Runtime/NumericalFormatTest.cpp b/flang-rt/unittests/Runtime/NumericalFormatTest.cpp
similarity index 99%
rename from flang/unittests/Runtime/NumericalFormatTest.cpp
rename to flang-rt/unittests/Runtime/NumericalFormatTest.cpp
index 274498b8e8695..5a8ead48dcef9 100644
--- a/flang/unittests/Runtime/NumericalFormatTest.cpp
+++ b/flang-rt/unittests/Runtime/NumericalFormatTest.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/NumericalFormatTest.cpp ---------*- C++ -*-===//
+//===-- unittests/Runtime/NumericalFormatTest.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 "CrashHandlerFixture.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
 #include "flang/Runtime/io-api-consts.h"
 #include <algorithm>
 #include <array>
diff --git a/flang/unittests/Runtime/Pointer.cpp b/flang-rt/unittests/Runtime/Pointer.cpp
similarity index 96%
rename from flang/unittests/Runtime/Pointer.cpp
rename to flang-rt/unittests/Runtime/Pointer.cpp
index 4ce13ebc50a56..a4645f5b6bf98 100644
--- a/flang/unittests/Runtime/Pointer.cpp
+++ b/flang-rt/unittests/Runtime/Pointer.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Pointer.cpp--------- -------------*- C++-*-===//
+//===-- unittests/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.
@@ -8,8 +8,8 @@
 
 #include "flang/Runtime/pointer.h"
 #include "gtest/gtest.h"
-#include "tools.h"
-#include "flang/Runtime/descriptor.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/tools.h"
 
 using namespace Fortran::runtime;
 
diff --git a/flang/unittests/Runtime/Ragged.cpp b/flang-rt/unittests/Runtime/Ragged.cpp
similarity index 94%
rename from flang/unittests/Runtime/Ragged.cpp
rename to flang-rt/unittests/Runtime/Ragged.cpp
index 5049bc83405f1..feadd032f59bd 100644
--- a/flang/unittests/Runtime/Ragged.cpp
+++ b/flang-rt/unittests/Runtime/Ragged.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Ragged.cpp ----------------------*- C++ -*-===//
+//===-- unittests/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.
diff --git a/flang/unittests/Runtime/Random.cpp b/flang-rt/unittests/Runtime/Random.cpp
similarity index 92%
rename from flang/unittests/Runtime/Random.cpp
rename to flang-rt/unittests/Runtime/Random.cpp
index cb739b9451429..bf4b540e768fd 100644
--- a/flang/unittests/Runtime/Random.cpp
+++ b/flang-rt/unittests/Runtime/Random.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Random.cpp ----------------------*- C++ -*-===//
+//===-- unittests/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.
@@ -6,10 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang/Runtime//random.h"
+#include "flang/Runtime/random.h"
 #include "gtest/gtest.h"
-#include "flang/Runtime/descriptor.h"
-#include "flang/Runtime/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/unittests/Runtime/Reduction.cpp b/flang-rt/unittests/Runtime/Reduction.cpp
similarity index 99%
rename from flang/unittests/Runtime/Reduction.cpp
rename to flang-rt/unittests/Runtime/Reduction.cpp
index 29675399abf5c..3701a32042c58 100644
--- a/flang/unittests/Runtime/Reduction.cpp
+++ b/flang-rt/unittests/Runtime/Reduction.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Reductions.cpp ----------------------------===//
+//===-- unittests/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.
@@ -7,14 +7,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/reduction.h"
-#include "gtest/gtest.h"
 #include "tools.h"
+#include "gtest/gtest.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"
-#include "flang/Runtime/descriptor.h"
 #include "flang/Runtime/reduce.h"
-#include "flang/Runtime/type-code.h"
 #include <cstdint>
 #include <cstring>
 #include <string>
diff --git a/flang/unittests/Runtime/RuntimeCrashTest.cpp b/flang-rt/unittests/Runtime/RuntimeCrashTest.cpp
similarity index 98%
rename from flang/unittests/Runtime/RuntimeCrashTest.cpp
rename to flang-rt/unittests/Runtime/RuntimeCrashTest.cpp
index 72a0b290cf864..e716dac2d1203 100644
--- a/flang/unittests/Runtime/RuntimeCrashTest.cpp
+++ b/flang-rt/unittests/Runtime/RuntimeCrashTest.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/CrashHandlerFixture.cpp ---------*- C++ -*-===//
+//===-- unittests/Runtime/RuntimeCrashTest.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 @@
 //===----------------------------------------------------------------------===//
 #include "CrashHandlerFixture.h"
 #include "tools.h"
-#include "../../runtime/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/unittests/Runtime/Stop.cpp b/flang-rt/unittests/Runtime/Stop.cpp
similarity index 96%
rename from flang/unittests/Runtime/Stop.cpp
rename to flang-rt/unittests/Runtime/Stop.cpp
index b13602eaee5ea..5d1af20e77de7 100644
--- a/flang/unittests/Runtime/Stop.cpp
+++ b/flang-rt/unittests/Runtime/Stop.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Stop.cpp ----------------------------------===//
+//===-- unittests/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,9 +9,10 @@
 /// Test runtime API for STOP statement and runtime API to kill the program.
 //
 //===----------------------------------------------------------------------===//
+
 #include "flang/Runtime/stop.h"
 #include "CrashHandlerFixture.h"
-#include "../../runtime/environment.h"
+#include "flang-rt/runtime/environment.h"
 #include <cstdlib>
 #include <gtest/gtest.h>
 
diff --git a/flang/unittests/Runtime/Support.cpp b/flang-rt/unittests/Runtime/Support.cpp
similarity index 96%
rename from flang/unittests/Runtime/Support.cpp
rename to flang-rt/unittests/Runtime/Support.cpp
index 8c8de73b5b979..c97a6eae3a155 100644
--- a/flang/unittests/Runtime/Support.cpp
+++ b/flang-rt/unittests/Runtime/Support.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Support.cpp ----------------------*- C++-*-===//
+//===-- unittests/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.
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/support.h"
-#include "gtest/gtest.h"
 #include "tools.h"
-#include "flang/Runtime/descriptor.h"
+#include "gtest/gtest.h"
+#include "flang-rt/runtime/descriptor.h"
 
 using namespace Fortran::runtime;
 using Fortran::common::TypeCategory;
diff --git a/flang/unittests/Runtime/TemporaryStack.cpp b/flang-rt/unittests/Runtime/TemporaryStack.cpp
similarity index 98%
rename from flang/unittests/Runtime/TemporaryStack.cpp
rename to flang-rt/unittests/Runtime/TemporaryStack.cpp
index 4f701e09b2945..ff8efe123d7cc 100644
--- a/flang/unittests/Runtime/TemporaryStack.cpp
+++ b/flang-rt/unittests/Runtime/TemporaryStack.cpp
@@ -1,4 +1,4 @@
-//===--- flang/unittests/Runtime/TemporaryStack.cpp -------------*- C++ -*-===//
+//===-- unittests/Runtime/TemporaryStack.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 "gtest/gtest.h"
-#include "tools.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"
-#include "flang/Runtime/descriptor.h"
 #include "flang/Runtime/temporary-stack.h"
-#include "flang/Runtime/type-code.h"
 #include <vector>
 
 using namespace Fortran::runtime;
diff --git a/flang/unittests/Runtime/Time.cpp b/flang-rt/unittests/Runtime/Time.cpp
similarity index 98%
rename from flang/unittests/Runtime/Time.cpp
rename to flang-rt/unittests/Runtime/Time.cpp
index 9309d7b1ceffa..548c0834e34a3 100644
--- a/flang/unittests/Runtime/Time.cpp
+++ b/flang-rt/unittests/Runtime/Time.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Time.cpp ----------------------------===//
+//===-- unittests/Runtime/Time.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/unittests/Runtime/Transformational.cpp b/flang-rt/unittests/Runtime/Transformational.cpp
similarity index 99%
rename from flang/unittests/Runtime/Transformational.cpp
rename to flang-rt/unittests/Runtime/Transformational.cpp
index 1d84b7e23779a..06df96a3cc45a 100644
--- a/flang/unittests/Runtime/Transformational.cpp
+++ b/flang-rt/unittests/Runtime/Transformational.cpp
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/Transformational.cpp ----------------------===//
+//===-- unittests/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.
@@ -7,10 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "flang/Runtime/transformational.h"
-#include "gtest/gtest.h"
 #include "tools.h"
+#include "gtest/gtest.h"
+#include "flang-rt/runtime/type-code.h"
 #include "flang/Common/float128.h"
-#include "flang/Runtime/type-code.h"
 #include <vector>
 
 using namespace Fortran::runtime;
diff --git a/flang/unittests/Runtime/tools.h b/flang-rt/unittests/Runtime/tools.h
similarity index 85%
rename from flang/unittests/Runtime/tools.h
rename to flang-rt/unittests/Runtime/tools.h
index 0347edace5c05..36a4c2bd9c8b7 100644
--- a/flang/unittests/Runtime/tools.h
+++ b/flang-rt/unittests/Runtime/tools.h
@@ -1,4 +1,4 @@
-//===-- flang/unittests/Runtime/tools.h -------------------------*- C++ -*-===//
+//===-- unittests/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,14 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef FORTRAN_UNITTESTS_RUNTIME_TOOLS_H_
-#define FORTRAN_UNITTESTS_RUNTIME_TOOLS_H_
+#ifndef FLANG_RT_UNITTESTS_RUNTIME_TOOLS_H_
+#define FLANG_RT_UNITTESTS_RUNTIME_TOOLS_H_
 
 #include "gtest/gtest.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/descriptor.h"
-#include "flang/Runtime/type-code.h"
 #include <cstdint>
 #include <cstring>
 #include <vector>
@@ -54,4 +54,4 @@ static OwningPtr<Descriptor> MakeArray(const std::vector<int> &shape,
 }
 
 } // namespace Fortran::runtime
-#endif // FORTRAN_UNITTESTS_RUNTIME_TOOLS_H_
+#endif // FLANG_RT_UNITTESTS_RUNTIME_TOOLS_H_
diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index c012b884ae3be..4dda492b43119 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -23,6 +23,7 @@ if (LLVM_ENABLE_EH)
 endif()
 
 set(FLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
+set(FLANG_RT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../flang-rt")
 
 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE)
   message(FATAL_ERROR "In-source builds are not allowed. \
@@ -34,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 +238,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, either add LLVM_ENABLE_RUNTIMES=flang-rt, or 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 +366,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 +463,7 @@ if (APPLE)
 endif()
 
 include(AddFlang)
+include(FlangCommon)
 
 if (FLANG_INCLUDE_TESTS)
   add_compile_definitions(FLANG_INCLUDE_TESTS=1)
@@ -562,7 +557,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 0000000000000..1b8606843b224
--- /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 e422a31a0b402..0b3b551ffbfba 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,7 +164,32 @@ 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.
@@ -203,20 +200,21 @@ are currently supported.
 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 (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:
 
 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 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 machines.
 
 ##### 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 387d4b2e62e0f..9396d956e2233 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -43,6 +43,12 @@ page](https://llvm.org/releases/).
  * 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". 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/examples/ExternalHelloWorld/CMakeLists.txt b/flang/examples/ExternalHelloWorld/CMakeLists.txt
index b61948718a5e3..dbb69475976dd 100644
--- a/flang/examples/ExternalHelloWorld/CMakeLists.txt
+++ b/flang/examples/ExternalHelloWorld/CMakeLists.txt
@@ -1,6 +1,6 @@
 # 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
diff --git a/flang/include/flang/Runtime/allocatable.h b/flang/include/flang/Runtime/allocatable.h
index 58061d9862095..714d85ec073c6 100644
--- a/flang/include/flang/Runtime/allocatable.h
+++ b/flang/include/flang/Runtime/allocatable.h
@@ -11,7 +11,7 @@
 #ifndef FORTRAN_RUNTIME_ALLOCATABLE_H_
 #define FORTRAN_RUNTIME_ALLOCATABLE_H_
 
-#include "flang/Runtime/descriptor.h"
+#include "flang/Runtime/descriptor-consts.h"
 #include "flang/Runtime/entry-names.h"
 
 namespace Fortran::runtime {
diff --git a/flang/include/flang/Runtime/descriptor-consts.h b/flang/include/flang/Runtime/descriptor-consts.h
index abcdbc4a12002..acd7bc5ddbdef 100644
--- a/flang/include/flang/Runtime/descriptor-consts.h
+++ b/flang/include/flang/Runtime/descriptor-consts.h
@@ -9,6 +9,7 @@
 #ifndef FORTRAN_RUNTIME_DESCRIPTOR_CONSTS_H_
 #define FORTRAN_RUNTIME_DESCRIPTOR_CONSTS_H_
 
+#include "flang/Common/Fortran-consts.h"
 #include "flang/Common/ISO_Fortran_binding_wrapper.h"
 #include "flang/Common/api-attrs.h"
 #include <cstddef>
@@ -29,6 +30,7 @@ class DerivedType;
 namespace Fortran::runtime {
 class Descriptor;
 using SubscriptValue = ISO::CFI_index_t;
+using common::TypeCategory;
 
 /// Returns size in bytes of the descriptor (not the data)
 /// This must be at least as large as the largest descriptor of any target
diff --git a/flang/include/flang/Runtime/pointer.h b/flang/include/flang/Runtime/pointer.h
index 704144f08114f..67c4fe266f55c 100644
--- a/flang/include/flang/Runtime/pointer.h
+++ b/flang/include/flang/Runtime/pointer.h
@@ -12,7 +12,7 @@
 #ifndef FORTRAN_RUNTIME_POINTER_H_
 #define FORTRAN_RUNTIME_POINTER_H_
 
-#include "flang/Runtime/descriptor.h"
+#include "flang/Runtime/descriptor-consts.h"
 #include "flang/Runtime/entry-names.h"
 
 namespace Fortran::runtime {
diff --git a/flang/runtime/CMakeLists.txt b/flang/runtime/CMakeLists.txt
index 1f4ee69598918..fbd1e1e2cc56e 100644
--- a/flang/runtime/CMakeLists.txt
+++ b/flang/runtime/CMakeLists.txt
@@ -16,6 +16,7 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
   set(CMAKE_CXX_EXTENSIONS OFF)
 
   set(FLANG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
+  set(FLANG_RT_SOURCE_DIR "${FLANG_SOURCE_DIR}/../flang-rt")
 
   set(LLVM_COMMON_CMAKE_UTILS "${FLANG_SOURCE_DIR}/../cmake")
   set(LLVM_CMAKE_UTILS "${FLANG_SOURCE_DIR}/../llvm/cmake")
@@ -57,8 +58,31 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
     REAL(16) is mapped to __float128, or libm for targets where REAL(16) \
     is mapped to long double, etc."
     )
+
+  option(FLANG_CUF_RUNTIME
+    "Compile CUDA Fortran runtime sources" OFF)
+  if (FLANG_CUF_RUNTIME)
+    find_package(CUDAToolkit REQUIRED)
+  endif()
 endif()
 
+# Runtime files are in Flang-RT's source dir.
+function (runtime_source_files outvar)
+  cmake_parse_arguments(ARG "" "SUBDIR" "" ${ARGN})
+
+  set(new_sources "")
+  foreach (source IN LISTS "${outvar}")
+    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}")
+  endforeach ()
+  set("${outvar}" ${new_sources} PARENT_SCOPE)
+endfunction ()
+
+# Runtime includes are in Flang-RT's source dir.
+include_directories(BEFORE "${FLANG_RT_SOURCE_DIR}/include")
+
 set(linked_libraries "")
 
 # function checks
@@ -108,7 +132,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
   set(NO_RTTI_FLAGS "-qnoeh -qnortti")
 endif ()
 
-configure_file(config.h.cmake config.h)
+configure_file("${FLANG_RT_SOURCE_DIR}/cmake/config.h.cmake.in" config.h)
 # include_directories is used here instead of target_include_directories
 # because add_flang_library creates multiple objects (STATIC/SHARED, OBJECT)
 # with different names
@@ -252,6 +276,7 @@ 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}")
@@ -285,6 +310,8 @@ if (NOT TARGET flang_rt.quadmath)
     list(APPEND sources ${f128_sources})
   endif()
 endif()
+runtime_source_files(sources SUBDIR "runtime")
+
 
 if (NOT DEFINED MSVC)
   add_flang_library(flang_rt.runtime
diff --git a/flang/runtime/CUDA/CMakeLists.txt b/flang/runtime/CUDA/CMakeLists.txt
index 1fd3bf22a83cf..aac1f62661810 100644
--- a/flang/runtime/CUDA/CMakeLists.txt
+++ b/flang/runtime/CUDA/CMakeLists.txt
@@ -13,7 +13,7 @@ include_directories(${CUDAToolkit_INCLUDE_DIRS})
 # added to the library name.
 set(CUFRT_LIBNAME flang_rt.cuda_${CUDAToolkit_VERSION_MAJOR})
 
-add_flang_library(${CUFRT_LIBNAME}
+set(sources
   allocator.cpp
   allocatable.cpp
   descriptor.cpp
@@ -24,6 +24,11 @@ add_flang_library(${CUFRT_LIBNAME}
   pointer.cpp
   registration.cpp
 )
+runtime_source_files(sources SUBDIR "cuda")
+
+add_flang_library(${CUFRT_LIBNAME}
+  ${sources}
+)
 
 if (BUILD_SHARED_LIBS)
   set(CUDA_RT_TARGET CUDA::cudart)
diff --git a/flang/runtime/Float128Math/CMakeLists.txt b/flang/runtime/Float128Math/CMakeLists.txt
index 3c382d16a21cd..cdac5f893cf91 100644
--- a/flang/runtime/Float128Math/CMakeLists.txt
+++ b/flang/runtime/Float128Math/CMakeLists.txt
@@ -68,6 +68,7 @@ set(sources
   y1.cpp
   yn.cpp
   )
+runtime_source_files(sources SUBDIR "quadmath")
 
 include_directories(AFTER "${CMAKE_CURRENT_SOURCE_DIR}/..")
 add_library(FortranFloat128MathILib INTERFACE)
diff --git a/flang/test/lit.cfg.py b/flang/test/lit.cfg.py
index c6266f3976f7c..0ba80f9a03f2e 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.runtime.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 697ba3fa79763..5b66e592bcfee 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/flang/unittests/CMakeLists.txt b/flang/unittests/CMakeLists.txt
index c54ceb3332abf..a02f791135f1f 100644
--- a/flang/unittests/CMakeLists.txt
+++ b/flang/unittests/CMakeLists.txt
@@ -12,6 +12,8 @@ endif()
 add_custom_target(FlangUnitTests)
 set_target_properties(FlangUnitTests PROPERTIES FOLDER "Flang/Tests")
 
+include_directories("${FLANG_RT_SOURCE_DIR}/include")
+
 function(add_flang_unittest_offload_properties target)
   # Do not apply runtime properties if not even compiling the runtime.
   if (NOT FLANG_INCLUDE_RUNTIME)
@@ -61,7 +63,13 @@ function(add_flang_nongtest_unittest test_name)
       set(suffix .test)
   endif()
 
-  add_executable(${test_name}${suffix} ${test_name}.cpp)
+  # Sources for runtime tests are in Flang-RT.
+  set(test_filepath "${FLANG_RT_SOURCE_DIR}/unittests/Evaluate/${test_name}.cpp")
+  if (NOT EXISTS "${test_filepath}")
+    set(test_filepath "${test_name}.cpp")
+  endif ()
+
+  add_executable(${test_name}${suffix} "${test_filepath}")
   set_target_properties(${test_name}${suffix} PROPERTIES FOLDER "Flang/Tests/Unit")
 
   if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
diff --git a/flang/unittests/Runtime/CMakeLists.txt b/flang/unittests/Runtime/CMakeLists.txt
index f3743be49b015..40afec3e113f3 100644
--- a/flang/unittests/Runtime/CMakeLists.txt
+++ b/flang/unittests/Runtime/CMakeLists.txt
@@ -1,34 +1,34 @@
 add_flang_unittest(FlangRuntimeTests
-  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
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/AccessTest.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Allocatable.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/ArrayConstructor.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/BufferTest.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/CharacterTest.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/CommandTest.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Complex.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/CrashHandlerFixture.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Derived.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/ExternalIOTest.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Format.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Inquiry.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/ListInputTest.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/LogicalFormatTest.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Matmul.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/MatmulTranspose.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/MiscIntrinsic.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Namelist.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Numeric.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/NumericalFormatTest.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Pointer.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Ragged.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Random.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Reduction.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/RuntimeCrashTest.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Stop.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Support.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Time.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/TemporaryStack.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/Transformational.cpp
 )
 
 target_link_libraries(FlangRuntimeTests
diff --git a/flang/unittests/Runtime/CUDA/CMakeLists.txt b/flang/unittests/Runtime/CUDA/CMakeLists.txt
index 860b2664d623b..6901da3920a46 100644
--- a/flang/unittests/Runtime/CUDA/CMakeLists.txt
+++ b/flang/unittests/Runtime/CUDA/CMakeLists.txt
@@ -1,9 +1,9 @@
 if (FLANG_CUF_RUNTIME)
 
 add_flang_unittest(FlangCufRuntimeTests
-  Allocatable.cpp
-  AllocatorCUF.cpp
-  Memory.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/CUDA/Allocatable.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/CUDA/AllocatorCUF.cpp
+  ${FLANG_RT_SOURCE_DIR}/unittests/Runtime/CUDA/Memory.cpp
 )
 
 if (BUILD_SHARED_LIBS)
diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index f5293e8663243..88512d0f1dd96 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -164,12 +164,18 @@ if ("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
     "https://compiler-rt.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 55422c2a4c023..caccb62d666ce 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 08f2fa522420b..f254cf10806d7 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 70e85c123e412..2370b41fb7f0b 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 4a6b317a03f66..7f1e2ae065d6c 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 5fdcdcf3fb4db159c2bad7a488ba931f11ae8d3c Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Thu, 13 Feb 2025 16:55:51 +0100
Subject: [PATCH 2/2] clang-format

---
 flang-rt/lib/runtime/descriptor.cpp             |  7 ++++---
 flang-rt/lib/runtime/io-stmt.cpp                | 13 ++++++-------
 flang-rt/lib/runtime/type-info.cpp              |  2 +-
 flang-rt/unittests/Runtime/Transformational.cpp |  4 ++--
 4 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/flang-rt/lib/runtime/descriptor.cpp b/flang-rt/lib/runtime/descriptor.cpp
index 8241a34a4990c..b893090ed2a21 100644
--- a/flang-rt/lib/runtime/descriptor.cpp
+++ b/flang-rt/lib/runtime/descriptor.cpp
@@ -141,9 +141,10 @@ RT_API_ATTRS OwningPtr<Descriptor> Descriptor::Create(
 
 RT_API_ATTRS std::size_t Descriptor::SizeInBytes() const {
   const DescriptorAddendum *addendum{Addendum()};
-  std::size_t bytes{ sizeof *this - sizeof(Dimension) + raw_.rank * sizeof(Dimension) +
-      (addendum ? addendum->SizeInBytes() : 0)};
-  assert (bytes <= MaxDescriptorSizeInBytes(raw_.rank,addendum) && "Descriptor must fit compiler-allocated space");
+  std::size_t bytes{sizeof *this - sizeof(Dimension) +
+      raw_.rank * sizeof(Dimension) + (addendum ? addendum->SizeInBytes() : 0)};
+  assert(bytes <= MaxDescriptorSizeInBytes(raw_.rank, addendum) &&
+      "Descriptor must fit compiler-allocated space");
   return bytes;
 }
 
diff --git a/flang-rt/lib/runtime/io-stmt.cpp b/flang-rt/lib/runtime/io-stmt.cpp
index b0823ffd9e703..c4350119d66ab 100644
--- a/flang-rt/lib/runtime/io-stmt.cpp
+++ b/flang-rt/lib/runtime/io-stmt.cpp
@@ -370,8 +370,8 @@ int NoUnitIoStatementState::EndIoStatement() {
 template <Direction DIR>
 ExternalIoStatementState<DIR>::ExternalIoStatementState(
     ExternalFileUnit &unit, const char *sourceFile, int sourceLine)
-    : ExternalIoStatementBase{unit, sourceFile, sourceLine}, mutableModes_{
-                                                                 unit.modes} {
+    : ExternalIoStatementBase{unit, sourceFile, sourceLine},
+      mutableModes_{unit.modes} {
   if constexpr (DIR == Direction::Output) {
     // If the last statement was a non-advancing IO input statement, the unit
     // furthestPositionInRecord was not advanced, but the positionInRecord may
@@ -990,9 +990,8 @@ ChildFormattedIoStatementState<DIR, CHAR>::ChildFormattedIoStatementState(
     ChildIo &child, const CHAR *format, std::size_t formatLength,
     const Descriptor *formatDescriptor, const char *sourceFile, int sourceLine)
     : ChildIoStatementState<DIR>{child, sourceFile, sourceLine},
-      mutableModes_{child.parent().mutableModes()}, format_{*this, format,
-                                                        formatLength,
-                                                        formatDescriptor} {}
+      mutableModes_{child.parent().mutableModes()},
+      format_{*this, format, formatLength, formatDescriptor} {}
 
 template <Direction DIR, typename CHAR>
 void ChildFormattedIoStatementState<DIR, CHAR>::CompleteOperation() {
@@ -1448,8 +1447,8 @@ bool InquireNoUnitState::Inquire(
 
 InquireUnconnectedFileState::InquireUnconnectedFileState(
     OwningPtr<char> &&path, const char *sourceFile, int sourceLine)
-    : NoUnitIoStatementState{*this, sourceFile, sourceLine}, path_{std::move(
-                                                                 path)} {}
+    : NoUnitIoStatementState{*this, sourceFile, sourceLine},
+      path_{std::move(path)} {}
 
 bool InquireUnconnectedFileState::Inquire(
     InquiryKeywordHash inquiry, char *result, std::size_t length) {
diff --git a/flang-rt/lib/runtime/type-info.cpp b/flang-rt/lib/runtime/type-info.cpp
index 82182696d70c6..e196478702e9a 100644
--- a/flang-rt/lib/runtime/type-info.cpp
+++ b/flang-rt/lib/runtime/type-info.cpp
@@ -86,7 +86,7 @@ RT_API_ATTRS std::size_t Component::SizeInBytes(
   } else if (category() == TypeCategory::Derived) {
     const DerivedType *type{derivedType()};
     return Descriptor::SizeInBytes(
-         rank_, true, type ? type->LenParameters() : 0);
+        rank_, true, type ? type->LenParameters() : 0);
   } else {
     return Descriptor::SizeInBytes(rank_);
   }
diff --git a/flang-rt/unittests/Runtime/Transformational.cpp b/flang-rt/unittests/Runtime/Transformational.cpp
index 06df96a3cc45a..89dd24b39750f 100644
--- a/flang-rt/unittests/Runtime/Transformational.cpp
+++ b/flang-rt/unittests/Runtime/Transformational.cpp
@@ -26,8 +26,8 @@ using BesselX0FuncType =
     std::function<void(Descriptor &, int32_t, int32_t, const char *, int)>;
 
 template <int KIND>
-constexpr CppTypeFor<TypeCategory::Real, KIND>
-    besselEpsilon = CppTypeFor<TypeCategory::Real, KIND>(1e-4);
+constexpr CppTypeFor<TypeCategory::Real, KIND> besselEpsilon =
+    CppTypeFor<TypeCategory::Real, KIND>(1e-4);
 
 template <int KIND>
 static void testBesselJn(BesselFuncType<KIND> rtFunc, int32_t n1, int32_t n2,



More information about the llvm-commits mailing list