[flang-commits] [flang] [flang][cuda] Remove the need of special definition for CUFInit (PR #124965)

via flang-commits flang-commits at lists.llvm.org
Wed Jan 29 10:20:54 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

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

<details>
<summary>Changes</summary>

This patch addresses post commit review comments from #<!-- -->124859. 

The extra compile definition is not necessary and goes against the effort to separate the runtimes from the flang compiler itself. The function declaration for `CUFInit` can be accessed anyway since the header are always present.

A program compiled with cuda enabled but no cufruntime would just fail at link time as expected. 

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


4 Files Affected:

- (modified) flang/CMakeLists.txt (-1) 
- (modified) flang/include/flang/Runtime/CUDA/init.h (-1) 
- (modified) flang/lib/Optimizer/Builder/Runtime/Main.cpp (+1-5) 
- (added) flang/test/Lower/CUDA/cuda-init.cuf (+11) 


``````````diff
diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index fb7ab4759ad37e..2e27bc2279ac47 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -475,7 +475,6 @@ option(FLANG_CUF_RUNTIME
   "Compile CUDA Fortran runtime sources" OFF)
 if (FLANG_CUF_RUNTIME)
   find_package(CUDAToolkit REQUIRED)
-  add_compile_definitions(FLANG_CUDA_SUPPORT=1)
 endif()
 
 add_subdirectory(include)
diff --git a/flang/include/flang/Runtime/CUDA/init.h b/flang/include/flang/Runtime/CUDA/init.h
index 24bc6838227208..d1c8fcbf7d5875 100644
--- a/flang/include/flang/Runtime/CUDA/init.h
+++ b/flang/include/flang/Runtime/CUDA/init.h
@@ -9,7 +9,6 @@
 #ifndef FORTRAN_RUNTIME_CUDA_INIT_H_
 #define FORTRAN_RUNTIME_CUDA_INIT_H_
 
-#include "common.h"
 #include "flang/Runtime/entry-names.h"
 
 extern "C" {
diff --git a/flang/lib/Optimizer/Builder/Runtime/Main.cpp b/flang/lib/Optimizer/Builder/Runtime/Main.cpp
index 5156fd54020777..973744837d378a 100644
--- a/flang/lib/Optimizer/Builder/Runtime/Main.cpp
+++ b/flang/lib/Optimizer/Builder/Runtime/Main.cpp
@@ -14,11 +14,9 @@
 #include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
 #include "flang/Optimizer/Dialect/FIROps.h"
 #include "flang/Optimizer/Dialect/FIRType.h"
+#include "flang/Runtime/CUDA/init.h"
 #include "flang/Runtime/main.h"
 #include "flang/Runtime/stop.h"
-#ifdef FLANG_CUDA_SUPPORT
-#include "flang/Runtime/CUDA/init.h"
-#endif
 
 using namespace Fortran::runtime;
 
@@ -66,13 +64,11 @@ void fir::runtime::genMain(
 
   builder.create<fir::CallOp>(loc, startFn, args);
 
-#ifdef FLANG_CUDA_SUPPORT
   if (initCuda) {
     auto initFn = builder.createFunction(
         loc, RTNAME_STRING(CUFInit), mlir::FunctionType::get(context, {}, {}));
     builder.create<fir::CallOp>(loc, initFn);
   }
-#endif
 
   builder.create<fir::CallOp>(loc, qqMainFn);
   builder.create<fir::CallOp>(loc, stopFn);
diff --git a/flang/test/Lower/CUDA/cuda-init.cuf b/flang/test/Lower/CUDA/cuda-init.cuf
new file mode 100644
index 00000000000000..71ec1e3f9df56d
--- /dev/null
+++ b/flang/test/Lower/CUDA/cuda-init.cuf
@@ -0,0 +1,11 @@
+! RUN: bbc -emit-fir -hlfir -fcuda %s -o - | FileCheck %s --check-prefixes=ALL,CUDA
+! RUN: bbc -emit-fir -hlfir %s -o - | FileCheck %s --check-prefixes=ALL,NOCUDA
+
+program test_init
+
+end 
+
+! ALL-LABEL: func.func @main
+! ALL: fir.call @_FortranAProgramStart
+! CUDA: fir.call @_FortranACUFInit() fastmath<contract> : () -> ()
+! NOCUDA-NOT: fir.call @_FortranACUFInit() fastmath<contract> : () -> ()

``````````

</details>


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


More information about the flang-commits mailing list