Author: Fangrui Song
Date: 2026-10-07T07:58:29Z
New Revision: b0ba49d8c5433320684dede908bfabe3f2caf633

URL: 
https://github.com/llvm/llvm-project/commit/b0ba49d8c5433320684dede908bfabe3f2caf633
DIFF: 
https://github.com/llvm/llvm-project/commit/b0ba49d8c5433320684dede908bfabe3f2caf633.diff

LOG: [clang] Honor -discard-value-names for LLVM IR input (#229674)

For IR input, CodeGenAction forces setDiscardValueNames(false) to avoid
the crash in #44241, so names created by the optimizer (e.g. the
inliner's `.i`) are kept. c53d807321f6 fixed that crash in
Value::setNameImpl. Apply CodeGenOpts.DiscardValueNames after loading
the module and remove the "ignoring -fdiscard-value-names" warning.

LLM-aided. `clang -O3 -c sqlite3.bc` executes 0.7% fewer instructions.

Added: 
    clang/test/CodeGen/discard-name-values.ll

Modified: 
    clang/include/clang/Basic/DiagnosticDriverKinds.td
    clang/lib/CodeGen/CodeGenAction.cpp
    clang/lib/Driver/ToolChains/Clang.cpp

Removed: 
    clang/test/CodeGen/PR44896.ll


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td 
b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index df6ff0c2cf399f..e8a66dae748fff 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -487,9 +487,6 @@ def warn_drv_dwarf_version_limited_by_target : Warning<
   InGroup<UnsupportedTargetOpt>;
 def warn_c_kext : Warning<
   "ignoring -fapple-kext which is valid for C++ and Objective-C++ only">;
-def warn_ignoring_fdiscard_for_bitcode : Warning<
-  "ignoring -fdiscard-value-names for LLVM Bitcode">,
-  InGroup<UnusedCommandLineArgument>;
 def warn_drv_input_file_unused : Warning<
   "%0: '%1' input unused%select{ when '%3' is present|}2">,
   InGroup<UnusedCommandLineArgument>;

diff  --git a/clang/lib/CodeGen/CodeGenAction.cpp 
b/clang/lib/CodeGen/CodeGenAction.cpp
index de677a385d832b..1abd8b0455a19b 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -617,9 +617,8 @@ void CodeGenAction::ExecuteAction() {
   if (!CodeGenOpts.LinkBitcodePostopt && Result.LinkInModules(&*TheModule))
     return;
 
-  // PR44896: Force DiscardValueNames as false. DiscardValueNames cannot be
-  // true here because the valued names are needed for reading textual IR.
-  Ctx.setDiscardValueNames(false);
+  // Textual IR needs value names while parsing; discard new names afterwards.
+  Ctx.setDiscardValueNames(CodeGenOpts.DiscardValueNames);
   Ctx.setDiagnosticHandler(Result.createDiagnosticHandler());
 
   Ctx.setDefaultTargetCPU(TargetOpts.CPU);

diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 33507396561e21..2325e7cc552ef9 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5797,15 +5797,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction 
&JA,
 
   // Discard value names in no-asserts builds unless otherwise specified.
   if (Args.hasFlag(options::OPT_fdiscard_value_names,
-                   options::OPT_fno_discard_value_names, !IsAssertBuild)) {
-    if (Args.hasArg(options::OPT_fdiscard_value_names) &&
-        llvm::any_of(Inputs, [](const clang::driver::InputInfo &II) {
-          return types::isLLVMIR(II.getType());
-        })) {
-      D.Diag(diag::warn_ignoring_fdiscard_for_bitcode);
-    }
+                   options::OPT_fno_discard_value_names, !IsAssertBuild))
     CmdArgs.push_back("-discard-value-names");
-  }
 
   // Set the main file name, so that debug info works even with
   // -save-temps.

diff  --git a/clang/test/CodeGen/PR44896.ll b/clang/test/CodeGen/PR44896.ll
deleted file mode 100644
index 6547ea12168f65..00000000000000
--- a/clang/test/CodeGen/PR44896.ll
+++ /dev/null
@@ -1,16 +0,0 @@
-; REQUIRES: x86-registered-target
-; RUN: %clang --target=x86_64-linux-gnu -fdiscard-value-names -S %s -o 
/dev/null 2>&1 | FileCheck --check-prefix=WARNING %s
-; RUN: %clang --target=x86_64-linux-gnu -S %s -o /dev/null 2>&1 | FileCheck 
--check-prefix=NOWARNING %s
-; RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s -discard-value-names 
-o /dev/null
-; PR 44896
-
-; WARNING: ignoring -fdiscard-value-names for LLVM Bitcode
-; NOWARNING-NOT: ignoring -fdiscard-value-names for LLVM Bitcode
-
-target datalayout = 
"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
-target triple = "x86_64--linux-gnu"
-
-define linkonce ptr @b(ptr %a) {
-  ret ptr %a
-}
-

diff  --git a/clang/test/CodeGen/discard-name-values.ll 
b/clang/test/CodeGen/discard-name-values.ll
new file mode 100644
index 00000000000000..246bca2d102a59
--- /dev/null
+++ b/clang/test/CodeGen/discard-name-values.ll
@@ -0,0 +1,21 @@
+; REQUIRES: x86-registered-target
+;; Textual IR keeps its names while parsing; -discard-value-names only drops
+;; names created afterwards (here by the inliner).
+; RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O1 -emit-llvm %s -o - | 
FileCheck %s --check-prefixes=CHECK,KEEP
+; RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O1 -emit-llvm %s 
-discard-value-names -o - | FileCheck %s --check-prefixes=CHECK,DISCARD
+
+; CHECK-LABEL: define {{.*}} @g(i32 {{.*}}%a)
+; KEEP:          %y.i = add i32 %a, 1
+; DISCARD:       %1 = add i32 %a, 1
+
+target triple = "x86_64-unknown-linux-gnu"
+
+define internal i32 @f(i32 %x) {
+  %y = add i32 %x, 1
+  ret i32 %y
+}
+
+define i32 @g(i32 %a) {
+  %r = call i32 @f(i32 %a)
+  ret i32 %r
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to