]> granicus.if.org Git - clang/commitdiff
[Driver] Add option to manually control discarding value names in LLVM IR.
authorEric Fiselier <eric@efcs.ca>
Wed, 7 Feb 2018 18:36:51 +0000 (18:36 +0000)
committerEric Fiselier <eric@efcs.ca>
Wed, 7 Feb 2018 18:36:51 +0000 (18:36 +0000)
Summary:
Currently, assertion-disabled Clang builds emit value names when generating LLVM IR. This is controlled by the `NDEBUG` macro, and is not easily overridable. In order to get IR output containing names from a release build of Clang, the user must manually construct the CC1 invocation w/o the `-discard-value-names` option. This is less than ideal.

For example, Godbolt uses a release build of Clang, and so when asked to emit LLVM IR the result lacks names, making it harder to read. Manually invoking CC1 on Compiler Explorer is not feasible.

This patch adds the driver options `-fdiscard-value-names` and `-fno-discard-value-names` which allow the user to override the default behavior. If neither is specified, the old behavior remains.

Reviewers: erichkeane, aaron.ballman, lebedev.ri

Reviewed By: aaron.ballman

Subscribers: bogner, cfe-commits

Differential Revision: https://reviews.llvm.org/D42887

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@324498 91177308-0d34-0410-b5e6-96231b3b80d8

docs/UsersManual.rst
include/clang/Driver/Options.td
lib/Driver/ToolChains/Clang.cpp
test/Driver/clang_f_opts.c

index 84dfe9fc3d733bc9a42acf54eb6f313b54090973..09e9f9e6609f6a4e7a2592f6be35e2aed43eb8b7 100644 (file)
@@ -1855,6 +1855,27 @@ features. You can "tune" the debug info for one of several different debuggers.
   must come first.)
 
 
+Controlling LLVM IR Output
+--------------------------
+
+Controlling Value Names in LLVM IR
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Emitting value names in LLVM IR increases the size and verbosity of the IR.
+By default, value names are only emitted in assertion-enabled builds of Clang.
+However, when reading IR it can be useful to re-enable the emission of value
+names to improve readability.
+
+.. option:: -fdiscard-value-names
+
+  Discard value names when generating LLVM IR.
+
+.. option:: -fno-discard-value-names
+
+  Do not discard value names when generating LLVM IR. This option can be used
+  to re-enable names for release builds of Clang.
+
+
 Comment Parsing Options
 -----------------------
 
index f44f2d1bb0c33aa3444d5dc8ea8cc440db487b48..a64d6ad43176025a5467d5f5e749c3c105bbf67b 100644 (file)
@@ -790,6 +790,10 @@ def fdiagnostics_show_template_tree : Flag<["-"], "fdiagnostics-show-template-tr
     HelpText<"Print a template comparison tree for differing templates">;
 def fdeclspec : Flag<["-"], "fdeclspec">, Group<f_clang_Group>,
   HelpText<"Allow __declspec as a keyword">, Flags<[CC1Option]>;
+def fdiscard_value_names : Flag<["-"], "fdiscard-value-names">, Group<f_clang_Group>,
+  HelpText<"Discard value names in LLVM IR">, Flags<[DriverOption]>;
+def fno_discard_value_names : Flag<["-"], "fno-discard-value-names">, Group<f_clang_Group>,
+  HelpText<"Do not discard value names in LLVM IR">, Flags<[DriverOption]>;
 def fdollars_in_identifiers : Flag<["-"], "fdollars-in-identifiers">, Group<f_Group>,
   HelpText<"Allow '$' in identifiers">, Flags<[CC1Option]>;
 def fdwarf2_cfi_asm : Flag<["-"], "fdwarf2-cfi-asm">, Group<clang_ignored_f_Group>;
index ef5e64bc9443e82f322b7c0a41a83dcc79d4e08f..e58ae819a1cf595ddedfd9a9dc3fd259b5f5504a 100644 (file)
@@ -3266,13 +3266,24 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   if (!C.isForDiagnostics())
     CmdArgs.push_back("-disable-free");
 
-// Disable the verification pass in -asserts builds.
 #ifdef NDEBUG
-  CmdArgs.push_back("-disable-llvm-verifier");
-  // Discard LLVM value names in -asserts builds.
-  CmdArgs.push_back("-discard-value-names");
+  const bool IsAssertBuild = false;
+#else
+  const bool IsAssertBuild = true;
 #endif
 
+  // Disable the verification pass in -asserts builds.
+  if (!IsAssertBuild)
+    CmdArgs.push_back("disable-llvm-verifier");
+
+  // Discard value names in assert builds unless otherwise specified.
+  if (const Arg *A = Args.getLastArg(options::OPT_fdiscard_value_names,
+                                     options::OPT_fno_discard_value_names)) {
+    if (A->getOption().matches(options::OPT_fdiscard_value_names))
+      CmdArgs.push_back("-discard-value-names");
+  } else if (!IsAssertBuild)
+    CmdArgs.push_back("-discard-value-names");
+
   // Set the main file name, so that debug info works even with
   // -save-temps.
   CmdArgs.push_back("-main-file-name");
index 5755c5d36f50b724b12cbd2cc7141d968a871c3e..9c90f0e58f42327fd6625890f59022272ebcbb21 100644 (file)
 // RUN: %clang -### -S %s 2>&1 | FileCheck -check-prefix=CHECK-NO-CF-PROTECTION-BRANCH %s
 // CHECK-CF-PROTECTION-BRANCH: -fcf-protection=branch
 // CHECK-NO-CF-PROTECTION-BRANCH-NOT: -fcf-protection=branch
+
+// RUN: %clang -### -S -fdiscard-value-names %s 2>&1 | FileCheck -check-prefix=CHECK-DISCARD-NAMES %s
+// RUN: %clang -### -S -fno-discard-value-names %s 2>&1 | FileCheck -check-prefix=CHECK-NO-DISCARD-NAMES %s
+// CHECK-DISCARD-NAMES: "-discard-value-names"
+// CHECK-NO-DISCARD-NAMES-NOT: "-discard-value-names"