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

via flang-commits flang-commits at lists.llvm.org
Thu Aug 28 13:07:03 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-parser

Author: Valentin Clement (バレンタイン クレメン) (clementval)

<details>
<summary>Changes</summary>

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. 

---
Full diff: https://github.com/llvm/llvm-project/pull/155913.diff


4 Files Affected:

- (modified) flang/lib/Frontend/CompilerInvocation.cpp (+5) 
- (modified) flang/lib/Parser/parsing.cpp (-1) 
- (modified) flang/test/Parser/cuf-sanity-tree.CUF (+1-1) 
- (modified) flang/test/Parser/cuf-sanity-unparse.CUF (+1-1) 


``````````diff
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 8b07a50824899..4a70416eb4678 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -1566,6 +1566,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.isPPC()) {
     // '__powerpc__' is a generic macro for any PowerPC cases. e.g. Max integer
diff --git a/flang/lib/Parser/parsing.cpp b/flang/lib/Parser/parsing.cpp
index 8fcac7b3cacb1..2fcb04335d363 100644
--- a/flang/lib/Parser/parsing.cpp
+++ b/flang/lib/Parser/parsing.cpp
@@ -89,7 +89,6 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
   if (options.features.IsEnabled(LanguageFeature::CUDA)) {
     prescanner.AddCompilerDirectiveSentinel("$cuf");
     prescanner.AddCompilerDirectiveSentinel("@cuf");
-    preprocessor_.Define("_CUDA", "1");
   }
   ProvenanceRange range{allSources.AddIncludedFile(
       *sourceFile, ProvenanceRange{}, options.isModuleFile)};
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/155913


More information about the flang-commits mailing list