[flang-commits] [flang] 1277a11 - [flang][cuda] Define _CUDA only when preprocessor is enabled (#155913)

via flang-commits flang-commits at lists.llvm.org
Thu Aug 28 14:44:49 PDT 2025


Author: Valentin Clement (バレンタイン クレメン)
Date: 2025-08-28T21:44:46Z
New Revision: 1277a1111d420b244a0e595ad9b74827841b723a

URL: https://github.com/llvm/llvm-project/commit/1277a1111d420b244a0e595ad9b74827841b723a
DIFF: https://github.com/llvm/llvm-project/commit/1277a1111d420b244a0e595ad9b74827841b723a.diff

LOG: [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.

Added: 
    

Modified: 
    flang/lib/Frontend/CompilerInvocation.cpp
    flang/lib/Parser/parsing.cpp
    flang/test/Parser/cuf-sanity-tree.CUF
    flang/test/Parser/cuf-sanity-unparse.CUF

Removed: 
    


################################################################################
diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 4719a242035ed..fb3a132cae30e 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -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");

diff  --git a/flang/lib/Parser/parsing.cpp b/flang/lib/Parser/parsing.cpp
index ceea74766d7ea..8a8c6ef673a8c 100644
--- a/flang/lib/Parser/parsing.cpp
+++ b/flang/lib/Parser/parsing.cpp
@@ -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);

diff  --git a/flang/test/Parser/cuf-sanity-tree.CUF b/flang/test/Parser/cuf-sanity-tree.CUF
index a8b2f93913ca9..83d7540b8dec5 100644
--- a/flang/test/Parser/cuf-sanity-tree.CUF
+++ b/flang/test/Parser/cuf-sanity-tree.CUF
@@ -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

diff  --git a/flang/test/Parser/cuf-sanity-unparse.CUF b/flang/test/Parser/cuf-sanity-unparse.CUF
index 2e2df9ac6646a..ede98093c442d 100644
--- a/flang/test/Parser/cuf-sanity-unparse.CUF
+++ b/flang/test/Parser/cuf-sanity-unparse.CUF
@@ -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


        


More information about the flang-commits mailing list