Re: Build with LTO / -flto on macOS

From: Wolfgang Walther <walther(at)technowledgy(dot)de>
To: Peter Eisentraut <peter(at)eisentraut(dot)org>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Andres Freund <andres(at)anarazel(dot)de>
Subject: Re: Build with LTO / -flto on macOS
Date: 2024-06-03 15:07:22
Message-ID: 721bf39a-ed8a-44b0-8b8e-be3bd81db748@technowledgy.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Peter Eisentraut:
> It's probably worth clarifying that this option is needed on macOS only
> if LTO is also enabled.  For standard (non-LTO) builds, the
> export-dynamic behavior is already the default on macOS (otherwise
> nothing in PostgreSQL would work).

Right, man page say this:

> Preserves all global symbols in main executables during LTO. Without
this option, Link Time Optimization is allowed to inline and remove
global functions. This option is used when a main executable may load a
plug-in which requires certain symbols from the main executable.

Peter:
> I don't think we explicitly offer LTO builds as part of the make build
> system, so anyone trying this would do it sort of self-service, by
> passing additional options to configure or make.  In which case they
> might as well pass the -export_dynamic option along in the same way?

The challenge is that it defeats the purpose of LTO to pass this along
to everything, e.g. via CFLAGS. The Makefiles set this in LDFLAGS_EX_BE
only, so it only affects the backend binary. This is not at all obvious
and took me quite a while to figure out why LTO silently didn't strip
symbols from other binaries. It does work to explicitly set
LDFLAGS_EX_BE, though.

Also, passing the LTO flag on Linux "just works" (clang, not GCC
necessarily).

> I don't mind addressing this in PG18, but I would hesitate with
> backpatching.  With macOS, it's always hard to figure out whether these
> kinds of options work the same way going versions back.

All the versions for ld64 are in [1]. It seems this was introduced in
ld64-224.1 [2] the first time. It was not there in ld64-136 [3]. Finally
the man page has **exactly** the same wording in the latest version
ld64-609 [4].

We could go further and compare the source, but I think it's safe to
assume that this flag hasn't changed much and should not affect non-LTO
builds. And for even older versions it would just not be supported, so
configure would not use it.

Best,

Wolfgang

[1]: https://opensource.apple.com/source/ld64/
[2]:
https://opensource.apple.com/source/ld64/ld64-224.1/doc/man/man1/ld.1.auto.html
[3]:
https://opensource.apple.com/source/ld64/ld64-136/doc/man/man1/ld.1.auto.html
[4]:
https://opensource.apple.com/source/ld64/ld64-609/doc/man/man1/ld.1.auto.html

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2024-06-03 15:10:58 Re: An improved README experience for PostgreSQL
Previous Message Nathan Bossart 2024-06-03 14:56:12 Re: Optimizing COPY with SIMD