[WPD] Miscompile with -fno-split-lto-unit
$ cat y.cc
struct B {
virtual void f() {}
};
struct D1 : B {
void f() override {}
};
struct D2 : B {
void f() override {}
};
B *b1();
B *b2();
int main(int argc, char **argv) {
if (argc) {
static_cast<D1 *>(b1())->f();
} else {
static_cast<D2 *>(b2())->f();
}
}
$ cat z.cc
struct B {
virtual void f() {}
};
struct D1 : B {
void f() override {}
};
struct D2 : B {
void f() override {}
};
B *b1() { return new D1; }
B *b2() { return new D2; }
$ ./build/rel/bin/clang++ -o a.out y.cc z.cc -flto=thin -O3 -fuse-ld=lld -fwhole-program-vtables -mllvm -import-instr-limit=0 -Wl,--thinlto-jobs=1 -fno-rtti -fno-split-lto-unit -fvisibility=hidden && ./a.out
# a.out crashes
With opaque pointers, the assume(typetest)s in the if/else become assume(phi(typetest1,typetest2)), causing WPD to miss them. But the miscompile is that LowerTypeTests comes along and thinks that the type tests don't have any satisfying virtual functions, even though the IR contains the corresponding metadata entries. The import summary seems to be missing entries for the type ids.
Where should we be adding the type id entries in the import summary for the -fno-split-lto-unit case?
@teresajohnson @pcc
The type id summary is created during WPD. Search for "getOrInsertTypeIdSummary" in WholeProgramDevirt.cpp. The first case is for the hybrid LTO aka -fsplit-lto-unit case and the second is for the index only ak -fno-split-lto-unit case.
Hmm, in both cases it looks like we would only create the resolution if we have found at least one assume(type.test) sequence, because the info is only added to CallSlots if findDevirtualizableCallsForTypeTest found it (in the index-only / non-split case this is called during ModuleSummaryAnalysis which adds them to the summary, in the split / hybrid case this is invoked directly during WPD), and findDevirtualizableCallsForTypeTest doesn't handle the phi case.
So perhaps this is broken if the type id is only ever seen in the phi case? It doesn't look like this is going to be any better with or without -fsplit-lto-units though.
I'm not seeing either getOrInsertTypeIdSummary in WholeProgramDevirt.cpp being called in either the -fsplit-lto-unit case or -fno-split-lto-unit case with the provided repro.
The problem is basically what I described earlier. Because type test not directly feeding assume aren't summarized for ThinLTO index-only (non-split) WPD, we never invoked getOrInsertTypeIdSummary which would have added the default type test resolution of "Unknown". So they got an Unsat resolution and were lowered to false.
Fixed by unconditionally adding the default entries for all type id used by a global (i.e. vtable). Just mailed D134012 for review.
I found this was not an issue for split LTO, as the type id summary entries are added during LTT when the split regular LTO portion is lowered there, generating type test resolutions that are exported to the ThinLTO portion via the summary. This handling doesn't happen for non-split ThinLTO.
(As an aside, note that in the example's command, -mllvm -import-instr-limit=0 doesn't do anything since that only affects the ThinLTO whole program step and therefore needs to be passed via the linker. But it wasn't needed here anyway.)