[all-commits] [llvm/llvm-project] 9e17a9: [flang][Evaluate] Fold type-only inquiries of cond...
Vineet Kumar via All-commits
all-commits at lists.llvm.org
Tue Sep 29 13:54:29 PDT 2026
Branch: refs/heads/users/vntkmr/flang-conditional-args/02-Fold-type-only-inquiries-of-conditional-arguments
Home: https://github.com/llvm/llvm-project
Commit: 9e17a9e56764af15ad08608e7d11d36bf89ba4d6
https://github.com/llvm/llvm-project/commit/9e17a9e56764af15ad08608e7d11d36bf89ba4d6
Author: Vineet Kumar <vineetk at hpe.com>
Date: 2026-09-29 (Tue, 29 Sep 2026)
Changed paths:
M flang/lib/Evaluate/fold-implementation.h
M flang/test/Evaluate/fold-conditional-arg.f90
A flang/test/Semantics/conditional-arg-inquiry.f90
Log Message:
-----------
[flang][Evaluate] Fold type-only inquiries of conditional arguments
Fold a reference to an intrinsic inquiry function whose result depends
only on the declared type, kind type parameters, or rank of an argument
when that argument is a conditional argument with a non-constant
condition:
integer, parameter :: k = kind((flag ? a : b))
Every consequent-arg of a conditional argument has the same declared
type and kind type parameters (F2023 C1538) and the same rank unless
all of them are assumed-rank (C1539), so such an inquiry has the same
value whichever consequent-arg is chosen at run time.
FoldOperation(FunctionRef) folds the reference by substituting the
first consequent-arg for the conditional argument in a copy of the
reference and keeps the result when it is a constant.
The qualifying arguments are the dummy arguments that the intrinsic
table marks with ArgFlag::onlyConstantInquiry, which
IntrinsicInterface::Match records as
DummyDataObject::Attr::OnlyIntrinsicInquiry: X in KIND, BIT_SIZE,
DIGITS, EPSILON, HUGE, MAXEXPONENT, MINEXPONENT, PRECISION, RADIX,
RANGE, RANK, TINY, A in NEW_LINE, and X in the IEEE_SUPPORT_*
inquiry functions. IsConstantExpr() already uses the same attribute
to accept these references as constant expressions; this change makes
folding produce the value.
Inquiries whose result can differ between consequent-args stay
unfolded: SIZE, SHAPE, LBOUND, UBOUND, LEN, STORAGE_SIZE, ALLOCATED,
ASSOCIATED, IS_CONTIGUOUS, and RANK when every consequent-arg is
assumed-rank. A conditional argument with a .NIL. consequent also
stays unfolded. A .NIL. consequent can leave an OPTIONAL dummy
argument absent at run time, and an inquiry can define a separate
result for an absent argument, so no present consequent-arg can stand
in for the whole argument. A TODO records the two-variant fold that
could handle that case once llvm/llvm-project#227390 lets .NIL. reach
the folder.
KIND of a conditional argument with a constant condition, such as
kind((.true. ? a : b)), folds as well. FoldOperation skips argument
folding for KIND, so the condition was not resolved before.
No portability warning is emitted, which matches the existing handling
of kind(a + b) and of a conditional expression in a constant
expression.
Tests: Evaluate/fold-conditional-arg.f90 gains a case for each covered
inquiry, plus constant-condition, multi-branch, expression-consequent,
and keyword-argument forms. Semantics/conditional-arg-inquiry.f90
checks that the inquiries listed above still do not fold.
These changes were generated with the assistance of AI tooling and have
been reviewed, tested, and validated by the author.
To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications
More information about the All-commits
mailing list