[flang-commits] [flang] [flang][cuda] Decouple CUFInit from CUDA language features (PR #209846)

via flang-commits flang-commits at lists.llvm.org
Wed Jul 15 10:53:28 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-fir-hlfir

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

<details>
<summary>Changes</summary>

In some cases, we do not want to emit the CUFInit function in the main. Add a specific language feature for it so we can decouple it. 

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


5 Files Affected:

- (modified) flang/include/flang/Support/Fortran-features.h (+1-1) 
- (modified) flang/lib/Lower/Bridge.cpp (+4-2) 
- (modified) flang/lib/Support/Fortran-features.cpp (+1) 
- (modified) flang/test/Lower/CUDA/cuda-init.cuf (+2-1) 
- (modified) flang/tools/bbc/bbc.cpp (+7) 


``````````diff
diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index be4c60afb1cc7..9bbbb7dd5f899 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -61,7 +61,7 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
     MultipleProgramUnitsOnSameLine, AllocatedForAssociated,
     OpenMPThreadprivateEquivalence, RelaxedCLocChecks, CudaPinned,
     OpenAccDefaultNoneScalarsStrict, OpenACCMultipleNamesInRoutine,
-    EnumerationType)
+    EnumerationType, CUDAInit)
 
 // Portability and suspicious usage warnings
 ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index e2c895351c977..2cce44cdc8a70 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -618,8 +618,10 @@ class FirConverter : public Fortran::lower::AbstractConverter {
       createBuilderOutsideOfFuncOpAndDo([&]() {
         fir::runtime::genMain(*builder, toLocation(),
                               bridge.getEnvironmentDefaults(),
-                              getFoldingContext().languageFeatures().IsEnabled(
-                                  Fortran::common::LanguageFeature::CUDA),
+                              (getFoldingContext().languageFeatures().IsEnabled(
+                                   Fortran::common::LanguageFeature::CUDA) &&
+                               getFoldingContext().languageFeatures().IsEnabled(
+                                   Fortran::common::LanguageFeature::CUDAInit)),
                               getFoldingContext().languageFeatures().IsEnabled(
                                   Fortran::common::LanguageFeature::Coarray));
       });
diff --git a/flang/lib/Support/Fortran-features.cpp b/flang/lib/Support/Fortran-features.cpp
index b9f4c3a804167..d94729200df06 100644
--- a/flang/lib/Support/Fortran-features.cpp
+++ b/flang/lib/Support/Fortran-features.cpp
@@ -135,6 +135,7 @@ LanguageFeatureControl::LanguageFeatureControl() {
   disable_.set(LanguageFeature::CudaManaged);
   disable_.set(LanguageFeature::CudaUnified);
   disable_.set(LanguageFeature::CudaPinned);
+  disable_.set(LanguageFeature::CUDAInit);
   disable_.set(LanguageFeature::ImplicitNoneTypeNever);
   disable_.set(LanguageFeature::ImplicitNoneTypeAlways);
   disable_.set(LanguageFeature::ImplicitNoneExternal);
diff --git a/flang/test/Lower/CUDA/cuda-init.cuf b/flang/test/Lower/CUDA/cuda-init.cuf
index 7f58ec3c87dbf..ab5ef13916c1f 100644
--- a/flang/test/Lower/CUDA/cuda-init.cuf
+++ b/flang/test/Lower/CUDA/cuda-init.cuf
@@ -1,4 +1,5 @@
-! RUN: bbc -emit-fir -fcuda %s -o - | FileCheck %s --check-prefixes=ALL,CUDA
+! RUN: bbc -emit-fir -fcuda -fcuda-init %s -o - | FileCheck %s --check-prefixes=ALL,CUDA
+! RUN: bbc -emit-fir -fcuda %s -o - | FileCheck %s --check-prefixes=ALL,NOCUDA
 ! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes=ALL,NOCUDA
 
 program test_init
diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp
index 124c566bc6b13..e1017a0005b50 100644
--- a/flang/tools/bbc/bbc.cpp
+++ b/flang/tools/bbc/bbc.cpp
@@ -230,6 +230,10 @@ static llvm::cl::opt<bool> enableCUDA("fcuda",
                                       llvm::cl::desc("enable CUDA Fortran"),
                                       llvm::cl::init(false));
 
+static llvm::cl::opt<bool> enableCUDAInit("fcuda-init",
+                                          llvm::cl::desc("enable CUDA Init"),
+                                          llvm::cl::init(false));
+
 static llvm::cl::opt<bool>
     enableDoConcurrentOffload("fdoconcurrent-offload",
                               llvm::cl::desc("enable do concurrent offload"),
@@ -685,6 +689,9 @@ int main(int argc, char **argv) {
   if (enableCUDA) {
     options.features.Enable(Fortran::common::LanguageFeature::CUDA);
   }
+  if (enableCUDAInit) {
+    options.features.Enable(Fortran::common::LanguageFeature::CUDAInit);
+  }
 
   if (enableDoConcurrentOffload) {
     options.features.Enable(

``````````

</details>


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


More information about the flang-commits mailing list