summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVitaly Buka <vitalybuka@google.com>2018-02-14 22:41:15 +0000
committerVitaly Buka <vitalybuka@google.com>2018-02-14 22:41:15 +0000
commit44396faabcb72ef03db1c357fe493dd9b4a1bed9 (patch)
treee04afaeb1e0e904450bce3f7d6429e0bcfb41ea9
parentde8140066a7cedff3f8f9e828d4430ad4e80f3d3 (diff)
downloadbcm5719-llvm-44396faabcb72ef03db1c357fe493dd9b4a1bed9.tar.gz
bcm5719-llvm-44396faabcb72ef03db1c357fe493dd9b4a1bed9.zip
[ThinLTO/CFI] Include TYPE_ID summaries into GLOBALVAL_SUMMARY_BLOCK
Summary: TypeID summaries are used by CFI and need to be serialized by ThinLTO indexing for later use by LTO Backend. Reviewers: tejohnson, pcc Subscribers: mehdi_amini, inglorion, eraman, hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D42611 llvm-svn: 325182
-rw-r--r--clang/test/CMakeLists.txt1
-rw-r--r--clang/test/CodeGen/thinlto-distributed-cfi-devirt.ll101
-rw-r--r--clang/test/CodeGen/thinlto-distributed-cfi.ll67
-rw-r--r--clang/test/CodeGen/thinlto-distributed.ll21
-rw-r--r--llvm/include/llvm/Bitcode/LLVMBitCodes.h5
-rw-r--r--llvm/include/llvm/IR/ModuleSummaryIndex.h1
-rw-r--r--llvm/lib/Bitcode/Reader/BitcodeReader.cpp55
-rw-r--r--llvm/lib/Bitcode/Writer/BitcodeWriter.cpp53
-rw-r--r--llvm/test/ThinLTO/X86/cfi-icall.ll3
-rw-r--r--llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp1
10 files changed, 306 insertions, 2 deletions
diff --git a/clang/test/CMakeLists.txt b/clang/test/CMakeLists.txt
index 4b0e94b666b..8efe3600c54 100644
--- a/clang/test/CMakeLists.txt
+++ b/clang/test/CMakeLists.txt
@@ -96,6 +96,7 @@ if( NOT CLANG_BUILT_STANDALONE )
llvm-bcanalyzer
llvm-cat
llvm-dis
+ llvm-lto2
llvm-modextract
llvm-nm
llvm-objdump
diff --git a/clang/test/CodeGen/thinlto-distributed-cfi-devirt.ll b/clang/test/CodeGen/thinlto-distributed-cfi-devirt.ll
new file mode 100644
index 00000000000..64d454c03ff
--- /dev/null
+++ b/clang/test/CodeGen/thinlto-distributed-cfi-devirt.ll
@@ -0,0 +1,101 @@
+; REQUIRES: x86-registered-target
+
+; Backend test for distribute ThinLTO with CFI.
+; It additionally enables -fwhole-program-vtables to get more information in
+; TYPE_IDs of GLOBALVAL_SUMMARY_BLOCK.
+
+; RUN: opt -thinlto-bc -o %t.o %s
+
+; RUN: llvm-lto2 run -thinlto-distributed-indexes %t.o \
+; RUN: -o %t2.index \
+; RUN: -r=%t.o,test,px \
+; RUN: -r=%t.o,_ZN1A1nEi,p \
+; RUN: -r=%t.o,_ZN1B1fEi,p \
+; RUN: -r=%t.o,_ZN1C1fEi,p \
+; RUN: -r=%t.o,_ZTV1B, \
+; RUN: -r=%t.o,_ZTV1C, \
+; RUN: -r=%t.o,_ZN1A1nEi, \
+; RUN: -r=%t.o,_ZN1B1fEi, \
+; RUN: -r=%t.o,_ZN1C1fEi, \
+; RUN: -r=%t.o,_ZTV1B,px \
+; RUN: -r=%t.o,_ZTV1C,px
+
+; Ensure that typeids are in the index.
+; RUN: llvm-bcanalyzer -dump %t.o.thinlto.bc | FileCheck %s
+; CHECK-LABEL: <GLOBALVAL_SUMMARY_BLOCK
+; CHECK: <TYPE_ID op0=0 op1=6 op2=4 op3=7 op4=0 op5=0 op6=0 op7=0 op8=0 op9=0 op10=6 op11=0 op12=0 op13=8 op14=1 op15=6 op16=9 op17=0/>
+; CHECK-LABEL: </GLOBALVAL_SUMMARY_BLOCK
+; CHECK-LABEL: <STRTAB_BLOCK
+; CHECK: blob data = '_ZTS1A_ZN1A1nEi'
+; CHECK-LABEL: </STRTAB_BLOCK
+
+; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \
+; RUN: -emit-obj -fthinlto-index=%t.o.thinlto.bc \
+; RUN: -emit-llvm -o - -x ir %t.o | FileCheck %s --check-prefixes=CHECK-IR
+
+; Check that backend does not fail generating native code.
+; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \
+; RUN: -emit-obj -fthinlto-index=%t.o.thinlto.bc \
+; RUN: -o %t.native.o -x ir %t.o
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-grtev4-linux-gnu"
+
+%struct.A = type { i32 (...)** }
+%struct.B = type { %struct.A }
+%struct.C = type { %struct.A }
+
+@_ZTV1B = constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* undef, i8* bitcast (i32 (%struct.B*, i32)* @_ZN1B1fEi to i8*), i8* bitcast (i32 (%struct.A*, i32)* @_ZN1A1nEi to i8*)] }, !type !0, !type !1
+@_ZTV1C = constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* undef, i8* bitcast (i32 (%struct.C*, i32)* @_ZN1C1fEi to i8*), i8* bitcast (i32 (%struct.A*, i32)* @_ZN1A1nEi to i8*)] }, !type !0, !type !2
+
+; CHECK-IR-LABEL: define i32 @test
+define i32 @test(%struct.A* %obj, i32 %a) {
+entry:
+ %0 = bitcast %struct.A* %obj to i8**
+ %vtable5 = load i8*, i8** %0
+
+ ; Check that the call was devirtualized.
+ ; CHECK-IR: %call = tail call i32 @_ZN1A1nEi
+ %1 = tail call { i8*, i1 } @llvm.type.checked.load(i8* %vtable5, i32 8, metadata !"_ZTS1A")
+ %2 = extractvalue { i8*, i1 } %1, 1
+ br i1 %2, label %cont, label %trap
+
+trap:
+ tail call void @llvm.trap()
+ unreachable
+
+cont:
+ %3 = extractvalue { i8*, i1 } %1, 0
+ %4 = bitcast i8* %3 to i32 (%struct.A*, i32)*
+ %call = tail call i32 %4(%struct.A* nonnull %obj, i32 %a)
+ %vtable16 = load i8*, i8** %0
+ %5 = tail call { i8*, i1 } @llvm.type.checked.load(i8* %vtable16, i32 0, metadata !"_ZTS1A")
+ %6 = extractvalue { i8*, i1 } %5, 1
+ br i1 %6, label %cont2, label %trap
+
+cont2:
+ %7 = extractvalue { i8*, i1 } %5, 0
+ %8 = bitcast i8* %7 to i32 (%struct.A*, i32)*
+
+ ; Check that traps are conditional. Invalid TYPE_ID can cause
+ ; unconditional traps.
+ ; CHECK-IR: br i1 {{.*}}, label %trap
+
+ ; We still have to call it as virtual.
+ ; CHECK-IR: %call3 = tail call i32 %8
+ %call3 = tail call i32 %8(%struct.A* nonnull %obj, i32 %call)
+ ret i32 %call3
+}
+; CHECK-IR-LABEL: ret i32
+; CHECK-IR-LABEL: }
+
+declare { i8*, i1 } @llvm.type.checked.load(i8*, i32, metadata)
+declare void @llvm.trap()
+
+declare i32 @_ZN1B1fEi(%struct.B* %this, i32 %a)
+declare i32 @_ZN1A1nEi(%struct.A* %this, i32 %a)
+declare i32 @_ZN1C1fEi(%struct.C* %this, i32 %a)
+
+!0 = !{i64 16, !"_ZTS1A"}
+!1 = !{i64 16, !"_ZTS1B"}
+!2 = !{i64 16, !"_ZTS1C"}
diff --git a/clang/test/CodeGen/thinlto-distributed-cfi.ll b/clang/test/CodeGen/thinlto-distributed-cfi.ll
new file mode 100644
index 00000000000..5e8e39d7d7f
--- /dev/null
+++ b/clang/test/CodeGen/thinlto-distributed-cfi.ll
@@ -0,0 +1,67 @@
+; REQUIRES: x86-registered-target
+
+; Backend test for distribute ThinLTO with CFI.
+
+; RUN: opt -thinlto-bc -o %t.o %s
+
+; RUN: llvm-lto2 run -thinlto-distributed-indexes %t.o \
+; RUN: -o %t2.index \
+; RUN: -r=%t.o,test,px \
+; RUN: -r=%t.o,_ZTV1B, \
+; RUN: -r=%t.o,_ZN1B1fEi, \
+; RUN: -r=%t.o,_ZTV1B,px
+
+; Check that typeids are in the index.
+; RUN: llvm-bcanalyzer -dump %t.o.thinlto.bc | FileCheck %s
+; CHECK-LABEL: <GLOBALVAL_SUMMARY_BLOCK
+; CHECK: <TYPE_ID op0=0 op1=6 op2=3 op3=0 op4=0 op5=0 op6=0 op7=0/>
+; CHECK-LABEL: </GLOBALVAL_SUMMARY_BLOCK
+; CHECK-LABEL: <STRTAB_BLOCK
+; CHECK: blob data = '_ZTS1A'
+; CHECK-LABEL: </STRTAB_BLOCK
+
+; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \
+; RUN: -emit-obj -fthinlto-index=%t.o.thinlto.bc \
+; RUN: -emit-llvm -o - -x ir %t.o | FileCheck %s --check-prefixes=CHECK-IR
+
+; Ensure that backend does not fail generating native code.
+; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \
+; RUN: -emit-obj -fthinlto-index=%t.o.thinlto.bc \
+; RUN: -o %t.native.o -x ir %t.o
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-grtev4-linux-gnu"
+
+%struct.B = type { %struct.A }
+%struct.A = type { i32 (...)** }
+
+@_ZTV1B = constant { [3 x i8*] } { [3 x i8*] [i8* undef, i8* undef, i8* undef] }, !type !0
+
+; CHECK-IR-LABEL: define void @test
+define void @test(i8* %b) {
+entry:
+ ; Ensure that traps are conditional. Invalid TYPE_ID can cause
+ ; unconditional traps.
+ ; CHECK-IR: br i1 {{.*}}, label %trap
+ %0 = bitcast i8* %b to i8**
+ %vtable2 = load i8*, i8** %0
+ %1 = tail call i1 @llvm.type.test(i8* %vtable2, metadata !"_ZTS1A")
+ br i1 %1, label %cont, label %trap
+
+trap:
+ tail call void @llvm.trap()
+ unreachable
+
+cont:
+ ; CHECK-IR-LABEL: ret void
+ ret void
+}
+; CHECK-IR-LABEL: }
+
+declare i1 @llvm.type.test(i8*, metadata)
+declare void @llvm.trap()
+
+declare i32 @_ZN1B1fEi(%struct.B* %this, i32 %a)
+
+!0 = !{i64 16, !"_ZTS1A"}
+!1 = !{i64 16, !"_ZTS1B"}
diff --git a/clang/test/CodeGen/thinlto-distributed.ll b/clang/test/CodeGen/thinlto-distributed.ll
new file mode 100644
index 00000000000..ae21d7b3e9a
--- /dev/null
+++ b/clang/test/CodeGen/thinlto-distributed.ll
@@ -0,0 +1,21 @@
+; REQUIRES: x86-registered-target
+
+; Trivial test for distributes ThinLTO
+
+; RUN: opt -thinlto-bc -o %t.o %s
+
+; RUN: llvm-lto2 run -thinlto-distributed-indexes %t.o \
+; RUN: -o %t2.index \
+; RUN: -r=%t.o,main,px
+
+; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \
+; RUN: -emit-obj -fthinlto-index=%t.o.thinlto.bc \
+; RUN: -o %t.native.o -x ir %t.o
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-grtev4-linux-gnu"
+
+define i32 @main() {
+entry:
+ ret i32 0
+}
diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
index 1610ac0805c..11689975938 100644
--- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h
+++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
@@ -263,6 +263,11 @@ enum GlobalValueSummarySymtabCodes {
FS_PERMODULE_RELBF = 19,
// Index-wide flags
FS_FLAGS = 20,
+ // Maps type identifier to summary information for that type identifier.
+ // TYPE_ID: [typeid, kind, bitwidth, align, size, bitmask, inlinebits,
+ // n x (typeid, kind, name, numrba,
+ // numrba x (numarg, numarg x arg, kind, info, byte, bit))]
+ FS_TYPE_ID = 21,
};
enum MetadataCodes {
diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h
index d75986b37fb..75b22a9dd0b 100644
--- a/llvm/include/llvm/IR/ModuleSummaryIndex.h
+++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h
@@ -671,7 +671,6 @@ private:
/// Mapping from type identifiers to summary information for that type
/// identifier.
- // FIXME: Add bitcode read/write support for this field.
std::map<std::string, TypeIdSummary> TypeIdMap;
/// Mapping from original ID to GUID. If original ID can map to multiple
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 6da39a76310..a6e42b2eb71 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -5071,6 +5071,56 @@ ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record,
return Ret;
}
+static void
+parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot,
+ WholeProgramDevirtResolution &Wpd) {
+ uint64_t ArgNum = Record[Slot++];
+ WholeProgramDevirtResolution::ByArg &B =
+ Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}];
+ Slot += ArgNum;
+
+ B.TheKind =
+ static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]);
+ B.Info = Record[Slot++];
+ B.Byte = Record[Slot++];
+ B.Bit = Record[Slot++];
+}
+
+static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record,
+ StringRef Strtab, size_t &Slot,
+ TypeIdSummary &TypeId) {
+ uint64_t Id = Record[Slot++];
+ WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id];
+
+ Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]);
+ Wpd.SingleImplName = {Strtab.data() + Record[Slot],
+ static_cast<size_t>(Record[Slot + 1])};
+ Slot += 2;
+
+ uint64_t ResByArgNum = Record[Slot++];
+ for (uint64_t I = 0; I != ResByArgNum; ++I)
+ parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd);
+}
+
+static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record,
+ StringRef Strtab,
+ ModuleSummaryIndex &TheIndex) {
+ size_t Slot = 0;
+ TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary(
+ {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])});
+ Slot += 2;
+
+ TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]);
+ TypeId.TTRes.SizeM1BitWidth = Record[Slot++];
+ TypeId.TTRes.AlignLog2 = Record[Slot++];
+ TypeId.TTRes.SizeM1 = Record[Slot++];
+ TypeId.TTRes.BitMask = Record[Slot++];
+ TypeId.TTRes.InlineBits = Record[Slot++];
+
+ while (Slot < Record.size())
+ parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId);
+}
+
// Eagerly parse the entire summary block. This populates the GlobalValueSummary
// objects in the index.
Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
@@ -5388,6 +5438,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
{Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])});
break;
}
+
case bitc::FS_CFI_FUNCTION_DECLS: {
std::set<std::string> &CfiFunctionDecls = TheIndex.cfiFunctionDecls();
for (unsigned I = 0; I != Record.size(); I += 2)
@@ -5395,6 +5446,10 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
{Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])});
break;
}
+
+ case bitc::FS_TYPE_ID:
+ parseTypeIdSummaryRecord(Record, Strtab, TheIndex);
+ break;
}
}
llvm_unreachable("Exit infinite loop");
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index b087f010de1..5a473d6684c 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -3366,6 +3366,51 @@ static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream,
FS->type_checked_load_const_vcalls());
}
+static void writeWholeProgramDevirtResolutionByArg(
+ SmallVector<uint64_t, 64> &NameVals, const std::vector<uint64_t> &args,
+ const WholeProgramDevirtResolution::ByArg &ByArg) {
+ NameVals.push_back(args.size());
+ NameVals.insert(NameVals.end(), args.begin(), args.end());
+
+ NameVals.push_back(ByArg.TheKind);
+ NameVals.push_back(ByArg.Info);
+ NameVals.push_back(ByArg.Byte);
+ NameVals.push_back(ByArg.Bit);
+}
+
+static void writeWholeProgramDevirtResolution(
+ SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
+ uint64_t Id, const WholeProgramDevirtResolution &Wpd) {
+ NameVals.push_back(Id);
+
+ NameVals.push_back(Wpd.TheKind);
+ NameVals.push_back(StrtabBuilder.add(Wpd.SingleImplName));
+ NameVals.push_back(Wpd.SingleImplName.size());
+
+ NameVals.push_back(Wpd.ResByArg.size());
+ for (auto &A : Wpd.ResByArg)
+ writeWholeProgramDevirtResolutionByArg(NameVals, A.first, A.second);
+}
+
+static void writeTypeIdSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
+ StringTableBuilder &StrtabBuilder,
+ const std::string &Id,
+ const TypeIdSummary &Summary) {
+ NameVals.push_back(StrtabBuilder.add(Id));
+ NameVals.push_back(Id.size());
+
+ NameVals.push_back(Summary.TTRes.TheKind);
+ NameVals.push_back(Summary.TTRes.SizeM1BitWidth);
+ NameVals.push_back(Summary.TTRes.AlignLog2);
+ NameVals.push_back(Summary.TTRes.SizeM1);
+ NameVals.push_back(Summary.TTRes.BitMask);
+ NameVals.push_back(Summary.TTRes.InlineBits);
+
+ for (auto &W : Summary.WPDRes)
+ writeWholeProgramDevirtResolution(NameVals, StrtabBuilder, W.first,
+ W.second);
+}
+
// Helper to emit a single function summary record.
void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
@@ -3783,6 +3828,14 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
NameVals.clear();
}
+ if (!Index.typeIds().empty()) {
+ for (auto &S : Index.typeIds()) {
+ writeTypeIdSummaryRecord(NameVals, StrtabBuilder, S.first, S.second);
+ Stream.EmitRecord(bitc::FS_TYPE_ID, NameVals);
+ NameVals.clear();
+ }
+ }
+
Stream.ExitBlock();
}
diff --git a/llvm/test/ThinLTO/X86/cfi-icall.ll b/llvm/test/ThinLTO/X86/cfi-icall.ll
index ef5d33c8a5a..40c8e1aeedc 100644
--- a/llvm/test/ThinLTO/X86/cfi-icall.ll
+++ b/llvm/test/ThinLTO/X86/cfi-icall.ll
@@ -22,8 +22,9 @@ declare i1 @llvm.type.test(i8* %ptr, metadata %type) nounwind readnone
; COMBINED: <GLOBALVAL_SUMMARY_BLOCK
; COMBINED: <CFI_FUNCTION_DEFS op0=0 op1=3/>
; COMBINED: <CFI_FUNCTION_DECLS op0=3 op1=3/>
+; COMBINED: <TYPE_ID op0=6 op1=7 op2=4 op3=7 op4=0 op5=0 op6=0 op7=0/>
; COMBINED: </GLOBALVAL_SUMMARY_BLOCK>
; COMBINED: <STRTAB_BLOCK
-; COMBINED-NEXT: <BLOB abbrevid=4/> blob data = 'foobar'
+; COMBINED-NEXT: <BLOB abbrevid=4/> blob data = 'foobartypeid1'
; COMBINED-NEXT: </STRTAB_BLOCK>
diff --git a/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp b/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
index 857e0486b96..090a8f17c79 100644
--- a/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
+++ b/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
@@ -324,6 +324,7 @@ static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
STRINGIFY_CODE(FS, VALUE_GUID)
STRINGIFY_CODE(FS, CFI_FUNCTION_DEFS)
STRINGIFY_CODE(FS, CFI_FUNCTION_DECLS)
+ STRINGIFY_CODE(FS, TYPE_ID)
}
case bitc::METADATA_ATTACHMENT_ID:
switch(CodeID) {
OpenPOWER on IntegriCloud