Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang][cuda] Remove the need of special compile definition for CUFInit #124965

Merged
merged 1 commit into from
Jan 29, 2025

Conversation

clementval
Copy link
Contributor

@clementval clementval commented Jan 29, 2025

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. The insertion of the call is only based on the language feature options from the folding context.

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

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Jan 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 29, 2025

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

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

Changes

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 --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> : () -> ()

@clementval clementval changed the title [flang][cuda] Remove the need of special definition for CUFInit [flang][cuda] Remove the need of special compile definition for CUFInit Jan 29, 2025
@clementval clementval merged commit ab1ee91 into llvm:main Jan 29, 2025
11 checks passed
@clementval clementval deleted the cuf_remove_def branch January 29, 2025 21:12
@Meinersbur
Copy link
Member

Please allow for the reviewers that had concerns to confirm whether they have been addressed.

@clementval
Copy link
Contributor Author

Please allow for the reviewers that had concerns to confirm whether they have been addressed.

Is it addressed?

@Meinersbur
Copy link
Member

Yes. I still think it would have been cleaner to revert first. Now

if (FLANG_CUF_RUNTIME)
  find_package(CUDAToolkit REQUIRED)
endif()

has moved from line 484 to line 476 for no reason.

@clementval
Copy link
Contributor Author

Yes. I still think it would have been cleaner to revert first. Now

if (FLANG_CUF_RUNTIME)
  find_package(CUDAToolkit REQUIRED)
endif()

has moved from line 484 to line 476 for no reason.

I can move it back with an NFC if you prefer it to stay at the same place.

@Meinersbur
Copy link
Member

Up to you, I already fixed the merge conflict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants