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

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Thu Aug 28 14:27:30 PDT 2025


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

>From a20ddea0536a8e8a65af618cd72c96c8f5469e83 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Thu, 28 Aug 2025 13:04:26 -0700
Subject: [PATCH] [flang][cuda] Define _CUDA only when preprocessor is enabled

---
 flang/lib/Frontend/CompilerInvocation.cpp | 5 +++++
 flang/lib/Parser/parsing.cpp              | 1 -
 flang/test/Parser/cuf-sanity-tree.CUF     | 2 +-
 flang/test/Parser/cuf-sanity-unparse.CUF  | 2 +-
 4 files changed, 7 insertions(+), 3 deletions(-)

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



More information about the flang-commits mailing list