https://github.com/Meinersbur created https://github.com/llvm/llvm-project/pull/246686
The intrinsic modules `iso_c_binding` and `iso_fortran_env` allow Fortran programs to inspect properties of type kinds. Since #171515 these can be different for each target triple due to using per-triple modules files. However, the available types are also affected by compile-time switches such as `-fdisable-integer-N` and `-fdisable-real-N`. So far the intrinsic modules just assumed that all of them are available, with the exception of `REAL(16)` which assumes availability of `REAL(16)` based on where Flang has been compiled [sic!]. There is a precedence of the value of a constant depending on compiler flags which is `numeric_storage_size` (#87566) in `iso_fortran_env.mod`. This PR is replicating this design. That is: 1. Introduce `__builtin_selected_int_kind`, `__builtin_selected_unsigned_kind`, and `__builtin_selected_real_kind` as deferred-folding variants of `selected_int_kind`, `selected_unsigned_kind`, and `selected_real_kind`. 2. These builtins are not folded to a constant immediately, but only when used in a non-intrinsic module/program. That is, the presence of `-fdisable-integer-N`/`-fdisable-real-N` is treated as "undetermined". 3. Remove `iso_fortran_env_impl.cpp`. There are no kind constants in the object files anymore. They cannot since the value of these constants conceptually unknown when compiling Flang-RT. This solves the dreaded "Use Clang for things things that only Flang can reliable know" problem (#197687). After this change, `iso_fortran_env_impl.f90` could be re-merged into `iso_fortran_env_impl.f90` without requiring Flang to build the shared/static Flang-RT library (#169525). It also should make #221907 easier as one use of the `FLANG_RUNTIME_F128_MATH_LIB` configure-time option is gone. Implementation notes: * This also introduces `-fdisable-real-16` * The intrinsic module `ieee_arithmetic.mod` needs to be usable even if it contains kinds that are currently disabled. Flang hard-codes a exception for this. * `selected_int_kind`, `selected_unsigned_kind`, and `selected_real_kind` are still available but in contrast to the `__builtin_` variants never deferred. I considered defining them in terms of the builtins like `numeric_storage_size` and `ieee_selected_real_kind` already do. However, the AI convinced me against doing that as the are apparently more special than simple symbols. * In a user module, there is a difference between aliasing the intrinsic symbol and using it: ``` module m use iso_c_binding, only: my_long_double => c_long_double use iso_fortran_env, only: my_nss => numeric_storage_size integer, parameter :: copied_long_double = my_long_double integer, parameter :: copied_nss = my_nss end module ``` For users of `m.mod`, the alias is still sensitive to `-fdisable-real-N` while the copied parameter is not. This is already the case for `numeric_storage_size` and I did not change that. >From 673ad6af17e31892383d628cb911b41873eff210 Mon Sep 17 00:00:00 2001 From: Michael Kruse <[email protected]> Date: Fri, 27 Nov 2026 16:11:09 +0100 Subject: [PATCH] [Flang] Kind-selection at compile-time --- clang/include/clang/Options/FlangOptions.td | 2 + clang/lib/Driver/ToolChains/Flang.cpp | 5 + flang-rt/lib/runtime/CMakeLists.txt | 1 - flang-rt/lib/runtime/__fortran_builtins.f90 | 5 +- flang-rt/lib/runtime/__fortran_type_info.f90 | 2 +- flang-rt/lib/runtime/ieee_arithmetic.f90 | 59 +--- flang-rt/lib/runtime/iso_c_binding.f90 | 20 +- flang-rt/lib/runtime/iso_fortran_env_impl.cpp | 253 ------------------ flang-rt/lib/runtime/iso_fortran_env_impl.f90 | 131 ++++----- .../compare_iso_fortran_env_symbols.f90 | 14 - flang-rt/test/Driver/iso_fortran_env_impl.f90 | 25 -- .../include/flang/Evaluate/check-expression.h | 10 + flang/lib/Evaluate/check-expression.cpp | 61 ++++- flang/lib/Evaluate/fold-integer.cpp | 8 +- flang/lib/Evaluate/intrinsics.cpp | 24 ++ flang/lib/Frontend/CompilerInvocation.cpp | 3 + flang/lib/Lower/Bridge.cpp | 24 +- flang/lib/Semantics/expression.cpp | 10 +- .../Semantics/deferred-folding-builtins.f90 | 15 ++ .../test/Semantics/disabled-integer-kinds.f90 | 70 +++++ flang/test/Semantics/disabled-real-kinds.f90 | 55 ++++ flang/test/Semantics/modfile90.f90 | 22 ++ 22 files changed, 374 insertions(+), 445 deletions(-) delete mode 100644 flang-rt/lib/runtime/iso_fortran_env_impl.cpp delete mode 100644 flang-rt/test/Driver/compare_iso_fortran_env_symbols.f90 delete mode 100644 flang-rt/test/Driver/iso_fortran_env_impl.f90 create mode 100644 flang/test/Semantics/deferred-folding-builtins.f90 create mode 100644 flang/test/Semantics/disabled-integer-kinds.f90 create mode 100644 flang/test/Semantics/disabled-real-kinds.f90 create mode 100644 flang/test/Semantics/modfile90.f90 diff --git a/clang/include/clang/Options/FlangOptions.td b/clang/include/clang/Options/FlangOptions.td index ad39d052bee6af..d3f27bd89bb1c3 100644 --- a/clang/include/clang/Options/FlangOptions.td +++ b/clang/include/clang/Options/FlangOptions.td @@ -162,6 +162,8 @@ def fdisable_real_3 : Flag<["-"],"fdisable-real-3">, Group<f_Group>, HelpText<"Disable real(KIND=3) from TargetCharacteristics">, Flags<[HelpHidden]>; def fdisable_real_10 : Flag<["-"],"fdisable-real-10">, Group<f_Group>, HelpText<"Disable real(KIND=10) from TargetCharacteristics">, Flags<[HelpHidden]>; +def fdisable_real_16 : Flag<["-"],"fdisable-real-16">, Group<f_Group>, + HelpText<"Disable real(KIND=16) from TargetCharacteristics">, Flags<[HelpHidden]>; def fdisable_integer_2 : Flag<["-"],"fdisable-integer-2">, Group<f_Group>, HelpText<"Disable integer(KIND=2) from TargetCharacteristics">, Flags<[HelpHidden]>; def fdisable_integer_16 : Flag<["-"],"fdisable-integer-16">, Group<f_Group>, diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp index 6a464323c6c225..47bbb9bf245c66 100644 --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -153,6 +153,11 @@ void Flang::addFortranDialectOptions(const ArgList &Args, options::OPT_fdefault_real_8, options::OPT_fdefault_integer_8, options::OPT_fdefault_double_8, + options::OPT_fdisable_real_3, + options::OPT_fdisable_real_10, + options::OPT_fdisable_real_16, + options::OPT_fdisable_integer_2, + options::OPT_fdisable_integer_16, options::OPT_flarge_sizes, options::OPT_fno_automatic, options::OPT_fhermetic_module_files, diff --git a/flang-rt/lib/runtime/CMakeLists.txt b/flang-rt/lib/runtime/CMakeLists.txt index 41ad231a32b3ac..e3f5208c4c8a64 100644 --- a/flang-rt/lib/runtime/CMakeLists.txt +++ b/flang-rt/lib/runtime/CMakeLists.txt @@ -113,7 +113,6 @@ set(supported_sources # List of source not used for GPU offloading. set(host_sources - iso_fortran_env_impl.cpp command.cpp complex-powi.cpp complex-reduction.c diff --git a/flang-rt/lib/runtime/__fortran_builtins.f90 b/flang-rt/lib/runtime/__fortran_builtins.f90 index 773b5679cfd714..97e544af56a2a4 100644 --- a/flang-rt/lib/runtime/__fortran_builtins.f90 +++ b/flang-rt/lib/runtime/__fortran_builtins.f90 @@ -40,8 +40,7 @@ intrinsic :: sizeof ! extension public :: sizeof - intrinsic :: selected_int_kind - integer, parameter :: int64 = selected_int_kind(18) + integer, parameter :: int64 = 8 type, bind(c), public :: __builtin_c_ptr integer(kind=int64), private :: __address @@ -114,7 +113,7 @@ type(__builtin_c_ptr), private :: info end type - integer, parameter, public :: __builtin_atomic_int_kind = selected_int_kind(18) + integer, parameter, public :: __builtin_atomic_int_kind = int64 integer, parameter, public :: & __builtin_atomic_logical_kind = __builtin_atomic_int_kind diff --git a/flang-rt/lib/runtime/__fortran_type_info.f90 b/flang-rt/lib/runtime/__fortran_type_info.f90 index d8cf385b8665ed..abec4f7085513a 100644 --- a/flang-rt/lib/runtime/__fortran_type_info.f90 +++ b/flang-rt/lib/runtime/__fortran_type_info.f90 @@ -21,7 +21,7 @@ ! to be exported by this MODULE. private - integer, parameter :: int64 = selected_int_kind(18) + integer, parameter :: int64 = 8 type :: DerivedType ! "TBP" bindings appear first. Inherited bindings, with overrides already diff --git a/flang-rt/lib/runtime/ieee_arithmetic.f90 b/flang-rt/lib/runtime/ieee_arithmetic.f90 index 0277e3b74d4ab3..36d00435f953dd 100644 --- a/flang-rt/lib/runtime/ieee_arithmetic.f90 +++ b/flang-rt/lib/runtime/ieee_arithmetic.f90 @@ -171,7 +171,10 @@ end function ieee_round_ne #define SPECIFICS_L(G) \ G(1) G(2) G(4) G(8) -#if FLANG_SUPPORT_R16 +! Note that a REAL kind can be disabled using -fdisable-real-N even though the +! platform supports it. The intrinsic module must mode the definitions +! available if enabled but also error it disabled. Flang has an exception for +! this file that skips the definitions using disabled kinds. #if __x86_64__ #define SPECIFICS_R(G) \ G(2) G(3) G(4) G(8) G(10) G(16) @@ -179,15 +182,6 @@ end function ieee_round_ne #define SPECIFICS_R(G) \ G(2) G(3) G(4) G(8) G(16) #endif -#else -#if __x86_64__ -#define SPECIFICS_R(G) \ - G(2) G(3) G(4) G(8) G(10) -#else -#define SPECIFICS_R(G) \ - G(2) G(3) G(4) G(8) -#endif -#endif #define SPECIFICS_II(G) \ G(1,1) G(1,2) G(1,4) G(1,8) G(1,16) \ @@ -196,7 +190,6 @@ end function ieee_round_ne G(8,1) G(8,2) G(8,4) G(8,8) G(8,16) \ G(16,1) G(16,2) G(16,4) G(16,8) G(16,16) -#if FLANG_SUPPORT_R16 #if __x86_64__ #define SPECIFICS_RI(G) \ G(2,1) G(2,2) G(2,4) G(2,8) G(2,16) \ @@ -213,24 +206,7 @@ end function ieee_round_ne G(8,1) G(8,2) G(8,4) G(8,8) G(8,16) \ G(16,1) G(16,2) G(16,4) G(16,8) G(16,16) #endif -#else -#if __x86_64__ -#define SPECIFICS_RI(G) \ - G(2,1) G(2,2) G(2,4) G(2,8) \ - G(3,1) G(3,2) G(3,4) G(3,8) \ - G(4,1) G(4,2) G(4,4) G(4,8) \ - G(8,1) G(8,2) G(8,4) G(8,8) \ - G(10,1) G(10,2) G(10,4) G(10,8) -#else -#define SPECIFICS_RI(G) \ - G(2,1) G(2,2) G(2,4) G(2,8) \ - G(3,1) G(3,2) G(3,4) G(3,8) \ - G(4,1) G(4,2) G(4,4) G(4,8) \ - G(8,1) G(8,2) G(8,4) G(8,8) -#endif -#endif -#if FLANG_SUPPORT_R16 #if __x86_64__ #define SPECIFICS_RR(G) \ G(2,2) G(2,3) G(2,4) G(2,8) G(2,10) G(2,16) \ @@ -260,33 +236,6 @@ end function ieee_round_ne G(8,8,2) G(8,8,3) G(8,8,4) G(8,8,8) G(16,8,16) \ G(16,16,2) G(16,16,3) G(16,16,4) G(16,16,8) G(16,16,16) #endif -#else -#if __x86_64__ -#define SPECIFICS_RR(G) \ - G(2,2) G(2,3) G(2,4) G(2,8) G(2,10) \ - G(3,2) G(3,3) G(3,4) G(3,8) G(3,10) \ - G(4,2) G(4,3) G(4,4) G(4,8) G(4,10) \ - G(8,2) G(8,3) G(8,4) G(8,8) G(8,10) \ - G(10,2) G(10,3) G(10,4) G(10,8) G(10,10) -#define SPECIFICS_rRR(G) \ - G(2,2,2) G(2,2,3) G(4,2,4) G(8,2,8) G(10,2,10) \ - G(2,3,2) G(3,3,3) G(4,3,4) G(8,3,8) G(10,3,10) \ - G(4,4,2) G(4,4,3) G(4,4,4) G(8,4,8) G(10,4,10) \ - G(8,8,2) G(8,8,3) G(8,8,4) G(8,8,8) G(10,8,10) \ - G(10,10,2) G(10,10,3) G(10,10,4) G(10,10,8) G(10,10,10) -#else -#define SPECIFICS_RR(G) \ - G(2,2) G(2,3) G(2,4) G(2,8) \ - G(3,2) G(3,3) G(3,4) G(3,8) \ - G(4,2) G(4,3) G(4,4) G(4,8) \ - G(8,2) G(8,3) G(8,4) G(8,8) -#define SPECIFICS_rRR(G) \ - G(2,2,2) G(2,2,3) G(4,2,4) G(8,2,8) \ - G(2,3,2) G(3,3,3) G(4,3,4) G(8,3,8) \ - G(4,4,2) G(4,4,3) G(4,4,4) G(8,4,8) \ - G(8,8,2) G(8,8,3) G(8,8,4) G(8,8,8) -#endif -#endif #define IEEE_CLASS_R(XKIND) \ simple elemental type(ieee_class_type) function ieee_class_a##XKIND(x); \ diff --git a/flang-rt/lib/runtime/iso_c_binding.f90 b/flang-rt/lib/runtime/iso_c_binding.f90 index 619b1f2e5c6dd5..7664abd9d8b656 100644 --- a/flang-rt/lib/runtime/iso_c_binding.f90 +++ b/flang-rt/lib/runtime/iso_c_binding.f90 @@ -34,6 +34,9 @@ module iso_c_binding c_loc, c_null_funptr, c_null_ptr, c_ptr, c_sizeof, f_c_string, & operator(==), operator(/=) + intrinsic :: __builtin_selected_int_kind, __builtin_selected_unsigned_kind, & + __builtin_selected_real_kind + ! Table 18.2 (in clause 18.3.1) ! TODO: Specialize (via macros?) for alternative targets integer, parameter, public :: & @@ -41,7 +44,7 @@ module iso_c_binding c_int16_t = 2, & c_int32_t = 4, & c_int64_t = 8, & - c_int128_t = 16 ! anticipating future addition + c_int128_t = merge(16, -1, __builtin_selected_int_kind(38) == 16) integer, parameter, public :: & c_int = c_int32_t, & c_short = c_int16_t, & @@ -77,15 +80,18 @@ module iso_c_binding c_int_least128_t = c_int128_t, & c_int_fast128_t = c_int128_t + logical, parameter :: & + hasReal10 = __builtin_selected_real_kind(18, 4931) == 10, & + hasReal16 = __builtin_selected_real_kind(33, 4931) == 16 integer, parameter, public :: & c_float = 4, & c_double = 8, & #if defined(__x86_64__) - c_long_double = 10 + c_long_double = merge(10, merge(-1, -3, hasReal16), hasReal10) #elif defined(_WIN32) && defined(__aarch64__) c_long_double = 8 #else - c_long_double = 16 + c_long_double = merge(16, merge(-1, -3, hasReal10), hasReal16) #endif integer, parameter, public :: & @@ -116,14 +122,14 @@ module iso_c_binding c_float16 = 2, & c_float16_complex = c_float16 integer, parameter, public :: & - c_float128 = 16, & + c_float128 = merge(16, merge(-1, -3, hasReal10), hasReal16), & c_float128_complex = c_float128 integer, parameter, public :: & c_uint8_t = 1, & c_uint16_t = 2, & c_uint32_t = 4, & c_uint64_t = 8, & - c_uint128_t = 16 + c_uint128_t = merge(16, -1, __builtin_selected_unsigned_kind(38) == 16) integer, parameter, public :: & c_unsigned_char = c_uint8_t, & c_unsigned_short = c_uint16_t, & @@ -134,11 +140,7 @@ module iso_c_binding c_unsigned_long = c_uint64_t, & #endif c_unsigned_long_long = c_uint64_t, & -#if defined(__powerpc__) || defined(_WIN32) c_uintmax_t = c_uint64_t -#else - c_uintmax_t = c_uint128_t -#endif integer, parameter, public :: & c_uint_fast8_t = c_uint8_t, & c_uint_fast16_t = c_uint16_t, & diff --git a/flang-rt/lib/runtime/iso_fortran_env_impl.cpp b/flang-rt/lib/runtime/iso_fortran_env_impl.cpp deleted file mode 100644 index 70230d0f16c7a7..00000000000000 --- a/flang-rt/lib/runtime/iso_fortran_env_impl.cpp +++ /dev/null @@ -1,253 +0,0 @@ -//===-- lib/runtime/iso_fortran_env_impl.cpp --------------------*- 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 -// -//===----------------------------------------------------------------------===// -// -// Provides linkable symbols for the iso_fortran_env_impl module constants. It -// replaces the Fortran source (flang/module/iso_fortran_env_impl.f90) to remove -// the runtime library's build dependency on the Fortran compiler. -// -// The values here must stay in sync with the Fortran module source which -// generates the .mod file during the flang compiler build. -// -// Symbol naming follows Flang's module variable mangling: -// _QM<module_name>EC<constant_name> -// -//===----------------------------------------------------------------------===// - -#include "flang/Common/float128.h" -#include "flang/Common/float80.h" -#include "flang/Common/type-kinds.h" -#include <cstdint> - -// Fortran merge(tsource, fsource, mask) to match the source module style. -static constexpr std::int32_t Merge( - std::int32_t tsource, std::int32_t fsource, bool mask) { - return mask ? tsource : fsource; -} - -// Fortran digits() for integer kind K returns 8*K - 1 (excludes sign bit). -static constexpr int IntDigits(int kind) { return 8 * kind - 1; } - -// Fortran digits() for unsigned kind K returns 8*K (no sign bit). -static constexpr int UintDigits(int kind) { return 8 * kind; } - -// Fortran digits() for real kind K returns the binary precision. -static constexpr int RealDigits(int kind) { - return Fortran::common::PrecisionOfRealKind(kind); -} - -// The smallest valid real kind, used as a safe fallback when a selected -// real kind is unavailable (so digits() can be called without error). -static constexpr std::int32_t safeRealFallback{2}; - -// Integer kinds, selected -> safe -> validated. -static constexpr std::int32_t selectedInt8{1}; -static constexpr std::int32_t selectedInt16{2}; -static constexpr std::int32_t selectedInt32{4}; -static constexpr std::int32_t selectedInt64{8}; -static constexpr std::int32_t selectedInt128{16}; - -static constexpr std::int32_t safeInt8{ - Merge(selectedInt8, 1, selectedInt8 >= 0)}; -static constexpr std::int32_t safeInt16{ - Merge(selectedInt16, 1, selectedInt16 >= 0)}; -static constexpr std::int32_t safeInt32{ - Merge(selectedInt32, 1, selectedInt32 >= 0)}; -static constexpr std::int32_t safeInt64{ - Merge(selectedInt64, 1, selectedInt64 >= 0)}; -static constexpr std::int32_t safeInt128{ - Merge(selectedInt128, 1, selectedInt128 >= 0)}; - -static constexpr std::int32_t int8{Merge( - selectedInt8, Merge(-2, -1, selectedInt8 >= 0), IntDigits(safeInt8) == 7)}; -static constexpr std::int32_t int16{Merge(selectedInt16, - Merge(-2, -1, selectedInt16 >= 0), IntDigits(safeInt16) == 15)}; -static constexpr std::int32_t int32{Merge(selectedInt32, - Merge(-2, -1, selectedInt32 >= 0), IntDigits(safeInt32) == 31)}; -static constexpr std::int32_t int64{Merge(selectedInt64, - Merge(-2, -1, selectedInt64 >= 0), IntDigits(safeInt64) == 63)}; -static constexpr std::int32_t int128{Merge(selectedInt128, - Merge(-2, -1, selectedInt128 >= 0), IntDigits(safeInt128) == 127)}; - -// Unsigned kinds, same selection as integer, validated with unsigned digits. -static constexpr std::int32_t selectedUInt8{selectedInt8}; -static constexpr std::int32_t selectedUInt16{selectedInt16}; -static constexpr std::int32_t selectedUInt32{selectedInt32}; -static constexpr std::int32_t selectedUInt64{selectedInt64}; -static constexpr std::int32_t selectedUInt128{selectedInt128}; - -static constexpr std::int32_t safeUInt8{safeInt8}; -static constexpr std::int32_t safeUInt16{safeInt16}; -static constexpr std::int32_t safeUInt32{safeInt32}; -static constexpr std::int32_t safeUInt64{safeInt64}; -static constexpr std::int32_t safeUInt128{safeInt128}; - -static constexpr std::int32_t uint8{Merge(selectedUInt8, - Merge(-2, -1, selectedUInt8 >= 0), UintDigits(safeUInt8) == 8)}; -static constexpr std::int32_t uint16{Merge(selectedUInt16, - Merge(-2, -1, selectedUInt16 >= 0), UintDigits(safeUInt16) == 16)}; -static constexpr std::int32_t uint32{Merge(selectedUInt32, - Merge(-2, -1, selectedUInt32 >= 0), UintDigits(safeUInt32) == 32)}; -static constexpr std::int32_t uint64{Merge(selectedUInt64, - Merge(-2, -1, selectedUInt64 >= 0), UintDigits(safeUInt64) == 64)}; -static constexpr std::int32_t uint128{Merge(selectedUInt128, - Merge(-2, -1, selectedUInt128 >= 0), UintDigits(safeUInt128) == 128)}; - -// Logical kinds mirror integer kinds. -static constexpr std::int32_t logical8{int8}; -static constexpr std::int32_t logical16{int16}; -static constexpr std::int32_t logical32{int32}; -static constexpr std::int32_t logical64{int64}; - -// Real kinds, selected -> safe -> validated. -static constexpr std::int32_t selectedReal16{2}; -static constexpr std::int32_t selectedBfloat16{3}; -static constexpr std::int32_t selectedReal32{4}; -static constexpr std::int32_t selectedReal64{8}; - -#if FLANG_RT_SUPPORTS_REAL16 -static constexpr std::int32_t selectedReal80{16}; -#elif HAS_FLOAT80 -static constexpr std::int32_t selectedReal80{10}; -#else -static constexpr std::int32_t selectedReal80{-3}; -#endif - -#if FLANG_RT_SUPPORTS_REAL16 -static constexpr std::int32_t selectedReal64x2{16}; -static constexpr std::int32_t selectedReal128{16}; -#elif HAS_FLOAT80 -static constexpr std::int32_t selectedReal64x2{-1}; -static constexpr std::int32_t selectedReal128{-1}; -#else -static constexpr std::int32_t selectedReal64x2{-1}; -static constexpr std::int32_t selectedReal128{-3}; -#endif - -static constexpr std::int32_t safeReal16{ - Merge(selectedReal16, safeRealFallback, selectedReal16 >= 0)}; -static constexpr std::int32_t safeBfloat16{ - Merge(selectedBfloat16, safeRealFallback, selectedBfloat16 >= 0)}; -static constexpr std::int32_t safeReal32{ - Merge(selectedReal32, safeRealFallback, selectedReal32 >= 0)}; -static constexpr std::int32_t safeReal64{ - Merge(selectedReal64, safeRealFallback, selectedReal64 >= 0)}; -static constexpr std::int32_t safeReal80{ - Merge(selectedReal80, safeRealFallback, selectedReal80 >= 0)}; -static constexpr std::int32_t safeReal64x2{ - Merge(selectedReal64x2, safeRealFallback, selectedReal64x2 >= 0)}; -static constexpr std::int32_t safeReal128{ - Merge(selectedReal128, safeRealFallback, selectedReal128 >= 0)}; - -static constexpr std::int32_t real16{Merge(selectedReal16, - Merge(-2, -1, selectedReal16 >= 0), RealDigits(safeReal16) == 11)}; -static constexpr std::int32_t bfloat16{Merge(selectedBfloat16, - Merge(-2, -1, selectedBfloat16 >= 0), RealDigits(safeBfloat16) == 8)}; -static constexpr std::int32_t real32{Merge(selectedReal32, - Merge(-2, -1, selectedReal32 >= 0), RealDigits(safeReal32) == 24)}; -static constexpr std::int32_t real64{Merge(selectedReal64, - Merge(-2, -1, selectedReal64 >= 0), RealDigits(safeReal64) == 53)}; -static constexpr std::int32_t real80{Merge(selectedReal80, - Merge(-2, -1, selectedReal80 >= 0), RealDigits(safeReal80) == 64)}; -static constexpr std::int32_t real64x2{Merge(selectedReal64x2, - Merge(-2, -1, selectedReal64x2 >= 0), RealDigits(safeReal64x2) == 106)}; -static constexpr std::int32_t real128{Merge(selectedReal128, - Merge(-2, -1, selectedReal128 >= 0), RealDigits(safeReal128) == 113)}; - -// Exported symbols with Flang module-variable mangling. -#define FORTRAN_NAMED_CONST(name) _QMiso_fortran_env_implEC##name - -extern "C" { - -extern const std::int32_t FORTRAN_NAMED_CONST(selectedint8){selectedInt8}; -extern const std::int32_t FORTRAN_NAMED_CONST(selectedint16){selectedInt16}; -extern const std::int32_t FORTRAN_NAMED_CONST(selectedint32){selectedInt32}; -extern const std::int32_t FORTRAN_NAMED_CONST(selectedint64){selectedInt64}; -extern const std::int32_t FORTRAN_NAMED_CONST(selectedint128){selectedInt128}; - -extern const std::int32_t FORTRAN_NAMED_CONST(safeint8){safeInt8}; -extern const std::int32_t FORTRAN_NAMED_CONST(safeint16){safeInt16}; -extern const std::int32_t FORTRAN_NAMED_CONST(safeint32){safeInt32}; -extern const std::int32_t FORTRAN_NAMED_CONST(safeint64){safeInt64}; -extern const std::int32_t FORTRAN_NAMED_CONST(safeint128){safeInt128}; - -extern const std::int32_t FORTRAN_NAMED_CONST(int8){int8}; -extern const std::int32_t FORTRAN_NAMED_CONST(int16){int16}; -extern const std::int32_t FORTRAN_NAMED_CONST(int32){int32}; -extern const std::int32_t FORTRAN_NAMED_CONST(int64){int64}; -extern const std::int32_t FORTRAN_NAMED_CONST(int128){int128}; - -extern const std::int32_t FORTRAN_NAMED_CONST(selecteduint8){selectedUInt8}; -extern const std::int32_t FORTRAN_NAMED_CONST(selecteduint16){selectedUInt16}; -extern const std::int32_t FORTRAN_NAMED_CONST(selecteduint32){selectedUInt32}; -extern const std::int32_t FORTRAN_NAMED_CONST(selecteduint64){selectedUInt64}; -extern const std::int32_t FORTRAN_NAMED_CONST(selecteduint128){selectedUInt128}; - -extern const std::int32_t FORTRAN_NAMED_CONST(safeuint8){safeUInt8}; -extern const std::int32_t FORTRAN_NAMED_CONST(safeuint16){safeUInt16}; -extern const std::int32_t FORTRAN_NAMED_CONST(safeuint32){safeUInt32}; -extern const std::int32_t FORTRAN_NAMED_CONST(safeuint64){safeUInt64}; -extern const std::int32_t FORTRAN_NAMED_CONST(safeuint128){safeUInt128}; - -extern const std::int32_t FORTRAN_NAMED_CONST(uint8){uint8}; -extern const std::int32_t FORTRAN_NAMED_CONST(uint16){uint16}; -extern const std::int32_t FORTRAN_NAMED_CONST(uint32){uint32}; -extern const std::int32_t FORTRAN_NAMED_CONST(uint64){uint64}; -extern const std::int32_t FORTRAN_NAMED_CONST(uint128){uint128}; - -extern const std::int32_t FORTRAN_NAMED_CONST(logical8){logical8}; -extern const std::int32_t FORTRAN_NAMED_CONST(logical16){logical16}; -extern const std::int32_t FORTRAN_NAMED_CONST(logical32){logical32}; -extern const std::int32_t FORTRAN_NAMED_CONST(logical64){logical64}; - -extern const std::int32_t FORTRAN_NAMED_CONST(selectedreal16){selectedReal16}; -extern const std::int32_t FORTRAN_NAMED_CONST(selectedbfloat16){ - selectedBfloat16}; -extern const std::int32_t FORTRAN_NAMED_CONST(selectedreal32){selectedReal32}; -extern const std::int32_t FORTRAN_NAMED_CONST(selectedreal64){selectedReal64}; -extern const std::int32_t FORTRAN_NAMED_CONST(selectedreal80){selectedReal80}; -extern const std::int32_t FORTRAN_NAMED_CONST(selectedreal64x2){ - selectedReal64x2}; -extern const std::int32_t FORTRAN_NAMED_CONST(selectedreal128){selectedReal128}; - -extern const std::int32_t FORTRAN_NAMED_CONST(safereal16){safeReal16}; -extern const std::int32_t FORTRAN_NAMED_CONST(safebfloat16){safeBfloat16}; -extern const std::int32_t FORTRAN_NAMED_CONST(safereal32){safeReal32}; -extern const std::int32_t FORTRAN_NAMED_CONST(safereal64){safeReal64}; -extern const std::int32_t FORTRAN_NAMED_CONST(safereal80){safeReal80}; -extern const std::int32_t FORTRAN_NAMED_CONST(safereal64x2){safeReal64x2}; -extern const std::int32_t FORTRAN_NAMED_CONST(safereal128){safeReal128}; - -extern const std::int32_t FORTRAN_NAMED_CONST(real16){real16}; -extern const std::int32_t FORTRAN_NAMED_CONST(bfloat16){bfloat16}; -extern const std::int32_t FORTRAN_NAMED_CONST(real32){real32}; -extern const std::int32_t FORTRAN_NAMED_CONST(real64){real64}; -extern const std::int32_t FORTRAN_NAMED_CONST(real80){real80}; -extern const std::int32_t FORTRAN_NAMED_CONST(real64x2){real64x2}; -extern const std::int32_t FORTRAN_NAMED_CONST(real128){real128}; - -extern const std::int32_t FORTRAN_NAMED_CONST( - __builtin_integer_kinds)[] = FORTRAN_INTEGER_KINDS; - -extern const std::int32_t FORTRAN_NAMED_CONST( - __builtin_logical_kinds)[] = FORTRAN_LOGICAL_KINDS; - -// Target-filtered subset of FORTRAN_REAL_KINDS from type-kinds.h. -extern const std::int32_t FORTRAN_NAMED_CONST(__builtin_real_kinds)[]{ - 2, - 3, - 4, - 8, -#if HAS_FLOAT80 - 10, -#endif -#if FLANG_RT_SUPPORTS_REAL16 - 16, -#endif -}; - -} // extern "C" diff --git a/flang-rt/lib/runtime/iso_fortran_env_impl.f90 b/flang-rt/lib/runtime/iso_fortran_env_impl.f90 index d0726e621bbd12..5da18bafb341bd 100644 --- a/flang-rt/lib/runtime/iso_fortran_env_impl.f90 +++ b/flang-rt/lib/runtime/iso_fortran_env_impl.f90 @@ -6,82 +6,91 @@ ! !===------------------------------------------------------------------------===! -! This MODULE implements part of the ISO_FORTRAN_ENV module file, which -! partially requires linkable symbols for some entities defined -! (e.g., real_kinds). +! This MODULE implements part of the ISO_FORTRAN_ENV module file. ! -! The runtime symbols are in flang-rt/lib/runtime/iso_fortran_env_impl.cpp and -! must be kept up-to-date. +! The available kinds depend on compiler options (e.g. -fdisable-integer-16, +! -fdisable-real-16) of the compilation that uses this module, so the +! __builtin_selected_*_kind intrinsics are only folded when the module file is +! read. For the same reason, the kinds cannot be validated with +! digits(int(0,kind=...)) etc.; instead, the selected kind is compared with the +! Flang kind that has the expected number of digits. +! +! There is no object file providing linkable symbols for these constants; +! Flang defines them in the using program from the values folded when reading +! the module file. module iso_fortran_env_impl implicit none private ! INTEGER types + intrinsic :: __builtin_selected_int_kind integer, parameter, public :: & - selectedInt8 = selected_int_kind(2), & - selectedInt16 = selected_int_kind(4), & - selectedInt32 = selected_int_kind(9), & - selectedInt64 = selected_int_kind(18),& - selectedInt128 = selected_int_kind(38), & - safeInt8 = merge(selectedInt8, selected_int_kind(0), & + selectedInt8 = __builtin_selected_int_kind(2), & + selectedInt16 = __builtin_selected_int_kind(4), & + selectedInt32 = __builtin_selected_int_kind(9), & + selectedInt64 = __builtin_selected_int_kind(18),& + selectedInt128 = __builtin_selected_int_kind(38), & + safeInt8 = merge(selectedInt8, __builtin_selected_int_kind(0), & selectedInt8 >= 0), & - safeInt16 = merge(selectedInt16, selected_int_kind(0), & + safeInt16 = merge(selectedInt16, __builtin_selected_int_kind(0), & selectedInt16 >= 0), & - safeInt32 = merge(selectedInt32, selected_int_kind(0), & + safeInt32 = merge(selectedInt32, __builtin_selected_int_kind(0), & selectedInt32 >= 0), & - safeInt64 = merge(selectedInt64, selected_int_kind(0), & + safeInt64 = merge(selectedInt64, __builtin_selected_int_kind(0), & selectedInt64 >= 0), & - safeInt128 = merge(selectedInt128, selected_int_kind(0), & + safeInt128 = merge(selectedInt128, __builtin_selected_int_kind(0), & selectedInt128 >= 0) integer, parameter, public :: & int8 = merge(selectedInt8, merge(-2, -1, selectedInt8 >= 0), & - digits(int(0,kind=safeInt8)) == 7), & + selectedInt8 == 1), & int16 = merge(selectedInt16, merge(-2, -1, selectedInt16 >= 0), & - digits(int(0,kind=safeInt16)) == 15), & + selectedInt16 == 2), & int32 = merge(selectedInt32, merge(-2, -1, selectedInt32 >= 0), & - digits(int(0,kind=safeInt32)) == 31), & + selectedInt32 == 4), & int64 = merge(selectedInt64, merge(-2, -1, selectedInt64 >= 0), & - digits(int(0,kind=safeInt64)) == 63), & + selectedInt64 == 8), & int128 = merge(selectedInt128, merge(-2, -1, selectedInt128 >= 0), & - digits(int(0,kind=safeInt128)) == 127) + selectedInt128 == 16) ! UNSIGNED types + intrinsic :: __builtin_selected_unsigned_kind integer, parameter, public :: & - selectedUInt8 = selected_unsigned_kind(2), & - selectedUInt16 = selected_unsigned_kind(4), & - selectedUInt32 = selected_unsigned_kind(9), & - selectedUInt64 = selected_unsigned_kind(18),& - selectedUInt128 = selected_unsigned_kind(38), & - safeUInt8 = merge(selectedUInt8, selected_unsigned_kind(0), & + selectedUInt8 = __builtin_selected_unsigned_kind(2), & + selectedUInt16 = __builtin_selected_unsigned_kind(4), & + selectedUInt32 = __builtin_selected_unsigned_kind(9), & + selectedUInt64 = __builtin_selected_unsigned_kind(18),& + selectedUInt128 = __builtin_selected_unsigned_kind(38), & + safeUInt8 = merge(selectedUInt8, __builtin_selected_unsigned_kind(0), & selectedUInt8 >= 0), & - safeUInt16 = merge(selectedUInt16, selected_unsigned_kind(0), & + safeUInt16 = merge(selectedUInt16, __builtin_selected_unsigned_kind(0), & selectedUInt16 >= 0), & - safeUInt32 = merge(selectedUInt32, selected_unsigned_kind(0), & + safeUInt32 = merge(selectedUInt32, __builtin_selected_unsigned_kind(0), & selectedUInt32 >= 0), & - safeUInt64 = merge(selectedUInt64, selected_unsigned_kind(0), & + safeUInt64 = merge(selectedUInt64, __builtin_selected_unsigned_kind(0), & selectedUInt64 >= 0), & - safeUInt128 = merge(selectedUInt128, selected_unsigned_kind(0), & + safeUInt128 = merge(selectedUInt128, __builtin_selected_unsigned_kind(0), & selectedUInt128 >= 0) integer, parameter, public :: & uint8 = merge(selectedUInt8, merge(-2, -1, selectedUInt8 >= 0), & - digits(uint(0,kind=safeUInt8)) == 8), & + selectedUInt8 == 1), & uint16 = merge(selectedUInt16, merge(-2, -1, selectedUInt16 >= 0), & - digits(uint(0,kind=safeUInt16)) == 16), & + selectedUInt16 == 2), & uint32 = merge(selectedUInt32, merge(-2, -1, selectedUInt32 >= 0), & - digits(uint(0,kind=safeUInt32)) == 32), & + selectedUInt32 == 4), & uint64 = merge(selectedUInt64, merge(-2, -1, selectedUInt64 >= 0), & - digits(uint(0,kind=safeUInt64)) == 64), & + selectedUInt64 == 8), & uint128 = merge(selectedUInt128, merge(-2, -1, selectedUInt128 >= 0), & - digits(uint(0,kind=safeUInt128)) == 128) + selectedUInt128 == 16) integer, parameter, dimension(*), public :: __builtin_integer_kinds = [ & - selected_int_kind(0), & - [(pack([selected_int_kind(k)], & - selected_int_kind(k) >= 0 .and. & - selected_int_kind(k) /= selected_int_kind(k-1)), & + __builtin_selected_int_kind(0), & + [(pack([__builtin_selected_int_kind(k)], & + __builtin_selected_int_kind(k) >= 0 .and. & + __builtin_selected_int_kind(k) /= & + __builtin_selected_int_kind(k-1)), & integer :: k=1, 39)]] ! LOGICAL TYPES @@ -96,44 +105,44 @@ module iso_fortran_env_impl ] ! REAL types + intrinsic :: __builtin_selected_real_kind integer, parameter, public :: & - selectedReal16 = selected_real_kind(3, 4), & ! IEEE half - selectedBfloat16 = selected_real_kind(2, 37), & ! truncated IEEE single - selectedReal32 = selected_real_kind(6, 37), & ! IEEE single - selectedReal64 = selected_real_kind(15, 307), & ! IEEE double - selectedReal80 = selected_real_kind(18, 4931), & ! 80x87 extended - selectedReal64x2 = selected_real_kind(31, 307), & ! "double-double" - selectedReal128 = selected_real_kind(33, 4931), & ! IEEE quad - safeReal16 = merge(selectedReal16, selected_real_kind(0,0), & + selectedReal16 = __builtin_selected_real_kind(3, 4), & ! IEEE half + selectedBfloat16 = __builtin_selected_real_kind(2, 37), & ! truncated IEEE single + selectedReal32 = __builtin_selected_real_kind(6, 37), & ! IEEE single + selectedReal64 = __builtin_selected_real_kind(15, 307), & ! IEEE double + selectedReal80 = __builtin_selected_real_kind(18, 4931), & ! 80x87 extended + selectedReal64x2 = __builtin_selected_real_kind(31, 307), & ! "double-double" + selectedReal128 = __builtin_selected_real_kind(33, 4931), & ! IEEE quad + safeReal16 = merge(selectedReal16, __builtin_selected_real_kind(0,0), & selectedReal16 >= 0), & - safeBfloat16 = merge(selectedBfloat16, selected_real_kind(0,0), & + safeBfloat16 = merge(selectedBfloat16, __builtin_selected_real_kind(0,0), & selectedBfloat16 >= 0), & - safeReal32 = merge(selectedReal32, selected_real_kind(0,0), & + safeReal32 = merge(selectedReal32, __builtin_selected_real_kind(0,0), & selectedReal32 >= 0), & - safeReal64 = merge(selectedReal64, selected_real_kind(0,0), & + safeReal64 = merge(selectedReal64, __builtin_selected_real_kind(0,0), & selectedReal64 >= 0), & - safeReal80 = merge(selectedReal80, selected_real_kind(0,0), & + safeReal80 = merge(selectedReal80, __builtin_selected_real_kind(0,0), & selectedReal80 >= 0), & - safeReal64x2 = merge(selectedReal64x2, selected_real_kind(0,0), & + safeReal64x2 = merge(selectedReal64x2, __builtin_selected_real_kind(0,0), & selectedReal64x2 >= 0), & - safeReal128 = merge(selectedReal128, selected_real_kind(0,0), & + safeReal128 = merge(selectedReal128, __builtin_selected_real_kind(0,0), & selectedReal128 >= 0) integer, parameter, public :: & real16 = merge(selectedReal16, merge(-2, -1, selectedReal16 >= 0), & - digits(real(0,kind=safeReal16)) == 11), & + selectedReal16 == 2), & bfloat16 = merge(selectedBfloat16, merge(-2, -1, selectedBfloat16 >= 0), & - digits(real(0,kind=safeBfloat16)) == 8), & + selectedBfloat16 == 3), & real32 = merge(selectedReal32, merge(-2, -1, selectedReal32 >= 0), & - digits(real(0,kind=safeReal32)) == 24), & + selectedReal32 == 4), & real64 = merge(selectedReal64, merge(-2, -1, selectedReal64 >= 0), & - digits(real(0,kind=safeReal64)) == 53), & + selectedReal64 == 8), & real80 = merge(selectedReal80, merge(-2, -1, selectedReal80 >= 0), & - digits(real(0,kind=safeReal80)) == 64), & - real64x2 = merge(selectedReal64x2, merge(-2, -1, selectedReal64x2 >= 0), & - digits(real(0,kind=safeReal64x2)) == 106), & + selectedReal80 == 10), & + real64x2 = merge(-2, -1, selectedReal64x2 >= 0), & ! no such Flang kind real128 = merge(selectedReal128, merge(-2, -1, selectedReal128 >= 0), & - digits(real(0,kind=safeReal128)) == 113) + selectedReal128 == 16) integer, parameter, dimension(*), public :: __builtin_real_kinds = [ & pack([real16], real16 >= 0), & diff --git a/flang-rt/test/Driver/compare_iso_fortran_env_symbols.f90 b/flang-rt/test/Driver/compare_iso_fortran_env_symbols.f90 deleted file mode 100644 index 8e17fe4e889140..00000000000000 --- a/flang-rt/test/Driver/compare_iso_fortran_env_symbols.f90 +++ /dev/null @@ -1,14 +0,0 @@ -! UNSUPPORTED: offload-cuda, system-windows -! REQUIRES: fortran-modules - -! RUN: %flang -c -funsigned %S/../../lib/runtime/iso_fortran_env_impl.f90 -o %t.f90.o - -! Extract defined symbol names and sizes from the Fortran object and the -! already-built runtime library (which was compiled with the correct CMake flags). -! RUN: llvm-nm --defined-only --format=posix --print-size %t.f90.o \ -! RUN: | grep '_QMiso_fortran_env_impl' | cut -d' ' -f1,4 | sort -u > %t.f90.syms -! RUN: llvm-nm --defined-only --format=posix --print-size "%libdir"/libflang_rt.runtime.* \ -! RUN: | grep '_QMiso_fortran_env_impl' | cut -d' ' -f1,4 | sort -u > %t.cpp.syms - -! The two symbol lists must be identical. -! RUN: diff %t.f90.syms %t.cpp.syms diff --git a/flang-rt/test/Driver/iso_fortran_env_impl.f90 b/flang-rt/test/Driver/iso_fortran_env_impl.f90 deleted file mode 100644 index d856a9595ec41b..00000000000000 --- a/flang-rt/test/Driver/iso_fortran_env_impl.f90 +++ /dev/null @@ -1,25 +0,0 @@ -! Verify that iso_fortran_env constants from the C++ runtime -! (iso_fortran_env_impl.cpp) match the Fortran module definitions. - -! UNSUPPORTED: offload-cuda -! REQUIRES: fortran-modules - -! RUN: %flang %isysroot -L"%libdir" %s -o %t -! RUN: env LD_LIBRARY_PATH="$LD_LIBRARY_PATH:%libdir" %t | FileCheck %s - -program check_iso_env - use iso_fortran_env, only: int8, int16, int32, int64, real32, real64 - - ! CHECK: PASS int8 1 - print '("PASS int8 ",I0)', int8 - ! CHECK: PASS int16 2 - print '("PASS int16 ",I0)', int16 - ! CHECK: PASS int32 4 - print '("PASS int32 ",I0)', int32 - ! CHECK: PASS int64 8 - print '("PASS int64 ",I0)', int64 - ! CHECK: PASS real32 4 - print '("PASS real32 ",I0)', real32 - ! CHECK: PASS real64 8 - print '("PASS real64 ",I0)', real64 -end program diff --git a/flang/include/flang/Evaluate/check-expression.h b/flang/include/flang/Evaluate/check-expression.h index 6d113f7a71d645..7e858d2e736f8c 100644 --- a/flang/include/flang/Evaluate/check-expression.h +++ b/flang/include/flang/Evaluate/check-expression.h @@ -80,6 +80,16 @@ void CheckRealWidening( void CheckRealWidening(const Expr<SomeType> &, const std::optional<DynamicType> &, FoldingContext &); +/// Predicate: true when an expression references a deferred folding intrinsic, +/// directly or via the initializers of named constants. +bool HasDeferredFolding(const Expr<SomeType> &); +/// Predicate: true when the symbol is a named constant whose initializer +/// HasDeferredFolding(). +bool HasDeferredFolding(const Symbol &); +/// Predicate: true when the symbol is owned by one of the intrinsic modules +/// whose named constants may have initializers with deferred folding. +bool IsDeferredFoldingAllowed(const Symbol &); + // Validate the value of a named constant, the static initial // value of a non-pointer non-allocatable non-dummy variable, or the // default initializer of a component of a derived type (or instantiation diff --git a/flang/lib/Evaluate/check-expression.cpp b/flang/lib/Evaluate/check-expression.cpp index a1a1e4600a8722..25e2b42701714b 100644 --- a/flang/lib/Evaluate/check-expression.cpp +++ b/flang/lib/Evaluate/check-expression.cpp @@ -646,12 +646,9 @@ std::optional<Expr<SomeType>> NonPointerInitializationExpr(const Symbol &symbol, return {std::move(folded)}; } } else if (IsNamedConstant(symbol)) { - if (symbol.name() == "numeric_storage_size" && - symbol.owner().IsModule() && - DEREF(symbol.owner().symbol()).name() == "iso_fortran_env") { - // Very special case: numeric_storage_size is not folded until - // it read from the iso_fortran_env module file, as its value - // depends on compilation options. + if (IsDeferredFoldingAllowed(symbol) && HasDeferredFolding(folded)) { + // Very special case: Not folded until read from an intrinsic module + // file, as its value depends on compilation options. return {std::move(folded)}; } context.messages().Say( @@ -675,6 +672,58 @@ std::optional<Expr<SomeType>> NonPointerInitializationExpr(const Symbol &symbol, return std::nullopt; } +/// Predicate: true for builtin intrinsic functions whose references are not +/// folded while compiling an intrinsic module source, but only when the module +/// file is read, because their values depend on compiler options of the +/// compilation that uses the module (e.g. -fdefault-real-8, -fdisable-real-16). +static bool IsDeferredFoldingIntrinsic(std::string_view name) { + return name == "__builtin_numeric_storage_size" || + name == "__builtin_selected_int_kind" || + name == "__builtin_selected_unsigned_kind" || + name == "__builtin_selected_real_kind"; +} + +namespace { +struct HasDeferredFoldingHelper : public AnyTraverse<HasDeferredFoldingHelper> { + using Base = AnyTraverse<HasDeferredFoldingHelper>; + HasDeferredFoldingHelper() : Base{*this} {} + using Base::operator(); + bool operator()(const SpecificIntrinsic &intrinsic) const { + return IsDeferredFoldingIntrinsic(intrinsic.name); + } + bool operator()(const Symbol &symbol) const { + return HasDeferredFolding(symbol); + } +}; +} // namespace + +bool HasDeferredFolding(const Expr<SomeType> &expr) { + return HasDeferredFoldingHelper{}(expr); +} + +bool HasDeferredFolding(const Symbol &symbol) { + const Symbol &ultimate{symbol.GetUltimate()}; + if (IsNamedConstant(ultimate)) { + if (const auto *object{ + ultimate.detailsIf<semantics::ObjectEntityDetails>()}) { + if (const auto &init{object->init()}) { + return HasDeferredFolding(*init); + } + } + } + return false; +} + +bool IsDeferredFoldingAllowed(const Symbol &symbol) { + const semantics::Scope &owner{symbol.GetUltimate().owner()}; + if (!owner.IsModule()) { + return false; + } + const auto &name{DEREF(owner.symbol()).name()}; + return name == "iso_fortran_env" || name == "iso_fortran_env_impl" || + name == "iso_c_binding"; +} + // Specification expression validation (10.1.11(2), C1010) class CheckSpecificationExprHelper : public AnyTraverse<CheckSpecificationExprHelper, diff --git a/flang/lib/Evaluate/fold-integer.cpp b/flang/lib/Evaluate/fold-integer.cpp index 1c86642b234661..6a1bc6898c5901 100644 --- a/flang/lib/Evaluate/fold-integer.cpp +++ b/flang/lib/Evaluate/fold-integer.cpp @@ -1401,7 +1401,10 @@ Expr<Type<TypeCategory::Integer>> FoldIntrinsicFunction(FoldingContext &context, kind, SelectedCharKind(*charVal->AsStdString(), defaultKind)); } } - } else if (name == "selected_int_kind" || name == "selected_unsigned_kind") { + } else if (name == "selected_int_kind" || name == "selected_unsigned_kind" || + ((name == "__builtin_selected_int_kind" || + name == "__builtin_selected_unsigned_kind") && + context.moduleFileName())) { if (auto p{ToInt64(args[0])}) { return MakeConstantExpr<T>( kind, context.targetCharacteristics().SelectedIntKind(*p)); @@ -1412,7 +1415,8 @@ Expr<Type<TypeCategory::Integer>> FoldIntrinsicFunction(FoldingContext &context, kind, context.targetCharacteristics().SelectedLogicalKind(*p)); } } else if (name == "selected_real_kind" || - name == "__builtin_ieee_selected_real_kind") { + name == "__builtin_ieee_selected_real_kind" || + (name == "__builtin_selected_real_kind" && context.moduleFileName())) { if (auto p{GetInt64ArgOr(args[0], 0)}) { if (auto r{GetInt64ArgOr(args[1], 0)}) { if (auto radix{GetInt64ArgOr(args[2], 2)}) { diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp index 4a28192e7ebcfa..ccc0d3259bb683 100644 --- a/flang/lib/Evaluate/intrinsics.cpp +++ b/flang/lib/Evaluate/intrinsics.cpp @@ -1156,6 +1156,30 @@ static const IntrinsicInterface genericIntrinsicFunction[]{ {ArgFlag::canBeMoldNull, ArgFlag::onlyConstantInquiry}}}, DefaultLogical}, {"__builtin_numeric_storage_size", {}, DefaultInt}, + {"__builtin_selected_int_kind", {{"r", AnyInt, Rank::scalar}}, + TypePattern{IntType, KindCode::exactKind, 4}, Rank::scalar, + IntrinsicClass::transformationalFunction}, + {"__builtin_selected_unsigned_kind", {{"r", AnyInt, Rank::scalar}}, + TypePattern{IntType, KindCode::exactKind, 4}, Rank::scalar, + IntrinsicClass::transformationalFunction}, + {"__builtin_selected_real_kind", + {{"p", AnyInt, Rank::scalar}, + {"r", AnyInt, Rank::scalar, Optionality::optional}, + {"radix", AnyInt, Rank::scalar, Optionality::optional}}, + TypePattern{IntType, KindCode::exactKind, 4}, Rank::scalar, + IntrinsicClass::transformationalFunction}, + {"__builtin_selected_real_kind", + {{"p", AnyInt, Rank::scalar, Optionality::optional}, + {"r", AnyInt, Rank::scalar}, + {"radix", AnyInt, Rank::scalar, Optionality::optional}}, + TypePattern{IntType, KindCode::exactKind, 4}, Rank::scalar, + IntrinsicClass::transformationalFunction}, + {"__builtin_selected_real_kind", + {{"p", AnyInt, Rank::scalar, Optionality::optional}, + {"r", AnyInt, Rank::scalar, Optionality::optional}, + {"radix", AnyInt, Rank::scalar}}, + TypePattern{IntType, KindCode::exactKind, 4}, Rank::scalar, + IntrinsicClass::transformationalFunction}, }; // TODO: Non-standard intrinsic functions diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index bd32168b700683..23325050402b22 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -639,6 +639,9 @@ static void parseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) { if (args.hasArg(clang::options::OPT_fdisable_real_10)) opts.disabledRealKinds.push_back(10); + if (args.hasArg(clang::options::OPT_fdisable_real_16)) + opts.disabledRealKinds.push_back(16); + if (args.hasArg(clang::options::OPT_fdisable_real_3)) opts.disabledRealKinds.push_back(3); diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp index 39c743d97473e9..d0789c7e4f5e7e 100644 --- a/flang/lib/Lower/Bridge.cpp +++ b/flang/lib/Lower/Bridge.cpp @@ -12,6 +12,7 @@ #include "flang/Lower/Bridge.h" +#include "flang/Evaluate/check-expression.h" #include "flang/Evaluate/tools.h" #include "flang/Evaluate/traverse.h" #include "flang/Lower/Allocatable.h" @@ -7231,19 +7232,16 @@ class FirConverter : public Fortran::lower::AbstractConverter { if (owningScope && mod.getScope() != *owningScope) continue; - // Very special case: The value of numeric_storage_size depends on - // compilation options and therefore its value is not yet known when - // building the builtins runtime. Instead, the parameter is folding a - // __numeric_storage_size() expression which is loaded into the user - // program. For the iso_fortran_env object file, omit the symbol as it - // is never used. - if (var.hasSymbol()) { - const Fortran::semantics::Symbol &sym = var.getSymbol(); - const Fortran::semantics::Scope &owner = sym.owner(); - if (sym.name() == "numeric_storage_size" && owner.IsModule() && - DEREF(owner.symbol()).name() == "iso_fortran_env") - continue; - } + // Very special case: The values of some intrinsic module constants + // (e.g. numeric_storage_size) depend on compilation options and + // therefore are not yet known when building the builtins runtime. + // Instead, the parameter is initialized by an expression which is only + // folded when the module file is loaded into the user program. For the + // intrinsic module's object file, omit the symbol as it is never used. + if (var.hasSymbol() && + Fortran::evaluate::IsDeferredFoldingAllowed(var.getSymbol()) && + Fortran::evaluate::HasDeferredFolding(var.getSymbol())) + continue; Fortran::lower::defineModuleVariable(*this, var); } for (auto &eval : mod.evaluationList) diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp index ed89b84f8bfbae..7cf3915213bb6b 100644 --- a/flang/lib/Semantics/expression.cpp +++ b/flang/lib/Semantics/expression.cpp @@ -4991,13 +4991,19 @@ bool ExpressionAnalyzer::CheckIntrinsicKind( return true; } else if (foldingContext_.targetCharacteristics().CanSupportType( category, kind)) { + const semantics::Scope &scope{ + context_.FindScope(GetContextualMessages().at())}; if (const semantics::Scope *modFileScope{ - semantics::FindModuleFileContaining( - context_.FindScope(GetContextualMessages().at()))}; + semantics::FindModuleFileContaining(scope)}; modFileScope && modFileScope->parent().IsIntrinsicModules()) { // Ignore usage of unsupported intrinsic type kinds in intrinsic module // files. They might be USE'd into a cross-compilation or into a // compilation with a disabled REAL kind. + } else if (const semantics::Scope *module{ + semantics::FindModuleContaining(scope)}; + module && module->GetName() == "ieee_arithmetic") { + // Likewise when building the intrinsic module itself, which declares + // specific procedures for all REAL kinds, enabled or not. } else { Say("%s(KIND=%jd) is not an enabled type for this target"_err_en_US, ToUpperCase(EnumToString(category)), kind); diff --git a/flang/test/Semantics/deferred-folding-builtins.f90 b/flang/test/Semantics/deferred-folding-builtins.f90 new file mode 100644 index 00000000000000..c2079281b9a03b --- /dev/null +++ b/flang/test/Semantics/deferred-folding-builtins.f90 @@ -0,0 +1,15 @@ +! RUN: %python %S/test_errors.py %s %flang_fc1 +! Only the intrinsic modules may define named constants whose values are folded +! when their module file is read. +module m + intrinsic :: __builtin_numeric_storage_size, __builtin_selected_int_kind, & + __builtin_selected_unsigned_kind, __builtin_selected_real_kind + !ERROR: Value of named constant 'nss' (__builtin_numeric_storage_size()) cannot be computed as a constant value + integer, parameter :: nss = __builtin_numeric_storage_size() + !ERROR: Value of named constant 'ik' (__builtin_selected_int_kind(9_4)) cannot be computed as a constant value + integer, parameter :: ik = __builtin_selected_int_kind(9) + !ERROR: Value of named constant 'uk' (__builtin_selected_unsigned_kind(9_4)) cannot be computed as a constant value + integer, parameter :: uk = __builtin_selected_unsigned_kind(9) + !ERROR: Value of named constant 'rk' (__builtin_selected_real_kind(18_4,4931_4)) cannot be computed as a constant value + integer, parameter :: rk = __builtin_selected_real_kind(18, 4931) +end diff --git a/flang/test/Semantics/disabled-integer-kinds.f90 b/flang/test/Semantics/disabled-integer-kinds.f90 new file mode 100644 index 00000000000000..8bdff95f2eb32e --- /dev/null +++ b/flang/test/Semantics/disabled-integer-kinds.f90 @@ -0,0 +1,70 @@ +! The INTEGER, UNSIGNED, and LOGICAL kind constants of ISO_FORTRAN_ENV and the +! 128-bit kinds of ISO_C_BINDING reflect the compiler options of the +! compilation that uses the module, not those used to build the module. +! REQUIRES: x86-registered-target +! RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -fdebug-unparse %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL +! RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -fdebug-unparse -fdisable-integer-2 %s 2>&1 | FileCheck %s --check-prefixes=CHECK,NO2 +! RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -fdebug-unparse -fdisable-integer-16 %s 2>&1 | FileCheck %s --check-prefixes=CHECK,NO16 +! RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -fdebug-unparse -fdisable-integer-2 -fdisable-integer-16 %s 2>&1 | FileCheck %s --check-prefixes=CHECK,NOBOTH + +! CHECK-NOT: error +use iso_fortran_env +use iso_c_binding + +! ALL: i16 = 2_4 +! NO2: i16 = -2_4 +! NO16: i16 = 2_4 +! NOBOTH: i16 = -2_4 +integer, parameter :: i16 = int16 + +! ALL: i128 = 16_4 +! NO2: i128 = 16_4 +! NO16: i128 = -1_4 +! NOBOTH: i128 = -1_4 +integer, parameter :: i128 = int128 + +! ALL: u16 = 2_4 +! NO2: u16 = -2_4 +! NO16: u16 = 2_4 +! NOBOTH: u16 = -2_4 +integer, parameter :: u16 = uint16 + +! ALL: u128 = 16_4 +! NO2: u128 = 16_4 +! NO16: u128 = -1_4 +! NOBOTH: u128 = -1_4 +integer, parameter :: u128 = uint128 + +! ALL: ikinds(*) = [INTEGER(4)::1_4,2_4,4_4,8_4,16_4] +! NO2: ikinds(*) = [INTEGER(4)::1_4,4_4,8_4,16_4] +! NO16: ikinds(*) = [INTEGER(4)::1_4,2_4,4_4,8_4] +! NOBOTH: ikinds(*) = [INTEGER(4)::1_4,4_4,8_4] +integer, parameter :: ikinds(*) = integer_kinds + +! ALL: lkinds(*) = [INTEGER(4)::1_4,2_4,4_4,8_4] +! NO2: lkinds(*) = [INTEGER(4)::1_4,4_4,8_4] +! NO16: lkinds(*) = [INTEGER(4)::1_4,2_4,4_4,8_4] +! NOBOTH: lkinds(*) = [INTEGER(4)::1_4,4_4,8_4] +integer, parameter :: lkinds(*) = logical_kinds + +! ALL: ci128 = 16_4 +! NO2: ci128 = 16_4 +! NO16: ci128 = -1_4 +! NOBOTH: ci128 = -1_4 +integer, parameter :: ci128 = c_int128_t + +! ALL: cu128 = 16_4 +! NO2: cu128 = 16_4 +! NO16: cu128 = -1_4 +! NOBOTH: cu128 = -1_4 +integer, parameter :: cu128 = c_uint128_t + +! CHECK: cimax = 8_4 +integer, parameter :: cimax = c_intmax_t + +! CHECK: cumax = 8_4 +integer, parameter :: cumax = c_uintmax_t + +! CHECK: i32 = 4_4 +integer, parameter :: i32 = int32 +end diff --git a/flang/test/Semantics/disabled-real-kinds.f90 b/flang/test/Semantics/disabled-real-kinds.f90 new file mode 100644 index 00000000000000..452e5e5dbcb907 --- /dev/null +++ b/flang/test/Semantics/disabled-real-kinds.f90 @@ -0,0 +1,55 @@ +! The REAL kind constants of ISO_FORTRAN_ENV reflect the compiler options of +! the compilation that uses the module, not those used to build the module. +! REQUIRES: x86-registered-target +! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL +! RUN: %flang_fc1 -fdebug-unparse -fdisable-real-10 %s 2>&1 | FileCheck %s --check-prefixes=CHECK,NO10 +! RUN: %flang_fc1 -fdebug-unparse -fdisable-real-16 %s 2>&1 | FileCheck %s --check-prefixes=CHECK,NO16 +! RUN: %flang_fc1 -fdebug-unparse -fdisable-real-10 -fdisable-real-16 %s 2>&1 | FileCheck %s --check-prefixes=CHECK,NO10-16 + +! CHECK-NOT: error +use iso_fortran_env +use ieee_arithmetic +use iso_c_binding + +! ALL: r80 = 10_4 +! NO10: r80 = -2_4 +! NO16: r80 = 10_4 +! NO10-16: r80 = -1_4 +integer, parameter :: r80 = real80 + +! ALL: r128 = 16_4 +! NO10: r128 = 16_4 +! NO16: r128 = -1_4 +! NO10-16: r128 = -1_4 +integer, parameter :: r128 = real128 + +! ALL: r64x2 = -2_4 +! NO10: r64x2 = -2_4 +! NO16: r64x2 = -1_4 +! NO10-16: r64x2 = -1_4 +integer, parameter :: r64x2 = real64x2 + +! ALL: kinds(*) = [INTEGER(4)::2_4,3_4,4_4,8_4,10_4,16_4] +! NO10: kinds(*) = [INTEGER(4)::2_4,3_4,4_4,8_4,16_4] +! NO16: kinds(*) = [INTEGER(4)::2_4,3_4,4_4,8_4,10_4] +! NO10-16: kinds(*) = [INTEGER(4)::2_4,3_4,4_4,8_4] +integer, parameter :: kinds(*) = real_kinds + +! ALL: cld = 10_4 +! NO10: cld = -1_4 +! NO16: cld = 10_4 +! NO10-16: cld = -3_4 +integer, parameter :: cld = c_long_double + +! ALL: cf128 = 16_4 +! NO10: cf128 = 16_4 +! NO16: cf128 = -1_4 +! NO10-16: cf128 = -3_4 +integer, parameter :: cf128 = c_float128 + +! CHECK: r32 = 4_4 +integer, parameter :: r32 = real32 + +! CHECK: PRINT *, ieee_arithmetic$ieee_arithmetic$ieee_is_finite_a4(1._4) +print *, ieee_is_finite(1.0) +end diff --git a/flang/test/Semantics/modfile90.f90 b/flang/test/Semantics/modfile90.f90 new file mode 100644 index 00000000000000..0c2c15a179d2d9 --- /dev/null +++ b/flang/test/Semantics/modfile90.f90 @@ -0,0 +1,22 @@ +! RUN: %python %S/test_modfile.py %s %flang_fc1 +! References to __builtin_selected_real_kind, and named constants depending on +! them, are not folded in the intrinsic modules until the module file is read. +module iso_fortran_env_impl + intrinsic :: __builtin_selected_real_kind + integer, parameter :: k = __builtin_selected_real_kind(6, 37) + integer, parameter :: safe = merge(k, -1, k >= 0) + integer, parameter :: kinds(*) = [pack([k], k >= 0)] + integer, parameter :: folded = selected_real_kind(6, 37) +end + +!Expect: iso_fortran_env_impl.mod +!module iso_fortran_env_impl +!intrinsic::__builtin_selected_real_kind +!integer(4),parameter::k=__builtin_selected_real_kind(6_4,37_4) +!integer(4),parameter::safe=merge(k,-1_4,k>=0_4) +!intrinsic::merge +!integer(4),parameter::kinds(1_8:*)=[INTEGER(4)::pack([INTEGER(4)::k],k>=0_4)] +!intrinsic::pack +!integer(4),parameter::folded=4_4 +!intrinsic::selected_real_kind +!end _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
