[flang-commits] [flang] [flang][cuda][openacc] Emit an error when CUDA symbols are imported with CUDA disabled (PR #205427)
Valentin Clement バレンタイン クレメン via flang-commits
flang-commits at lists.llvm.org
Tue Jun 23 13:54:51 PDT 2026
https://github.com/clementval created https://github.com/llvm/llvm-project/pull/205427
Only look for for module symbols.
>From b28890d743ed7e007ca555f63341a204b09aef7f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Valentin=20Clement=20=28=E3=83=90=E3=83=AC=E3=83=B3?=
=?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=B3=20=E3=82=AF=E3=83=AC=E3=83=A1?=
=?UTF-8?q?=E3=83=B3=29?= <clementval at gmail.com>
Date: Mon, 22 Jun 2026 16:53:26 -0700
Subject: [PATCH] [flang][cuda][openacc] Emit an error when CUDA symbols are
imported with CUDA disabled
---
flang/lib/Semantics/mod-file.cpp | 21 +++++++++++++++++++++
flang/test/Semantics/modfile84.f90 | 30 ++++++++++++++++++++++++++++++
2 files changed, 51 insertions(+)
create mode 100644 flang/test/Semantics/modfile84.f90
diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index 89a535c6ff6f9..a5bc4f5efedc0 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -72,6 +72,7 @@ static bool FileContentsMatch(
const std::string &, const std::string &, const std::string &);
static ModuleCheckSumType ComputeCheckSum(const std::string_view &);
static std::string CheckSumString(ModuleCheckSumType);
+static bool ScopeHasCUDAModuleVariables(const Scope &);
// Collect symbols needed for a subprogram interface
class SubprogramSymbolCollector {
@@ -1788,6 +1789,14 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
if (isIntrinsic.value_or(false)) {
moduleSymbol->attrs().set(Attr::INTRINSIC);
}
+ if (context_.languageFeatures().IsEnabled(
+ common::LanguageFeature::OpenACC) &&
+ !context_.languageFeatures().IsEnabled(common::LanguageFeature::CUDA) &&
+ ScopeHasCUDAModuleVariables(*moduleSymbol->scope())) {
+ Say("use", name, ancestorName,
+ "CUDA is not enabled, but '%s' defines CUDA symbols"_err_en_US,
+ sourceFile->path());
+ }
return moduleSymbol->scope();
} else {
return nullptr;
@@ -1823,6 +1832,18 @@ static std::optional<SourceName> GetSubmoduleParent(
}
}
+static bool ScopeHasCUDAModuleVariables(const Scope &scope) {
+ for (const auto &[_, symbolRef] : scope) {
+ const Symbol &symbol{*symbolRef};
+ if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}) {
+ if (object->cudaDataAttr()) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
void SubprogramSymbolCollector::Collect() {
const auto &details{symbol_.get<SubprogramDetails>()};
isInterface_ = details.isInterface();
diff --git a/flang/test/Semantics/modfile84.f90 b/flang/test/Semantics/modfile84.f90
new file mode 100644
index 0000000000000..94498ac7afaa1
--- /dev/null
+++ b/flang/test/Semantics/modfile84.f90
@@ -0,0 +1,30 @@
+! RUN: split-file %s %t
+! RUN: %flang_fc1 -fsyntax-only -x cuda -module-dir %t %t/m.cuf
+! RUN: not %flang_fc1 -fsyntax-only -fopenacc -module-dir %t %t/use.f90 2>&1 | FileCheck %s
+! RUN: %flang_fc1 -fsyntax-only -x cuda -module-dir %t %t/m2.cuf
+! RUN: %flang_fc1 -fsyntax-only -fopenacc -module-dir %t %t/use2.f90
+
+!--- m.cuf
+module modfile84m
+ real, device :: d
+contains
+ attributes(device) subroutine s()
+ end subroutine
+end module
+
+!--- m2.cuf
+module modfile84m2
+contains
+ attributes(device) subroutine s()
+ end subroutine
+end module
+
+!--- use.f90
+use modfile84m
+end
+
+!--- use2.f90
+use modfile84m2
+end
+
+! CHECK: error: Cannot use module file for module 'modfile84m': CUDA is not enabled, but '{{.*modfile84m.mod}}' defines CUDA symbols
More information about the flang-commits
mailing list