[flang-commits] [clang] [flang] [flang][cuda] Add -fcuda option (PR #84944)

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Tue Mar 12 14:10:05 PDT 2024


https://github.com/clementval updated https://github.com/llvm/llvm-project/pull/84944

>From e42e8fe7f1dfe503a6735ef76e9d6483f5c9b5ec Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Tue, 12 Mar 2024 09:33:17 -0700
Subject: [PATCH 1/2] [flang][cuda] Add -fcuda option

---
 clang/include/clang/Driver/Options.td     |  3 +++
 clang/lib/Driver/ToolChains/Flang.cpp     |  1 +
 flang/lib/Frontend/CompilerInvocation.cpp |  6 ++++++
 flang/lib/Frontend/FrontendAction.cpp     | 11 ++++++++---
 flang/test/Driver/cuda-option.f90         | 13 +++++++++++++
 flang/test/Driver/driver-help-hidden.f90  |  1 +
 flang/test/Driver/driver-help.f90         |  2 ++
 7 files changed, 34 insertions(+), 3 deletions(-)
 create mode 100644 flang/test/Driver/cuda-option.f90

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index aca8c9b0d5487a..bd28ec90bf7283 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -6488,6 +6488,9 @@ defm stack_arrays : BoolOptionWithoutMarshalling<"f", "stack-arrays",
 defm loop_versioning : BoolOptionWithoutMarshalling<"f", "version-loops-for-stride",
   PosFlag<SetTrue, [], [ClangOption], "Create unit-strided versions of loops">,
    NegFlag<SetFalse, [], [ClangOption], "Do not create unit-strided loops (default)">>;
+
+def fcuda : Flag<["-"], "fcuda">, Group<f_Group>,
+  HelpText<"Enable CUDA">;
 } // let Visibility = [FC1Option, FlangOption]
 
 def J : JoinedOrSeparate<["-"], "J">,
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 6168b42dc78292..9b47ab0e7fcbf3 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -41,6 +41,7 @@ void Flang::addFortranDialectOptions(const ArgList &Args,
                             options::OPT_fopenmp,
                             options::OPT_fopenmp_version_EQ,
                             options::OPT_fopenacc,
+                            options::OPT_fcuda,
                             options::OPT_finput_charset_EQ,
                             options::OPT_fimplicit_none,
                             options::OPT_fno_implicit_none,
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 4707de0e976ca7..435f1df152c40d 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -877,6 +877,12 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
   if (args.hasArg(clang::driver::options::OPT_flarge_sizes))
     res.getDefaultKinds().set_sizeIntegerKind(8);
 
+  // -fcuda
+  if (args.hasArg(clang::driver::options::OPT_fcuda)) {
+    res.getFrontendOpts().features.Enable(
+        Fortran::common::LanguageFeature::CUDA);
+  }
+
   // -fopenmp and -fopenacc
   if (args.hasArg(clang::driver::options::OPT_fopenacc)) {
     res.getFrontendOpts().features.Enable(
diff --git a/flang/lib/Frontend/FrontendAction.cpp b/flang/lib/Frontend/FrontendAction.cpp
index 599b4e11f0cfbd..bb1c239540d9f5 100644
--- a/flang/lib/Frontend/FrontendAction.cpp
+++ b/flang/lib/Frontend/FrontendAction.cpp
@@ -86,9 +86,14 @@ bool FrontendAction::beginSourceFile(CompilerInstance &ci,
     invoc.collectMacroDefinitions();
   }
 
-  // Enable CUDA Fortran if source file is *.cuf/*.CUF.
-  invoc.getFortranOpts().features.Enable(Fortran::common::LanguageFeature::CUDA,
-                                         getCurrentInput().getIsCUDAFortran());
+  if (!invoc.getFortranOpts().features.IsEnabled(
+          Fortran::common::LanguageFeature::CUDA)) {
+    // Enable CUDA Fortran if source file is *.cuf/*.CUF and not already
+    // enabled.
+    invoc.getFortranOpts().features.Enable(
+        Fortran::common::LanguageFeature::CUDA,
+        getCurrentInput().getIsCUDAFortran());
+  }
 
   // Decide between fixed and free form (if the user didn't express any
   // preference, use the file extension to decide)
diff --git a/flang/test/Driver/cuda-option.f90 b/flang/test/Driver/cuda-option.f90
new file mode 100644
index 00000000000000..7bd1b3ddbffc3c
--- /dev/null
+++ b/flang/test/Driver/cuda-option.f90
@@ -0,0 +1,13 @@
+! Test -fcuda option
+! RUN: %flang -fc1 -cpp -fcuda -fdebug-unparse %s -o - | FileCheck %s
+
+program main
+#if _CUDA
+  integer :: var = _CUDA
+#endif
+  integer, device :: dvar
+end program
+
+! CHECK-LABEL: PROGRAM main
+! CHECK: INTEGER :: var = 1
+! CHECK: INTEGER, DEVICE :: dvar
diff --git a/flang/test/Driver/driver-help-hidden.f90 b/flang/test/Driver/driver-help-hidden.f90
index 44dbac44772b29..7b2e28263a825a 100644
--- a/flang/test/Driver/driver-help-hidden.f90
+++ b/flang/test/Driver/driver-help-hidden.f90
@@ -32,6 +32,7 @@
 ! CHECK-NEXT: -fbackslash             Specify that backslash in string introduces an escape character
 ! CHECK-NEXT: -fcolor-diagnostics     Enable colors in diagnostics
 ! CHECK-NEXT: -fconvert=<value>       Set endian conversion of data for unformatted files
+! CHECK-NEXT: -fcuda                  Enable CUDA
 ! CHECK-NEXT: -fdefault-double-8      Set the default double precision kind to an 8 byte wide type
 ! CHECK-NEXT: -fdefault-integer-8     Set the default integer and logical kind to an 8 byte wide type
 ! CHECK-NEXT: -fdefault-real-8        Set the default real kind to an 8 byte wide type
diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90
index b4280a454e3128..dd8a7573375d7a 100644
--- a/flang/test/Driver/driver-help.f90
+++ b/flang/test/Driver/driver-help.f90
@@ -28,6 +28,7 @@
 ! HELP-NEXT: -fbackslash             Specify that backslash in string introduces an escape character
 ! HELP-NEXT: -fcolor-diagnostics     Enable colors in diagnostics
 ! HELP-NEXT: -fconvert=<value>       Set endian conversion of data for unformatted files
+! HELP-NEXT: -fcuda                  Enable CUDA 
 ! HELP-NEXT: -fdefault-double-8      Set the default double precision kind to an 8 byte wide type
 ! HELP-NEXT: -fdefault-integer-8     Set the default integer and logical kind to an 8 byte wide type
 ! HELP-NEXT: -fdefault-real-8        Set the default real kind to an 8 byte wide type
@@ -165,6 +166,7 @@
 ! HELP-FC1-NEXT: -fbackslash             Specify that backslash in string introduces an escape character
 ! HELP-FC1-NEXT: -fcolor-diagnostics     Enable colors in diagnostics
 ! HELP-FC1-NEXT: -fconvert=<value>       Set endian conversion of data for unformatted files
+! HELP-FC1-NEXT: -fcuda                  Enable CUDA
 ! HELP-FC1-NEXT: -fdebug-dump-all        Dump symbols and the parse tree after the semantic checks
 ! HELP-FC1-NEXT: -fdebug-dump-parse-tree-no-sema
 ! HELP-FC1-NEXT:                         Dump the parse tree (skips the semantic checks)

>From 3194ed394f5a9baf5a3b0408463afc0d4eb837d1 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Tue, 12 Mar 2024 14:09:53 -0700
Subject: [PATCH 2/2] fall back to -x cuda

---
 clang/include/clang/Driver/Options.td     | 3 ---
 clang/lib/Driver/ToolChains/Flang.cpp     | 1 -
 flang/lib/Frontend/CompilerInvocation.cpp | 7 +++++--
 flang/test/Driver/cuda-option.f90         | 6 ++++--
 flang/test/Driver/driver-help-hidden.f90  | 1 -
 flang/test/Driver/driver-help.f90         | 2 --
 6 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index bd28ec90bf7283..aca8c9b0d5487a 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -6488,9 +6488,6 @@ defm stack_arrays : BoolOptionWithoutMarshalling<"f", "stack-arrays",
 defm loop_versioning : BoolOptionWithoutMarshalling<"f", "version-loops-for-stride",
   PosFlag<SetTrue, [], [ClangOption], "Create unit-strided versions of loops">,
    NegFlag<SetFalse, [], [ClangOption], "Do not create unit-strided loops (default)">>;
-
-def fcuda : Flag<["-"], "fcuda">, Group<f_Group>,
-  HelpText<"Enable CUDA">;
 } // let Visibility = [FC1Option, FlangOption]
 
 def J : JoinedOrSeparate<["-"], "J">,
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 9b47ab0e7fcbf3..6168b42dc78292 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -41,7 +41,6 @@ void Flang::addFortranDialectOptions(const ArgList &Args,
                             options::OPT_fopenmp,
                             options::OPT_fopenmp_version_EQ,
                             options::OPT_fopenacc,
-                            options::OPT_fcuda,
                             options::OPT_finput_charset_EQ,
                             options::OPT_fimplicit_none,
                             options::OPT_fno_implicit_none,
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 435f1df152c40d..2e3fa1f6e66039 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -581,6 +581,8 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
                 // pre-processed inputs.
                 .Case("f95", Language::Fortran)
                 .Case("f95-cpp-input", Language::Fortran)
+                // CUDA Fortran
+                .Case("cuda", Language::Fortran)
                 .Default(Language::Unknown);
 
     // Flang's intermediate representations.
@@ -877,8 +879,9 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
   if (args.hasArg(clang::driver::options::OPT_flarge_sizes))
     res.getDefaultKinds().set_sizeIntegerKind(8);
 
-  // -fcuda
-  if (args.hasArg(clang::driver::options::OPT_fcuda)) {
+  // -x cuda
+  auto language = args.getLastArgValue(clang::driver::options::OPT_x);
+  if (language.equals("cuda")) {
     res.getFrontendOpts().features.Enable(
         Fortran::common::LanguageFeature::CUDA);
   }
diff --git a/flang/test/Driver/cuda-option.f90 b/flang/test/Driver/cuda-option.f90
index 7bd1b3ddbffc3c..112e1cb6c77f8c 100644
--- a/flang/test/Driver/cuda-option.f90
+++ b/flang/test/Driver/cuda-option.f90
@@ -1,6 +1,6 @@
 ! Test -fcuda option
-! RUN: %flang -fc1 -cpp -fcuda -fdebug-unparse %s -o - | FileCheck %s
-
+! RUN: %flang -fc1 -cpp -x cuda -fdebug-unparse %s -o - | FileCheck %s
+! RUN: not %flang -fc1 -cpp %s -o - 2>&1 | FileCheck %s --check-prefix=ERROR
 program main
 #if _CUDA
   integer :: var = _CUDA
@@ -11,3 +11,5 @@ program main
 ! CHECK-LABEL: PROGRAM main
 ! CHECK: INTEGER :: var = 1
 ! CHECK: INTEGER, DEVICE :: dvar
+
+! ERROR: cuda-option.f90:8:19: error: expected end of statement
diff --git a/flang/test/Driver/driver-help-hidden.f90 b/flang/test/Driver/driver-help-hidden.f90
index 7b2e28263a825a..44dbac44772b29 100644
--- a/flang/test/Driver/driver-help-hidden.f90
+++ b/flang/test/Driver/driver-help-hidden.f90
@@ -32,7 +32,6 @@
 ! CHECK-NEXT: -fbackslash             Specify that backslash in string introduces an escape character
 ! CHECK-NEXT: -fcolor-diagnostics     Enable colors in diagnostics
 ! CHECK-NEXT: -fconvert=<value>       Set endian conversion of data for unformatted files
-! CHECK-NEXT: -fcuda                  Enable CUDA
 ! CHECK-NEXT: -fdefault-double-8      Set the default double precision kind to an 8 byte wide type
 ! CHECK-NEXT: -fdefault-integer-8     Set the default integer and logical kind to an 8 byte wide type
 ! CHECK-NEXT: -fdefault-real-8        Set the default real kind to an 8 byte wide type
diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90
index dd8a7573375d7a..b4280a454e3128 100644
--- a/flang/test/Driver/driver-help.f90
+++ b/flang/test/Driver/driver-help.f90
@@ -28,7 +28,6 @@
 ! HELP-NEXT: -fbackslash             Specify that backslash in string introduces an escape character
 ! HELP-NEXT: -fcolor-diagnostics     Enable colors in diagnostics
 ! HELP-NEXT: -fconvert=<value>       Set endian conversion of data for unformatted files
-! HELP-NEXT: -fcuda                  Enable CUDA 
 ! HELP-NEXT: -fdefault-double-8      Set the default double precision kind to an 8 byte wide type
 ! HELP-NEXT: -fdefault-integer-8     Set the default integer and logical kind to an 8 byte wide type
 ! HELP-NEXT: -fdefault-real-8        Set the default real kind to an 8 byte wide type
@@ -166,7 +165,6 @@
 ! HELP-FC1-NEXT: -fbackslash             Specify that backslash in string introduces an escape character
 ! HELP-FC1-NEXT: -fcolor-diagnostics     Enable colors in diagnostics
 ! HELP-FC1-NEXT: -fconvert=<value>       Set endian conversion of data for unformatted files
-! HELP-FC1-NEXT: -fcuda                  Enable CUDA
 ! HELP-FC1-NEXT: -fdebug-dump-all        Dump symbols and the parse tree after the semantic checks
 ! HELP-FC1-NEXT: -fdebug-dump-parse-tree-no-sema
 ! HELP-FC1-NEXT:                         Dump the parse tree (skips the semantic checks)



More information about the flang-commits mailing list