Skip to content

Commit fbdc350

Browse files
yamtsunfishcode
andcommitted
Add LTO build option
An old PR which I used as a base: WebAssembly#150 Co-Authored-by: Dan Gohman <[email protected]>
1 parent 3184536 commit fbdc350

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Makefile

Lines changed: 27 additions & 0 deletions
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
@@ -396,6 +406,18 @@ ASMFLAGS += -matomics
396406
CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
397407
endif
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+
399421
ifeq ($(WASI_SNAPSHOT), p2)
400422
CFLAGS += -D__wasilibc_use_wasip2
401423
endif
@@ -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.
458480
SYSROOT_LIB := $(SYSROOT)/lib/$(TARGET_TRIPLE)
481+
ifneq ($(LTO),no)
482+
override SYSROOT_LIB := $(SYSROOT_LIB)/llvm-lto/$(CLANG_VERSION)
483+
endif
459484
SYSROOT_INC = $(SYSROOT)/include/$(TARGET_TRIPLE)
460485
SYSROOT_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.
800826
ifneq ($(MALLOC_IMPL),none)
801827
finish: check-symbols
802828
endif
829+
endif
803830

804831
DEFINED_SYMBOLS = $(SYSROOT_SHARE)/defined-symbols.txt
805832
UNDEFINED_SYMBOLS = $(SYSROOT_SHARE)/undefined-symbols.txt

0 commit comments

Comments
 (0)