Skip to content

Commit 6f4f4f2

Browse files
xur-llvmzmodem
authored andcommitted
[remark][diagnostics] [codegen] Fix PR44896
This patch fixes PR44896. For IR input files, option fdiscard-value-names should be ignored as we need named values in loadModule(). Commit 60d3947 sets this option after loadModule() where valued names already created. This creates an inconsistent state in setNameImpl() that leads to a seg fault. This patch forces fdiscard-value-names to be false for IR input files. This patch also emits a warning of "ignoring -fdiscard-value-names" if option fdiscard-value-names is explictly enabled in the commandline for IR input files. Differential Revision: https://reviews.llvm.org/D74878 (cherry picked from commit 11857d4)
1 parent 8b0df8e commit 6f4f4f2

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ def warn_drv_unsupported_debug_info_opt_for_target : Warning<
271271
InGroup<UnsupportedTargetOpt>;
272272
def warn_c_kext : Warning<
273273
"ignoring -fapple-kext which is valid for C++ and Objective-C++ only">;
274+
def warn_ignoring_fdiscard_for_bitcode : Warning<
275+
"ignoring -fdiscard-value-names for LLVM Bitcode">,
276+
InGroup<UnusedCommandLineArgument>;
274277
def warn_drv_input_file_unused : Warning<
275278
"%0: '%1' input unused%select{ when '%3' is present|}2">,
276279
InGroup<UnusedCommandLineArgument>;

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,9 @@ void CodeGenAction::ExecuteAction() {
11461146
CI.getTargetOpts(), CI.getLangOpts(),
11471147
CI.getFrontendOpts().ShowTimers,
11481148
std::move(LinkModules), *VMContext, nullptr);
1149+
// PR44896: Force DiscardValueNames as false. DiscardValueNames cannot be
1150+
// true here because the valued names are needed for reading textual IR.
1151+
Ctx.setDiscardValueNames(false);
11491152
Ctx.setDiagnosticHandler(
11501153
std::make_unique<ClangDiagnosticHandler>(CodeGenOpts, &Result));
11511154

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4266,8 +4266,16 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
42664266

42674267
// Discard value names in assert builds unless otherwise specified.
42684268
if (Args.hasFlag(options::OPT_fdiscard_value_names,
4269-
options::OPT_fno_discard_value_names, !IsAssertBuild))
4269+
options::OPT_fno_discard_value_names, !IsAssertBuild)) {
4270+
if (Args.hasArg(options::OPT_fdiscard_value_names) &&
4271+
(std::any_of(Inputs.begin(), Inputs.end(),
4272+
[](const clang::driver::InputInfo &II) {
4273+
return types::isLLVMIR(II.getType());
4274+
}))) {
4275+
D.Diag(diag::warn_ignoring_fdiscard_for_bitcode);
4276+
}
42704277
CmdArgs.push_back("-discard-value-names");
4278+
}
42714279

42724280
// Set the main file name, so that debug info works even with
42734281
// -save-temps.

clang/test/CodeGen/PR44896.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: %clang -fdiscard-value-names -S %s -o /dev/null 2>&1 | FileCheck --check-prefix=WARNING %s
2+
; RUN: %clang -S %s -o /dev/null 2>&1 | FileCheck --check-prefix=NOWARNING %s
3+
; RUN: %clang_cc1 -S -emit-llvm %s -discard-value-names -o /dev/null
4+
; PR 44896
5+
6+
; WARNING: ignoring -fdiscard-value-names for LLVM Bitcode
7+
; NOWARNING-NOT: ignoring -fdiscard-value-names for LLVM Bitcode
8+
9+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
10+
target triple = "x86_64--linux-gnu"
11+
12+
define linkonce i8* @b(i8* %a) {
13+
ret i8* %a
14+
}
15+

0 commit comments

Comments
 (0)