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

            Bug ID: 100674
           Summary: creation of alias symbols not used for optimization
           Product: gcc
           Version: 10.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marian.buschsieweke at ovgu dot de
  Target Milestone: ---

Let's consider the following source code:

    int foo(int a, int b) {
        return a + b;
    }

    __attribute__((weak))
    int bar(int a, int b)
    {
        return foo(a, b);
    }

And now the alternative implementation of the above:

    int foo(int a, int b) {
        return a + b;
    }

    int bar(int a, int b) __attribute__((weak, alias("foo")));

For the first version, GCC will emit machine code for bar which is just calling
foo, in the second GCC will emit bar as an alias for symbol foo (as explicitly
instructed to do). IMO, it would be valid (and obviously more efficient) to
also just emit an alias for the first example.

Note: Dropping the weak attribute doesn't change the output (other than no
longer marking the symbol foo as weak, as expected). I decided to include the
weak attribute in the example to point to this corner case that the alias can
indeed be a weak symbol, while the original symbol is not.

My use case is a general initialization routine, which should be possible to be
replaced by a more specific initialization routine. However, the more specific
one should still be able reuse the code from the general one by just calling
it. Hence, I need two symbols for the general routine - the regular init
function as weak symbol, and a second name for it so that the more specific one
taking preference over the weak general one can call that. IMO the first
version of the source code is much easier to read. But I would still like the
binary to be as efficient as the second one :-)

Reply via email to