Skip to content

Commit b9e15a8

Browse files
yamtsunfishcode
andauthored
Add LTO build option (#505)
* Add LTO build option An old PR which I used as a base: #150 Co-Authored-by: Dan Gohman <[email protected]> * Exclude atexit.c from LTO This fixes a failure in wasi-sdk "make check". ("undefined symbol: __cxa_atexit" for ctors_dtors.c test) * avoid specifying multiple lto flags for LIBC_NONLTO_OBJS --------- Co-authored-by: Dan Gohman <[email protected]>
1 parent d43dcc6 commit b9e15a8

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Makefile

+35
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ BUILD_LIBC_TOP_HALF ?= yes
2525
BUILD_LIBSETJMP ?= yes
2626
# The directory where we will store intermediate artifacts.
2727
OBJDIR ?= 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
2939
BINDING_WORK_DIR ?= build/bindings
3040
# URL from which to retrieve the WIT files used to generate the WASIp2 bindings
@@ -251,6 +261,11 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
251261
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/complex/*.c)) \
252262
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/crypt/*.c)
253263

264+
LIBC_NONLTO_SOURCES = \
265+
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
266+
exit/atexit.c \
267+
)
268+
254269
ifeq ($(WASI_SNAPSHOT), p2)
255270
LIBC_TOP_HALF_MUSL_SOURCES += \
256271
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
@@ -396,6 +411,18 @@ ASMFLAGS += -matomics
396411
CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
397412
endif
398413

414+
ifneq ($(LTO),no)
415+
ifeq ($(LTO),full)
416+
CFLAGS += -flto=full
417+
else
418+
ifeq ($(LTO),thin)
419+
CFLAGS += -flto=thin
420+
else
421+
$(error unknown LTO value: $(LTO))
422+
endif
423+
endif
424+
endif
425+
399426
ifeq ($(WASI_SNAPSHOT), p2)
400427
CFLAGS += -D__wasilibc_use_wasip2
401428
endif
@@ -452,10 +479,14 @@ LIBWASI_EMULATED_SIGNAL_MUSL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_MUSL_S
452479
LIBDL_OBJS = $(call objs,$(LIBDL_SOURCES))
453480
LIBSETJMP_OBJS = $(call objs,$(LIBSETJMP_SOURCES))
454481
LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES))
482+
LIBC_NONLTO_OBJS = $(call objs,$(LIBC_NONLTO_SOURCES))
455483

456484
# These variables describe the locations of various files and
457485
# directories in the generated sysroot tree.
458486
SYSROOT_LIB := $(SYSROOT)/lib/$(TARGET_TRIPLE)
487+
ifneq ($(LTO),no)
488+
override SYSROOT_LIB := $(SYSROOT_LIB)/llvm-lto/$(CLANG_VERSION)
489+
endif
459490
SYSROOT_INC = $(SYSROOT)/include/$(TARGET_TRIPLE)
460491
SYSROOT_SHARE = $(SYSROOT)/share/$(TARGET_TRIPLE)
461492

@@ -627,6 +658,8 @@ $(SYSROOT_LIB)/libsetjmp.a: $(LIBSETJMP_OBJS)
627658

628659
$(PIC_OBJS): CFLAGS += -fPIC -fvisibility=default
629660

661+
$(LIBC_NONLTO_OBJS): CFLAGS := $(filter-out -flto% -fno-lto, $(CFLAGS)) -fno-lto
662+
630663
$(MUSL_PRINTSCAN_OBJS): CFLAGS += \
631664
-D__wasilibc_printscan_no_long_double \
632665
-D__wasilibc_printscan_full_support_option="\"add -lc-printscan-long-double to the link command\""
@@ -794,12 +827,14 @@ finish: startup_files libc dummy_libs
794827
# The build succeeded! The generated sysroot is in $(SYSROOT).
795828
#
796829

830+
ifeq ($(LTO),no)
797831
# The check for defined and undefined symbols expects there to be a heap
798832
# alloctor (providing malloc, calloc, free, etc). Skip this step if the build
799833
# is done without a malloc implementation.
800834
ifneq ($(MALLOC_IMPL),none)
801835
finish: check-symbols
802836
endif
837+
endif
803838

804839
DEFINED_SYMBOLS = $(SYSROOT_SHARE)/defined-symbols.txt
805840
UNDEFINED_SYMBOLS = $(SYSROOT_SHARE)/undefined-symbols.txt

0 commit comments

Comments
 (0)