[flang-commits] [flang] [flang][CUDA] Allow constant to match device actual in specific proce… (PR #178658)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Thu Jan 29 05:57:26 PST 2026


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/178658

…dure

When scanning the specific procedures of a generic interface for a match for a set of actual arguments, accept a constant actual argument as a match for a dummy argument with the DEVICE attribute.

>From da8b3b805f19192d35754299d9052e4db8f8dd01 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Thu, 29 Jan 2026 05:53:58 -0800
Subject: [PATCH] [flang][CUDA] Allow constant to match device actual in
 specific procedure

When scanning the specific procedures of a generic interface for
a match for a set of actual arguments, accept a constant actual
argument as a match for a dummy argument with the DEVICE attribute.
---
 flang/lib/Semantics/check-call.cpp |  5 +++--
 flang/lib/Semantics/expression.cpp |  4 +---
 flang/test/Semantics/bug2131.cuf   | 26 ++++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 5 deletions(-)
 create mode 100644 flang/test/Semantics/bug2131.cuf

diff --git a/flang/lib/Semantics/check-call.cpp b/flang/lib/Semantics/check-call.cpp
index d4aa9c3525aaf..d4f406d1ee27c 100644
--- a/flang/lib/Semantics/check-call.cpp
+++ b/flang/lib/Semantics/check-call.cpp
@@ -1107,8 +1107,9 @@ static void CheckExplicitDataArg(const characteristics::DummyDataObject &dummy,
         actualDataAttr = common::CUDADataAttr::Device;
       }
       // For device procedures, treat actual arguments with VALUE attribute as
-      // device data
-      if (!actualDataAttr && actualLastSymbol && IsValue(*actualLastSymbol) &&
+      // device data; also constant actual arguments.
+      if (!actualDataAttr &&
+          (!actualLastSymbol || IsValue(*actualLastSymbol)) &&
           (*procedure.cudaSubprogramAttrs ==
               common::CUDASubprogramAttrs::Device)) {
         actualDataAttr = common::CUDADataAttr::Device;
diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index e20ce698abc65..c7f31657b0b4a 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -3009,7 +3009,6 @@ auto ExpressionAnalyzer::ResolveGeneric(const Symbol &symbol,
           }
           crtMatchingDistance = ComputeCudaMatchingDistance(
               context_.languageFeatures(), *procedure, localActuals);
-        } else {
         }
       }
     }
@@ -3163,13 +3162,12 @@ void ExpressionAnalyzer::EmitGenericResolutionError(const Symbol &symbol,
               : "No specific function of generic '%s' matches the actual arguments"_err_en_US,
           symbol.name())}) {
     parser::ContextualMessages &messages{GetContextualMessages()};
-    semantics::Scope &scope{context_.FindScope(messages.at())};
     for (const Symbol &specific : tried) {
       if (auto procChars{characteristics::Procedure::Characterize(
               specific, GetFoldingContext())}) {
         if (procChars->HasExplicitInterface()) {
           auto reasons{semantics::CheckExplicitInterface(*procChars, arguments,
-              context_, &scope, /*intrinsic=*/nullptr,
+              context_, /*scope=*/nullptr, /*intrinsic=*/nullptr,
               /*allocActualArgumentConversions=*/false,
               /*extentErrors=*/false,
               /*ignoreImplicitVsExplicit=*/false)};
diff --git a/flang/test/Semantics/bug2131.cuf b/flang/test/Semantics/bug2131.cuf
new file mode 100644
index 0000000000000..7d2f52b45dea2
--- /dev/null
+++ b/flang/test/Semantics/bug2131.cuf
@@ -0,0 +1,26 @@
+!RUN: %python %S/test_errors.py %s %flang_fc1 -x cuda
+module m
+  interface randomNumber
+     module procedure rngScalar, rngArray
+  end interface
+  integer :: host_n = 3
+ contains
+  attributes(device) function rngScalar() result(res)
+    real :: res
+    res = 123.
+  end
+  attributes(device) function rngArray(n) result(res)
+    integer, intent(in) :: n
+    real :: res(n)
+    res = 123.
+  end
+  attributes(device) function randomPointInUnitSphere() result(res)
+    real :: res(3)
+    integer :: n
+    res = randomNumber(3) ! ok, constant
+    n = 3
+    res = randomNumber(n) ! ok, local
+    !ERROR: No specific function of generic 'randomnumber' matches the actual arguments
+    res = randomNumber(host_n)
+  end
+end



More information about the flang-commits mailing list