[flang-commits] [flang] [flang][runtime] Use cuda::std::variant in the CUDA build. (PR #86615)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Mon Mar 25 19:00:38 PDT 2024


https://github.com/vzakhari created https://github.com/llvm/llvm-project/pull/86615

Added `FLANG_LIBCUDACXX_PATH` CMake variable to specify
installation of header-only libcudacxx library.
If it is specified, the `<cuda/std/variant>` is used to provide
implementation of `std::variant`.


>From 5278237bfff148e982b0934277e8bddeb67a56cf Mon Sep 17 00:00:00 2001
From: Slava Zakharin <szakharin at nvidia.com>
Date: Mon, 25 Mar 2024 17:58:55 -0700
Subject: [PATCH] [flang][runtime] Use cuda::std::variant in the CUDA build.

Added `FLANG_LIBCUDACXX_PATH` CMake variable to specify
installation of header-only libcudacxx library.
If it is specified, the `<cuda/std/variant>` is used to provide
implementation of `std::variant`.
---
 flang/include/flang/Common/idioms.h   |  2 +-
 flang/include/flang/Common/template.h |  2 +-
 flang/include/flang/Common/unwrap.h   |  2 +-
 flang/include/flang/Common/variant.h  | 30 +++++++++++++++++++++++++++
 flang/include/flang/Common/visit.h    |  2 +-
 flang/runtime/CMakeLists.txt          |  8 +++++++
 flang/runtime/io-stmt.h               |  2 +-
 flang/runtime/unit.cpp                |  6 ------
 flang/runtime/unit.h                  |  8 +------
 9 files changed, 44 insertions(+), 18 deletions(-)
 create mode 100644 flang/include/flang/Common/variant.h

diff --git a/flang/include/flang/Common/idioms.h b/flang/include/flang/Common/idioms.h
index f6c9cbbc0f7cd0..38a2bd9a449fd7 100644
--- a/flang/include/flang/Common/idioms.h
+++ b/flang/include/flang/Common/idioms.h
@@ -24,6 +24,7 @@
 #endif
 
 #include "enum-class.h"
+#include "variant.h"
 #include "visit.h"
 #include <array>
 #include <functional>
@@ -33,7 +34,6 @@
 #include <string>
 #include <tuple>
 #include <type_traits>
-#include <variant>
 
 #if __GNUC__ == 7
 // Avoid a deduction bug in GNU 7.x headers by forcing the answer.
diff --git a/flang/include/flang/Common/template.h b/flang/include/flang/Common/template.h
index 2ab3b8bce1df92..51d09fb42ce368 100644
--- a/flang/include/flang/Common/template.h
+++ b/flang/include/flang/Common/template.h
@@ -9,12 +9,12 @@
 #ifndef FORTRAN_COMMON_TEMPLATE_H_
 #define FORTRAN_COMMON_TEMPLATE_H_
 
+#include "variant.h"
 #include "flang/Common/idioms.h"
 #include <functional>
 #include <optional>
 #include <tuple>
 #include <type_traits>
-#include <variant>
 #include <vector>
 
 // Utility templates for metaprogramming and for composing the
diff --git a/flang/include/flang/Common/unwrap.h b/flang/include/flang/Common/unwrap.h
index edb343d77b5374..84582174e4b300 100644
--- a/flang/include/flang/Common/unwrap.h
+++ b/flang/include/flang/Common/unwrap.h
@@ -12,11 +12,11 @@
 #include "indirection.h"
 #include "reference-counted.h"
 #include "reference.h"
+#include "variant.h"
 #include "visit.h"
 #include <memory>
 #include <optional>
 #include <type_traits>
-#include <variant>
 
 // Given a nest of variants, optionals, &/or pointers, Unwrap<>() isolates
 // a packaged value of a specific type if it is present and returns a pointer
diff --git a/flang/include/flang/Common/variant.h b/flang/include/flang/Common/variant.h
new file mode 100644
index 00000000000000..1af85876afac00
--- /dev/null
+++ b/flang/include/flang/Common/variant.h
@@ -0,0 +1,30 @@
+//===-- include/flang/Common/variant.h --------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// A single way to expose C++ variant class in files that can be used
+// in F18 runtime build. With inclusion of this file std::variant
+// and the related names become available, though, they may correspond
+// to alternative definitions (e.g. from cuda::std namespace).
+
+#ifndef FORTRAN_COMMON_VARIANT_H
+#define FORTRAN_COMMON_VARIANT_H
+
+#if RT_USE_LIBCUDACXX
+#include <cuda/std/variant>
+namespace std {
+using cuda::std::get;
+using cuda::std::monostate;
+using cuda::std::variant;
+using cuda::std::variant_size_v;
+using cuda::std::visit;
+} // namespace std
+#else // !RT_USE_LIBCUDACXX
+#include <variant>
+#endif // !RT_USE_LIBCUDACXX
+
+#endif // FORTRAN_COMMON_VARIANT_H
diff --git a/flang/include/flang/Common/visit.h b/flang/include/flang/Common/visit.h
index 54f8ca70b313c7..4d0897301e01db 100644
--- a/flang/include/flang/Common/visit.h
+++ b/flang/include/flang/Common/visit.h
@@ -21,9 +21,9 @@
 #ifndef FORTRAN_COMMON_VISIT_H_
 #define FORTRAN_COMMON_VISIT_H_
 
+#include "variant.h"
 #include "flang/Common/api-attrs.h"
 #include <type_traits>
-#include <variant>
 
 namespace Fortran::common {
 namespace log2visit {
diff --git a/flang/runtime/CMakeLists.txt b/flang/runtime/CMakeLists.txt
index 335ef370727461..184d448989213a 100644
--- a/flang/runtime/CMakeLists.txt
+++ b/flang/runtime/CMakeLists.txt
@@ -173,6 +173,7 @@ set(sources
 option(FLANG_EXPERIMENTAL_CUDA_RUNTIME
   "Compile Fortran runtime as CUDA sources (experimental)" OFF
   )
+set(FLANG_LIBCUDACXX_PATH "" CACHE PATH "Path to libcu++ package installation")
 
 # List of files that are buildable for all devices.
 set(supported_files
@@ -259,6 +260,13 @@ if (FLANG_EXPERIMENTAL_CUDA_RUNTIME)
   set_source_files_properties(${supported_files} PROPERTIES COMPILE_OPTIONS
     "${CUDA_COMPILE_OPTIONS}"
     )
+
+  if (EXISTS "${FLANG_LIBCUDACXX_PATH}/include")
+    # When using libcudacxx headers files, we have to use them
+    # for all files of F18 runtime.
+    include_directories(AFTER ${FLANG_LIBCUDACXX_PATH}/include)
+    add_compile_definitions(RT_USE_LIBCUDACXX=1)
+  endif()
 endif()
 
 set(FLANG_EXPERIMENTAL_OMP_OFFLOAD_BUILD "off" CACHE STRING
diff --git a/flang/runtime/io-stmt.h b/flang/runtime/io-stmt.h
index e0dafa9c763c4f..8b5752311de5c3 100644
--- a/flang/runtime/io-stmt.h
+++ b/flang/runtime/io-stmt.h
@@ -21,9 +21,9 @@
 #include "flang/Common/visit.h"
 #include "flang/Runtime/descriptor.h"
 #include "flang/Runtime/io-api.h"
+#include <flang/Common/variant.h>
 #include <functional>
 #include <type_traits>
-#include <variant>
 
 namespace Fortran::runtime::io {
 
diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index b5aa307eade815..6c648d3bd83467 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -482,10 +482,7 @@ bool ExternalFileUnit::SetDirectRec(
 
 void ExternalFileUnit::EndIoStatement() {
   io_.reset();
-  RT_DIAG_PUSH
-  RT_DIAG_DISABLE_CALL_HOST_FROM_DEVICE_WARN
   u_.emplace<std::monostate>();
-  RT_DIAG_POP
   lock_.Drop();
 }
 
@@ -752,10 +749,7 @@ std::int32_t ExternalFileUnit::ReadHeaderOrFooter(std::int64_t frameOffset) {
 
 void ChildIo::EndIoStatement() {
   io_.reset();
-  RT_DIAG_PUSH
-  RT_DIAG_DISABLE_CALL_HOST_FROM_DEVICE_WARN
   u_.emplace<std::monostate>();
-  RT_DIAG_POP
 }
 
 Iostat ChildIo::CheckFormattingAndDirection(
diff --git a/flang/runtime/unit.h b/flang/runtime/unit.h
index 8b7db5cbc90b43..a6ee5971a16524 100644
--- a/flang/runtime/unit.h
+++ b/flang/runtime/unit.h
@@ -25,7 +25,7 @@
 #include "flang/Runtime/memory.h"
 #include <cstdlib>
 #include <cstring>
-#include <variant>
+#include <flang/Common/variant.h>
 
 namespace Fortran::runtime::io {
 
@@ -152,10 +152,7 @@ class ExternalFileUnit : public ConnectionState,
 #else
     lock_.Take();
 #endif
-    RT_DIAG_PUSH
-    RT_DIAG_DISABLE_CALL_HOST_FROM_DEVICE_WARN
     A &state{u_.emplace<A>(std::forward<X>(xs)...)};
-    RT_DIAG_POP
     if constexpr (!std::is_same_v<A, OpenStatementState>) {
       state.mutableModes() = ConnectionState::modes;
     }
@@ -265,10 +262,7 @@ class ChildIo {
 
   template <typename A, typename... X>
   RT_API_ATTRS IoStatementState &BeginIoStatement(X &&...xs) {
-    RT_DIAG_PUSH
-    RT_DIAG_DISABLE_CALL_HOST_FROM_DEVICE_WARN
     A &state{u_.emplace<A>(std::forward<X>(xs)...)};
-    RT_DIAG_POP
     io_.emplace(state);
     return *io_;
   }



More information about the flang-commits mailing list