52 Commits

Author SHA1 Message Date
Valentin Clement (バレンタイン クレメン)
bb179c483a
[flang][rt] Allow ReportFatalUserError to be build on device (#133979) 2025-04-01 13:50:42 -07:00
Valentin Clement (バレンタイン クレメン)
afa32d3e0e
[flang][cuda] Fix char argument
This would fail with `error: argument of type "char" is incompatible with parameter of type "const char *"`
2025-04-01 11:00:50 -07:00
Valentin Clement (バレンタイン クレメン)
01889de8e9
[flang][device] Enable Stop functions on device build (#133803)
Update `StopStatement` and `StopStatementText` to be build for the
device.
2025-04-01 10:06:45 -07:00
Slava Zakharin
1ab3a4f234
[flang-rt][NFC] Work around CTK12.8 compilation failure. (#133833)
It happened in https://lab.llvm.org/buildbot/#/builders/152/builds/1131
when the buildbot was switched from CTK12.3 to CTK12.8.
The logs are gone by now, so the above link is useless.

The error was:
error: ‘auto’ not permitted in template argument

This workaround helps, but I also reported the issue to NVCC devs.
2025-04-01 08:04:45 -07:00
Jean-Didier PAILLEUX
513a91a5f1
[flang/flang-rt] Implement PERROR intrinsic form GNU Extension (#132406)
Add the implementation of the `PERROR(STRING) ` intrinsic from the GNU
Extension to prints on the stderr a newline-terminated error message
corresponding to the last system error prefixed by `STRING`.
(https://gcc.gnu.org/onlinedocs/gfortran/PERROR.html)
2025-04-01 15:47:54 +02:00
Valentin Clement (バレンタイン クレメン)
0b31f08537
[flang][cuda] Add support for NV_CUDAFOR_DEVICE_IS_MANAGED (#133778)
Add support for the environment variable `NV_CUDAFOR_DEVICE_IS_MANAGED`
as described in the documentation:
https://docs.nvidia.com/hpc-sdk/compilers/cuda-fortran-prog-guide/index.html#controlling-device-data-is-managed.

This mainly switch device allocation to managed allocation.
2025-03-31 13:17:21 -07:00
Peter Klausler
4ea5aa09de
[flang][NFC] Restore I/O runtime API header name (#132423)
flang/include/flang/Runtime/io-api.h was changed into io-api-consts.h,
then wrapped into a new io-api.h that includes io-api-consts.h, does
some redundant includes and declarations, and then declares the
prototype of one function, InquiryKeywordHashDecode.

Make that function static in io-stmt.cpp prior to its sole call site,
then undo the renaming, to reduce confusion and redundancy.
2025-03-26 12:09:16 -07:00
Michael Kruse
27539c3f90 Revert "[Flang] Remove FLANG_INCLUDE_RUNTIME (#124126)"
The production buildbot master apparently has not yet been restarted
since https://github.com/llvm/llvm-zorg/pull/393 landed.

This reverts commit 96d1baedefc3581b53bc4389bb171760bec6f191.
2025-03-26 19:02:13 +01:00
Michael Kruse
96d1baedef
[Flang] Remove FLANG_INCLUDE_RUNTIME (#124126)
Remove the FLANG_INCLUDE_RUNTIME option which was replaced by
LLVM_ENABLE_RUNTIMES=flang-rt.

The FLANG_INCLUDE_RUNTIME option was added in #122336 which disables the
non-runtimes build instructions for the Flang runtime so they do not
conflict with the LLVM_ENABLE_RUNTIMES=flang-rt option added in #110217.
In order to not maintain multiple build instructions for the same thing,
this PR completely removes the old build instructions (effectively
forcing FLANG_INCLUDE_RUNTIME=OFF).

As per discussion in
https://discourse.llvm.org/t/buildbot-changes-with-llvm-enable-runtimes-flang-rt/83571/2
we now implicitly add LLVM_ENABLE_RUNTIMES=flang-rt whenever Flang is
compiled in a bootstrapping (non-standalone) build. Because it is
possible to build Flang-RT separately, this behavior can be disabled
using `-DFLANG_ENABLE_FLANG_RT=OFF`. Also see the discussion an
implicitly adding runtimes/projects in #123964.
2025-03-26 18:50:41 +01:00
Slava Zakharin
613a077b05
[flang] Generate quadmath_wrapper.h for Flang Evaluate. (#132817)
When building Flang with Clang, we need to do the same quadmath.h
wrapping as we do for flang-rt. I extracted the CMake code
into FlangCommon.cmake, and cleaned up the arguments passing
to execute_process (note that `-###` was treated as `-` in the original
code, because `#` starts a comment). I believe the Clang command
does not require the input source file, so I removed it as well.
2025-03-25 12:08:38 -07:00
Eugene Epshteyn
2c8e26081f
[flang] Add HOSTNM runtime and lowering intrinsics implementation (#131910)
Implement GNU extension intrinsic HOSTNM, both function and subroutine
forms. Add HOSTNM documentation to `flang/docs/Intrinsics.md`. Add
lowering and semantic unit tests.

(This change is modeled after GETCWD implementation.)
2025-03-25 13:17:17 -04:00
vdonaldson
92e0560347
[flang] ieee_denorm (#132307)
Add support for the nonstandard ieee_denorm exception for real kinds 3,
4, 8 on x86 processors.
2025-03-25 13:02:43 -04:00
Joseph Huber
60eb89f9fa [flang-rt] Fix typo using static instead of shared
Summary:
I copied this from the static usage, replaced the shared on the
dependency but not on the target.
2025-03-25 10:24:32 -05:00
Michael Kruse
ea68d830d9 [flang-rt][NFC] Fix indention 2025-03-24 15:15:43 +01:00
Joseph Huber
85974a0537
[flang-rt] Add experimental support for GPU build (#131826)
Summary:
This patch adds initial support for compiling `flang-rt` directly for
the GPU. The method used here matches what's already done for `libc` and
`libc++` for the GPU and builds off of those projects.

Mainly this requires setting up some flags and setting the sources that
currently work. This will deposit the resulting library in the
appropriate directory. These files are then intended to be linked via
`-Xoffload-linker` support in the offloading driver.
```
lib/clang/21/lib/nvptx64-nvidia-cuda/libflang_rt.runtime.a
lib/clang/21/lib/amdgcn-amd-amdhsa/libflang_rt.runtime.a
```

This is obviously missing a lot of functions, mainly the `io` support.
Most of what we cannot support is due to using POSIX things that just
don't make sense on the GPU. Stuff like `pthreads` or `sema`.

Getting unit tests to run on this will also be a challenge. We could run
tests the same way we do with `libc`, but the problem there is that the
`libc` test suite is freestanding while `gtest` currently doesn't
compile on the GPU bcause it uses a lot of weird stuff. If the unit
tests were simply `int main` then it would work.

I don't understand the actual runtime code very well, I'd appreciate
some guidance on how to actually support Fortran IO from this interface.
As I understand it, Fortran IO requires a stack-like operation, which
conflicts with the SIMT model GPUs use. Worst case scenario we could
burn some LDS to keep a stack, or serialize it somehow since we can
always just iterate over all the active lanes.

Building this right now looks like this, which depends on the arguments
added in https://github.com/llvm/llvm-project/pull/131695.
```
    -DRUNTIMES_nvptx64-nvidia-cuda_LLVM_ENABLE_RUNTIMES=compiler-rt;libc;libcxx;libcxxabi;flang-rt \
    -DRUNTIMES_amdgcn-amd-amdhsa_LLVM_ENABLE_RUNTIMES=compiler-rt;libc;libcxx;libcxxabi;flang-rt \
    -DRUNTIMES_nvptx64-nvidia-cuda_FLANG_RT_LIBC_PROVIDER=llvm \
    -DRUNTIMES_nvptx64-nvidia-cuda_FLANG_RT_LIBCXX_PROVIDER=llvm \
    -DRUNTIMES_amdgcn-amd-amdhsa_FLANG_RT_LIBC_PROVIDER=llvm \
    -DRUNTIMES_amdgcn-amd-amdhsa_FLANG_RT_LIBCXX_PROVIDER=llvm
```
2025-03-24 08:31:42 -05:00
Joseph Huber
038cdd236f
[flang-rt] Add support for using LLVM in-tree libc/libc++ (#131695)
Summary:
This patch adds an interface that uses an in-tree build of LLVM's libc
and libc++.

This is done using the `-DFLANG_RT_LIBC_PROVIDER=llvm` and
`-DFLANG_RT_LIBCXX_PROVIDER=llvm` options. Using `libc` works in terms
of CMake, but the LLVM libc is not yet complete enough to compile all
the files.
2025-03-24 06:05:24 -05:00
Valentin Clement (バレンタイン クレメン)
ecaef010f3
[flang][cuda] Support corner case of data transfer (#132451)
The flang runtime will complain when the number of elements in the two
descriptors involved in the data transfer are not matching.

In some cases, we can still perform the data transfer to match the
behavior of the reference compiler.

When the RHS elements count is bigger than the LHS elements count and
both descriptors are contiguous, we can perform the data transfer with
the bare pointers and the number of bytes from the LHS.

We don't really have unit tests set up for data transfer, this is why I
didn't include one here.
2025-03-21 15:39:05 -07:00
jeanPerier
cd0a2a3f1b
[flang] add QSORT extension intrinsic to the runtime (#132033)
Add support for legacy Fortran intrinsic QSORT from lib 3f.
This is a thin Fortran wrapper over libc qsort.
2025-03-19 16:14:37 +01:00
Slava Zakharin
7a9473b1b0 [flang-rt] Fixed build issue in flang-runtime-cuda-clang. 2025-03-18 16:18:30 -07:00
Aiden Grossman
43c21f96a7
Revert "[Premerge] Add flang-rt (#128678)" (#131915)
This reverts commit 95d28fe503cc3d2bc0bb980442d3defaf199ea5a.

I did not fully realize the implications of this change when reviewing.
With how it is set up currently, it causes clang and all of the runtimes
to be built and tested everytime a change to MLIR is made. This is a
large regression in build/test time, which seems to have been causing
large queueing delays.

Reverting for now. Once we rework the runtimes build for premerge (which
I hope to do soon, ideally in the next week), I will make sure flang-rt
gets added in.
2025-03-18 15:52:59 -07:00
Slava Zakharin
7d7b58bc5d
[flang-rt] Added ShallowCopy API. (#131702)
This API will be used for copying non-contiguous arrays
into contiguous temporaries to support `-frepack-arrays`.
The builder factory API will be used in the following commits.
2025-03-18 12:58:25 -07:00
Slava Zakharin
02b4522358
[NFC][flang] Added deduction guide for StaticDescriptor class. (#131690)
I keep getting these warnings when building with clang-17:
`warning: 'StaticDescriptor' may not intend to support class template
argument deduction [-Wctad-maybe-unsupported]`

This change should help avoiding them.
2025-03-18 07:58:35 -07:00
Daniel Chen
a4510aa7cb
[flang-rt] replace the triple dir to 'aix' for flang-rt to be consistent with clang on AIX. (#130875)
This change is to put `libflang_rt.runtime.a` into
`build/lib/clang/21/lib/aix/libflang_rt.runtime.a`, which is consistent
with clang on AIX.
2025-03-17 11:30:35 -04:00
Slava Zakharin
f326036767
[flang-rt] Added IsContiguousUpTo runtime function. (#131048)
I want to be able to check if the storage is contiguous
in the innermost dimension, so I decided to add an entry point
that takes `dim` as the number of leading dimensions to check.
It seems that a runtime call might result in less code size
even when `dim` is 1, so here it is.
For opt-for-speed I am going to inline it in FIR.

Depends on #131047.
2025-03-14 17:13:21 -07:00
Slava Zakharin
c8b8415b1a
[flang-rt] Install flang_rt.cuda with the toolchain. (#131373) 2025-03-14 16:12:32 -07:00
Slava Zakharin
eeb27331dc
[flang-rt] Use --as-needed for linking flang-rt libraries. (#130856)
This change makes sure that there is no unnecessary library
dependencies like libc++/libstdc++.
2025-03-14 08:26:03 -07:00
Michael Kruse
0735537ec8 [Flang-RT] Append to library subdir
The code intended to append more directories as required, instead of
discarding the previous steps.

NFCI accept on Apple.
2025-03-13 13:06:20 +01:00
Michael Kruse
95d28fe503
[Premerge] Add flang-rt (#128678)
Flang's runtime can now be built using LLVM's LLVM_ENABLE_RUNTIMES
mechanism, with the intent to remove the old mechanism in #124126.
Update the pre-merge builders to use the new mechanism.

In the current form, #124126 actually will add
LLVM_ENABLE_RUNTIMES=flang-rt implicitly, so no change is strictly
needed. I still think it is a good idea to do it explicitly and in
advance.

On Windows, flang-rt also requires compiler-rt, but which is not
building on Windows anyway.
2025-03-13 12:17:59 +01:00
Michael Kruse
13261e856a [Flang-RT][NFC] Fix comment
The cases of LLVM_ENABLE_PER_TARGET_RUNTIME_DIR were swapped.

Noticed by @DanielCChen in #130875. Thanks!
2025-03-13 11:45:03 +01:00
Slava Zakharin
c542991703
[flang-rt] Fixed HAVE_LDBL_MANT_DIG_113 detection. (#131010)
I thought I guessed a fix in #130836, but I was wrong.
We actually had the same code in
`flang/cmake/modules/FlangCommon.cmake`.
The check does not pass in flang-rt bootstrap build, because
`-nostdinc++` is added for all `runtimes` checks.
I decided to make the check with the C header, though, I am still
unsure whether it is reliable with a clang that has not been
installed (it is taken from the build structure during flang-rt
configure step).
I verified that this PR enables REAL(16) math entries on aarch64.
2025-03-12 16:50:01 -07:00
Slava Zakharin
d547005f6c
[flang-rt] Enable -funwind-tables so that backtrace works. (#130848)
`backtrace` will not show anything unless it can unwind from
Fortran runtime, so it is useful to have this ability even
in the release build.
2025-03-11 16:14:53 -07:00
Slava Zakharin
6dbb5319ce
[flang-rt] Set HAVE_LDBL_MANT_DIG_113. (#130836)
This check enables flang_rt.quadmath build on aarch64.
2025-03-11 15:34:23 -07:00
Michael Kruse
7341753a2e
[Flang-RT] Environment introspection for quadmath.h (#130411)
When compiling Flang-RT with Clang, query Clang for the GCC installation
it uses. If found, create `quadmath_wrapper.h` that points to the
`quadmath.h` of that GCC installation.

`quadmath.h` is only available when compiling with gcc, and Clang has no
equivalent even though gcc's version compiles fine with Clang (at least
up to and including gcc 13). It is still available into gcc's
installation resource dir (in constrast to a system-wide indirectory
such as `/usr/include` or `/usr/local/include`) and therefore not
available to any compiler other than the gcc of that installation.
quadmath may also be a different OS package than gcc itself, so it is
not necessarily presesent.
 
Clang actually already appropriates a GCC installation for its libraries
such that `libquadmath.a` is already found, but it does not do so for
the include paths. Because adding that directory to the header search
path may have wide-reaching consquences, we create only a wrapper header
that points to the real `quadmath.h` in the same GCC installation that
Clang uses.
2025-03-11 14:18:06 +01:00
مهدي شينون (Mehdi Chinoune)
cf5aa559a8
[flang] Don't redefine pid_t on MinGW-w64. (#130288) 2025-03-10 17:27:47 +00:00
Daniel Chen
78631ac51b
[Flang] explicitly link the pthread library when building shared flang-rt. (#129956)
This patch is to explicitly link the pthread library when building
shared flang-rt.
On AIX, for example, it needs to link in `libpthread.a` explicitly in
order to resolve the references to those `pthread_*` functions in
`include/flang-rt/runtime/lock.h`
2025-03-07 14:13:38 -05:00
Kelvin Li
a7a65a824e
[flang] explicitly cast the pointer to void* in std::memcpy calls (NFC) (#129946)
This patch is to add the explicit cast to the first argument of std::memcpy.
2025-03-06 16:09:16 -05:00
Valentin Clement (バレンタイン クレメン)
c8898b09f9
[flang][rt] Use allocator registry to allocate the pointer payload (#129992)
pointer allocation is done through `AllocateValidatedPointerPayload`.
This function was not updated to use the registered allocators in the
descriptor to perform the allocation. This patch makes use of the
allocator.
The footer word is not set and not checked for allocator other than the
default one. The support will likely come in a follow up patch but this
will necessitate more functions to be registered to be able to set and
get the footer value when the allocation in on the device.
2025-03-06 08:47:27 -08:00
Slava Zakharin
9b1604065e
[flang-rt] Move unit-map.cpp to host-only sources list. (#129763)
This file is not enabled for the offload builds.
This patch aligns the list with flang/runtime/CMakeLists.txt
(that is about to be removed).
2025-03-04 14:39:16 -08:00
Slava Zakharin
f44fb56f62
[flang-rt] Set CUDA_SEPARABLE_COMPILATION for PTX library. (#129563)
`CUDA_SEPARABLE_COMPILATION` adds `-rdc=true`, which is needed
for the PTX library build.
2025-03-03 17:10:35 -08:00
Valentin Clement (バレンタイン クレメン)
ae84717d11
[flang][cuda] Fix descriptor sync in data transfer (#129333)
The destination descriptor on the device needs to be sync with the
destination descriptor on the host, not the src one.
2025-02-28 14:52:33 -08:00
Peter Klausler
abe1ecff54
[flang][runtime] Detect byte order reversal problems (#129093)
When reading an unformatted sequential file with variable-length
records, detect byte order reversal problems with the first record's
header and footer words, and emit a more detailed error message.
2025-02-27 16:16:15 -08:00
Michał Górny
d254fa877f
[flang-rt] Make FLANG_RT_INSTALL_RESOURCE_PATH configurable (#128561)
Make it possible to change the path used to install flang-rt library.
This is particularly necessary for standalone builds, where the CMake
script currently hardwires the default clang install path, and therefore
is incorrect for distributions that override it. However, for
consistency I have made it configurable unconditionally, preserving the
current defaults.
2025-02-25 08:47:47 +00:00
Kelvin Li
7262a1ea31
[flang] Set compile definitions for flang-rt build on AIX (#127919)
After commit b55f751, the flang-rt build on AIX is missing `-D_LARGE_FILE_API 
-D_XOPEN_SOURCE=700` in compiling the source. This patch is to add the 
compile definitions.

---------

Co-authored-by: Michael Kruse <github@meinersbur.de>
2025-02-24 10:03:26 -05:00
Michael Kruse
7e4fef69f9 [flang-rt][NFC] Fix grammar 2025-02-21 19:41:12 +01:00
Tom Stellard
2b340c10a6
flang: Fix build with latest libc++ (#127362)
I think this first stopped working with
954836634abb446f18719b14120c386a929a42d1. This patch fixes the following
error:


/home/runner/work/llvm-project/llvm-project/flang/runtime/io-api-minimal.cpp:153:11:
error: '__libcpp_verbose_abort' is missing exception specification
'noexcept'
   153 | void std::__libcpp_verbose_abort(char const *format, ...) {
       |           ^
| noexcept
/mnt/build/bin/../include/c++/v1/__verbose_abort:30:28: note: previous
declaration is here
30 | __printf__, 1, 2) void __libcpp_verbose_abort(const char* __format,
...) _LIBCPP_VERBOSE_ABORT_NOEXCEPT;
       |                            ^
1 error generated.
2025-02-19 06:53:30 -08:00
Eugene Epshteyn
01f2f81f2b
[flang-rt] Fixed uninitialized class member variable (#127681)
valgrind complained that `OpenStatementState::pathLength_` was used
before it was initialized, when a file was opened with
`status='scratch'`. The code seems to expect that `pathLengh_` should be
initialized to 0, so added default initialization to the declaration.
2025-02-18 15:52:51 -05:00
Michael Kruse
4c4fc4650f
[Flang-RT] Build libflang_rt.so (#121782)
Under non-Windows platforms, also create a dynamic library version of
the runtime. Build of either version of the library can be switched on
using FLANG_RT_ENABLE_STATIC=ON respectively FLANG_RT_ENABLE_SHARED=ON.
Default is to build only the static library, consistent with previous
behaviour. This is because the way the flang driver invokes the linker,
most linkers choose the dynamic library by default, if available.
Building the dynamic library therefore causes flang-built executables to
depend on `libflang_rt.so`, unless explicitly told otherwise.
2025-02-17 12:53:12 +01:00
Michael Kruse
b55f7512a7
[Flang] LLVM_ENABLE_RUNTIMES=flang-rt (#110217)
Extract Flang's runtime library to use the LLVM_ENABLE_RUNTIME
mechanism. It will only become active when
`LLVM_ENABLE_RUNTIMES=flang-rt` is used, which also changes the
`FLANG_INCLUDE_RUNTIME` to `OFF` so the old runtime build rules do not
conflict. This also means that unless `LLVM_ENABLE_RUNTIMES=flang-rt` is
passed, nothing changes with the current build process.

Motivation:
* Consistency with LLVM's other runtime libraries (compiler-rt, libc,
libcxx, openmp offload, ...)
* Allows compiling the runtime for multiple targets at once using the
LLVM_RUNTIME_TARGETS configuration options
* Installs the runtime into the compiler's per-target resource directory
so it can be automatically found even when cross-compiling

Also see RFC discussion at
https://discourse.llvm.org/t/rfc-use-llvm-enable-runtimes-for-flangs-runtime/80826
2025-02-16 15:39:52 +01:00
Michael Kruse
81c85ea30f [flang-rt] Fix aarch64-libcxx build failure
There seems to be multiple declarations of __libcpp_verbose_abort, some
with noexcept and some without. Reverting to the previous
forward-declaration (without noexcept) which seemes to have worked
before.
2025-02-16 13:46:30 +01:00
Michael Kruse
54f37133b7
[Flang][NFC] Move runtime library files to flang-rt (#110298)
Mostly mechanical changes in preparation of extracting the Flang-RT
"subproject" in #110217. This PR intends to only move pre-existing files
to the new folder structure, with no behavioral change. Common files
(headers, testing, cmake) shared by Flang-RT and Flang remain in
`flang/`.

Some cosmetic changes and files paths were necessary:
* Relative paths to the new path for the source files and
`add_subdirectory`.
 * Add the new location's include directory to `include_directories`
* The unittest/Evaluate directory has unitests for flang-rt and Flang. A
new `CMakeLists.txt` was introduced for the flang-rt tests.
 * Change the `#include` paths relative to the include directive
 * clang-format on the `#include` directives
* Since the paths are part if the copyright header and include guards, a
script was used to canonicalize those
* `test/Runtime` and runtime tests in `test/Driver` are moved, but the
lit.cfg.py mechanism to execute the will only be added in #110217.
2025-02-16 13:25:31 +01:00