Skip to content

Commit 88f319b

Browse files
clementvalgithub-actions[bot]
authored andcommitted
Automerge: [flang][cuda][openacc] Emit an error when CUDA symbols are imported with CUDA disabled (#205207)
2 parents 3e320bb + a6986f0 commit 88f319b

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

flang/lib/Semantics/mod-file.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "flang/Common/restorer.h"
1212
#include "flang/Evaluate/tools.h"
1313
#include "flang/Parser/message.h"
14+
#include "flang/Parser/parse-tree-visitor.h"
1415
#include "flang/Parser/parsing.h"
1516
#include "flang/Parser/unparse.h"
1617
#include "flang/Semantics/scope.h"
@@ -72,6 +73,7 @@ static bool FileContentsMatch(
7273
const std::string &, const std::string &, const std::string &);
7374
static ModuleCheckSumType ComputeCheckSum(const std::string_view &);
7475
static std::string CheckSumString(ModuleCheckSumType);
76+
static bool ProgramHasCUDAAttrs(const parser::Program &);
7577

7678
// Collect symbols needed for a subprogram interface
7779
class SubprogramSymbolCollector {
@@ -1703,6 +1705,13 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
17031705
return nullptr;
17041706
}
17051707
parser::Program &parseTree{context_.SaveParseTree(std::move(*parsedProgram))};
1708+
if (context_.languageFeatures().IsEnabled(common::LanguageFeature::OpenACC) &&
1709+
!context_.languageFeatures().IsEnabled(common::LanguageFeature::CUDA) &&
1710+
ProgramHasCUDAAttrs(parseTree)) {
1711+
Say("use", name, ancestorName,
1712+
"CUDA is not enabled, but '%s' defines CUDA symbols"_err_en_US,
1713+
sourceFile->path());
1714+
}
17061715
Scope *parentScope; // the scope this module/submodule goes into
17071716
if (!isIntrinsic.has_value()) {
17081717
for (const auto &dir : context_.intrinsicModuleDirectories()) {
@@ -1823,6 +1832,26 @@ static std::optional<SourceName> GetSubmoduleParent(
18231832
}
18241833
}
18251834

1835+
struct CUDAAttrProgramVisitor {
1836+
template <typename A> bool Pre(const A &) { return true; }
1837+
template <typename A> void Post(const A &) {}
1838+
bool Pre(const common::CUDADataAttr &) {
1839+
foundCUDAAttrs = true;
1840+
return false;
1841+
}
1842+
bool Pre(const common::CUDASubprogramAttrs &) {
1843+
foundCUDAAttrs = true;
1844+
return false;
1845+
}
1846+
bool foundCUDAAttrs{false};
1847+
};
1848+
1849+
static bool ProgramHasCUDAAttrs(const parser::Program &program) {
1850+
CUDAAttrProgramVisitor visitor;
1851+
parser::Walk(program, visitor);
1852+
return visitor.foundCUDAAttrs;
1853+
}
1854+
18261855
void SubprogramSymbolCollector::Collect() {
18271856
const auto &details{symbol_.get<SubprogramDetails>()};
18281857
isInterface_ = details.isInterface();

flang/test/Semantics/modfile84.f90

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
! RUN: split-file %s %t
2+
! RUN: %flang_fc1 -fsyntax-only -x cuda -module-dir %t %t/m.cuf
3+
! RUN: not %flang_fc1 -fsyntax-only -fopenacc -module-dir %t %t/use.f90 2>&1 | FileCheck %s
4+
5+
!--- m.cuf
6+
module modfile84m
7+
real, device :: d
8+
contains
9+
attributes(device) subroutine s()
10+
end subroutine
11+
end module
12+
13+
!--- use.f90
14+
use modfile84m
15+
end
16+
17+
! CHECK: error: Cannot use module file for module 'modfile84m': CUDA is not enabled, but '{{.*modfile84m.mod}}' defines CUDA symbols

0 commit comments

Comments
 (0)