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

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Wed Jan 29 10:20:14 PST 2025


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

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. 

>From 4e4a75c14be2eb735d9491981b18cf77af539891 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Wed, 29 Jan 2025 10:15:54 -0800
Subject: [PATCH] [flang][cuda] Remove the need of special definition for
 CUFInit

---
 flang/CMakeLists.txt                         |  1 -
 flang/include/flang/Runtime/CUDA/init.h      |  1 -
 flang/lib/Optimizer/Builder/Runtime/Main.cpp |  6 +-----
 flang/test/Lower/CUDA/cuda-init.cuf          | 11 +++++++++++
 4 files changed, 12 insertions(+), 7 deletions(-)
 create mode 100644 flang/test/Lower/CUDA/cuda-init.cuf

diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index fb7ab4759ad37e5..2e27bc2279ac479 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 24bc6838227208c..d1c8fcbf7d58758 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 5156fd540207779..973744837d378aa 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 000000000000000..71ec1e3f9df56d7
--- /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> : () -> ()



More information about the flang-commits mailing list