From fbdc3507a445474fff4a7ad5d55de2aa6aa69d95 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 4 Jun 2024 23:58:52 +0900 Subject: [PATCH 1/3] Add LTO build option An old PR which I used as a base: https://github.com/WebAssembly/wasi-libc/pull/150 Co-Authored-by: Dan Gohman --- Makefile | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Makefile b/Makefile index 4192ecbb..22dc6cbd 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,16 @@ BUILD_LIBC_TOP_HALF ?= yes BUILD_LIBSETJMP ?= yes # The directory where we will store intermediate artifacts. OBJDIR ?= build/$(TARGET_TRIPLE) + +# LTO; no, full, or thin +# Note: thin LTO here is just for experimentation. It has known issues: +# - https://github.com/llvm/llvm-project/issues/91700 +# - https://github.com/llvm/llvm-project/issues/91711 +LTO ?= no +ifneq ($(LTO),no) +CLANG_VERSION ?= $(shell ${CC} -dumpversion) +override OBJDIR := $(OBJDIR)/llvm-lto/$(CLANG_VERSION) +endif # The directory where we store files and tools for generating WASIp2 bindings BINDING_WORK_DIR ?= build/bindings # URL from which to retrieve the WIT files used to generate the WASIp2 bindings @@ -396,6 +406,18 @@ ASMFLAGS += -matomics CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) endif +ifneq ($(LTO),no) +ifeq ($(LTO),full) +CFLAGS += -flto=full +else +ifeq ($(LTO),thin) +CFLAGS += -flto=thin +else +$(error unknown LTO value: $(LTO)) +endif +endif +endif + ifeq ($(WASI_SNAPSHOT), p2) CFLAGS += -D__wasilibc_use_wasip2 endif @@ -456,6 +478,9 @@ LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES)) # These variables describe the locations of various files and # directories in the generated sysroot tree. SYSROOT_LIB := $(SYSROOT)/lib/$(TARGET_TRIPLE) +ifneq ($(LTO),no) +override SYSROOT_LIB := $(SYSROOT_LIB)/llvm-lto/$(CLANG_VERSION) +endif SYSROOT_INC = $(SYSROOT)/include/$(TARGET_TRIPLE) SYSROOT_SHARE = $(SYSROOT)/share/$(TARGET_TRIPLE) @@ -794,12 +819,14 @@ finish: startup_files libc dummy_libs # The build succeeded! The generated sysroot is in $(SYSROOT). # +ifeq ($(LTO),no) # The check for defined and undefined symbols expects there to be a heap # alloctor (providing malloc, calloc, free, etc). Skip this step if the build # is done without a malloc implementation. ifneq ($(MALLOC_IMPL),none) finish: check-symbols endif +endif DEFINED_SYMBOLS = $(SYSROOT_SHARE)/defined-symbols.txt UNDEFINED_SYMBOLS = $(SYSROOT_SHARE)/undefined-symbols.txt From 062f80eb9d78e50e0d8b0d8767ea4295535663c2 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 7 Jun 2024 14:36:44 +0900 Subject: [PATCH 2/3] Exclude atexit.c from LTO This fixes a failure in wasi-sdk "make check". ("undefined symbol: __cxa_atexit" for ctors_dtors.c test) --- Makefile | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Makefile b/Makefile index 22dc6cbd..5e0c4781 100644 --- a/Makefile +++ b/Makefile @@ -261,6 +261,11 @@ LIBC_TOP_HALF_MUSL_SOURCES = \ $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/complex/*.c)) \ $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/crypt/*.c) +LIBC_NONLTO_SOURCES = \ + $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \ + exit/atexit.c \ + ) + ifeq ($(WASI_SNAPSHOT), p2) LIBC_TOP_HALF_MUSL_SOURCES += \ $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \ @@ -474,6 +479,7 @@ LIBWASI_EMULATED_SIGNAL_MUSL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_MUSL_S LIBDL_OBJS = $(call objs,$(LIBDL_SOURCES)) LIBSETJMP_OBJS = $(call objs,$(LIBSETJMP_SOURCES)) LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES)) +LIBC_NONLTO_OBJS = $(call objs,$(LIBC_NONLTO_SOURCES)) # These variables describe the locations of various files and # directories in the generated sysroot tree. @@ -652,6 +658,8 @@ $(SYSROOT_LIB)/libsetjmp.a: $(LIBSETJMP_OBJS) $(PIC_OBJS): CFLAGS += -fPIC -fvisibility=default +$(LIBC_NONLTO_OBJS): CFLAGS += -fno-lto + $(MUSL_PRINTSCAN_OBJS): CFLAGS += \ -D__wasilibc_printscan_no_long_double \ -D__wasilibc_printscan_full_support_option="\"add -lc-printscan-long-double to the link command\"" From c71d4adb7f14f754eeebe6e97f934e48913291cc Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 8 Jul 2024 08:54:51 +0900 Subject: [PATCH 3/3] avoid specifying multiple lto flags for LIBC_NONLTO_OBJS --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5e0c4781..e0f02452 100644 --- a/Makefile +++ b/Makefile @@ -658,7 +658,7 @@ $(SYSROOT_LIB)/libsetjmp.a: $(LIBSETJMP_OBJS) $(PIC_OBJS): CFLAGS += -fPIC -fvisibility=default -$(LIBC_NONLTO_OBJS): CFLAGS += -fno-lto +$(LIBC_NONLTO_OBJS): CFLAGS := $(filter-out -flto% -fno-lto, $(CFLAGS)) -fno-lto $(MUSL_PRINTSCAN_OBJS): CFLAGS += \ -D__wasilibc_printscan_no_long_double \