https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128168

liu zhenyu <lzy20000419 at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lzy20000419 at outlook dot com

--- Comment #3 from liu zhenyu <lzy20000419 at outlook dot com> ---
Another testcase for the same problem, without a call through a helper and with
the overrider in a class that is only ever a base:

    #include <cstdio>
    unsigned tr;
    namespace {
    struct C0 { virtual unsigned g() { return 0; } };
    struct C1 : virtual C0 { unsigned g() { return 1; } };
    struct C2 : virtual C1 {
      C2() { C0 *p = this; tr = tr * 31 + p->g(); }
    };
    struct C4 : C2 { unsigned g() { return 4; } };
    }
    int main() { C4(); printf("%u\n", tr); }

    $ g++ -O2 t.cc && ./a.out
    4

Expected 1: C2 is under construction, so the final overrider is C1::g. g++ -O2
prints 4 with 14.2.0, 16.2.0 and 17.0.0 (20261113); -O0, -O2 -fno-tree-dse and
5.4.0 -O2 print 1. The destructor form (~C2() instead of C2(), struct C1 : C0)
behaves the same.

Godbolt (trunk): https://godbolt.org/z/qezcGPW8M

What happens on 14.2: dse1 removes the dead store of _ZTV2C1 in C4::C4 (it is
overwritten by the construction vtable), so C1's own vtable becomes
unreferenced. At the next remove_unreachable_nodes,
walk_polymorphic_call_targets computes the targets of the call in C2::C2 ("C0
(or a derived type) (maybe in construction)"). C1 is not instantiated, so
possible_polymorphic_call_targets_1 asks record_target_from_binfo to put C1::g
on bases_to_consider (methods reachable through construction vtables), but
record_target_from_binfo first does

      if (!flag_ltrans && anonymous)
        {
          tree vtable = BINFO_VTABLE (inner_binfo);
          ...
          if (!vnode || !vnode->definition)
            return;
        }

with the type's own vtable, which is exactly what is dead for a
non-instantiated anonymous type. So bases_to_consider stays empty, the target
list is {C4::g}, final, and the edge is made direct. On trunk
type_possibly_instantiated_p knows about derived construction vtables (the
PR120098 change), but the check above is unchanged.

The same check is also what breaks the testcase in comment 0 on trunk. There B
is counted as possibly instantiated (it has a derived construction vtable), so
possible_polymorphic_call_targets_1 calls record_target_from_binfo for B with
bases_to_consider == NULL and anonymous == true. The walk reaches the A
subobject of B, and inner_binfo's vtable is B's own vtable
_ZTVN12_GLOBAL__N_11BE, which is never emitted (not even at -O0) because B is
only ever constructed as a base of C (through _ZTCN12_GLOBAL__N_11CE0_NS_1BE).
So B::f is dropped, the list is {C::f}, and ipa-visibility makes the call in
call(A&) direct ("devirtualizing call in int {anonymous}::call(A&) to virtual
int {anonymous}::C::f()" in -fdump-ipa-visibility-details on trunk). With a
member helper instead of call(A&) (int helper() { return f(); } called from
B::B), 14.2, 14.4 and 15.1 print 2 and 15.3, 16.1 and trunk print 1, so that
form is already covered by the PR120098 change.

A destructor version, where marking the helper noipa does not help because the
call is folded inside the helper:

    #include <cstdio>
    namespace {
    struct A { virtual int f() = 0; virtual ~A() {} };
    __attribute__((noipa)) int call(A& a) { return a.f(); }
    struct B : virtual A { int f() override { return 1; } ~B() {
std::printf("%d\n", call(*this)); } };
    struct C : B { int f() override { return 2; } };
    }
    int main() { C c; }

-O0 and clang print 1; g++ -O2 prints 2 on 14.2.0 and 17.0.0 (20261113).
Godbolt: https://godbolt.org/z/a4jccvqEY

Not applying the own-vtable test to types that have virtual bases (their
base-subobject cdtors use construction vtables) fixes both testcases:

    @@ possible_polymorphic_call_targets_1
           record_target_from_binfo (nodes,
                                     (!possibly_instantiated
                                      && type_all_derivations_known_p
(type->type))
                                     ? &bases_to_consider : NULL,
                                     binfo, otr_type, type_binfos, otr_token,
                                     outer_type, offset,
                                     inserted, matched_vtables,
    -                                type->anonymous_namespace, completep);
    +                                type->anonymous_namespace
    +                                && !binfo_has_virtual_base_p (binfo),
    +                                completep);

(binfo_has_virtual_base_p walks BINFO_BASE_ITERATE for BINFO_VIRTUAL_P.) I
tested this only on 14.2, together with a 14.2 stand-in for the
construction-vtable part of type_possibly_instantiated_p. With that, comment 0,
the member-helper form and both testcases above print 1 at -O2, and over 530
-O2 and -O3 -fno-early-inlining compiles of g++.dg/{ipa,opt,tree-ssa} files
that use virtual the assembly is unchanged. I have not built trunk with the
patch.

Suggested fields: Known to work 5.4.0; Known to fail 6.5.0, 14.2.0, 16.2.0,
17.0.

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.

Reply via email to