[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.
This commit is contained in:
Valentin Clement (バレンタイン クレメン)
2025-08-28 14:44:46 -07:00
committed by GitHub
parent 11e796dee2
commit 1277a1111d
4 changed files with 7 additions and 5 deletions

View File

@@ -1754,6 +1754,11 @@ void CompilerInvocation::setDefaultPredefinitions() {
fortranOptions.predefinitions);
}
if (frontendOptions.features.IsEnabled(
Fortran::common::LanguageFeature::CUDA)) {
fortranOptions.predefinitions.emplace_back("_CUDA", "1");
}
llvm::Triple targetTriple{llvm::Triple(this->targetOpts.triple)};
if (targetTriple.isOSLinux()) {
fortranOptions.predefinitions.emplace_back("__linux__", "1");

View File

@@ -96,9 +96,6 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
prescanner.AddCompilerDirectiveSentinel("$cuf");
prescanner.AddCompilerDirectiveSentinel("@cuf");
}
if (options.features.IsEnabled(LanguageFeature::CUDA)) {
preprocessor_.Define("_CUDA", "1");
}
ProvenanceRange range{allSources.AddIncludedFile(
*sourceFile, ProvenanceRange{}, options.isModuleFile)};
prescanner.Prescan(range);

View File

@@ -1,4 +1,4 @@
! RUN: %flang_fc1 -fdebug-dump-parse-tree %s 2>&1 | FileCheck %s
! RUN: %flang_fc1 -fdebug-dump-parse-tree -x cuda %s 2>&1 | FileCheck %s
include "cuf-sanity-common"
!CHECK: Program -> ProgramUnit -> SubroutineSubprogram
!CHECK: | SubroutineStmt

View File

@@ -1,4 +1,4 @@
! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
! RUN: %flang_fc1 -fdebug-unparse -x cuda %s 2>&1 | FileCheck %s
include "cuf-sanity-common"
!CHECK: SUBROUTINE atcuf
!CHECK: END SUBROUTINE