Skip to content

Commit 1277a11

Browse files
authored
[flang][cuda] Define _CUDA only when preprocessor is enabled (#155913)
From the CUDA Fortran programming guide: > If CUDA Fortran is enabled in compilation, either by specifying -⁠cuda on the command line, and pre-processing is enabled by either the -⁠Mpreprocess compiler option or by using capital letters in the filename extension (.CUF, .F90, etc.) then the _CUDA macro is defined. Move the definition of `_CUDA` to the compiler invocation.
1 parent 11e796d commit 1277a11

File tree

4 files changed

+7
-5
lines changed

4 files changed

+7
-5
lines changed

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,11 @@ void CompilerInvocation::setDefaultPredefinitions() {
17541754
fortranOptions.predefinitions);
17551755
}
17561756

1757+
if (frontendOptions.features.IsEnabled(
1758+
Fortran::common::LanguageFeature::CUDA)) {
1759+
fortranOptions.predefinitions.emplace_back("_CUDA", "1");
1760+
}
1761+
17571762
llvm::Triple targetTriple{llvm::Triple(this->targetOpts.triple)};
17581763
if (targetTriple.isOSLinux()) {
17591764
fortranOptions.predefinitions.emplace_back("__linux__", "1");

flang/lib/Parser/parsing.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
9696
prescanner.AddCompilerDirectiveSentinel("$cuf");
9797
prescanner.AddCompilerDirectiveSentinel("@cuf");
9898
}
99-
if (options.features.IsEnabled(LanguageFeature::CUDA)) {
100-
preprocessor_.Define("_CUDA", "1");
101-
}
10299
ProvenanceRange range{allSources.AddIncludedFile(
103100
*sourceFile, ProvenanceRange{}, options.isModuleFile)};
104101
prescanner.Prescan(range);

flang/test/Parser/cuf-sanity-tree.CUF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: %flang_fc1 -fdebug-dump-parse-tree %s 2>&1 | FileCheck %s
1+
! RUN: %flang_fc1 -fdebug-dump-parse-tree -x cuda %s 2>&1 | FileCheck %s
22
include "cuf-sanity-common"
33
!CHECK: Program -> ProgramUnit -> SubroutineSubprogram
44
!CHECK: | SubroutineStmt

flang/test/Parser/cuf-sanity-unparse.CUF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
1+
! RUN: %flang_fc1 -fdebug-unparse -x cuda %s 2>&1 | FileCheck %s
22
include "cuf-sanity-common"
33
!CHECK: SUBROUTINE atcuf
44
!CHECK: END SUBROUTINE

0 commit comments

Comments
 (0)