[flang-commits] [flang] [flang] Fold KIND= arguments in intrinsic function references (PR #124666)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Jan 28 11:49:22 PST 2025


https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/124666

>From c0924c5f0ac30406dd5724450b6d765aaeefe259 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Mon, 27 Jan 2025 17:11:21 -0800
Subject: [PATCH] [flang] Fold KIND= arguments in intrinsic function references

KIND= arguments in e.g. ACHAR(..., KIND=...) intrinsic function references
must be compilation-time constant expressions.  The compiler was failing
to evaluate those expressions if they were not actually literaly constant
values.

Fixes https://github.com/llvm/llvm-project/issues/124618.
---
 flang/lib/Evaluate/intrinsics.cpp | 7 +++----
 flang/test/Evaluate/bug124618.f90 | 5 +++++
 2 files changed, 8 insertions(+), 4 deletions(-)
 create mode 100644 flang/test/Evaluate/bug124618.f90

diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index 29f8e5fcc49d53..ce9d4a43970928 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -2357,7 +2357,7 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
       if (kindArg) {
         if (auto *expr{kindArg->UnwrapExpr()}) {
           CHECK(expr->Rank() == 0);
-          if (auto code{ToInt64(*expr)}) {
+          if (auto code{ToInt64(Fold(context, common::Clone(*expr)))}) {
             if (context.targetCharacteristics().IsTypeEnabled(
                     *category, *code)) {
               if (*category == TypeCategory::Character) { // ACHAR & CHAR
@@ -2369,9 +2369,8 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
             }
           }
         }
-        messages.Say("'kind=' argument must be a constant scalar integer "
-                     "whose value is a supported kind for the "
-                     "intrinsic result type"_err_en_US);
+        messages.Say(
+            "'kind=' argument must be a constant scalar integer whose value is a supported kind for the intrinsic result type"_err_en_US);
         // use default kind below for error recovery
       } else if (kindDummyArg->flags.test(ArgFlag::defaultsToSameKind)) {
         CHECK(sameArg);
diff --git a/flang/test/Evaluate/bug124618.f90 b/flang/test/Evaluate/bug124618.f90
new file mode 100644
index 00000000000000..939985e588af27
--- /dev/null
+++ b/flang/test/Evaluate/bug124618.f90
@@ -0,0 +1,5 @@
+! RUN: %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck --allow-empty %s
+!CHECK-NOT: error:
+real x
+print *, char(48, kind=size([x])) ! folds down to 1
+end



More information about the flang-commits mailing list