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][driver] support -dumpversion and -dumpmachine #68896

Merged
merged 1 commit into from
Oct 24, 2023

Conversation

yuanfang-chen
Copy link
Collaborator

Match GCC driver. GCC has -cc1/-fc1 support too, but this patch does not address that.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category labels Oct 12, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 12, 2023

@llvm/pr-subscribers-clang
@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-flang-driver

Author: Yuanfang Chen (yuanfang-chen)

Changes

Match GCC driver. GCC has -cc1/-fc1 support too, but this patch does not address that.


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

7 Files Affected:

  • (modified) clang/include/clang/Driver/Driver.h (+6-1)
  • (modified) clang/include/clang/Driver/Options.td (+6-2)
  • (modified) clang/lib/Driver/Driver.cpp (+5-4)
  • (modified) flang/test/Driver/driver-help-hidden.f90 (+2)
  • (modified) flang/test/Driver/driver-help.f90 (+2)
  • (added) flang/test/Driver/dumpmachine.f90 (+8)
  • (added) flang/test/Driver/immediate-options.f90 (+2)
diff --git a/clang/include/clang/Driver/Driver.h b/clang/include/clang/Driver/Driver.h
index 3ee1bcf2a69c9bd..fdb8aaf3572ba31 100644
--- a/clang/include/clang/Driver/Driver.h
+++ b/clang/include/clang/Driver/Driver.h
@@ -12,6 +12,7 @@
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/HeaderInclude.h"
 #include "clang/Basic/LLVM.h"
+#include "clang/Basic/Version.h"
 #include "clang/Driver/Action.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "clang/Driver/InputInfo.h"
@@ -188,6 +189,9 @@ class Driver {
   /// Driver title to use with help.
   std::string DriverTitle;
 
+  /// Driver version.
+  std::string DriverVersion;
+
   /// Information about the host which can be overridden by the user.
   std::string HostBits, HostMachine, HostSystem, HostRelease;
 
@@ -373,7 +377,8 @@ class Driver {
 
   Driver(StringRef ClangExecutable, StringRef TargetTriple,
          DiagnosticsEngine &Diags, std::string Title = "clang LLVM compiler",
-         IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
+         IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr,
+         std::string Version = CLANG_VERSION_STRING);
 
   /// @name Accessors
   /// @{
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index c272a7f1c398aa6..cae7bd07fc3cc54 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1375,9 +1375,13 @@ def dsym_dir : JoinedOrSeparate<["-"], "dsym-dir">,
 def dumpdir : Separate<["-"], "dumpdir">, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"<dumppfx>">,
   HelpText<"Use <dumpfpx> as a prefix to form auxiliary and dump file names">;
-def dumpmachine : Flag<["-"], "dumpmachine">;
+def dumpmachine : Flag<["-"], "dumpmachine">,
+  Visibility<[ClangOption, FlangOption]>,
+  HelpText<"Display the compiler's target processor">;
+def dumpversion : Flag<["-"], "dumpversion">,
+  Visibility<[ClangOption, FlangOption]>,
+  HelpText<"Display the version of the compiler">;
 def dumpspecs : Flag<["-"], "dumpspecs">, Flags<[Unsupported]>;
-def dumpversion : Flag<["-"], "dumpversion">;
 def dylib__file : Separate<["-"], "dylib_file">;
 def dylinker__install__name : JoinedOrSeparate<["-"], "dylinker_install_name">;
 def dylinker : Flag<["-"], "dylinker">;
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 77328e1f99e5021..c84acb2beb17a70 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -190,14 +190,15 @@ std::string Driver::GetResourcesPath(StringRef BinaryPath,
 
 Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
                DiagnosticsEngine &Diags, std::string Title,
-               IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
+               IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
+               std::string Version)
     : Diags(Diags), VFS(std::move(VFS)), Mode(GCCMode),
       SaveTemps(SaveTempsNone), BitcodeEmbed(EmbedNone),
       Offload(OffloadHostDevice), CXX20HeaderType(HeaderMode_None),
       ModulesModeCXX20(false), LTOMode(LTOK_None),
       ClangExecutable(ClangExecutable), SysRoot(DEFAULT_SYSROOT),
-      DriverTitle(Title), CCCPrintBindings(false), CCPrintOptions(false),
-      CCLogDiagnostics(false), CCGenDiagnostics(false),
+      DriverTitle(Title), DriverVersion(Version), CCCPrintBindings(false),
+      CCPrintOptions(false), CCLogDiagnostics(false), CCGenDiagnostics(false),
       CCPrintProcessStats(false), CCPrintInternalStats(false),
       TargetTriple(TargetTriple), Saver(Alloc), PrependArg(nullptr),
       CheckInputsExist(true), ProbePrecompiled(true),
@@ -2081,7 +2082,7 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
   if (C.getArgs().hasArg(options::OPT_dumpversion)) {
     // Since -dumpversion is only implemented for pedantic GCC compatibility, we
     // return an answer which matches our definition of __VERSION__.
-    llvm::outs() << CLANG_VERSION_STRING << "\n";
+    llvm::outs() << DriverVersion << "\n";
     return false;
   }
 
diff --git a/flang/test/Driver/driver-help-hidden.f90 b/flang/test/Driver/driver-help-hidden.f90
index 807b0f938d27b5c..caea8880ba8fb8e 100644
--- a/flang/test/Driver/driver-help-hidden.f90
+++ b/flang/test/Driver/driver-help-hidden.f90
@@ -21,6 +21,8 @@
 ! CHECK-NEXT: -ccc-print-phases       Dump list of actions to perform
 ! CHECK-NEXT: -cpp                    Enable predefined and command line preprocessor macros
 ! CHECK-NEXT: -c                      Only run preprocess, compile, and assemble steps
+! CHECK-NEXT: -dumpmachine            Display the compiler's target processor
+! CHECK-NEXT: -dumpversion            Display the version of the compiler
 ! CHECK-NEXT: -D <macro>=<value>      Define <macro> to <value> (or 1 if <value> omitted)
 ! CHECK-NEXT: -emit-llvm              Use the LLVM representation for assembler and object files
 ! CHECK-NEXT: -E                      Only run the preprocessor
diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90
index 4894f90f5310439..1580c267cfc6ae6 100644
--- a/flang/test/Driver/driver-help.f90
+++ b/flang/test/Driver/driver-help.f90
@@ -17,6 +17,8 @@
 ! HELP-NEXT: -###                    Print (but do not run) the commands to run for this compilation
 ! HELP-NEXT: -cpp                    Enable predefined and command line preprocessor macros
 ! HELP-NEXT: -c                      Only run preprocess, compile, and assemble steps
+! HELP-NEXT: -dumpmachine            Display the compiler's target processor
+! HELP-NEXT: -dumpversion            Display the version of the compiler
 ! HELP-NEXT: -D <macro>=<value>      Define <macro> to <value> (or 1 if <value> omitted)
 ! HELP-NEXT: -emit-llvm              Use the LLVM representation for assembler and object files
 ! HELP-NEXT: -E                      Only run the preprocessor
diff --git a/flang/test/Driver/dumpmachine.f90 b/flang/test/Driver/dumpmachine.f90
new file mode 100644
index 000000000000000..b68705707eefa04
--- /dev/null
+++ b/flang/test/Driver/dumpmachine.f90
@@ -0,0 +1,8 @@
+! Test that -dumpmachine prints the target triple.
+
+! Note: Debian GCC may omit "unknown-".
+! RUN: %flang --target=x86_64-linux-gnu -dumpmachine | FileCheck %s --check-prefix=X86_64
+! X86_64: x86_64-unknown-linux-gnu
+
+! RUN: %flang --target=xxx-pc-freebsd -dumpmachine | FileCheck %s --check-prefix=FREEBSD
+! FREEBSD: xxx-pc-freebsd
diff --git a/flang/test/Driver/immediate-options.f90 b/flang/test/Driver/immediate-options.f90
new file mode 100644
index 000000000000000..81c1e7181ba7935
--- /dev/null
+++ b/flang/test/Driver/immediate-options.f90
@@ -0,0 +1,2 @@
+! RUN: %flang -dumpversion | FileCheck %s -check-prefix=DUMPVERSION
+! DUMPVERSION: {{[0-9]+\.[0-9.]+}}

@yuanfang-chen yuanfang-chen requested review from klausler and removed request for banach-space October 17, 2023 02:59
@klausler klausler requested review from banach-space and removed request for klausler October 17, 2023 15:10
Copy link
Contributor

@banach-space banach-space left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be implemented without any updates to Clang (beyond Options.td)?

clang/lib/Driver/Driver.cpp Outdated Show resolved Hide resolved
@yuanfang-chen
Copy link
Collaborator Author

Could this be implemented without any updates to Clang (beyond Options.td)?

The options could work by changing options.td only. However, CLANG_VERSION_STRING and FLANG_VERSION_STRING could be different. This is to handle that.

@banach-space
Copy link
Contributor

banach-space commented Oct 18, 2023

Could this be implemented without any updates to Clang (beyond Options.td)?

The options could work by changing options.td only. However, CLANG_VERSION_STRING and FLANG_VERSION_STRING could be different. This is to handle that.

You are adding a support for these new flags in flang-new, which is implemented in terms of clangDriver - this scenario (i.e. the two projects being out of sync) would be very unlikely at the moment (perhaps even impossible). And even if that was desired, you'd need to update Flang's driver too. I'd rather keep this to the required minimum. If you really want to keep this, you will need to update flang/tools/flang-driver/driver.cpp too.

Match GCC driver. GCC has -cc1/-fc1 support too, but this patch does not address that.
@yuanfang-chen
Copy link
Collaborator Author

You are adding a support for these new flags in flang-new, which is implemented in terms of clangDriver - this scenario (i.e. the two projects being out of sync) would be very unlikely at the moment (perhaps even impossible). And even if that was desired, you'd need to update Flang's driver too. I'd rather keep this to the required minimum. If you really want to keep this, you will need to update flang/tools/flang-driver/driver.cpp too.

Thanks for the explanation. I've updated the patch.

Copy link
Contributor

@banach-space banach-space left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@yuanfang-chen yuanfang-chen merged commit 1072fcd into llvm:main Oct 24, 2023
3 checks passed
@yuanfang-chen yuanfang-chen deleted the flang-dumpversion branch November 1, 2023 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category flang:driver flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants