From fee274c76a3d08481c91fa44054c57cbe21f7e60 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Mon, 8 Jul 2024 18:16:04 +0200 Subject: [PATCH 01/10] Makefile: let clang use -Og in debug builds --- Makefile | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Makefile b/Makefile index 313de44d56d..0998b2140c0 100644 --- a/Makefile +++ b/Makefile @@ -441,12 +441,8 @@ CXXFLAGS := -O3 -DNDEBUG $(filter-out -Os -ggdb,$(CXXFLAGS)) endif ifeq ($(ENABLE_DEBUG),1) -ifeq ($(CONFIG),clang) -CXXFLAGS := -O0 -DDEBUG $(filter-out -Os,$(CXXFLAGS)) -else CXXFLAGS := -Og -DDEBUG $(filter-out -Os,$(CXXFLAGS)) endif -endif ifeq ($(ENABLE_ABC),1) CXXFLAGS += -DYOSYS_ENABLE_ABC From ad22430b965deaa000928ecc79960c375570f505 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Mon, 8 Jul 2024 19:19:10 +0200 Subject: [PATCH 02/10] Makefile: use -O3 instead of -Os --- Makefile | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 0998b2140c0..dce3562097b 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,6 @@ ENABLE_PYOSYS := 0 ENABLE_GCOV := 0 ENABLE_GPROF := 0 ENABLE_DEBUG := 0 -ENABLE_NDEBUG := 0 ENABLE_CCACHE := 0 # sccache is not always a drop-in replacement for ccache in practice ENABLE_SCCACHE := 0 @@ -53,6 +52,8 @@ SANITIZER = # SANITIZER = undefined # SANITIZER = cfi +# Prefer using ENABLE_DEBUG over setting this +OPT_LEVEL := -O3 PROGRAM_PREFIX := OS := $(shell uname -s) @@ -209,7 +210,7 @@ endif ifeq ($(CONFIG),clang) CXX = clang++ -CXXFLAGS += -std=$(CXXSTD) -Os +CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) ABCMKARGS += ARCHFLAGS="-DABC_USE_STDINT_H $(ABC_ARCHFLAGS)" ifneq ($(SANITIZER),) @@ -231,14 +232,14 @@ endif else ifeq ($(CONFIG),gcc) CXX = g++ -CXXFLAGS += -std=$(CXXSTD) -Os +CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) ABCMKARGS += ARCHFLAGS="-DABC_USE_STDINT_H $(ABC_ARCHFLAGS)" else ifeq ($(CONFIG),gcc-static) LINKFLAGS := $(filter-out -rdynamic,$(LINKFLAGS)) -static LIBS := $(filter-out -lrt,$(LIBS)) CXXFLAGS := $(filter-out -fPIC,$(CXXFLAGS)) -CXXFLAGS += -std=$(CXXSTD) -Os +CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) ABCMKARGS = CC="$(CC)" CXX="$(CXX)" LD="$(CXX)" ABC_USE_LIBSTDCXX=1 LIBS="-lm -lpthread -static" OPTFLAGS="-O" \ ARCHFLAGS="-DABC_USE_STDINT_H -DABC_NO_DYNAMIC_LINKING=1 -Wno-unused-but-set-variable $(ARCHFLAGS)" ABC_USE_NO_READLINE=1 ifeq ($(DISABLE_ABC_THREADS),1) @@ -247,12 +248,12 @@ endif else ifeq ($(CONFIG),afl-gcc) CXX = AFL_QUIET=1 AFL_HARDEN=1 afl-gcc -CXXFLAGS += -std=$(CXXSTD) -Os +CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) ABCMKARGS += ARCHFLAGS="-DABC_USE_STDINT_H" else ifeq ($(CONFIG),cygwin) CXX = g++ -CXXFLAGS += -std=gnu++11 -Os +CXXFLAGS += -std=gnu++11 $(OPT_LEVEL) ABCMKARGS += ARCHFLAGS="-DABC_USE_STDINT_H" else ifeq ($(CONFIG),wasi) @@ -267,12 +268,12 @@ AR = $(WASI_SDK)/bin/ar RANLIB = $(WASI_SDK)/bin/ranlib WASIFLAGS := --sysroot $(WASI_SDK)/share/wasi-sysroot $(WASIFLAGS) endif -CXXFLAGS := $(WASIFLAGS) -std=$(CXXSTD) -Os -D_WASI_EMULATED_PROCESS_CLOCKS $(filter-out -fPIC,$(CXXFLAGS)) +CXXFLAGS := $(WASIFLAGS) -std=$(CXXSTD) $(OPT_LEVEL) -D_WASI_EMULATED_PROCESS_CLOCKS $(filter-out -fPIC,$(CXXFLAGS)) LINKFLAGS := $(WASIFLAGS) -Wl,-z,stack-size=1048576 $(filter-out -rdynamic,$(LINKFLAGS)) LIBS := -lwasi-emulated-process-clocks $(filter-out -lrt,$(LIBS)) ABCMKARGS += AR="$(AR)" RANLIB="$(RANLIB)" ABCMKARGS += ARCHFLAGS="$(WASIFLAGS) -D_WASI_EMULATED_PROCESS_CLOCKS -DABC_USE_STDINT_H -DABC_NO_DYNAMIC_LINKING -DABC_NO_RLIMIT" -ABCMKARGS += OPTFLAGS="-Os" +ABCMKARGS += OPTFLAGS="$(OPT_LEVEL)" EXE = .wasm DISABLE_SPAWN := 1 @@ -285,7 +286,7 @@ endif else ifeq ($(CONFIG),mxe) PKG_CONFIG = /usr/local/src/mxe/usr/bin/i686-w64-mingw32.static-pkg-config CXX = /usr/local/src/mxe/usr/bin/i686-w64-mingw32.static-g++ -CXXFLAGS += -std=$(CXXSTD) -Os -D_POSIX_SOURCE -DYOSYS_MXE_HACKS -Wno-attributes +CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) -D_POSIX_SOURCE -DYOSYS_MXE_HACKS -Wno-attributes CXXFLAGS := $(filter-out -fPIC,$(CXXFLAGS)) LINKFLAGS := $(filter-out -rdynamic,$(LINKFLAGS)) -s LIBS := $(filter-out -lrt,$(LIBS)) @@ -296,7 +297,7 @@ EXE = .exe else ifeq ($(CONFIG),msys2-32) CXX = i686-w64-mingw32-g++ -CXXFLAGS += -std=$(CXXSTD) -Os -D_POSIX_SOURCE -DYOSYS_WIN32_UNIX_DIR +CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) -D_POSIX_SOURCE -DYOSYS_WIN32_UNIX_DIR CXXFLAGS := $(filter-out -fPIC,$(CXXFLAGS)) LINKFLAGS := $(filter-out -rdynamic,$(LINKFLAGS)) -s LIBS := $(filter-out -lrt,$(LIBS)) @@ -306,7 +307,7 @@ EXE = .exe else ifeq ($(CONFIG),msys2-64) CXX = x86_64-w64-mingw32-g++ -CXXFLAGS += -std=$(CXXSTD) -Os -D_POSIX_SOURCE -DYOSYS_WIN32_UNIX_DIR +CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) -D_POSIX_SOURCE -DYOSYS_WIN32_UNIX_DIR CXXFLAGS := $(filter-out -fPIC,$(CXXFLAGS)) LINKFLAGS := $(filter-out -rdynamic,$(LINKFLAGS)) -s LIBS := $(filter-out -lrt,$(LIBS)) @@ -315,7 +316,7 @@ ABCMKARGS += LIBS="-lpthread -lshlwapi -s" ABC_USE_NO_READLINE=0 CC="x86_64-w64- EXE = .exe else ifeq ($(CONFIG),none) -CXXFLAGS += -std=$(CXXSTD) -Os +CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) ABCMKARGS += ARCHFLAGS="-DABC_USE_STDINT_H $(ABC_ARCHFLAGS)" else @@ -436,12 +437,8 @@ CXXFLAGS += -pg LINKFLAGS += -pg endif -ifeq ($(ENABLE_NDEBUG),1) -CXXFLAGS := -O3 -DNDEBUG $(filter-out -Os -ggdb,$(CXXFLAGS)) -endif - ifeq ($(ENABLE_DEBUG),1) -CXXFLAGS := -Og -DDEBUG $(filter-out -Os,$(CXXFLAGS)) +CXXFLAGS := -Og -DDEBUG $(filter-out $(OPT_LEVEL),$(CXXFLAGS)) endif ifeq ($(ENABLE_ABC),1) From 725746baa6357a494cc6f3ffb9d149c8b5a29125 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Tue, 9 Jul 2024 11:25:16 +0200 Subject: [PATCH 03/10] Makefile: LTO for all, lld for clang --- Makefile | 19 ++++++++++++++++++- flake.nix | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index dce3562097b..310b23dce1c 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ ENABLE_PYOSYS := 0 ENABLE_GCOV := 0 ENABLE_GPROF := 0 ENABLE_DEBUG := 0 +ENABLE_LTO := 1 ENABLE_CCACHE := 0 # sccache is not always a drop-in replacement for ccache in practice ENABLE_SCCACHE := 0 @@ -52,8 +53,11 @@ SANITIZER = # SANITIZER = undefined # SANITIZER = cfi -# Prefer using ENABLE_DEBUG over setting this +# Prefer using ENABLE_DEBUG over setting these OPT_LEVEL := -O3 +GCC_LTO := -flto=auto +CLANG_LTO := -flto=thin + PROGRAM_PREFIX := OS := $(shell uname -s) @@ -208,10 +212,15 @@ ifeq ($(OS), OpenBSD) ABC_ARCHFLAGS += "-DABC_NO_RLIMIT" endif +# This gets overridden later. +LTOFLAGS := $(GCC_LTO) + ifeq ($(CONFIG),clang) CXX = clang++ CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) +LINKFLAGS += -fuse-ld=lld ABCMKARGS += ARCHFLAGS="-DABC_USE_STDINT_H $(ABC_ARCHFLAGS)" +LTOFLAGS := $(CLANG_LTO) ifneq ($(SANITIZER),) $(info [Clang Sanitizer] $(SANITIZER)) @@ -227,6 +236,7 @@ endif ifneq ($(findstring cfi,$(SANITIZER)),) CXXFLAGS += -flto LINKFLAGS += -flto +LTOFLAGS = endif endif @@ -318,11 +328,18 @@ EXE = .exe else ifeq ($(CONFIG),none) CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) ABCMKARGS += ARCHFLAGS="-DABC_USE_STDINT_H $(ABC_ARCHFLAGS)" +LTOFLAGS = else $(error Invalid CONFIG setting '$(CONFIG)'. Valid values: clang, gcc, mxe, msys2-32, msys2-64, none) endif + +ifeq ($(ENABLE_LTO),1) +CXXFLAGS += $(LTOFLAGS) +LINKFLAGS += $(LTOFLAGS) +endif + ifeq ($(ENABLE_LIBYOSYS),1) TARGETS += libyosys.so endif diff --git a/flake.nix b/flake.nix index 1e202324647..bafdc7bc4dc 100644 --- a/flake.nix +++ b/flake.nix @@ -41,7 +41,7 @@ packages.default = yosys; defaultPackage = yosys; devShell = pkgs.mkShell { - buildInputs = with pkgs; [ clang bison flex libffi tcl readline python3 llvmPackages.libcxxClang zlib git gtest abc-verifier ]; + buildInputs = with pkgs; [ clang llvmPackages.bintools bison flex libffi tcl readline python3 llvmPackages.libcxxClang zlib git gtest abc-verifier ]; }; } ); From bf758b909779491c9195c86b76594a5bf9803ef6 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Mon, 22 Jul 2024 20:59:56 +0200 Subject: [PATCH 04/10] Makefile: turn off LTO on gcc due to regression --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 310b23dce1c..e3a1a51136d 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,7 @@ SANITIZER = # Prefer using ENABLE_DEBUG over setting these OPT_LEVEL := -O3 -GCC_LTO := -flto=auto +GCC_LTO := CLANG_LTO := -flto=thin PROGRAM_PREFIX := From a947572f38bb06c50e4ed22cec7c99a4db40de56 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Mon, 22 Jul 2024 21:33:46 +0200 Subject: [PATCH 05/10] Add lld to clang build environments and Dockerfile --- Dockerfile | 1 + README.md | 2 +- docs/source/getting_started/installation.rst | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 549c73c9712..9806696e051 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,7 @@ RUN apt-get update -qq \ && DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \ ca-certificates \ clang \ + lld \ curl \ libffi-dev \ libreadline-dev \ diff --git a/README.md b/README.md index bb1c4d443e4..59637b9bbb1 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ Xdot (graphviz) is used by the ``show`` command in yosys to display schematics. For example on Ubuntu Linux 16.04 LTS the following commands will install all prerequisites for building yosys: - $ sudo apt-get install build-essential clang bison flex \ + $ sudo apt-get install build-essential clang lld bison flex \ libreadline-dev gawk tcl-dev libffi-dev git \ graphviz xdot pkg-config python3 libboost-system-dev \ libboost-python-dev libboost-filesystem-dev zlib1g-dev diff --git a/docs/source/getting_started/installation.rst b/docs/source/getting_started/installation.rst index 1a89886089b..aea8b6a2dd2 100644 --- a/docs/source/getting_started/installation.rst +++ b/docs/source/getting_started/installation.rst @@ -94,7 +94,7 @@ Installing all prerequisites for Ubuntu 20.04: .. code:: console - sudo sudo apt-get install build-essential clang bison flex \ + sudo sudo apt-get install build-essential clang lld bison flex \ libreadline-dev gawk tcl-dev libffi-dev git make \ graphviz xdot pkg-config python3 libboost-system-dev \ libboost-python-dev libboost-filesystem-dev zlib1g-dev From 5d0558932ebfa3212e88602140aebc666ecf5f59 Mon Sep 17 00:00:00 2001 From: Roland Coeurjoly Date: Mon, 22 Jul 2024 20:11:08 +0200 Subject: [PATCH 06/10] Add llvmPackages.bintools to buildInputs, otherwise we get a linkage error --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index bafdc7bc4dc..90fa5328c09 100644 --- a/flake.nix +++ b/flake.nix @@ -18,7 +18,7 @@ yosys = pkgs.clangStdenv.mkDerivation { name = "yosys"; src = ./. ; - buildInputs = with pkgs; [ clang bison flex libffi tcl readline python3 llvmPackages.libcxxClang zlib git pkg-configUpstream ]; + buildInputs = with pkgs; [ clang bison flex libffi tcl readline python3 llvmPackages.libcxxClang zlib git pkg-configUpstream llvmPackages.bintools ]; checkInputs = with pkgs; [ gtest ]; propagatedBuildInputs = [ abc-verifier ]; preConfigure = "make config-clang"; From ad47844bbf525340ddea3c179754455a2c2325f9 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Wed, 24 Jul 2024 18:31:21 +0200 Subject: [PATCH 07/10] actions: macos install lld from llvm package --- .github/actions/setup-build-env/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/setup-build-env/action.yml b/.github/actions/setup-build-env/action.yml index 345b6db8ada..8f7ff4b426b 100644 --- a/.github/actions/setup-build-env/action.yml +++ b/.github/actions/setup-build-env/action.yml @@ -14,7 +14,7 @@ runs: if: runner.os == 'macOS' shell: bash run: | - HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 brew install bison flex gawk libffi pkg-config bash autoconf + HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 brew install bison flex gawk libffi pkg-config bash autoconf llvm - name: Linux runtime environment if: runner.os == 'Linux' From 29d53bc94a063c202c3f31d2093b9ae3e27a52e0 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Wed, 24 Jul 2024 19:50:34 +0200 Subject: [PATCH 08/10] actions: try fix GITHUB_PATH --- .github/actions/setup-build-env/action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/setup-build-env/action.yml b/.github/actions/setup-build-env/action.yml index 8f7ff4b426b..c5a0d14abdf 100644 --- a/.github/actions/setup-build-env/action.yml +++ b/.github/actions/setup-build-env/action.yml @@ -28,6 +28,7 @@ runs: shell: bash run: | echo "${{ github.workspace }}/.local/bin" >> $GITHUB_PATH + echo "$(brew --prefix llvm)/bin" >> $GITHUB_PATH echo "$(brew --prefix bison)/bin" >> $GITHUB_PATH echo "$(brew --prefix flex)/bin" >> $GITHUB_PATH echo "procs=$(sysctl -n hw.ncpu)" >> $GITHUB_ENV From 7cd27e118237b5736b8b80f1765635da65ed1256 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Wed, 24 Jul 2024 21:31:35 +0200 Subject: [PATCH 09/10] Makefile: remove accidental abc opt level override for wasi builds --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e3a1a51136d..789695388ae 100644 --- a/Makefile +++ b/Makefile @@ -283,7 +283,7 @@ LINKFLAGS := $(WASIFLAGS) -Wl,-z,stack-size=1048576 $(filter-out -rdynamic,$(LIN LIBS := -lwasi-emulated-process-clocks $(filter-out -lrt,$(LIBS)) ABCMKARGS += AR="$(AR)" RANLIB="$(RANLIB)" ABCMKARGS += ARCHFLAGS="$(WASIFLAGS) -D_WASI_EMULATED_PROCESS_CLOCKS -DABC_USE_STDINT_H -DABC_NO_DYNAMIC_LINKING -DABC_NO_RLIMIT" -ABCMKARGS += OPTFLAGS="$(OPT_LEVEL)" +ABCMKARGS += OPTFLAGS="-Os" EXE = .wasm DISABLE_SPAWN := 1 From af0c2fa659b89764d824b230722b266f92a5a450 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Mon, 29 Jul 2024 15:13:24 +0200 Subject: [PATCH 10/10] Brewfile: add llvm for lld --- Brewfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Brewfile b/Brewfile index b50c70a4b0e..18e4e2917e9 100644 --- a/Brewfile +++ b/Brewfile @@ -9,4 +9,5 @@ brew "python3" brew "tcl-tk" brew "xdot" brew "bash" -brew 'boost-python3' +brew "boost-python3" +brew "llvm"