Index: clang/test/CodeGen/Inputs/thinlto-distributed-cfi.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGen/Inputs/thinlto-distributed-cfi.cpp
@@ -0,0 +1,22 @@
+struct A {
+ virtual int f() = 0;
+};
+
+struct B : public A {
+ int f() override { return 50; };
+};
+
+struct C : public A {
+ int f() override { return 7; };
+};
+
+struct D {
+ virtual int d(int a, int b) { return a + b; };
+};
+
+int main(int argc, char *argv[]) {
+ A *b = new B;
+ A *c = new C;
+ D *d = new D;
+ return d->d(b->f(), c->f()) != 57;
+}
Index: clang/test/CodeGen/thinlto-distributed-cfi-devirt.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGen/thinlto-distributed-cfi-devirt.cpp
@@ -0,0 +1,82 @@
+// REQUIRES: x86-registered-target
+
+// Test that distribute ThinLTO with CFI creates working binary.
+// It additionally enables -fwhole-program-vtables to get more information in
+// TYPE_IDs of GLOBALVAL_SUMMARY_BLOCK.
+
+// RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu -emit-llvm-bc \
+// RUN: -flto=thin -flto-unit -fvisibility hidden \
+// RUN: -fwhole-program-vtables -x c++ \
+// RUN: -fsanitize=cfi-derived-cast,cfi-vcall \
+// RUN: -fsanitize-trap=cfi-derived-cast,cfi-vcall \
+// RUN: -fthin-link-bitcode=%t.indexing.o \
+// RUN: %S/Inputs/thinlto-distributed-cfi.cpp -o %t.o
+
+// RUN: llvm-lto2 run -thinlto-distributed-indexes %t.o \
+// RUN: -o %t2.index \
+// RUN: -r=%t.o,_Znwm, \
+// RUN: -r=%t.o,_ZN1BC2Ev,p \
+// RUN: -r=%t.o,_ZN1CC2Ev,p \
+// RUN: -r=%t.o,_ZN1DC2Ev,p \
+// RUN: -r=%t.o,_ZN1BC1Ev,p \
+// RUN: -r=%t.o,_ZN1CC1Ev,p \
+// RUN: -r=%t.o,_ZN1DC1Ev,p \
+// RUN: -r=%t.o,main,px \
+// RUN: -r=%t.o,_ZN1AC2Ev,p \
+// RUN: -r=%t.o,_ZN1B1fEv,p \
+// RUN: -r=%t.o,__cxa_pure_virtual, \
+// RUN: -r=%t.o,_ZN1C1fEv,p \
+// RUN: -r=%t.o,_ZN1D1dEii,p \
+// RUN: -r=%t.o,_ZTV1B, \
+// RUN: -r=%t.o,_ZTVN10__cxxabiv120__si_class_type_infoE, \
+// RUN: -r=%t.o,_ZTS1B,p \
+// RUN: -r=%t.o,_ZTVN10__cxxabiv117__class_type_infoE, \
+// RUN: -r=%t.o,_ZTS1A,p \
+// RUN: -r=%t.o,_ZTI1A,p \
+// RUN: -r=%t.o,_ZTI1B,p \
+// RUN: -r=%t.o,_ZTV1A, \
+// RUN: -r=%t.o,_ZTV1C, \
+// RUN: -r=%t.o,_ZTS1C,p \
+// RUN: -r=%t.o,_ZTI1C,p \
+// RUN: -r=%t.o,_ZTV1D, \
+// RUN: -r=%t.o,_ZTS1D,p \
+// RUN: -r=%t.o,_ZTI1D,p \
+// RUN: -r=%t.o,__cxa_pure_virtual, \
+// RUN: -r=%t.o,_ZN1B1fEv, \
+// RUN: -r=%t.o,_ZN1C1fEv, \
+// RUN: -r=%t.o,_ZN1D1dEii, \
+// RUN: -r=%t.o,_ZTV1B,p \
+// RUN: -r=%t.o,_ZTI1A, \
+// RUN: -r=%t.o,_ZTI1B, \
+// RUN: -r=%t.o,_ZTV1A,p \
+// RUN: -r=%t.o,_ZTV1C,p \
+// RUN: -r=%t.o,_ZTI1C, \
+// RUN: -r=%t.o,_ZTV1D,p \
+// RUN: -r=%t.o,_ZTI1D,
+
+// Ensure that typeids are in the index.
+// R1UN: llvm-bcanalyzer -dump %t.o.thinlto.bc | FileCheck %s
+// CHECK-LABEL:
+// CHECK:
+// CHECK-LABEL:
+// CHECK:
+// CHECK-LABEL: TypeIdMap;
/// Mapping from original ID to GUID. If original ID can map to multiple
Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp
===================================================================
--- llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -5071,6 +5071,56 @@
return Ret;
}
+static void
+parseWholeProgramDevirtResolutionByArg(ArrayRef 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(Record[Slot++]);
+ B.Info = Record[Slot++];
+ B.Byte = Record[Slot++];
+ B.Bit = Record[Slot++];
+}
+
+static void parseWholeProgramDevirtResolution(ArrayRef Record,
+ StringRef Strtab, size_t &Slot,
+ TypeIdSummary &TypeId) {
+ uint64_t Id = Record[Slot++];
+ WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id];
+
+ Wpd.TheKind = static_cast(Record[Slot++]);
+ Wpd.SingleImplName = {Strtab.data() + Record[Slot],
+ static_cast(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 Record,
+ StringRef Strtab,
+ ModuleSummaryIndex &TheIndex) {
+ size_t Slot = 0;
+ TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary(
+ {Strtab.data() + Record[Slot], static_cast(Record[Slot + 1])});
+ Slot += 2;
+
+ TypeId.TTRes.TheKind = static_cast(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 @@
{Strtab.data() + Record[I], static_cast(Record[I + 1])});
break;
}
+
case bitc::FS_CFI_FUNCTION_DECLS: {
std::set &CfiFunctionDecls = TheIndex.cfiFunctionDecls();
for (unsigned I = 0; I != Record.size(); I += 2)
@@ -5395,6 +5446,10 @@
{Strtab.data() + Record[I], static_cast(Record[I + 1])});
break;
}
+
+ case bitc::FS_TYPE_ID:
+ parseTypeIdSummaryRecord(Record, Strtab, TheIndex);
+ break;
}
}
llvm_unreachable("Exit infinite loop");
Index: llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
===================================================================
--- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -3366,6 +3366,51 @@
FS->type_checked_load_const_vcalls());
}
+static void writeWholeProgramDevirtResolutionByArg(
+ SmallVector &NameVals, const std::vector &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 &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 &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 &NameVals, GlobalValueSummary *Summary,
@@ -3783,6 +3828,14 @@
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();
}
Index: llvm/test/ThinLTO/X86/cfi-icall.ll
===================================================================
--- llvm/test/ThinLTO/X86/cfi-icall.ll
+++ llvm/test/ThinLTO/X86/cfi-icall.ll
@@ -22,8 +22,9 @@
; COMBINED:
; COMBINED:
+; COMBINED:
; COMBINED:
; COMBINED: blob data = 'foobar'
+; COMBINED-NEXT: blob data = 'foobartypeid1'
; COMBINED-NEXT:
Index: llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
===================================================================
--- llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
+++ llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
@@ -324,6 +324,7 @@
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) {