From 37dc2d96294484a7c9c6d80f7240f44856ca1baf Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Wed, 7 Feb 2018 04:05:59 +0000 Subject: [PATCH] [ThinLTO] Serialize WithGlobalValueDeadStripping index flag for distributed backends Summary: A recent fix to drop dead symbols (r323633) did not work for ThinLTO distributed backends because we lose the WithGlobalValueDeadStripping set on the index during the thin link. This patch adds a new flags record to the bitcode format for the index, and serializes this flag for the combined index (it would always be 0 for the per-module index generated by the compile step, so no need to serialize the new flags record there until/unless we add another flag that applies to the per-module indexes). Generally this flag should always be set for the distributed backends, which are necessarily performed after the thin link. However, if we were to simply set this flag on the index applied to the distributed backends (invoked via clang), we would lose the ability to disable dead stripping via -compute-dead=false for debugging purposes. Reviewers: grimar, pcc Subscribers: mehdi_amini, inglorion, eraman, llvm-commits Differential Revision: https://reviews.llvm.org/D42799 llvm-svn: 324444 --- include/llvm/Bitcode/LLVMBitCodes.h | 2 ++ lib/Bitcode/Reader/BitcodeReader.cpp | 10 ++++++++++ lib/Bitcode/Writer/BitcodeWriter.cpp | 5 +++++ test/Bitcode/thinlto-alias.ll | 1 + test/Bitcode/thinlto-deadstrip-flag.ll | 20 +++++++++++++++++++ .../thinlto-function-summary-callgraph-pgo.ll | 1 + ...ction-summary-callgraph-profile-summary.ll | 1 + ...ummary-callgraph-sample-profile-summary.ll | 1 + .../thinlto-function-summary-callgraph.ll | 1 + .../thinlto-function-summary-originalnames.ll | 1 + test/tools/gold/X86/thinlto.ll | 3 +++ test/tools/llvm-lto/thinlto.ll | 1 + tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp | 1 + 13 files changed, 48 insertions(+) create mode 100644 test/Bitcode/thinlto-deadstrip-flag.ll diff --git a/include/llvm/Bitcode/LLVMBitCodes.h b/include/llvm/Bitcode/LLVMBitCodes.h index 7a457744db7..ab76b0ba683 100644 --- a/include/llvm/Bitcode/LLVMBitCodes.h +++ b/include/llvm/Bitcode/LLVMBitCodes.h @@ -261,6 +261,8 @@ enum GlobalValueSummarySymtabCodes { // numrefs x valueid, // n x (valueid, relblockfreq)] FS_PERMODULE_RELBF = 19, + // Index-wide flags + FS_FLAGS = 20, }; enum MetadataCodes { diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 9cf1302b030..6da39a76310 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -5133,6 +5133,16 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { switch (BitCode) { default: // Default behavior: ignore. break; + case bitc::FS_FLAGS: { // [flags] + uint64_t Flags = Record[0]; + // Scan flags (set only on the combined index). + assert(Flags <= 1 && "Unexpected bits in flag"); + + // 1 bit: WithGlobalValueDeadStripping flag. + if (Flags & 0x1) + TheIndex.setWithGlobalValueDeadStripping(); + break; + } case bitc::FS_VALUE_GUID: { // [valueid, refguid] uint64_t ValueID = Record[0]; GlobalValue::GUID RefGUID = Record[1]; diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 76add4f7da3..fd9cde17847 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -3552,6 +3552,11 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3); Stream.EmitRecord(bitc::FS_VERSION, ArrayRef{INDEX_VERSION}); + // Write the index flags. Currently we only write a single flag, the value of + // withGlobalValueDeadStripping, which only applies to the combined index. + Stream.EmitRecord(bitc::FS_FLAGS, + ArrayRef{Index.withGlobalValueDeadStripping()}); + for (const auto &GVI : valueIds()) { Stream.EmitRecord(bitc::FS_VALUE_GUID, ArrayRef{GVI.second, GVI.first}); diff --git a/test/Bitcode/thinlto-alias.ll b/test/Bitcode/thinlto-alias.ll index 668c5cd22dc..a5020ff7119 100644 --- a/test/Bitcode/thinlto-alias.ll +++ b/test/Bitcode/thinlto-alias.ll @@ -22,6 +22,7 @@ ; COMBINED: ; COMBINED-NEXT: + +; Ensure dead stripping performed flag is not set on distributed index +; when option used to disable dead stripping computation. +; RUN: llvm-lto2 run %t.o -o %t.out -thinlto-distributed-indexes \ +; RUN: -r %t.o,glob,plx -compute-dead=false +; RUN: llvm-bcanalyzer -dump %t.o.thinlto.bc | FileCheck %s --check-prefix=NODEAD +; NODEAD: + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@glob = global i32 0 diff --git a/test/Bitcode/thinlto-function-summary-callgraph-pgo.ll b/test/Bitcode/thinlto-function-summary-callgraph-pgo.ll index 9376e801765..bb3e8e97835 100644 --- a/test/Bitcode/thinlto-function-summary-callgraph-pgo.ll +++ b/test/Bitcode/thinlto-function-summary-callgraph-pgo.ll @@ -24,6 +24,7 @@ ; COMBINED: ; COMBINED-NEXT: diff --git a/test/Bitcode/thinlto-function-summary-originalnames.ll b/test/Bitcode/thinlto-function-summary-originalnames.ll index afc9772484e..6387e6322ec 100644 --- a/test/Bitcode/thinlto-function-summary-originalnames.ll +++ b/test/Bitcode/thinlto-function-summary-originalnames.ll @@ -5,6 +5,7 @@ ; COMBINED: ; COMBINED-NEXT: ; COMBINED-NEXT: diff --git a/test/tools/gold/X86/thinlto.ll b/test/tools/gold/X86/thinlto.ll index 13d1bf3c46f..54cee219352 100644 --- a/test/tools/gold/X86/thinlto.ll +++ b/test/tools/gold/X86/thinlto.ll @@ -95,6 +95,7 @@ ; BACKEND1-NEXT: