@@ -25,6 +25,16 @@ BUILD_LIBC_TOP_HALF ?= yes
2525BUILD_LIBSETJMP ?= yes
2626# The directory where we will store intermediate artifacts.
2727OBJDIR ?= build/$(TARGET_TRIPLE )
28+
29+ # LTO; no, full, or thin
30+ # Note: thin LTO here is just for experimentation. It has known issues:
31+ # - https://github.com/llvm/llvm-project/issues/91700
32+ # - https://github.com/llvm/llvm-project/issues/91711
33+ LTO ?= no
34+ ifneq ($(LTO ) ,no)
35+ CLANG_VERSION ?= $(shell ${CC} -dumpversion)
36+ override OBJDIR := $(OBJDIR ) /llvm-lto/$(CLANG_VERSION )
37+ endif
2838# The directory where we store files and tools for generating WASIp2 bindings
2939BINDING_WORK_DIR ?= build/bindings
3040# URL from which to retrieve the WIT files used to generate the WASIp2 bindings
@@ -396,6 +406,18 @@ ASMFLAGS += -matomics
396406CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC )
397407endif
398408
409+ ifneq ($(LTO ) ,no)
410+ ifeq ($(LTO ) ,full)
411+ CFLAGS += -flto=full
412+ else
413+ ifeq ($(LTO ) ,thin)
414+ CFLAGS += -flto=thin
415+ else
416+ $(error unknown LTO value : $(LTO ) )
417+ endif
418+ endif
419+ endif
420+
399421ifeq ($(WASI_SNAPSHOT ) , p2)
400422CFLAGS += -D__wasilibc_use_wasip2
401423endif
@@ -456,6 +478,9 @@ LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES))
456478# These variables describe the locations of various files and
457479# directories in the generated sysroot tree.
458480SYSROOT_LIB := $(SYSROOT ) /lib/$(TARGET_TRIPLE )
481+ ifneq ($(LTO ) ,no)
482+ override SYSROOT_LIB := $(SYSROOT_LIB ) /llvm-lto/$(CLANG_VERSION )
483+ endif
459484SYSROOT_INC = $(SYSROOT ) /include/$(TARGET_TRIPLE )
460485SYSROOT_SHARE = $(SYSROOT ) /share/$(TARGET_TRIPLE )
461486
@@ -794,12 +819,14 @@ finish: startup_files libc dummy_libs
794819 # The build succeeded! The generated sysroot is in $(SYSROOT).
795820 #
796821
822+ ifeq ($(LTO ) ,no)
797823# The check for defined and undefined symbols expects there to be a heap
798824# alloctor (providing malloc, calloc, free, etc). Skip this step if the build
799825# is done without a malloc implementation.
800826ifneq ($(MALLOC_IMPL ) ,none)
801827finish : check-symbols
802828endif
829+ endif
803830
804831DEFINED_SYMBOLS = $(SYSROOT_SHARE ) /defined-symbols.txt
805832UNDEFINED_SYMBOLS = $(SYSROOT_SHARE ) /undefined-symbols.txt
0 commit comments