From f786a28856ab57ff53608c089a410771642fbac1 Mon Sep 17 00:00:00 2001 From: Luca Versari Date: Tue, 2 Jan 2024 04:12:40 +0100 Subject: [PATCH] Rewrite build system to build everything from scratch. This also builds a "true" thread-free llvm, and tries to otherwise minimize LLVM changes in favour of libc changes. --- .gitmodules | 3 + Makefile | 216 +- cmake/Platform/WASI.cmake | 4 +- cmake/toolchain.cmake | 24 + cpython-config-override | 45 + flake.lock | 61 + flake.nix | 29 + llvm | 2 +- llvm.patch | 4895 +++++++++++++++++++++++++++++++------ llvm_version_major.sh | 4 + test.sh | 31 +- wasi-libc | 1 + wasi-libc-polyfill.c | 170 ++ wasi-libc.patch | 491 ++++ wasi_shim.cpp | 109 - wasi_shim.h | 23 + 16 files changed, 5244 insertions(+), 864 deletions(-) create mode 100644 cmake/toolchain.cmake create mode 100644 cpython-config-override create mode 100644 flake.lock create mode 100644 flake.nix create mode 100755 llvm_version_major.sh create mode 160000 wasi-libc create mode 100644 wasi-libc-polyfill.c create mode 100644 wasi-libc.patch delete mode 100644 wasi_shim.cpp diff --git a/.gitmodules b/.gitmodules index 5bdd342..804fa07 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "cpython"] path = cpython url = https://github.com/python/cpython.git +[submodule "wasi-libc"] + path = wasi-libc + url = https://github.com/WebAssembly/wasi-libc.git diff --git a/Makefile b/Makefile index 4820d02..55b9f22 100644 --- a/Makefile +++ b/Makefile @@ -1,61 +1,171 @@ -WASI_SDK_PREFIX ?= /opt/wasi-sdk/ -PYTHON ?= /usr/bin/env python3 +PYTHON := /usr/bin/env python3 DIR := $(shell pwd) -WASMTIME ?= $(shell which wasmtime) -WASMTIME_FLAGS ?= --wasm max-wasm-stack=8388608 --dir {HOST_DIR}::{GUEST_DIR} \ - --env {ENV_VAR_NAME}={ENV_VAR_VALUE} {PYTHON_WASM} - -.PHONY: -all: test build/output.tar.br - -build/output.tar.br: build/output/python build/output/cpp - tar c -C build/output . | brotli --large_window=30 > $@ - -build/output/python: - rm -rf "$@" - ${PYTHON} cpython/Tools/wasm/wasi.py build \ - --host-runner="${WASMTIME} run ${WASMTIME_FLAGS}" -- \ - --prefix="${DIR}/$@" --exec-prefix="${DIR}/$@" \ - --disable-test-modules - make -C cpython/cross-build/wasm32-wasi install - -build/libwasishim.a: wasi_shim.cpp - ${WASI_SDK_PREFIX}/bin/clang++ -fno-exceptions -std=c++20 $< -O3 -c -o $@ - -build/llvm-sources: llvm.patch - rm -rf "$@" - cp -r llvm "$@" - cd "$@" && patch -p1 < ${DIR}/llvm.patch - -build/llvm-build: build/llvm-sources build/libwasishim.a - cmake -B build/llvm-build build/llvm-sources/llvm \ - -G Ninja -DCMAKE_BUILD_TYPE=Release \ - -DWASI_SDK_PREFIX=${WASI_SDK_PREFIX} \ - -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PREFIX}/share/cmake/wasi-sdk.cmake \ - -DLLVM_TARGETS_TO_BUILD=WebAssembly -DLLVM_ENABLE_PROJECTS="clang;lld" \ +SYSROOT := ${DIR}/build/sysroot +WASMTIME := $(shell which wasmtime) +CLANG_VERSION := $(shell /usr/bin/env bash ./llvm_version_major.sh llvm) + +OUTPUT := ${DIR}/build/output + +LLVM_HOST := ${DIR}/build/llvm-host + +WASM_CC := ${LLVM_HOST}/bin/clang +WASM_CXX := ${LLVM_HOST}/bin/clang++ +WASM_NM := ${LLVM_HOST}/bin/llvm-nm +WASM_AR := ${LLVM_HOST}/bin/llvm-ar +WASM_CFLAGS := -ffile-prefix-map=${DIR}=/ +WASM_CXXFLAGS := -ffile-prefix-map=${DIR}=/ +WASM_LDFLAGS := -Wl,-z -Wl,stack-size=1048576 +MAKE := make + +all: ${OUTPUT}.DONE test + +build: + mkdir -p build + +build/llvm-host.BUILT: llvm | build + rsync -a --delete llvm/ build/llvm-host-src + cmake -S build/llvm-host-src/llvm -B build/llvm-host-build \ + -DCMAKE_INSTALL_PREFIX="${DIR}/build/llvm-host" -DDEFAULT_SYSROOT=${SYSROOT} \ + -DCMAKE_BUILD_TYPE=Release \ + -DLLVM_TARGETS_TO_BUILD=WebAssembly -DLLVM_DEFAULT_TARGET_TRIPLE=wasm32-wasi \ + -DLLVM_ENABLE_PROJECTS="clang;lld" + $(MAKE) -C build/llvm-host-build install + touch $@ + +build/wasi-libc.BUILT: wasi-libc wasi-libc-polyfill.c wasi-libc.patch build/llvm-host.BUILT | build + rsync -a --delete wasi-libc/ build/wasi-libc + cd "build/wasi-libc" && patch -p1 < ${DIR}/wasi-libc.patch + cp wasi-libc-polyfill.c build/wasi-libc + $(MAKE) -C build/wasi-libc THREAD_MODEL=single \ + CC=${WASM_CC} AR=$(WASM_AR) NM=${WASM_NM} EXTRA_CFLAGS="${WASI_CFLAGS} -O2 -DNDEBUG" \ + INSTALL_DIR=${SYSROOT} install + touch $@ + +build/llvm.SRC: llvm llvm.patch | build + rsync -a --delete llvm/ build/llvm-src + cd "build/llvm-src" && patch -p1 < ${DIR}/llvm.patch + touch $@ + +build/compiler-rt-host.BUILT: build/llvm.SRC build/wasi-libc.BUILT + mkdir -p build/compiler-rt-build-host + cmake -B build/compiler-rt-build-host -S build/llvm-src/compiler-rt/lib/builtins \ + -DWASM_PREFIX=${LLVM_HOST} -DCMAKE_TOOLCHAIN_FILE=${DIR}/cmake/toolchain.cmake \ + -DCMAKE_C_FLAGS="-I${DIR} ${WASM_C}" \ + -DCOMPILER_RT_BAREMETAL_BUILD=On \ + -DCOMPILER_RT_INCLUDE_TESTS=OFF \ + -DCOMPILER_RT_HAS_FPIC_FLAG=OFF \ + -DCOMPILER_RT_DEFAULT_TARGET_ONLY=On \ + -DCOMPILER_RT_OS_DIR=wasi \ + -DCMAKE_INSTALL_PREFIX=${LLVM_HOST}/lib/clang/$(CLANG_VERSION)/ + $(MAKE) -C build/compiler-rt-build-host install + touch $@ + +build/compiler-rt.BUILT: build/llvm.SRC build/compiler-rt-host.BUILT + mkdir -p build/compiler-rt-build + cmake -B build/compiler-rt-build -S build/llvm-src/compiler-rt/lib/builtins \ + -DWASM_PREFIX=${LLVM_HOST} -DCMAKE_TOOLCHAIN_FILE=${DIR}/cmake/toolchain.cmake \ + -DCMAKE_C_FLAGS="-I${DIR} ${WASM_C}" \ + -DCOMPILER_RT_BAREMETAL_BUILD=On \ + -DCOMPILER_RT_INCLUDE_TESTS=OFF \ + -DCOMPILER_RT_HAS_FPIC_FLAG=OFF \ + -DCOMPILER_RT_DEFAULT_TARGET_ONLY=On \ + -DCOMPILER_RT_OS_DIR=wasi \ + -DCMAKE_INSTALL_PREFIX=${SYSROOT}/lib/clang/$(CLANG_VERSION)/ + $(MAKE) -C build/compiler-rt-build install + touch $@ + +build/libcxx.BUILT: build/compiler-rt.BUILT + mkdir -p build/libcxx-build + # We disable checking the C++ compiler as we are building -lc++, which is needed by the check. + cmake -B build/libcxx-build -S build/llvm-src/runtimes \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_SYSROOT=$(SYSROOT) -DCMAKE_INSTALL_PREFIX="${SYSROOT}" -DDEFAULT_SYSROOT="/" \ + -DWASM_PREFIX=${LLVM_HOST} -DCMAKE_TOOLCHAIN_FILE=${DIR}/cmake/toolchain.cmake \ + -DCMAKE_C_FLAGS="-I${DIR} ${WASM_C}" \ + -DCMAKE_CXX_FLAGS="-I${DIR} ${WASM_CXXFLAGS} -fno-exceptions" \ + -DCMAKE_POSITION_INDEPENDENT_CODE=OFF \ + -DCMAKE_C_COMPILER_WORKS=ON \ + -DCMAKE_CXX_COMPILER_WORKS=ON \ + -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ + -DLIBCXX_ENABLE_THREADS:BOOL=OFF \ + -DLIBCXX_HAS_PTHREAD_API:BOOL=OFF \ + -DLIBCXX_HAS_EXTERNAL_THREAD_API:BOOL=OFF \ + -DLIBCXX_HAS_WIN32_THREAD_API:BOOL=OFF \ + -DLIBCXX_ENABLE_SHARED:BOOL=OFF \ + -DLIBCXX_ENABLE_EXCEPTIONS:BOOL=OFF \ + -DLIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL=OFF \ + -DLIBCXX_CXX_ABI=libcxxabi \ + -DLIBCXX_HAS_MUSL_LIBC:BOOL=ON \ + -DLIBCXX_ABI_VERSION=2 \ + -DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=OFF \ + -DLIBCXXABI_ENABLE_SHARED:BOOL=OFF \ + -DLIBCXXABI_SILENT_TERMINATE:BOOL=ON \ + -DLIBCXXABI_ENABLE_THREADS:BOOL=OFF \ + -DLIBCXXABI_HAS_PTHREAD_API:BOOL=OFF \ + -DLIBCXXABI_HAS_EXTERNAL_THREAD_API:BOOL=OFF \ + -DLIBCXXABI_HAS_WIN32_THREAD_API:BOOL=OFF \ + -DLIBCXX_LIBDIR_SUFFIX=/wasm32-wasi \ + -DLIBCXXABI_LIBDIR_SUFFIX=/wasm32-wasi + $(MAKE) -C build/libcxx-build install + touch $@ + +build/llvm.BUILT: build/llvm.SRC build/libcxx.BUILT + cmake -B build/llvm-build -S build/llvm-src/llvm \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_SYSROOT=$(SYSROOT) -DCMAKE_INSTALL_PREFIX="${SYSROOT}" -DDEFAULT_SYSROOT=/ \ + -DWASM_PREFIX=${LLVM_HOST} -DCMAKE_TOOLCHAIN_FILE=${DIR}/cmake/toolchain.cmake \ + -DCMAKE_C_FLAGS="-I${DIR} ${WASM_C}" \ + -DCMAKE_CXX_FLAGS="-I${DIR} ${WASM_CXXFLAGS} -fno-exceptions" \ + -DLLVM_TARGETS_TO_BUILD=WebAssembly -DLLVM_ENABLE_PROJECTS="clang;lld;clang-tools-extra" \ -DLLVM_ENABLE_THREADS=OFF -DLLVM_INCLUDE_BENCHMARKS=OFF \ - -DLLVM_DEFAULT_TARGET_TRIPLE=wasm32-wasi -DCMAKE_MODULE_PATH=${DIR}/cmake \ - -DCMAKE_CXX_FLAGS="-I${DIR} -fno-exceptions" -DLLVM_INCLUDE_TESTS=OFF \ + -DLLVM_DEFAULT_TARGET_TRIPLE=wasm32-wasi \ + -DLLVM_INCLUDE_TESTS=OFF -DCLANG_PLUGIN_SUPPORT=OFF \ -DLLVM_BUILD_LLVM_DYLIB=OFF -DLLVM_INCLUDE_EXAMPLES=OFF -DLLVM_ENABLE_PIC=OFF \ -DLLVM_INCLUDE_UTILS=OFF -DLLVM_BUILD_UTILS=OFF -DLLVM_ENABLE_PLUGINS=OFF \ - -DCLANG_PLUGIN_SUPPORT=OFF \ - -DCMAKE_EXE_LINKER_FLAGS="-Wl,-z -Wl,stack-size=1048576 -lwasishim -L${DIR}/build" - ninja -C build/llvm-build - - -build/output/cpp: build/llvm-build - rm -rf "$@" - mkdir -p $@/root - cp -Lr ${WASI_SDK_PREFIX}/share/wasi-sysroot/* $@/root/ - cp -L ${WASI_SDK_PREFIX}/lib/clang/*/lib/wasi/libclang_rt.* $@/root/lib/wasm32-wasi/ - rm -rf $@/root/share/wasm32-wasi-threads $@/root/lib/wasm32-wasi-threads - cp -Lr $ $@ + +${OUTPUT}.DONE: ${OUTPUT}/cpp.tar.br ${OUTPUT}/python.tar.br + clean: rm -rf build/ cpython/cross-build + +.PHONY: all test clean diff --git a/cmake/Platform/WASI.cmake b/cmake/Platform/WASI.cmake index 87ac8ae..9c54840 100644 --- a/cmake/Platform/WASI.cmake +++ b/cmake/Platform/WASI.cmake @@ -1,4 +1,2 @@ set(WASI 1) - -# This is a lie, but llvm needs it. -set(UNIX 1) +set(TARGET_SUPPORTS_SHARED_LIBS FALSE) diff --git a/cmake/toolchain.cmake b/cmake/toolchain.cmake new file mode 100644 index 0000000..1287fbf --- /dev/null +++ b/cmake/toolchain.cmake @@ -0,0 +1,24 @@ +# Inspired by wasi-sdk's toolchain file +cmake_minimum_required(VERSION 3.4.0) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") + +set(CMAKE_SYSTEM_NAME WASI) +set(CMAKE_SYSTEM_VERSION 1) +set(CMAKE_SYSTEM_PROCESSOR wasm32) + +set(triple wasm32-wasi) + +set(CMAKE_C_COMPILER ${WASM_PREFIX}/bin/clang) +set(CMAKE_CXX_COMPILER ${WASM_PREFIX}/bin/clang++) +set(CMAKE_ASM_COMPILER ${WASM_PREFIX}/bin/clang) +set(CMAKE_AR ${WASM_PREFIX}/bin/llvm-ar) +set(CMAKE_RANLIB ${WASM_PREFIX}/bin/llvm-ranlib) +set(CMAKE_C_COMPILER_TARGET ${triple}) +set(CMAKE_CXX_COMPILER_TARGET ${triple}) +set(CMAKE_ASM_COMPILER_TARGET ${triple}) + +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) diff --git a/cpython-config-override b/cpython-config-override new file mode 100644 index 0000000..27ea9a2 --- /dev/null +++ b/cpython-config-override @@ -0,0 +1,45 @@ +# config.site override for cross compiling to wasm32-wasi platform, +# forked from the one written by Christian Heimes + +# cannot be detected in cross builds +ac_cv_buggy_getaddrinfo=no + +# WASI has no /dev/pt* +ac_cv_file__dev_ptmx=no +ac_cv_file__dev_ptc=no + +# get/setrlimit are not supported +ac_cv_header_sys_resource_h=no + +# undefined symbols / unsupported features +ac_cv_func_eventfd=no + +# WASI SDK 15.0 has no pipe syscall. +ac_cv_func_pipe=no + +# WASI SDK 15.0 cannot create fifos and special files. +ac_cv_func_mkfifo=no +ac_cv_func_mkfifoat=no +ac_cv_func_mknod=no +ac_cv_func_mknodat=no +ac_cv_func_makedev=no + +# fdopendir() fails on SDK 15.0, +# OSError: [Errno 28] Invalid argument: '.' +ac_cv_func_fdopendir=no + +# WASI sockets are limited to operations on given socket fd and inet sockets. +# Disable AF_UNIX and AF_PACKET support, see socketmodule.h. +ac_cv_header_sys_un_h=no +ac_cv_header_netpacket_packet_h=no + +# disable accept for WASM runtimes without sock_accept +#ac_cv_func_accept=no +#ac_cv_func_accept4=no + +# Disable int-conversion for wask-sdk as it triggers an error from version 17. +ac_cv_disable_int_conversion=yes + + +# Disable dlopen +ac_cv_func_dlopen=no diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..d079b56 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1701680307, + "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1703438236, + "narHash": "sha256-aqVBq1u09yFhL7bj1/xyUeJjzr92fXVvQSSEx6AdB1M=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5f64a12a728902226210bf01d25ec6cbb9d9265b", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..b9a8407 --- /dev/null +++ b/flake.nix @@ -0,0 +1,29 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem + (system: + let + pkgs = import nixpkgs { + inherit system; + }; + in + with pkgs; + { + devShells.default = mkShell { + buildInputs = [ + clang + cmake + gnumake + python3 + wasmtime + rsync + brotli + ]; + }; + } + ); +} diff --git a/llvm b/llvm index 797b68c..1da9d8a 160000 --- a/llvm +++ b/llvm @@ -1 +1 @@ -Subproject commit 797b68c0ba699994e1038ac33d3083541482bf19 +Subproject commit 1da9d8aea01a433ffc0b0339c9b63285cd471980 diff --git a/llvm.patch b/llvm.patch index a8cef37..a6c87e5 100644 --- a/llvm.patch +++ b/llvm.patch @@ -1,524 +1,4177 @@ -diff --git a/clang/tools/CMakeLists.txt b/clang/tools/CMakeLists.txt -index f60db6ef0..704a99a79 100644 ---- a/clang/tools/CMakeLists.txt -+++ b/clang/tools/CMakeLists.txt -@@ -12,7 +12,7 @@ add_clang_subdirectory(clang-linker-wrapper) - add_clang_subdirectory(clang-offload-packager) - add_clang_subdirectory(clang-offload-bundler) - add_clang_subdirectory(clang-scan-deps) --if(HAVE_CLANG_REPL_SUPPORT) -+if(HAVE_CLANG_REPL_SUPPORT AND NOT WASI) - add_clang_subdirectory(clang-repl) +diff '--color=auto' -r -u llvm/clang/CMakeLists.txt build/llvm-src/clang/CMakeLists.txt +--- llvm/clang/CMakeLists.txt 2023-12-29 21:47:41.264640866 +0100 ++++ build/llvm-src/clang/CMakeLists.txt 2024-01-02 03:15:13.625394909 +0100 +@@ -421,8 +421,11 @@ + # If libstdc++ is statically linked, clang-repl needs to statically link libstdc++ + # itself, which is not possible in many platforms because of current limitations in + # JIT stack. (more platforms need to be supported by JITLink) +-if(NOT LLVM_STATIC_LINK_CXX_STDLIB) ++# Moreover, clang-repl does not work on wasi. ++if(NOT LLVM_STATIC_LINK_CXX_STDLIB AND NOT WASI) + set(HAVE_CLANG_REPL_SUPPORT ON) ++else() ++ set(HAVE_CLANG_REPL_SUPPORT OFF) endif() -diff --git a/clang/tools/libclang/CIndexer.cpp b/clang/tools/libclang/CIndexer.cpp -index 77da2e4fa..2ca5da16a 100644 ---- a/clang/tools/libclang/CIndexer.cpp -+++ b/clang/tools/libclang/CIndexer.cpp -@@ -36,7 +36,7 @@ - #elif defined(_AIX) - #include - #include --#else -+#elif !defined(__wasi__) - #include - #endif - -@@ -125,13 +125,16 @@ const std::string &CIndexer::getClangResourcesPath() { - #elif defined(_AIX) - getClangResourcesPathImplAIX(LibClangPath); - #else -- Dl_info info; - std::string Path; -+#ifndef __wasi__ -+ Dl_info info; - // This silly cast below avoids a C++ warning. - if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) != 0) { - // We now have the CIndex directory, locate clang relative to it. - LibClangPath += info.dli_fname; -- } else if (!(Path = llvm::sys::fs::getMainExecutable(nullptr, nullptr)).empty()) { -+ } else -+#endif -+ if (!(Path = llvm::sys::fs::getMainExecutable(nullptr, nullptr)).empty()) { - // If we can't get the path using dladdr, try to get the main executable - // path. This may be needed when we're statically linking libclang with - // musl libc, for example. -diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake -index 6a3c49edc..0bfb74869 100644 ---- a/llvm/cmake/modules/HandleLLVMOptions.cmake -+++ b/llvm/cmake/modules/HandleLLVMOptions.cmake -@@ -194,7 +194,7 @@ else() - MESSAGE(SEND_ERROR "Unable to determine platform") - endif() + option(CLANG_ENABLE_ARCMT "Build ARCMT." ON) +diff '--color=auto' -r -u llvm/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h build/llvm-src/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h +--- llvm/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h 2023-12-29 16:36:20.441395353 +0100 ++++ build/llvm-src/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h 2024-01-02 03:15:13.625394909 +0100 +@@ -16,6 +16,7 @@ + #include "llvm/Support/Allocator.h" + #include "llvm/Support/ErrorOr.h" + #include "llvm/Support/VirtualFileSystem.h" ++#include "llvm/Support/thread.h" + #include + #include --if (CMAKE_SYSTEM_NAME MATCHES "OS390") -+if (CMAKE_SYSTEM_NAME MATCHES "OS390" OR WASI) - set(LLVM_HAVE_LINK_VERSION_SCRIPT 0) - endif() +@@ -36,7 +37,7 @@ + std::unique_ptr Original; -diff --git a/llvm/include/llvm/ADT/bit.h b/llvm/include/llvm/ADT/bit.h -index 12223facb..8e1860316 100644 ---- a/llvm/include/llvm/ADT/bit.h -+++ b/llvm/include/llvm/ADT/bit.h -@@ -28,7 +28,8 @@ - #endif + /// The mutex that must be locked before mutating directive tokens. +- std::mutex ValueLock; ++ llvm::mutex ValueLock; + SmallVector DepDirectiveTokens; + /// Accessor to the directive tokens that's atomic to avoid data races. + /// \p CachedFileContents has ownership of the pointer. +@@ -152,7 +153,7 @@ + public: + struct CacheShard { + /// The mutex that needs to be locked before mutation of any member. +- mutable std::mutex CacheLock; ++ mutable llvm::mutex CacheLock; - #if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \ -- defined(__Fuchsia__) || defined(__EMSCRIPTEN__) || defined(__OpenBSD__) -+ defined(__Fuchsia__) || defined(__EMSCRIPTEN__) || defined(__OpenBSD__) || \ -+ defined(__wasi__) - #include - #elif defined(_AIX) - #include -diff --git a/llvm/lib/ExecutionEngine/CMakeLists.txt b/llvm/lib/ExecutionEngine/CMakeLists.txt -index af6be62dd..4f3bf10f0 100644 ---- a/llvm/lib/ExecutionEngine/CMakeLists.txt -+++ b/llvm/lib/ExecutionEngine/CMakeLists.txt -@@ -31,7 +31,9 @@ endif() + /// Map from filenames to cached entries. + llvm::StringMap +diff '--color=auto' -r -u llvm/clang/lib/CMakeLists.txt build/llvm-src/clang/lib/CMakeLists.txt +--- llvm/clang/lib/CMakeLists.txt 2023-12-29 16:36:20.484397720 +0100 ++++ build/llvm-src/clang/lib/CMakeLists.txt 2024-01-02 03:15:13.625394909 +0100 +@@ -28,5 +28,7 @@ + if(CLANG_INCLUDE_TESTS) + add_subdirectory(Testing) + endif() ++if(NOT WASI) add_subdirectory(Interpreter) - add_subdirectory(JITLink) - add_subdirectory(MCJIT) -+if (NOT WASI) - add_subdirectory(Orc) +endif() - add_subdirectory(RuntimeDyld) + add_subdirectory(Support) +diff '--color=auto' -r -u llvm/clang/lib/Frontend/PrecompiledPreamble.cpp build/llvm-src/clang/lib/Frontend/PrecompiledPreamble.cpp +--- llvm/clang/lib/Frontend/PrecompiledPreamble.cpp 2023-12-29 16:36:20.538400692 +0100 ++++ build/llvm-src/clang/lib/Frontend/PrecompiledPreamble.cpp 2024-01-02 03:15:13.625394909 +0100 +@@ -159,7 +159,7 @@ + void removeFile(StringRef File); - if( LLVM_USE_OPROFILE ) -diff --git a/llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp b/llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp -index 4f8f883a7..6cbe400b0 100644 ---- a/llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp -+++ b/llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp -@@ -34,7 +34,9 @@ - #include "llvm/Support/raw_ostream.h" - #include - #include -+#ifndef __wasi__ - #include -+#endif - #include - #include - #include -@@ -340,7 +342,11 @@ static GenericValue lle_X_exit(FunctionType *FT, ArrayRef Args) { - static GenericValue lle_X_abort(FunctionType *FT, ArrayRef Args) { - //FIXME: should we report or raise here? - //report_fatal_error("Interpreted program raised SIGABRT"); -+#ifndef __wasi__ - raise (SIGABRT); -+#else -+ report_fatal_error("Interpreted program raised SIGABRT"); -+#endif - return GenericValue(); + private: +- std::mutex Mutex; ++ llvm::mutex Mutex; + llvm::StringSet<> Files; + }; + +@@ -169,20 +169,20 @@ } -diff --git a/llvm/lib/Support/CrashRecoveryContext.cpp b/llvm/lib/Support/CrashRecoveryContext.cpp -index f53aea177..0aa67a90d 100644 ---- a/llvm/lib/Support/CrashRecoveryContext.cpp -+++ b/llvm/lib/Support/CrashRecoveryContext.cpp -@@ -14,7 +14,9 @@ - #include "llvm/Support/thread.h" - #include - #include -+#ifndef __wasi__ - #include -+#endif + TemporaryFiles::~TemporaryFiles() { +- std::lock_guard Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + for (const auto &File : Files) + llvm::sys::fs::remove(File.getKey()); + } - using namespace llvm; + void TemporaryFiles::addFile(StringRef File) { +- std::lock_guard Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + auto IsInserted = Files.insert(File).second; + (void)IsInserted; + assert(IsInserted && "File has already been added"); + } -@@ -31,7 +33,9 @@ struct CrashRecoveryContextImpl { - const CrashRecoveryContextImpl *Next; + void TemporaryFiles::removeFile(StringRef File) { +- std::lock_guard Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + auto WasPresent = Files.erase(File); + (void)WasPresent; + assert(WasPresent && "File was not tracked"); +diff '--color=auto' -r -u llvm/clang/lib/Tooling/AllTUsExecution.cpp build/llvm-src/clang/lib/Tooling/AllTUsExecution.cpp +--- llvm/clang/lib/Tooling/AllTUsExecution.cpp 2023-12-29 16:36:20.643406471 +0100 ++++ build/llvm-src/clang/lib/Tooling/AllTUsExecution.cpp 2024-01-02 03:15:13.626394965 +0100 +@@ -34,7 +34,7 @@ + class ThreadSafeToolResults : public ToolResults { + public: + void addResult(StringRef Key, StringRef Value) override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + Results.addResult(Key, Value); + } - CrashRecoveryContext *CRC; -+#ifndef __wasi__ - ::jmp_buf JumpBuffer; -+#endif - volatile unsigned Failed : 1; - unsigned SwitchedThread : 1; - unsigned ValidJumpBuffer : 1; -@@ -72,9 +76,11 @@ public: +@@ -50,7 +50,7 @@ - CRC->RetCode = RetCode; + private: + InMemoryToolResults Results; +- std::mutex Mutex; ++ llvm::mutex Mutex; + }; -+#ifndef __wasi__ - // Jump back to the RunSafely we were called under. - if (ValidJumpBuffer) - longjmp(JumpBuffer, 1); -+#endif + } // namespace +@@ -87,20 +87,20 @@ + "Only support executing exactly 1 action at this point."); - // Otherwise let the caller decide of the outcome of the crash. Currently - // this occurs when using SEH on Windows with MSVC or clang-cl. -@@ -342,6 +348,7 @@ static void uninstallExceptionOrSignalHandlers() { - // reliable fashion -- if we get a signal outside of a crash recovery context we - // simply disable crash recovery and raise the signal again. + std::string ErrorMsg; +- std::mutex TUMutex; ++ llvm::mutex TUMutex; + auto AppendError = [&](llvm::Twine Err) { +- std::unique_lock LockGuard(TUMutex); ++ llvm::unique_lock LockGuard(TUMutex); + ErrorMsg += Err.str(); + }; -+#ifndef __wasi__ - #include + auto Log = [&](llvm::Twine Msg) { +- std::unique_lock LockGuard(TUMutex); ++ llvm::unique_lock LockGuard(TUMutex); + llvm::errs() << Msg.str() << "\n"; + }; + + std::vector Files; + llvm::Regex RegexFilter(Filter); +- for (const auto& File : Compilations.getAllFiles()) { ++ for (const auto &File : Compilations.getAllFiles()) { + if (RegexFilter.match(File)) + Files.push_back(File); + } +@@ -108,7 +108,7 @@ + const std::string TotalNumStr = std::to_string(Files.size()); + unsigned Counter = 0; + auto Count = [&]() { +- std::unique_lock LockGuard(TUMutex); ++ llvm::unique_lock LockGuard(TUMutex); + return ++Counter; + }; + +@@ -164,7 +164,7 @@ + "[AllTUsToolExecutorPlugin] Please provide a directory/file path in " + "the compilation database."); + return std::make_unique(std::move(OptionsParser), +- ExecutorConcurrency); ++ ExecutorConcurrency); + } + }; - static const int Signals[] = -@@ -389,8 +396,10 @@ static void CrashRecoverySignalHandler(int Signal) { - if (CRCI) - const_cast(CRCI)->HandleCrash(RetCode, Signal); +diff '--color=auto' -r -u llvm/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp build/llvm-src/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp +--- llvm/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp 2023-12-29 21:47:41.424649691 +0100 ++++ build/llvm-src/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp 2024-01-02 03:15:13.626394965 +0100 +@@ -54,7 +54,7 @@ + if (Contents->DepDirectives.load()) + return EntryRef(Filename, Entry); + +- std::lock_guard GuardLock(Contents->ValueLock); ++ llvm::lock_guard GuardLock(Contents->ValueLock); + + // Double-checked locking. + if (Contents->DepDirectives.load()) +@@ -111,7 +111,7 @@ + DependencyScanningFilesystemSharedCache::CacheShard::findEntryByFilename( + StringRef Filename) const { + assert(llvm::sys::path::is_absolute_gnu(Filename)); +- std::lock_guard LockGuard(CacheLock); ++ llvm::lock_guard LockGuard(CacheLock); + auto It = EntriesByFilename.find(Filename); + return It == EntriesByFilename.end() ? nullptr : It->getValue(); + } +@@ -119,7 +119,7 @@ + const CachedFileSystemEntry * + DependencyScanningFilesystemSharedCache::CacheShard::findEntryByUID( + llvm::sys::fs::UniqueID UID) const { +- std::lock_guard LockGuard(CacheLock); ++ llvm::lock_guard LockGuard(CacheLock); + auto It = EntriesByUID.find(UID); + return It == EntriesByUID.end() ? nullptr : It->getSecond(); + } +@@ -128,7 +128,7 @@ + DependencyScanningFilesystemSharedCache::CacheShard:: + getOrEmplaceEntryForFilename(StringRef Filename, + llvm::ErrorOr Stat) { +- std::lock_guard LockGuard(CacheLock); ++ llvm::lock_guard LockGuard(CacheLock); + auto Insertion = EntriesByFilename.insert({Filename, nullptr}); + if (Insertion.second) + Insertion.first->second = +@@ -140,7 +140,7 @@ + DependencyScanningFilesystemSharedCache::CacheShard::getOrEmplaceEntryForUID( + llvm::sys::fs::UniqueID UID, llvm::vfs::Status Stat, + std::unique_ptr Contents) { +- std::lock_guard LockGuard(CacheLock); ++ llvm::lock_guard LockGuard(CacheLock); + auto Insertion = EntriesByUID.insert({UID, nullptr}); + if (Insertion.second) { + CachedFileContents *StoredContents = nullptr; +@@ -157,7 +157,7 @@ + DependencyScanningFilesystemSharedCache::CacheShard:: + getOrInsertEntryForFilename(StringRef Filename, + const CachedFileSystemEntry &Entry) { +- std::lock_guard LockGuard(CacheLock); ++ llvm::lock_guard LockGuard(CacheLock); + return *EntriesByFilename.insert({Filename, &Entry}).first->getValue(); } -+#endif - static void installExceptionOrSignalHandlers() { -+#ifndef __wasi__ - // Setup the signal handler. - struct sigaction Handler; - Handler.sa_handler = CrashRecoverySignalHandler; -@@ -400,17 +409,21 @@ static void installExceptionOrSignalHandlers() { - for (unsigned i = 0; i != NumSignals; ++i) { - sigaction(Signals[i], &Handler, &PrevActions[i]); +diff '--color=auto' -r -u llvm/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp build/llvm-src/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp +--- llvm/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 2023-12-29 21:47:41.498653772 +0100 ++++ build/llvm-src/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 2024-01-02 03:15:13.626394965 +0100 +@@ -78,7 +78,7 @@ + static std::string CudaBinaryPath; + + /// Mutex lock to protect writes to shared TempFiles in parallel. +-static std::mutex TempFilesMutex; ++static llvm::mutex TempFilesMutex; + + /// Temporary files created by the linker wrapper. + static std::list> TempFiles; +@@ -200,7 +200,7 @@ + + /// Get a temporary filename suitable for output. + Expected createOutputFile(const Twine &Prefix, StringRef Extension) { +- std::scoped_lock Lock(TempFilesMutex); ++ llvm::scoped_lock Lock(TempFilesMutex); + SmallString<128> OutputFile; + if (SaveTemps) { + (Prefix + "." + Extension).toNullTerminatedStringRef(OutputFile); +@@ -1074,7 +1074,7 @@ + InputsForTarget.emplace_back(std::move(Input)); + InputMap.clear(); + +- std::mutex ImageMtx; ++ llvm::mutex ImageMtx; + DenseMap> Images; + auto Err = parallelForEachError(InputsForTarget, [&](auto &Input) -> Error { + llvm::TimeTraceScope TimeScope("Link device input"); +@@ -1126,7 +1126,7 @@ + return createFileError(*OutputOrErr, EC); + } + +- std::scoped_lock Guard(ImageMtx); ++ llvm::scoped_lock Guard(ImageMtx); + OffloadingImage TheImage{}; + TheImage.TheImageKind = + Args.hasArg(OPT_embed_bitcode) ? IMG_Bitcode : IMG_Object; +diff '--color=auto' -r -u llvm/clang/tools/clang-scan-deps/ClangScanDeps.cpp build/llvm-src/clang/tools/clang-scan-deps/ClangScanDeps.cpp +--- llvm/clang/tools/clang-scan-deps/ClangScanDeps.cpp 2023-12-29 21:47:41.498653772 +0100 ++++ build/llvm-src/clang/tools/clang-scan-deps/ClangScanDeps.cpp 2024-01-02 03:15:13.626394965 +0100 +@@ -28,10 +28,9 @@ + #include "llvm/Support/ThreadPool.h" + #include "llvm/Support/Threading.h" + #include "llvm/Support/Timer.h" ++#include "llvm/Support/thread.h" + #include "llvm/TargetParser/Host.h" +-#include + #include +-#include + + #include "Opts.inc" + +@@ -233,13 +232,13 @@ + public: + SharedStream(raw_ostream &OS) : OS(OS) {} + void applyLocked(llvm::function_ref Fn) { +- std::unique_lock LockGuard(Lock); ++ llvm::unique_lock LockGuard(Lock); + Fn(OS); + OS.flush(); } -+#endif + + private: +- std::mutex Lock; ++ llvm::mutex Lock; + raw_ostream &OS; + }; + +@@ -262,7 +261,7 @@ + const std::string &ClangBinaryName = + std::string(llvm::sys::path::filename(ClangBinaryPath)); + +- std::unique_lock LockGuard(CacheLock); ++ llvm::unique_lock LockGuard(CacheLock); + const auto &CachedResourceDir = Cache.find(ClangBinaryPath); + if (CachedResourceDir != Cache.end()) + return CachedResourceDir->second; +@@ -303,7 +302,7 @@ + + private: + std::map Cache; +- std::mutex CacheLock; ++ llvm::mutex CacheLock; + }; + + } // end anonymous namespace +@@ -375,7 +374,7 @@ + void mergeDeps(ModuleDepsGraph Graph, size_t InputIndex) { + std::vector NewMDs; + { +- std::unique_lock ul(Lock); ++ llvm::unique_lock ul(Lock); + for (const ModuleDeps &MD : Graph) { + auto I = Modules.find({MD.ID, 0}); + if (I != Modules.end()) { +@@ -530,7 +529,7 @@ + std::vector Commands; + }; + +- std::mutex Lock; ++ llvm::mutex Lock; + std::unordered_map + Modules; + std::vector Inputs; +@@ -615,7 +614,7 @@ + } + + void addRules(P1689Rule &Rule) { +- std::unique_lock LockGuard(Lock); ++ llvm::unique_lock LockGuard(Lock); + Rules.push_back(Rule); + } + +@@ -635,7 +634,7 @@ + } + } + +- std::mutex Lock; ++ llvm::mutex Lock; + std::vector Rules; + }; + +@@ -881,10 +880,10 @@ + std::optional FD; + P1689Deps PD; + +- std::mutex Lock; ++ llvm::mutex Lock; + size_t Index = 0; + auto GetNextInputIndex = [&]() -> std::optional { +- std::unique_lock LockGuard(Lock); ++ llvm::unique_lock LockGuard(Lock); + if (Index < Inputs.size()) + return Index++; + return {}; +@@ -944,13 +943,13 @@ + + if (!MakeformatOutputPath.empty() && !MakeformatOutput.empty() && + !HadErrors) { +- static std::mutex Lock; ++ static llvm::mutex Lock; + // With compilation database, we may open different files + // concurrently or we may write the same file concurrently. So we + // use a map here to allow multiple compile commands to write to the + // same file. Also we need a lock here to avoid data race. + static llvm::StringMap OSs; +- std::unique_lock LockGuard(Lock); ++ llvm::unique_lock LockGuard(Lock); + + auto OSIter = OSs.find(MakeformatOutputPath); + if (OSIter == OSs.end()) { +diff '--color=auto' -r -u llvm/clang/tools/libclang/CIndex.cpp build/llvm-src/clang/tools/libclang/CIndex.cpp +--- llvm/clang/tools/libclang/CIndex.cpp 2023-12-29 21:47:41.500653882 +0100 ++++ build/llvm-src/clang/tools/libclang/CIndex.cpp 2024-01-02 03:15:13.627395020 +0100 +@@ -2204,8 +2204,8 @@ + void VisitOMPMaskedTaskLoopDirective(const OMPMaskedTaskLoopDirective *D); + void + VisitOMPMasterTaskLoopSimdDirective(const OMPMasterTaskLoopSimdDirective *D); +- void VisitOMPMaskedTaskLoopSimdDirective( +- const OMPMaskedTaskLoopSimdDirective *D); ++ void ++ VisitOMPMaskedTaskLoopSimdDirective(const OMPMaskedTaskLoopSimdDirective *D); + void VisitOMPParallelMasterTaskLoopDirective( + const OMPParallelMasterTaskLoopDirective *D); + void VisitOMPParallelMaskedTaskLoopDirective( +@@ -2735,8 +2735,7 @@ + void OMPClauseEnqueue::VisitOMPDoacrossClause(const OMPDoacrossClause *C) { + VisitOMPClauseList(C); } +-void OMPClauseEnqueue::VisitOMPXAttributeClause(const OMPXAttributeClause *C) { +-} ++void OMPClauseEnqueue::VisitOMPXAttributeClause(const OMPXAttributeClause *C) {} + void OMPClauseEnqueue::VisitOMPXBareClause(const OMPXBareClause *C) {} - static void uninstallExceptionOrSignalHandlers() { -+#ifndef __wasi__ - // Restore the previous signal handlers. - for (unsigned i = 0; i != NumSignals; ++i) - sigaction(Signals[i], &PrevActions[i], nullptr); -+#endif + } // namespace +@@ -9650,10 +9649,10 @@ + return *this; } - #endif // !_WIN32 +-static llvm::ManagedStatic LoggingMutex; ++static llvm::ManagedStatic LoggingMutex; - bool CrashRecoveryContext::RunSafely(function_ref Fn) { -+#ifndef __wasi__ - // If crash recovery is disabled, do nothing. - if (gCrashRecoveryEnabled) { - assert(!Impl && "Crash recovery context already initialized!"); -@@ -422,6 +435,7 @@ bool CrashRecoveryContext::RunSafely(function_ref Fn) { - return false; + cxindex::Logger::~Logger() { +- std::lock_guard L(*LoggingMutex); ++ llvm::lock_guard L(*LoggingMutex); + + static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime(); + +diff '--color=auto' -r -u llvm/clang/tools/libclang/Indexing.cpp build/llvm-src/clang/tools/libclang/Indexing.cpp +--- llvm/clang/tools/libclang/Indexing.cpp 2023-12-29 16:36:21.646461676 +0100 ++++ build/llvm-src/clang/tools/libclang/Indexing.cpp 2024-01-02 03:15:13.627395020 +0100 +@@ -71,6 +71,7 @@ + llvm::sys::fs::UniqueID UniqueID; + time_t ModTime; + unsigned Offset; ++ + public: + PPRegion() : UniqueID(0, 0), ModTime(), Offset() {} + PPRegion(llvm::sys::fs::UniqueID UniqueID, unsigned offset, time_t modTime) +@@ -92,30 +93,29 @@ + + namespace llvm { + +- template <> +- struct DenseMapInfo { +- static inline PPRegion getEmptyKey() { +- return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-1), 0); +- } +- static inline PPRegion getTombstoneKey() { +- return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-2), 0); +- } +- +- static unsigned getHashValue(const PPRegion &S) { +- llvm::FoldingSetNodeID ID; +- const llvm::sys::fs::UniqueID &UniqueID = S.getUniqueID(); +- ID.AddInteger(UniqueID.getFile()); +- ID.AddInteger(UniqueID.getDevice()); +- ID.AddInteger(S.getOffset()); +- ID.AddInteger(S.getModTime()); +- return ID.ComputeHash(); +- } ++template <> struct DenseMapInfo { ++ static inline PPRegion getEmptyKey() { ++ return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-1), 0); ++ } ++ static inline PPRegion getTombstoneKey() { ++ return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-2), 0); ++ } ++ ++ static unsigned getHashValue(const PPRegion &S) { ++ llvm::FoldingSetNodeID ID; ++ const llvm::sys::fs::UniqueID &UniqueID = S.getUniqueID(); ++ ID.AddInteger(UniqueID.getFile()); ++ ID.AddInteger(UniqueID.getDevice()); ++ ID.AddInteger(S.getOffset()); ++ ID.AddInteger(S.getModTime()); ++ return ID.ComputeHash(); ++ } + +- static bool isEqual(const PPRegion &LHS, const PPRegion &RHS) { +- return LHS == RHS; +- } +- }; +-} ++ static bool isEqual(const PPRegion &LHS, const PPRegion &RHS) { ++ return LHS == RHS; ++ } ++}; ++} // namespace llvm + + namespace { + +@@ -123,19 +123,19 @@ + /// + /// Is thread-safe. + class ThreadSafeParsedRegions { +- mutable std::mutex Mutex; ++ mutable llvm::mutex Mutex; + llvm::DenseSet ParsedRegions; + + public: + ~ThreadSafeParsedRegions() = default; + + llvm::DenseSet getParsedRegions() const { +- std::lock_guard MG(Mutex); ++ llvm::lock_guard MG(Mutex); + return ParsedRegions; + } + + void addParsedRegions(ArrayRef Regions) { +- std::lock_guard MG(Mutex); ++ llvm::lock_guard MG(Mutex); + ParsedRegions.insert(Regions.begin(), Regions.end()); + } + }; +@@ -240,10 +240,11 @@ + + public: + IndexPPCallbacks(Preprocessor &PP, CXIndexDataConsumer &dataConsumer) +- : PP(PP), DataConsumer(dataConsumer), IsMainFileEntered(false) { } ++ : PP(PP), DataConsumer(dataConsumer), IsMainFileEntered(false) {} + + void FileChanged(SourceLocation Loc, FileChangeReason Reason, +- SrcMgr::CharacteristicKind FileType, FileID PrevFID) override { ++ SrcMgr::CharacteristicKind FileType, ++ FileID PrevFID) override { + if (IsMainFileEntered) + return; + +@@ -263,10 +264,11 @@ + OptionalFileEntryRef File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, + SrcMgr::CharacteristicKind FileType) override { +- bool isImport = (IncludeTok.is(tok::identifier) && +- IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import); ++ bool isImport = ++ (IncludeTok.is(tok::identifier) && ++ IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import); + DataConsumer.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled, +- Imported); ++ Imported); + } + + /// MacroDefined - This hook is called whenever a macro definition is seen. +@@ -274,8 +276,7 @@ + + /// MacroUndefined - This hook is called whenever a macro #undef is seen. + /// MI is released immediately following this callback. +- void MacroUndefined(const Token &MacroNameTok, +- const MacroDefinition &MD, ++ void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, + const MacroDirective *UD) override {} + + /// MacroExpands - This is called by when a macro invocation is found. +@@ -317,8 +318,8 @@ + + class CaptureDiagnosticConsumer : public DiagnosticConsumer { + SmallVector Errors; +-public: + ++public: + void HandleDiagnostic(DiagnosticsEngine::Level level, + const Diagnostic &Info) override { + if (level >= DiagnosticsEngine::Error) +@@ -457,8 +458,8 @@ + + IndexerCallbacks CB; + memset(&CB, 0, sizeof(CB)); +- unsigned ClientCBSize = index_callbacks_size < sizeof(CB) +- ? index_callbacks_size : sizeof(CB); ++ unsigned ClientCBSize = ++ index_callbacks_size < sizeof(CB) ? index_callbacks_size : sizeof(CB); + memcpy(&CB, client_index_callbacks, ClientCBSize); + + IndexSessionData *IdxSession = static_cast(cxIdxAction); +@@ -478,23 +479,23 @@ + CaptureDiag = new CaptureDiagnosticConsumer(); + + // Configure the diagnostics. +- IntrusiveRefCntPtr +- Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions, +- CaptureDiag, +- /*ShouldOwnClient=*/true)); ++ IntrusiveRefCntPtr Diags( ++ CompilerInstance::createDiagnostics(new DiagnosticOptions, CaptureDiag, ++ /*ShouldOwnClient=*/true)); + + // Recover resources if we crash before exiting this function. +- llvm::CrashRecoveryContextCleanupRegistrar > +- DiagCleanup(Diags.get()); ++ llvm::CrashRecoveryContextCleanupRegistrar< ++ DiagnosticsEngine, ++ llvm::CrashRecoveryContextReleaseRefCleanup> ++ DiagCleanup(Diags.get()); + + std::unique_ptr> Args( + new std::vector()); + + // Recover resources if we crash before exiting this method. +- llvm::CrashRecoveryContextCleanupRegistrar > +- ArgsCleanup(Args.get()); +- ++ llvm::CrashRecoveryContextCleanupRegistrar> ++ ArgsCleanup(Args.get()); ++ + Args->insert(Args->end(), command_line_args, + command_line_args + num_command_line_args); + +@@ -541,7 +542,7 @@ + + // Since libclang is primarily used by batch tools dealing with + // (often very broken) source code, where spell-checking can have a +- // significant negative impact on performance (particularly when ++ // significant negative impact on performance (particularly when + // precompiled headers are involved), we disable it. + CInvok->getLangOpts().SpellChecking = false; + +@@ -562,33 +563,31 @@ + new CXTUOwner(MakeCXTranslationUnit(CXXIdx, std::move(Unit)))); + + // Recover resources if we crash before exiting this method. +- llvm::CrashRecoveryContextCleanupRegistrar +- CXTUCleanup(CXTU.get()); ++ llvm::CrashRecoveryContextCleanupRegistrar CXTUCleanup(CXTU.get()); + + // Enable the skip-parsed-bodies optimization only for C++; this may be + // revisited. + bool SkipBodies = (index_options & CXIndexOpt_SkipParsedBodiesInSession) && +- CInvok->getLangOpts().CPlusPlus; ++ CInvok->getLangOpts().CPlusPlus; + if (SkipBodies) + CInvok->getFrontendOpts().SkipFunctionBodies = true; + +- auto DataConsumer = +- std::make_shared(client_data, CB, index_options, +- CXTU->getTU()); ++ auto DataConsumer = std::make_shared( ++ client_data, CB, index_options, CXTU->getTU()); + auto IndexAction = std::make_unique( + DataConsumer, getIndexingOptionsFromCXOptions(index_options), + SkipBodies ? IdxSession->SkipBodyData.get() : nullptr); + + // Recover resources if we crash before exiting this method. +- llvm::CrashRecoveryContextCleanupRegistrar +- IndexActionCleanup(IndexAction.get()); ++ llvm::CrashRecoveryContextCleanupRegistrar IndexActionCleanup( ++ IndexAction.get()); + + bool Persistent = requestedToGetTU; + bool OnlyLocalDecls = false; + bool PrecompilePreamble = false; + bool CreatePreambleOnFirstParse = false; + bool CacheCodeCompletionResults = false; +- PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts(); ++ PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts(); + PPOpts.AllowPCHWithCompilerErrors = true; + + if (requestedToGetTU) { +@@ -597,8 +596,8 @@ + CreatePreambleOnFirstParse = + TU_options & CXTranslationUnit_CreatePreambleOnFirstParse; + // FIXME: Add a flag for modules. +- CacheCodeCompletionResults +- = TU_options & CXTranslationUnit_CacheCompletionResults; ++ CacheCodeCompletionResults = ++ TU_options & CXTranslationUnit_CacheCompletionResults; + } + + if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) { +@@ -638,7 +637,8 @@ + // clang_indexTranslationUnit Implementation + //===----------------------------------------------------------------------===// + +-static void indexPreprocessingRecord(ASTUnit &Unit, CXIndexDataConsumer &IdxCtx) { ++static void indexPreprocessingRecord(ASTUnit &Unit, ++ CXIndexDataConsumer &IdxCtx) { + Preprocessor &PP = Unit.getPreprocessor(); + if (!PP.getPreprocessingRecord()) + return; +@@ -653,8 +653,7 @@ + // if the location points to such a file. + if (isModuleFile && Unit.isInMainFileID(Loc)) + Loc = SourceLocation(); +- IdxCtx.ppIncludedFile(Loc, ID->getFileName(), +- ID->getFile(), ++ IdxCtx.ppIncludedFile(Loc, ID->getFileName(), ID->getFile(), + ID->getKind() == InclusionDirective::Import, + !ID->wasInQuotes(), ID->importedModule()); } +@@ -680,8 +679,8 @@ + + IndexerCallbacks CB; + memset(&CB, 0, sizeof(CB)); +- unsigned ClientCBSize = index_callbacks_size < sizeof(CB) +- ? index_callbacks_size : sizeof(CB); ++ unsigned ClientCBSize = ++ index_callbacks_size < sizeof(CB) ? index_callbacks_size : sizeof(CB); + memcpy(&CB, client_index_callbacks, ClientCBSize); + + CXIndexDataConsumer DataConsumer(client_data, CB, index_options, TU); +@@ -709,7 +708,8 @@ + DataConsumer.startedTranslationUnit(); + + indexPreprocessingRecord(*Unit, DataConsumer); +- indexASTUnit(*Unit, DataConsumer, getIndexingOptionsFromCXOptions(index_options)); ++ indexASTUnit(*Unit, DataConsumer, ++ getIndexingOptionsFromCXOptions(index_options)); + DataConsumer.indexDiagnostics(); + + return CXError_Success; +@@ -729,8 +729,8 @@ + return nullptr; + + const DeclInfo *DI = static_cast(DInfo); +- if (const ObjCContainerDeclInfo * +- ContInfo = dyn_cast(DI)) ++ if (const ObjCContainerDeclInfo *ContInfo = ++ dyn_cast(DI)) + return &ContInfo->ObjCContDeclInfo; + + return nullptr; +@@ -742,21 +742,20 @@ + return nullptr; + + const DeclInfo *DI = static_cast(DInfo); +- if (const ObjCInterfaceDeclInfo * +- InterInfo = dyn_cast(DI)) ++ if (const ObjCInterfaceDeclInfo *InterInfo = ++ dyn_cast(DI)) + return &InterInfo->ObjCInterDeclInfo; + + return nullptr; + } + + const CXIdxObjCCategoryDeclInfo * +-clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){ ++clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo) { + if (!DInfo) + return nullptr; + + const DeclInfo *DI = static_cast(DInfo); +- if (const ObjCCategoryDeclInfo * +- CatInfo = dyn_cast(DI)) ++ if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast(DI)) + return &CatInfo->ObjCCatDeclInfo; + + return nullptr; +@@ -768,13 +767,12 @@ + return nullptr; + + const DeclInfo *DI = static_cast(DInfo); +- +- if (const ObjCInterfaceDeclInfo * +- InterInfo = dyn_cast(DI)) ++ ++ if (const ObjCInterfaceDeclInfo *InterInfo = ++ dyn_cast(DI)) + return InterInfo->ObjCInterDeclInfo.protocols; +- +- if (const ObjCProtocolDeclInfo * +- ProtInfo = dyn_cast(DI)) ++ ++ if (const ObjCProtocolDeclInfo *ProtInfo = dyn_cast(DI)) + return &ProtInfo->ObjCProtoRefListInfo; + + if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast(DI)) +@@ -801,8 +799,8 @@ + return nullptr; + + const AttrInfo *DI = static_cast(AInfo); +- if (const IBOutletCollectionInfo * +- IBInfo = dyn_cast(DI)) ++ if (const IBOutletCollectionInfo *IBInfo = ++ dyn_cast(DI)) + return &IBInfo->IBCollInfo; + + return nullptr; +@@ -860,17 +858,14 @@ + delete static_cast(idxAction); + } + +-int clang_indexSourceFile(CXIndexAction idxAction, +- CXClientData client_data, ++int clang_indexSourceFile(CXIndexAction idxAction, CXClientData client_data, + IndexerCallbacks *index_callbacks, +- unsigned index_callbacks_size, +- unsigned index_options, ++ unsigned index_callbacks_size, unsigned index_options, + const char *source_filename, +- const char * const *command_line_args, ++ const char *const *command_line_args, + int num_command_line_args, + struct CXUnsavedFile *unsaved_files, +- unsigned num_unsaved_files, +- CXTranslationUnit *out_TU, ++ unsigned num_unsaved_files, CXTranslationUnit *out_TU, + unsigned TU_options) { + SmallVector Args; + Args.push_back("clang"); +@@ -909,7 +904,8 @@ + llvm::CrashRecoveryContext CRC; + + if (!RunSafely(CRC, IndexSourceFileImpl)) { +- fprintf(stderr, "libclang: crash detected during indexing source file: {\n"); ++ fprintf(stderr, ++ "libclang: crash detected during indexing source file: {\n"); + fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); + fprintf(stderr, " 'command_line_args' : ["); + for (int i = 0; i != num_command_line_args; ++i) { +@@ -928,7 +924,7 @@ + fprintf(stderr, "],\n"); + fprintf(stderr, " 'options' : %d,\n", TU_options); + fprintf(stderr, "}\n"); +- ++ + return 1; + } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { + if (out_TU) +@@ -942,11 +938,8 @@ + CXClientData client_data, + IndexerCallbacks *index_callbacks, + unsigned index_callbacks_size, +- unsigned index_options, +- CXTranslationUnit TU) { +- LOG_FUNC_SECTION { +- *Log << TU; +- } ++ unsigned index_options, CXTranslationUnit TU) { ++ LOG_FUNC_SECTION { *Log << TU; } + + CXErrorCode result; + auto IndexTranslationUnitImpl = [=, &result]() { +@@ -959,7 +952,7 @@ + + if (!RunSafely(CRC, IndexTranslationUnitImpl)) { + fprintf(stderr, "libclang: crash detected during indexing TU\n"); +- ++ + return 1; } -+#endif - Fn(); +@@ -967,23 +960,26 @@ + } + + void clang_indexLoc_getFileLocation(CXIdxLoc location, +- CXIdxClientFile *indexFile, +- CXFile *file, +- unsigned *line, +- unsigned *column, ++ CXIdxClientFile *indexFile, CXFile *file, ++ unsigned *line, unsigned *column, + unsigned *offset) { +- if (indexFile) *indexFile = nullptr; +- if (file) *file = nullptr; +- if (line) *line = 0; +- if (column) *column = 0; +- if (offset) *offset = 0; ++ if (indexFile) ++ *indexFile = nullptr; ++ if (file) ++ *file = nullptr; ++ if (line) ++ *line = 0; ++ if (column) ++ *column = 0; ++ if (offset) ++ *offset = 0; + + SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); + if (!location.ptr_data[0] || Loc.isInvalid()) + return; + + CXIndexDataConsumer &DataConsumer = +- *static_cast(location.ptr_data[0]); ++ *static_cast(location.ptr_data[0]); + DataConsumer.translateLoc(Loc, indexFile, file, line, column, offset); + } + +@@ -993,6 +989,6 @@ + return clang_getNullLocation(); + + CXIndexDataConsumer &DataConsumer = +- *static_cast(location.ptr_data[0]); ++ *static_cast(location.ptr_data[0]); + return cxloc::translateSourceLocation(DataConsumer.getASTContext(), Loc); + } +diff '--color=auto' -r -u llvm/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp build/llvm-src/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp +--- llvm/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp 2023-12-29 16:36:20.201382144 +0100 ++++ build/llvm-src/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp 2024-01-02 03:15:13.627395020 +0100 +@@ -136,7 +136,7 @@ + std::error_code OK; + + const char *Overview = +- R"(Generates documentation from source code and comments. ++ R"(Generates documentation from source code and comments. + + Example usage for files without flags (default): + +@@ -264,13 +264,13 @@ + + // Add a reference to this Info in the Index + { +- std::lock_guard Guard(IndexMutex); ++ llvm::lock_guard Guard(IndexMutex); + clang::doc::Generator::addInfoToIndex(CDCtx.Idx, Reduced.get().get()); + } + + // Save in the result map (needs a lock due to threaded access). + { +- std::lock_guard Guard(USRToInfoMutex); ++ llvm::lock_guard Guard(USRToInfoMutex); + USRToInfo[Group.getKey()] = std::move(Reduced.get()); + } + }); +diff '--color=auto' -r -u llvm/clang-tools-extra/clang-include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp build/llvm-src/clang-tools-extra/clang-include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp +--- llvm/clang-tools-extra/clang-include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp 2023-12-29 16:36:20.203382254 +0100 ++++ build/llvm-src/clang-tools-extra/clang-include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp 2024-01-02 03:15:13.627395020 +0100 +@@ -78,10 +78,10 @@ + bool Merge(llvm::StringRef MergeDir, llvm::StringRef OutputFile) { + std::error_code EC; + SymbolInfo::SignalMap Symbols; +- std::mutex SymbolMutex; ++ llvm::mutex SymbolMutex; + auto AddSymbols = [&](ArrayRef NewSymbols) { + // Synchronize set accesses. +- std::unique_lock LockGuard(SymbolMutex); ++ llvm::unique_lock LockGuard(SymbolMutex); + for (const auto &Symbol : NewSymbols) { + Symbols[Symbol.Symbol] += Symbol.Signals; + } +@@ -124,8 +124,8 @@ return true; -@@ -471,7 +485,9 @@ bool CrashRecoveryContext::throwIfCrash(int RetCode) { - ::RaiseException(RetCode, 0, 0, NULL); + } + +-} // namespace clang + } // namespace find_all_symbols ++} // namespace clang + + int main(int argc, const char **argv) { + auto ExpectedParser = +diff '--color=auto' -r -u llvm/clang-tools-extra/clang-include-fixer/SymbolIndexManager.h build/llvm-src/clang-tools-extra/clang-include-fixer/SymbolIndexManager.h +--- llvm/clang-tools-extra/clang-include-fixer/SymbolIndexManager.h 2023-12-29 16:36:20.202382199 +0100 ++++ build/llvm-src/clang-tools-extra/clang-include-fixer/SymbolIndexManager.h 2024-01-02 03:15:13.627395020 +0100 +@@ -12,15 +12,14 @@ + #include "SymbolIndex.h" + #include "find-all-symbols/SymbolInfo.h" + #include "llvm/ADT/StringRef.h" ++#include "llvm/Support/thread.h" + + #ifdef _MSC_VER + // Disable warnings from ppltasks.h transitively included by . + #pragma warning(push) +-#pragma warning(disable:4530) ++#pragma warning(disable : 4530) + #endif + +-#include +- + #ifdef _MSC_VER + #pragma warning(pop) + #endif +@@ -34,11 +33,11 @@ + public: + void addSymbolIndex(std::function()> F) { + #if LLVM_ENABLE_THREADS +- auto Strategy = std::launch::async; ++ auto Strategy = llvm::launch::async; #else - llvm::sys::unregisterHandlers(); -+#ifndef __wasi__ - raise(RetCode - 128); +- auto Strategy = std::launch::deferred; ++ auto Strategy = llvm::launch::deferred; + #endif +- SymbolIndices.push_back(std::async(Strategy, F)); ++ SymbolIndices.push_back(llvm::async(Strategy, F)); + } + + /// Search for header files to be included for an identifier. +@@ -56,7 +55,7 @@ + llvm::StringRef FileName = "") const; + + private: +- std::vector>> SymbolIndices; ++ std::vector>> SymbolIndices; + }; + + } // namespace include_fixer +Only in llvm/libcxx/include: .future.swp +diff '--color=auto' -r -u llvm/lld/COFF/Driver.cpp build/llvm-src/lld/COFF/Driver.cpp +--- llvm/lld/COFF/Driver.cpp 2023-12-29 21:47:41.708665354 +0100 ++++ build/llvm-src/lld/COFF/Driver.cpp 2024-01-02 03:15:13.628395075 +0100 +@@ -47,10 +47,10 @@ + #include "llvm/Support/TargetSelect.h" + #include "llvm/Support/VirtualFileSystem.h" + #include "llvm/Support/raw_ostream.h" ++#include "llvm/Support/thread.h" + #include "llvm/TargetParser/Triple.h" + #include "llvm/ToolDrivers/llvm-lib/LibDriver.h" + #include +-#include + #include + #include + #include +@@ -128,18 +128,18 @@ + // (a limited resource on Windows) for the duration that the future is pending. + using MBErrPair = std::pair, std::error_code>; + +-// Create a std::future that opens and maps a file using the best strategy for ++// Create a llvm::future that opens and maps a file using the best strategy for + // the host platform. +-static std::future createFutureForFile(std::string path) { ++static llvm::future createFutureForFile(std::string path) { + #if _WIN64 + // On Windows, file I/O is relatively slow so it is best to do this + // asynchronously. But 32-bit has issues with potentially launching tons + // of threads +- auto strategy = std::launch::async; ++ auto strategy = llvm::launch::async; + #else +- auto strategy = std::launch::deferred; ++ auto strategy = llvm::launch::deferred; + #endif +- return std::async(strategy, [=]() { ++ return llvm::async(strategy, [=]() { + auto mbOrErr = MemoryBuffer::getFile(path, /*IsText=*/false, + /*RequiresNullTerminator=*/false); + if (!mbOrErr) +@@ -243,7 +243,7 @@ + } + + void LinkerDriver::enqueuePath(StringRef path, bool wholeArchive, bool lazy) { +- auto future = std::make_shared>( ++ auto future = std::make_shared>( + createFutureForFile(std::string(path))); + std::string pathStr = std::string(path); + enqueueTask([=]() { +@@ -345,7 +345,7 @@ + "could not get the filename for the member defining symbol " + + toCOFFString(ctx, sym)); + auto future = +- std::make_shared>(createFutureForFile(childName)); ++ std::make_shared>(createFutureForFile(childName)); + enqueueTask([=]() { + auto mbOrErr = future->get(); + if (mbOrErr.second) +diff '--color=auto' -r -u llvm/lld/Common/ErrorHandler.cpp build/llvm-src/lld/Common/ErrorHandler.cpp +--- llvm/lld/Common/ErrorHandler.cpp 2023-12-29 16:36:22.526510111 +0100 ++++ build/llvm-src/lld/Common/ErrorHandler.cpp 2024-01-02 03:15:13.628395075 +0100 +@@ -46,7 +46,7 @@ + } + + void ErrorHandler::flushStreams() { +- std::lock_guard lock(mu); ++ llvm::lock_guard lock(mu); + outs().flush(); + errs().flush(); + } +@@ -172,16 +172,14 @@ + return std::string(logName); + + static std::regex regexes[] = { +- std::regex( +- R"(^undefined (?:\S+ )?symbol:.*\n)" +- R"(>>> referenced by .+\((\S+):(\d+)\))"), ++ std::regex(R"(^undefined (?:\S+ )?symbol:.*\n)" ++ R"(>>> referenced by .+\((\S+):(\d+)\))"), + std::regex( + R"(^undefined (?:\S+ )?symbol:.*\n>>> referenced by (\S+):(\d+))"), + std::regex(R"(^undefined symbol:.*\n>>> referenced by (.*):)"), + std::regex( + R"(^duplicate symbol: .*\n>>> defined in (\S+)\n>>> defined in.*)"), +- std::regex( +- R"(^duplicate symbol: .*\n>>> defined at .+\((\S+):(\d+)\))"), ++ std::regex(R"(^duplicate symbol: .*\n>>> defined at .+\((\S+):(\d+)\))"), + std::regex(R"(^duplicate symbol: .*\n>>> defined at (\S+):(\d+))"), + std::regex( + R"(.*\n>>> defined in .*\n>>> referenced by .+\((\S+):(\d+)\))"), +@@ -224,14 +222,14 @@ + void ErrorHandler::log(const Twine &msg) { + if (!verbose || disableOutput) + return; +- std::lock_guard lock(mu); ++ llvm::lock_guard lock(mu); + reportDiagnostic(logName, Colors::RESET, "", msg); + } + + void ErrorHandler::message(const Twine &msg, llvm::raw_ostream &s) { + if (disableOutput) + return; +- std::lock_guard lock(mu); ++ llvm::lock_guard lock(mu); + s << msg << "\n"; + s.flush(); + } +@@ -245,7 +243,7 @@ + if (suppressWarnings) + return; + +- std::lock_guard lock(mu); ++ llvm::lock_guard lock(mu); + reportDiagnostic(getLocation(msg), Colors::MAGENTA, "warning", msg); + sep = getSeparator(msg); + } +@@ -269,7 +267,7 @@ + + bool exit = false; + { +- std::lock_guard lock(mu); ++ llvm::lock_guard lock(mu); + + if (errorLimit == 0 || errorCount < errorLimit) { + reportDiagnostic(getLocation(msg), Colors::RED, "error", msg); +diff '--color=auto' -r -u llvm/lld/Common/Filesystem.cpp build/llvm-src/lld/Common/Filesystem.cpp +--- llvm/lld/Common/Filesystem.cpp 2023-12-29 16:36:22.526510111 +0100 ++++ build/llvm-src/lld/Common/Filesystem.cpp 2024-01-02 03:15:13.628395075 +0100 +@@ -18,10 +18,10 @@ + #include "llvm/Support/Parallel.h" + #include "llvm/Support/Path.h" + #include "llvm/Support/TimeProfiler.h" ++#include "llvm/Support/thread.h" + #if LLVM_ON_UNIX + #include + #endif +-#include + + using namespace llvm; + using namespace lld; +@@ -91,12 +91,12 @@ + return; + + // close and therefore remove TempPath in background. +- std::mutex m; +- std::condition_variable cv; ++ llvm::mutex m; ++ llvm::condition_variable cv; + bool started = false; +- std::thread([&, fd] { ++ llvm::thread([&, fd] { + { +- std::lock_guard l(m); ++ llvm::lock_guard l(m); + started = true; + cv.notify_all(); + } +@@ -105,7 +105,7 @@ + + // GLIBC 2.26 and earlier have race condition that crashes an entire process + // if the main thread calls exit(2) while other thread is starting up. +- std::unique_lock l(m); ++ llvm::unique_lock l(m); + cv.wait(l, [&] { return started; }); + #endif + } +diff '--color=auto' -r -u llvm/lld/ELF/InputFiles.cpp build/llvm-src/lld/ELF/InputFiles.cpp +--- llvm/lld/ELF/InputFiles.cpp 2023-12-29 21:47:41.711665519 +0100 ++++ build/llvm-src/lld/ELF/InputFiles.cpp 2024-01-02 03:15:13.628395075 +0100 +@@ -30,6 +30,7 @@ + #include "llvm/Support/RISCVAttributeParser.h" + #include "llvm/Support/TarWriter.h" + #include "llvm/Support/raw_ostream.h" ++#include "llvm/Support/thread.h" + #include + + using namespace llvm; +@@ -55,12 +56,12 @@ + + // Returns "", "foo.a(bar.o)" or "baz.o". + std::string lld::toString(const InputFile *f) { +- static std::mutex mu; ++ static llvm::mutex mu; + if (!f) + return ""; + + { +- std::lock_guard lock(mu); ++ llvm::lock_guard lock(mu); + if (f->toStringCache.empty()) { + if (f->archiveName.empty()) + f->toStringCache = f->getName(); +@@ -1173,7 +1174,7 @@ + // Called after all ObjFile::parse is called for all ObjFiles. This checks + // duplicate symbols and may do symbol property merge in the future. + template void ObjFile::postParse() { +- static std::mutex mu; ++ static llvm::mutex mu; + ArrayRef eSyms = this->getELFSyms(); + for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { + const Elf_Sym &eSym = eSyms[i]; +@@ -1214,7 +1215,7 @@ + sym.getName()); + } + if (sym.file == this) { +- std::lock_guard lock(mu); ++ llvm::lock_guard lock(mu); + ctx.nonPrevailingSyms.emplace_back(&sym, secIdx); + } + continue; +@@ -1227,7 +1228,7 @@ + + if (sym.binding == STB_WEAK || binding == STB_WEAK) + continue; +- std::lock_guard lock(mu); ++ llvm::lock_guard lock(mu); + ctx.duplicates.push_back({&sym, this, sec, eSym.st_value}); + } + } +@@ -1678,9 +1679,10 @@ + llvm_unreachable("unknown visibility"); + } + +-static void +-createBitcodeSymbol(Symbol *&sym, const std::vector &keptComdats, +- const lto::InputFile::Symbol &objSym, BitcodeFile &f) { ++static void createBitcodeSymbol(Symbol *&sym, ++ const std::vector &keptComdats, ++ const lto::InputFile::Symbol &objSym, ++ BitcodeFile &f) { + uint8_t binding = objSym.isWeak() ? STB_WEAK : STB_GLOBAL; + uint8_t type = objSym.isTLS() ? STT_TLS : STT_NOTYPE; + uint8_t visibility = mapVisibility(objSym.getVisibility()); +diff '--color=auto' -r -u llvm/lld/ELF/InputSection.cpp build/llvm-src/lld/ELF/InputSection.cpp +--- llvm/lld/ELF/InputSection.cpp 2023-12-29 21:47:41.711665519 +0100 ++++ build/llvm-src/lld/ELF/InputSection.cpp 2024-01-02 03:15:13.628395075 +0100 +@@ -19,9 +19,9 @@ + #include "llvm/Support/Compiler.h" + #include "llvm/Support/Compression.h" + #include "llvm/Support/Endian.h" ++#include "llvm/Support/thread.h" + #include "llvm/Support/xxhash.h" + #include +-#include + #include + #include + +@@ -73,7 +73,7 @@ + // If SHF_COMPRESSED is set, parse the header. The legacy .zdebug format is no + // longer supported. + if (flags & SHF_COMPRESSED) +- invokeELFT(parseCompressedHeader,); ++ invokeELFT(parseCompressedHeader, ); + } + + // Drop SHF_GROUP bit unless we are producing a re-linkable object file. +@@ -123,8 +123,8 @@ + void InputSectionBase::decompress() const { + uint8_t *uncompressedBuf; + { +- static std::mutex mu; +- std::lock_guard lock(mu); ++ static llvm::mutex mu; ++ llvm::lock_guard lock(mu); + uncompressedBuf = bAlloc().Allocate(size); + } + +@@ -1139,7 +1139,8 @@ + // conservative. + if (Defined *d = dyn_cast(rel.sym)) + if (InputSection *isec = cast_or_null(d->section)) +- if (!isec || !isec->getFile() || isec->getFile()->splitStack) ++ if (!isec || !isec->getFile() || ++ isec->getFile()->splitStack) + continue; + + if (enclosingPrologueAttempted(rel.offset, prologues)) +diff '--color=auto' -r -u llvm/lld/ELF/Relocations.cpp build/llvm-src/lld/ELF/Relocations.cpp +--- llvm/lld/ELF/Relocations.cpp 2023-12-29 21:47:41.712665574 +0100 ++++ build/llvm-src/lld/ELF/Relocations.cpp 2024-01-02 03:15:13.629395130 +0100 +@@ -56,6 +56,7 @@ + #include "llvm/BinaryFormat/ELF.h" + #include "llvm/Demangle/Demangle.h" + #include "llvm/Support/Endian.h" ++#include "llvm/Support/thread.h" + #include + + using namespace llvm; +@@ -504,8 +505,7 @@ + } + + // Custom error message if Sym is defined in a discarded section. +-template +-static std::string maybeReportDiscarded(Undefined &sym) { ++template static std::string maybeReportDiscarded(Undefined &sym) { + auto *file = dyn_cast_or_null>(sym.file); + if (!file || !sym.discardedSecIdx) + return ""; +@@ -558,8 +558,8 @@ + }; + + std::vector undefs; +-std::mutex relocMutex; +-} ++llvm::mutex relocMutex; ++} // namespace + + // Check whether the definition name def is a mangled function name that matches + // the reference name ref. +@@ -804,7 +804,7 @@ + // Returns true if the undefined symbol will produce an error message. + static bool maybeReportUndefined(Undefined &sym, InputSectionBase &sec, + uint64_t offset) { +- std::lock_guard lock(relocMutex); ++ llvm::lock_guard lock(relocMutex); + // If versioned, issue an error (even if the symbol is weak) because we don't + // know the defining filename which is required to construct a Verneed entry. + if (sym.hasVersionSuffix) { +@@ -860,7 +860,7 @@ + Partition &part = isec.getPartition(); + + if (sym.isTagged()) { +- std::lock_guard lock(relocMutex); ++ llvm::lock_guard lock(relocMutex); + part.relaDyn->addRelativeReloc(target->relativeRel, isec, offsetInSec, sym, + addend, type, expr); + // With MTE globals, we always want to derive the address tag by `ldg`-ing +@@ -1016,7 +1016,7 @@ + // We set the final symbols values for linker script defined symbols later. + // They always can be computed as a link time constant. + if (sym.scriptDefined) +- return true; ++ return true; + + error("relocation " + toString(type) + " cannot refer to absolute symbol: " + + toString(sym) + getLocation(*sec, sym, relOff)); +@@ -1067,7 +1067,7 @@ + // We were asked not to generate PLT entries for ifuncs. Instead, pass the + // direct relocation on through. + if (LLVM_UNLIKELY(isIfunc) && config->zIfuncNoplt) { +- std::lock_guard lock(relocMutex); ++ llvm::lock_guard lock(relocMutex); + sym.exportDynamic = true; + mainPart->relaDyn->addSymbolReloc(type, *sec, offset, sym, addend, type); + return; +@@ -1132,7 +1132,7 @@ + } else if (rel != 0) { + if (config->emachine == EM_MIPS && rel == target->symbolicRel) + rel = target->relativeRel; +- std::lock_guard lock(relocMutex); ++ llvm::lock_guard lock(relocMutex); + sec->getPartition().relaDyn->addSymbolReloc(rel, *sec, offset, sym, + addend, type); + +@@ -1282,11 +1282,10 @@ + // relaxation. + // For PPC64, if the file has missing R_PPC64_TLSGD/R_PPC64_TLSLD, disable + // relaxation as well. +- bool toExecRelax = !config->shared && config->emachine != EM_ARM && +- config->emachine != EM_HEXAGON && +- config->emachine != EM_LOONGARCH && +- config->emachine != EM_RISCV && +- !c.file->ppc64DisableTLSRelax; ++ bool toExecRelax = ++ !config->shared && config->emachine != EM_ARM && ++ config->emachine != EM_HEXAGON && config->emachine != EM_LOONGARCH && ++ config->emachine != EM_RISCV && !c.file->ppc64DisableTLSRelax; + + // If we are producing an executable and the symbol is non-preemptable, it + // must be defined and the code sequence can be relaxed to use Local-Exec. +diff '--color=auto' -r -u llvm/lld/include/lld/Common/ErrorHandler.h build/llvm-src/lld/include/lld/Common/ErrorHandler.h +--- llvm/lld/include/lld/Common/ErrorHandler.h 2023-12-29 16:36:22.539510827 +0100 ++++ build/llvm-src/lld/include/lld/Common/ErrorHandler.h 2024-01-02 03:15:13.629395130 +0100 +@@ -73,12 +73,12 @@ + #include "llvm/ADT/STLExtras.h" + #include "llvm/Support/Error.h" + #include "llvm/Support/FileOutputBuffer.h" +-#include ++#include "llvm/Support/thread.h" + + namespace llvm { + class DiagnosticInfo; + class raw_ostream; +-} ++} // namespace llvm + + namespace lld { + +@@ -136,7 +136,7 @@ + // be indirectly called from multiple threads, we protect them using a mutex. + // In the future, we plan on supporting several concurrent linker contexts, + // which explains why the mutex is not a global but part of this context. +- std::mutex mu; ++ llvm::mutex mu; + llvm::raw_ostream *stdoutOS{}; + llvm::raw_ostream *stderrOS{}; + }; +diff '--color=auto' -r -u llvm/lld/include/lld/Common/Memory.h build/llvm-src/lld/include/lld/Common/Memory.h +--- llvm/lld/include/lld/Common/Memory.h 2023-12-29 16:36:22.539510827 +0100 ++++ build/llvm-src/lld/include/lld/Common/Memory.h 2024-01-02 04:11:00.425366127 +0100 +@@ -57,7 +57,7 @@ + + // Creates new instances of T off a (almost) contiguous arena/object pool. The + // instances are destroyed whenever lldMain() goes out of scope. +-template T *make(U &&... args) { ++template T *make(U &&...args) { + return new (getSpecificAllocSingleton().Allocate()) + T(std::forward(args)...); + } +@@ -65,7 +65,11 @@ + template + inline llvm::SpecificBumpPtrAllocator & + getSpecificAllocSingletonThreadLocal() { ++#if LLVM_ENABLE_THREADS + thread_local SpecificAlloc instance; ++#else ++ static SpecificAlloc instance; +#endif + return instance.alloc; + } + +Only in build/llvm-src/lld/include/lld/Common: .Memory.h.swp +diff '--color=auto' -r -u llvm/lld/MachO/Writer.cpp build/llvm-src/lld/MachO/Writer.cpp +--- llvm/lld/MachO/Writer.cpp 2023-12-29 16:36:22.537510716 +0100 ++++ build/llvm-src/lld/MachO/Writer.cpp 2024-01-02 03:15:13.629395130 +0100 +@@ -30,6 +30,7 @@ + #include "llvm/Support/Path.h" + #include "llvm/Support/ThreadPool.h" + #include "llvm/Support/TimeProfiler.h" ++#include "llvm/Support/thread.h" + #include "llvm/Support/xxhash.h" + + #include +@@ -1104,13 +1105,13 @@ + symtabSection, indirectSymtabSection, + dataInCodeSection, functionStartsSection, + }; +- SmallVector> threadFutures; ++ SmallVector> threadFutures; + threadFutures.reserve(linkEditSections.size()); + for (LinkEditSection *osec : linkEditSections) + if (osec) + threadFutures.emplace_back(threadPool.async( + [](LinkEditSection *osec) { osec->finalizeContents(); }, osec)); +- for (std::shared_future &future : threadFutures) ++ for (llvm::shared_future &future : threadFutures) + future.wait(); + + // Now that __LINKEDIT is filled out, do a proper calculation of its +@@ -1185,12 +1186,12 @@ + std::vector> chunks = split(data, 1024 * 1024); + // Leave one slot for filename + std::vector hashes(chunks.size() + 1); +- SmallVector> threadFutures; ++ SmallVector> threadFutures; + threadFutures.reserve(chunks.size()); + for (size_t i = 0; i < chunks.size(); ++i) + threadFutures.emplace_back(threadPool.async( + [&](size_t j) { hashes[j] = xxh3_64bits(chunks[j]); }, i)); +- for (std::shared_future &future : threadFutures) ++ for (llvm::shared_future &future : threadFutures) + future.wait(); + // Append the output filename so that identical binaries with different names + // don't get the same UUID. +diff '--color=auto' -r -u llvm/llvm/cmake/modules/HandleLLVMOptions.cmake build/llvm-src/llvm/cmake/modules/HandleLLVMOptions.cmake +--- llvm/llvm/cmake/modules/HandleLLVMOptions.cmake 2023-12-29 21:47:41.761668277 +0100 ++++ build/llvm-src/llvm/cmake/modules/HandleLLVMOptions.cmake 2024-01-02 03:15:13.629395130 +0100 +@@ -186,6 +186,10 @@ + else() + set(LLVM_HAVE_LINK_VERSION_SCRIPT 1) + endif() ++elseif(WASI) ++ set(LLVM_ON_WIN32 0) ++ set(LLVM_ON_UNIX 1) ++ set(LLVM_HAVE_LINK_VERSION_SCRIPT 0) + elseif(CMAKE_SYSTEM_NAME STREQUAL "Generic") + set(LLVM_ON_WIN32 0) + set(LLVM_ON_UNIX 0) +diff '--color=auto' -r -u llvm/llvm/include/llvm/ADT/bit.h build/llvm-src/llvm/include/llvm/ADT/bit.h +--- llvm/llvm/include/llvm/ADT/bit.h 2023-12-29 21:47:41.774668994 +0100 ++++ build/llvm-src/llvm/include/llvm/ADT/bit.h 2024-01-02 03:15:13.629395130 +0100 +@@ -24,12 +24,12 @@ #endif - return true; + + #if defined(_MSC_VER) && !defined(_DEBUG) +-#include // for _byteswap_{ushort,ulong,uint64} ++#include // for _byteswap_{ushort,ulong,uint64} + #endif + + #if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \ + defined(__Fuchsia__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) || \ +- defined(__OpenBSD__) || defined(__DragonFly__) ++ defined(__OpenBSD__) || defined(__DragonFly__) || defined(__wasi__) + #include + #elif defined(_AIX) + #include +diff '--color=auto' -r -u llvm/llvm/include/llvm/DebugInfo/GSYM/GsymCreator.h build/llvm-src/llvm/include/llvm/DebugInfo/GSYM/GsymCreator.h +--- llvm/llvm/include/llvm/DebugInfo/GSYM/GsymCreator.h 2023-12-29 16:36:23.143544071 +0100 ++++ build/llvm-src/llvm/include/llvm/DebugInfo/GSYM/GsymCreator.h 2024-01-02 03:15:13.629395130 +0100 +@@ -11,8 +11,6 @@ + + #include + #include +-#include +-#include + + #include "llvm/ADT/AddressRanges.h" + #include "llvm/ADT/ArrayRef.h" +@@ -23,6 +21,7 @@ + #include "llvm/Support/Endian.h" + #include "llvm/Support/Error.h" + #include "llvm/Support/Path.h" ++#include "llvm/Support/thread.h" + + namespace llvm { + +@@ -132,7 +131,7 @@ + /// of FunctionInfo objects, see "llvm/DebugInfo/GSYM/FunctionInfo.h". + class GsymCreator { + // Private member variables require Mutex protections +- mutable std::mutex Mutex; ++ mutable llvm::mutex Mutex; + std::vector Funcs; + StringTableBuilder StrTab; + StringSet<> StringStorage; +@@ -147,7 +146,6 @@ + bool Finalized = false; + bool Quiet; + +- + /// Get the first function start address. + /// + /// \returns The start address of the first FunctionInfo or std::nullopt if +@@ -286,9 +284,7 @@ + /// When we have a segment, we know that function infos will be added in + /// ascending address range order without having to be finalized. We also + /// don't need to sort and unique entries during the finalize function call. +- void setIsSegment() { +- IsSegment = true; +- } ++ void setIsSegment() { IsSegment = true; } + + public: + GsymCreator(bool Quiet = false); +@@ -373,8 +369,7 @@ + /// + /// \param Callback A callback function that will get called with each + /// FunctionInfo. If the callback returns false, stop iterating. +- void forEachFunctionInfo( +- std::function const &Callback); ++ void forEachFunctionInfo(std::function const &Callback); + + /// Thread safe const iteration over all function infos. + /// +@@ -429,14 +424,11 @@ + /// + /// \param Addr The address to use as the base address of the GSYM file + /// when it is saved to disk. +- void setBaseAddress(uint64_t Addr) { +- BaseAddress = Addr; +- } ++ void setBaseAddress(uint64_t Addr) { BaseAddress = Addr; } + + /// Whether the transformation should be quiet, i.e. not output warnings. + bool isQuiet() const { return Quiet; } + +- + /// Create a segmented GSYM creator starting with function info index + /// \a FuncIdx. + /// +diff '--color=auto' -r -u llvm/llvm/include/llvm/Debuginfod/Debuginfod.h build/llvm-src/llvm/include/llvm/Debuginfod/Debuginfod.h +--- llvm/llvm/include/llvm/Debuginfod/Debuginfod.h 2023-12-29 21:47:41.786669656 +0100 ++++ build/llvm-src/llvm/include/llvm/Debuginfod/Debuginfod.h 2024-01-02 03:15:13.629395130 +0100 +@@ -92,8 +92,8 @@ + }; + + class DebuginfodLog { +- std::mutex QueueMutex; +- std::condition_variable QueueCondition; ++ llvm::mutex QueueMutex; ++ llvm::condition_variable QueueCondition; + std::queue LogEntryQueue; + + public: +diff '--color=auto' -r -u llvm/llvm/include/llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h build/llvm-src/llvm/include/llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h +--- llvm/llvm/include/llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h 2023-12-29 16:36:23.150544456 +0100 ++++ build/llvm-src/llvm/include/llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h 2024-01-02 03:15:13.629395130 +0100 +@@ -24,10 +24,9 @@ + #include "llvm/Support/MSVCErrorWorkarounds.h" + #include "llvm/Support/Memory.h" + #include "llvm/Support/RecyclingAllocator.h" ++#include "llvm/Support/thread.h" + + #include +-#include +-#include + + namespace llvm { + namespace jitlink { +@@ -42,7 +41,6 @@ + /// and their implemetations should include any necessary synchronization. + class JITLinkMemoryManager { + public: +- + /// Represents a finalized allocation. + /// + /// Finalized allocations must be passed to the +diff '--color=auto' -r -u llvm/llvm/include/llvm/IR/ValueMap.h build/llvm-src/llvm/include/llvm/IR/ValueMap.h +--- llvm/llvm/include/llvm/IR/ValueMap.h 2023-12-29 16:36:23.171545612 +0100 ++++ build/llvm-src/llvm/include/llvm/IR/ValueMap.h 2024-01-02 03:15:13.630395186 +0100 +@@ -42,18 +42,15 @@ + + namespace llvm { + +-template ++template + class ValueMapCallbackVH; +-template +-class ValueMapIterator; +-template +-class ValueMapConstIterator; ++template class ValueMapIterator; ++template class ValueMapConstIterator; + + /// This class defines the default behavior for configurable aspects of + /// ValueMap<>. User Configs should inherit from this class to be as compatible + /// as possible with future versions of ValueMap. +-template +-struct ValueMapConfig { ++template struct ValueMapConfig { + using mutex_type = MutexT; + + /// If FollowRAUW is true, the ValueMap will update mappings on RAUW. If it's +@@ -66,21 +63,24 @@ + // override all the defaults. + struct ExtraData {}; + +- template ++ template + static void onRAUW(const ExtraDataT & /*Data*/, KeyT /*Old*/, KeyT /*New*/) {} +- template +- static void onDelete(const ExtraDataT &/*Data*/, KeyT /*Old*/) {} ++ template ++ static void onDelete(const ExtraDataT & /*Data*/, KeyT /*Old*/) {} + + /// Returns a mutex that should be acquired around any changes to the map. + /// This is only acquired from the CallbackVH (and held around calls to onRAUW + /// and onDelete) and not inside other ValueMap methods. NULL means that no + /// mutex is necessary. +- template +- static mutex_type *getMutex(const ExtraDataT &/*Data*/) { return nullptr; } ++ template ++ static mutex_type *getMutex(const ExtraDataT & /*Data*/) { ++ return nullptr; ++ } + }; + + /// See the file comment. +-template> ++template > + class ValueMap { + friend class ValueMapCallbackVH; + +@@ -152,9 +152,7 @@ + return Map.find_as(Val) == Map.end() ? 0 : 1; + } + +- iterator find(const KeyT &Val) { +- return iterator(Map.find_as(Val)); +- } ++ iterator find(const KeyT &Val) { return iterator(Map.find_as(Val)); } + const_iterator find(const KeyT &Val) const { + return const_iterator(Map.find_as(Val)); + } +@@ -181,8 +179,7 @@ + } + + /// insert - Range insertion of pairs. +- template +- void insert(InputIt I, InputIt E) { ++ template void insert(InputIt I, InputIt E) { + for (; I != E; ++I) + insert(*I); + } +@@ -195,17 +192,13 @@ + Map.erase(I); + return true; + } +- void erase(iterator I) { +- return Map.erase(I.base()); +- } ++ void erase(iterator I) { return Map.erase(I.base()); } + +- value_type& FindAndConstruct(const KeyT &Key) { ++ value_type &FindAndConstruct(const KeyT &Key) { + return Map.FindAndConstruct(Wrap(Key)); + } + +- ValueT &operator[](const KeyT &Key) { +- return Map[Wrap(Key)]; +- } ++ ValueT &operator[](const KeyT &Key) { return Map[Wrap(Key)]; } + + /// isPointerIntoBucketsArray - Return true if the specified pointer points + /// somewhere into the ValueMap's array of buckets (i.e. either to a key or +@@ -230,7 +223,7 @@ + // the const_cast incorrect) is if it gets inserted into the map. But then + // this function must have been called from a non-const method, making the + // const_cast ok. +- return ValueMapCVH(key, const_cast(this)); ++ return ValueMapCVH(key, const_cast(this)); + } + }; + +@@ -247,7 +240,7 @@ + ValueMapT *Map; + + ValueMapCallbackVH(KeyT Key, ValueMapT *Map) +- : CallbackVH(const_cast(static_cast(Key))), ++ : CallbackVH(const_cast(static_cast(Key))), + Map(Map) {} + + // Private constructor used to create empty/tombstone DenseMap keys. +@@ -260,11 +253,11 @@ + // Make a copy that won't get changed even when *this is destroyed. + ValueMapCallbackVH Copy(*this); + typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data); +- std::unique_lock Guard; ++ llvm::unique_lock Guard; + if (M) +- Guard = std::unique_lock(*M); +- Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this. +- Copy.Map->Map.erase(Copy); // Definitely destroys *this. ++ Guard = llvm::unique_lock(*M); ++ Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this. ++ Copy.Map->Map.erase(Copy); // Definitely destroys *this. + } + + void allUsesReplacedWith(Value *new_key) override { +@@ -273,9 +266,9 @@ + // Make a copy that won't get changed even when *this is destroyed. + ValueMapCallbackVH Copy(*this); + typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data); +- std::unique_lock Guard; ++ llvm::unique_lock Guard; + if (M) +- Guard = std::unique_lock(*M); ++ Guard = llvm::unique_lock(*M); + + KeyT typed_new_key = cast(new_key); + // Can destroy *this: +@@ -286,14 +279,14 @@ + // removed the old mapping. + if (I != Copy.Map->Map.end()) { + ValueT Target(std::move(I->second)); +- Copy.Map->Map.erase(I); // Definitely destroys *this. ++ Copy.Map->Map.erase(I); // Definitely destroys *this. + Copy.Map->insert(std::make_pair(typed_new_key, std::move(Target))); + } + } + } + }; + +-template ++template + struct DenseMapInfo> { + using VH = ValueMapCallbackVH; + +@@ -313,9 +306,7 @@ + return DenseMapInfo::getHashValue(Val); + } + +- static bool isEqual(const VH &LHS, const VH &RHS) { +- return LHS == RHS; +- } ++ static bool isEqual(const VH &LHS, const VH &RHS) { return LHS == RHS; } + + static bool isEqual(const KeyT &LHS, const VH &RHS) { + return LHS == RHS.getValPtr(); +@@ -342,7 +333,7 @@ + + struct ValueTypeProxy { + const KeyT first; +- ValueT& second; ++ ValueT &second; + + ValueTypeProxy *operator->() { return this; } + +@@ -356,23 +347,19 @@ + return Result; + } + +- ValueTypeProxy operator->() const { +- return operator*(); +- } ++ ValueTypeProxy operator->() const { return operator*(); } + +- bool operator==(const ValueMapIterator &RHS) const { +- return I == RHS.I; +- } +- bool operator!=(const ValueMapIterator &RHS) const { +- return I != RHS.I; +- } ++ bool operator==(const ValueMapIterator &RHS) const { return I == RHS.I; } ++ bool operator!=(const ValueMapIterator &RHS) const { return I != RHS.I; } + +- inline ValueMapIterator& operator++() { // Preincrement ++ inline ValueMapIterator &operator++() { // Preincrement + ++I; + return *this; + } +- ValueMapIterator operator++(int) { // Postincrement +- ValueMapIterator tmp = *this; ++*this; return tmp; ++ ValueMapIterator operator++(int) { // Postincrement ++ ValueMapIterator tmp = *this; ++ ++*this; ++ return tmp; + } + }; + +@@ -392,13 +379,13 @@ + ValueMapConstIterator() : I() {} + ValueMapConstIterator(BaseT I) : I(I) {} + ValueMapConstIterator(ValueMapIterator Other) +- : I(Other.base()) {} ++ : I(Other.base()) {} + + BaseT base() const { return I; } + + struct ValueTypeProxy { + const KeyT first; +- const ValueT& second; ++ const ValueT &second; + ValueTypeProxy *operator->() { return this; } + operator std::pair() const { + return std::make_pair(first, second); +@@ -410,23 +397,19 @@ + return Result; + } + +- ValueTypeProxy operator->() const { +- return operator*(); +- } ++ ValueTypeProxy operator->() const { return operator*(); } + +- bool operator==(const ValueMapConstIterator &RHS) const { +- return I == RHS.I; +- } +- bool operator!=(const ValueMapConstIterator &RHS) const { +- return I != RHS.I; +- } ++ bool operator==(const ValueMapConstIterator &RHS) const { return I == RHS.I; } ++ bool operator!=(const ValueMapConstIterator &RHS) const { return I != RHS.I; } + +- inline ValueMapConstIterator& operator++() { // Preincrement ++ inline ValueMapConstIterator &operator++() { // Preincrement + ++I; + return *this; + } +- ValueMapConstIterator operator++(int) { // Postincrement +- ValueMapConstIterator tmp = *this; ++*this; return tmp; ++ ValueMapConstIterator operator++(int) { // Postincrement ++ ValueMapConstIterator tmp = *this; ++ ++*this; ++ return tmp; + } + }; + +diff '--color=auto' -r -u llvm/llvm/include/llvm/Support/BalancedPartitioning.h build/llvm-src/llvm/include/llvm/Support/BalancedPartitioning.h +--- llvm/llvm/include/llvm/Support/BalancedPartitioning.h 2023-12-29 16:36:23.186546437 +0100 ++++ build/llvm-src/llvm/include/llvm/Support/BalancedPartitioning.h 2024-01-02 03:15:13.630395186 +0100 +@@ -41,10 +41,9 @@ + + #include "raw_ostream.h" + #include "llvm/ADT/ArrayRef.h" ++#include "llvm/Support/thread.h" + + #include +-#include +-#include + #include + #include + +@@ -116,8 +115,8 @@ + /// threads. + struct BPThreadPool { + ThreadPool &TheThreadPool; +- std::mutex mtx; +- std::condition_variable cv; ++ llvm::mutex mtx; ++ llvm::condition_variable cv; + /// The number of threads that could spawn more threads + std::atomic NumActiveThreads = 0; + /// Only true when all threads are down spawning new threads +diff '--color=auto' -r -u llvm/llvm/include/llvm/Support/FileCollector.h build/llvm-src/llvm/include/llvm/Support/FileCollector.h +--- llvm/llvm/include/llvm/Support/FileCollector.h 2023-12-29 16:36:23.189546603 +0100 ++++ build/llvm-src/llvm/include/llvm/Support/FileCollector.h 2024-01-02 03:15:13.630395186 +0100 +@@ -12,7 +12,7 @@ + #include "llvm/ADT/StringMap.h" + #include "llvm/ADT/StringSet.h" + #include "llvm/Support/VirtualFileSystem.h" +-#include ++#include "llvm/Support/thread.h" + #include + + namespace llvm { +@@ -42,7 +42,7 @@ + std::error_code &EC) = 0; + + /// Synchronizes access to internal data structures. +- std::mutex Mutex; ++ llvm::mutex Mutex; + + /// Tracks already seen files so they can be skipped. + StringSet<> Seen; +diff '--color=auto' -r -u llvm/llvm/include/llvm/Support/Mutex.h build/llvm-src/llvm/include/llvm/Support/Mutex.h +--- llvm/llvm/include/llvm/Support/Mutex.h 2023-12-29 16:36:23.191546713 +0100 ++++ build/llvm-src/llvm/include/llvm/Support/Mutex.h 2024-01-02 03:15:13.630395186 +0100 +@@ -1,4 +1,5 @@ +-//===- llvm/Support/Mutex.h - Mutex Operating System Concept -----*- C++ -*-===// ++//===- llvm/Support/Mutex.h - Mutex Operating System Concept -----*- C++ ++//-*-===// + // + // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + // See https://llvm.org/LICENSE.txt for license information. +@@ -14,62 +15,59 @@ + #define LLVM_SUPPORT_MUTEX_H + + #include "llvm/Support/Threading.h" ++#include "llvm/Support/thread.h" + #include +-#include + +-namespace llvm +-{ +- namespace sys +- { +- /// SmartMutex - A mutex with a compile time constant parameter that +- /// indicates whether this mutex should become a no-op when we're not +- /// running in multithreaded mode. +- template +- class SmartMutex { +- std::recursive_mutex impl; +- unsigned acquired = 0; +- +- public: +- bool lock() { +- if (!mt_only || llvm_is_multithreaded()) { +- impl.lock(); +- return true; +- } +- // Single-threaded debugging code. This would be racy in +- // multithreaded mode, but provides not basic checks in single +- // threaded mode. +- ++acquired; +- return true; +- } +- +- bool unlock() { +- if (!mt_only || llvm_is_multithreaded()) { +- impl.unlock(); +- return true; +- } +- // Single-threaded debugging code. This would be racy in +- // multithreaded mode, but provides not basic checks in single +- // threaded mode. +- assert(acquired && "Lock not acquired before release!"); +- --acquired; +- return true; +- } +- +- bool try_lock() { +- if (!mt_only || llvm_is_multithreaded()) +- return impl.try_lock(); +- return true; +- } +- }; +- +- /// Mutex - A standard, always enforced mutex. +- typedef SmartMutex Mutex; ++namespace llvm { ++namespace sys { ++/// SmartMutex - A mutex with a compile time constant parameter that ++/// indicates whether this mutex should become a no-op when we're not ++/// running in multithreaded mode. ++template class SmartMutex { ++ llvm::recursive_mutex impl; ++ unsigned acquired = 0; ++ ++public: ++ bool lock() { ++ if (!mt_only || llvm_is_multithreaded()) { ++ impl.lock(); ++ return true; ++ } ++ // Single-threaded debugging code. This would be racy in ++ // multithreaded mode, but provides not basic checks in single ++ // threaded mode. ++ ++acquired; ++ return true; ++ } + +- template +- using SmartScopedLock = std::lock_guard>; ++ bool unlock() { ++ if (!mt_only || llvm_is_multithreaded()) { ++ impl.unlock(); ++ return true; ++ } ++ // Single-threaded debugging code. This would be racy in ++ // multithreaded mode, but provides not basic checks in single ++ // threaded mode. ++ assert(acquired && "Lock not acquired before release!"); ++ --acquired; ++ return true; ++ } + +- typedef SmartScopedLock ScopedLock; ++ bool try_lock() { ++ if (!mt_only || llvm_is_multithreaded()) ++ return impl.try_lock(); ++ return true; + } +-} ++}; ++ ++/// Mutex - A standard, always enforced mutex. ++typedef SmartMutex Mutex; ++ ++template ++using SmartScopedLock = llvm::lock_guard>; ++ ++typedef SmartScopedLock ScopedLock; ++} // namespace sys ++} // namespace llvm + + #endif +diff '--color=auto' -r -u llvm/llvm/include/llvm/Support/Parallel.h build/llvm-src/llvm/include/llvm/Support/Parallel.h +--- llvm/llvm/include/llvm/Support/Parallel.h 2023-12-29 16:36:23.192546768 +0100 ++++ build/llvm-src/llvm/include/llvm/Support/Parallel.h 2024-01-02 03:15:13.630395186 +0100 +@@ -14,11 +14,10 @@ + #include "llvm/Support/Error.h" + #include "llvm/Support/MathExtras.h" + #include "llvm/Support/Threading.h" ++#include "llvm/Support/thread.h" + + #include +-#include + #include +-#include + + namespace llvm { + +@@ -58,8 +57,8 @@ + namespace detail { + class Latch { + uint32_t Count; +- mutable std::mutex Mutex; +- mutable std::condition_variable Cond; ++ mutable llvm::mutex Mutex; ++ mutable llvm::condition_variable Cond; + + public: + explicit Latch(uint32_t Count = 0) : Count(Count) {} +@@ -69,18 +68,18 @@ + } + + void inc() { +- std::lock_guard lock(Mutex); ++ llvm::lock_guard lock(Mutex); + ++Count; + } + + void dec() { +- std::lock_guard lock(Mutex); ++ llvm::lock_guard lock(Mutex); + if (--Count == 0) + Cond.notify_all(); + } + + void sync() const { +- std::unique_lock lock(Mutex); ++ llvm::unique_lock lock(Mutex); + Cond.wait(lock, [&] { return Count == 0; }); + } + }; +diff '--color=auto' -r -u llvm/llvm/include/llvm/Support/RWMutex.h build/llvm-src/llvm/include/llvm/Support/RWMutex.h +--- llvm/llvm/include/llvm/Support/RWMutex.h 2023-12-29 16:36:23.192546768 +0100 ++++ build/llvm-src/llvm/include/llvm/Support/RWMutex.h 2024-01-02 03:15:13.630395186 +0100 +@@ -15,9 +15,8 @@ + + #include "llvm/Config/llvm-config.h" + #include "llvm/Support/Threading.h" ++#include "llvm/Support/thread.h" + #include +-#include +-#include + + #if defined(__APPLE__) + #define LLVM_USE_RW_MUTEX_IMPL +@@ -90,7 +89,7 @@ + /// running in multithreaded mode. + template class SmartRWMutex { + #if !defined(LLVM_USE_RW_MUTEX_IMPL) +- std::shared_mutex impl; ++ llvm::shared_mutex impl; + #else + RWMutexImpl impl; + #endif +@@ -155,7 +154,7 @@ + /// ScopedReader - RAII acquisition of a reader lock + #if !defined(LLVM_USE_RW_MUTEX_IMPL) + template +-using SmartScopedReader = const std::shared_lock>; ++using SmartScopedReader = const llvm::shared_lock>; + #else + template struct SmartScopedReader { + SmartRWMutex &mutex; +@@ -172,7 +171,7 @@ + /// ScopedWriter - RAII acquisition of a writer lock + #if !defined(LLVM_USE_RW_MUTEX_IMPL) + template +-using SmartScopedWriter = std::lock_guard>; ++using SmartScopedWriter = llvm::lock_guard>; + #else + template struct SmartScopedWriter { + SmartRWMutex &mutex; +diff '--color=auto' -r -u llvm/llvm/include/llvm/Support/thread.h build/llvm-src/llvm/include/llvm/Support/thread.h +--- llvm/llvm/include/llvm/Support/thread.h 2023-12-29 16:36:23.195546933 +0100 ++++ build/llvm-src/llvm/include/llvm/Support/thread.h 2024-01-02 03:56:24.245012614 +0100 +@@ -27,10 +27,47 @@ + + #if LLVM_ENABLE_THREADS + ++#include ++#include ++#include ++#include + #include ++#include + + namespace llvm { + ++namespace this_thread { ++using namespace std::this_thread; ++} ++ ++using mutex = std::mutex; ++using recursive_mutex = std::recursive_mutex; ++using shared_mutex = std::shared_mutex; ++template using shared_lock = std::shared_lock; ++template using lock_guard = std::lock_guard; ++template using unique_lock = std::unique_lock; ++template using scoped_lock = std::scoped_lock; ++using condition_variable = std::condition_variable; ++using launch = std::launch; ++template using shared_future = std::shared_future; ++template using future = std::future; ++template using promise = std::promise; ++ ++template ++[[nodiscard]] std::future< ++ std::invoke_result_t, std::decay_t...>> ++async(Function &&f, Args &&...args) { ++ return std::async(std::forward(f), std::forward(args)...); ++} ++ ++template ++[[nodiscard]] std::future< ++ std::invoke_result_t, std::decay_t...>> ++async(std::launch policy, Function &&f, Args &&...args) { ++ return std::async(policy, std::forward(f), ++ std::forward(args)...); ++} ++ + #if LLVM_ON_UNIX || _WIN32 + + /// LLVM thread following std::thread interface with added constructor to +@@ -202,8 +239,8 @@ + }; + + namespace this_thread { +- inline thread::id get_id() { return std::this_thread::get_id(); } +-} ++inline thread::id get_id() { return std::this_thread::get_id(); } ++} // namespace this_thread + + #endif // LLVM_ON_UNIX || _WIN32 + +@@ -211,6 +248,11 @@ + + #else // !LLVM_ENABLE_THREADS + ++#include ++#include ++#include ++#include ++#include + #include + + namespace llvm { +@@ -237,6 +279,158 @@ + static unsigned hardware_concurrency() { return 1; }; + }; + ++enum class launch : unsigned { async = 1, deferred = 2 }; ++ ++inline launch operator|(launch a, launch b) { ++ return launch((unsigned)a | (unsigned)b); ++} ++ ++struct mutex { ++ void lock() {} ++ void unlock() {} ++}; ++struct recursive_mutex { ++ void lock() {} ++ void unlock() {} ++ bool try_lock() { return true; } ++}; ++struct shared_mutex { ++ void lock_shared() {} ++ void unlock_shared() {} ++ void lock() {} ++ void unlock() {} ++}; ++ ++template struct lock_guard { ++ lock_guard(T &) {} ++ void lock() {} ++ void unlock() {} ++}; ++ ++template struct unique_lock { ++ unique_lock() {} ++ unique_lock(T &) {} ++ void lock() {} ++ void unlock() {} ++}; ++ ++template struct shared_lock { ++ shared_lock() {} ++ shared_lock(T &) {} ++ void lock() {} ++ void unlock() {} ++ void lock_shared() {} ++ void unlock_shared() {} ++}; ++ ++template struct scoped_lock { ++ explicit scoped_lock(T &...) {} ++}; ++ ++struct condition_variable { ++ void notify_all() {} ++ void notify_one() {} ++ void wait(unique_lock &) { ++ report_fatal_error("Cannot wait on tasks with no threads"); ++ } ++ template ++ void wait(unique_lock &lock, Predicate stop_waiting) { ++ if (stop_waiting()) { ++ return; ++ } ++ wait(lock); ++ } ++}; ++ ++template struct future_state { ++ std::optional value; ++ std::function run; ++ ++ T &get() { ++ if (!value.has_value()) { ++ value = run(); ++ } ++ return *value; ++ } ++}; ++ ++template <> struct future_state { ++ std::function run; ++ bool has_ran = false; ++ ++ void get() { ++ if (!has_ran) { ++ run(); ++ has_ran = true; ++ } ++ } ++}; ++ ++template struct future; ++ ++template struct shared_future_get { using type = const T &; }; ++template <> struct shared_future_get { using type = void; }; ++ ++template struct shared_future { ++ shared_future(future &&f) { ++ state = std::move(f.state); ++ f.state = nullptr; ++ } ++ ++ typename shared_future_get::type get() const { return (*state).get(); } ++ ++ void wait() const { get(); } ++ ++ std::shared_ptr> state; ++}; ++ ++template struct future { ++ shared_future share() { return shared_future(std::move(*this)); } ++ ++ T get() const { return std::move((*state).get()); } ++ ++ std::shared_ptr> state; ++}; ++ ++template <> struct future { ++ shared_future share() { return shared_future(std::move(*this)); } ++ ++ void get() const { return (*state).get(); } ++ ++ std::shared_ptr> state; ++}; ++ ++template ++[[nodiscard]] future< ++ std::invoke_result_t, std::decay_t...>> ++async(launch policy, Function &&f, Args &&...args) { ++ if (!((unsigned)policy & (unsigned)launch::deferred)) { ++ report_fatal_error("Cannot asynchonously launch tasks with no threads"); ++ } ++ using T = std::invoke_result_t, std::decay_t...>; ++ std::shared_ptr> state = std::make_shared>(); ++ auto all_args = std::make_tuple(std::forward(args)...); ++ state->run = [all_args = std::move(all_args), f = std::move(f)]() { ++ return std::apply(f, all_args); ++ }; ++ return future{state}; ++} ++ ++template ++[[nodiscard]] future< ++ std::invoke_result_t, std::decay_t...>> ++async(Function &&f, Args &&...args) { ++ return async(launch::async | launch::deferred, std::forward(f), ++ std::forward(args)...); ++} ++ ++namespace this_thread { ++template ++void sleep_for(const std::chrono::duration &) { ++ // noop ++} ++} // namespace this_thread ++ + } // namespace llvm + + #endif // LLVM_ENABLE_THREADS +diff '--color=auto' -r -u llvm/llvm/include/llvm/Support/ThreadPool.h build/llvm-src/llvm/include/llvm/Support/ThreadPool.h +--- llvm/llvm/include/llvm/Support/ThreadPool.h 2023-12-29 16:36:23.194546878 +0100 ++++ build/llvm-src/llvm/include/llvm/Support/ThreadPool.h 2024-01-02 03:15:13.630395186 +0100 +@@ -19,13 +19,9 @@ + #include "llvm/Support/Threading.h" + #include "llvm/Support/thread.h" + +-#include +- +-#include + #include + #include + #include +-#include + #include + + namespace llvm { +@@ -80,14 +76,14 @@ + /// Asynchronous submission of a task to the pool. The returned future can be + /// used to wait for the task to finish and is *non-blocking* on destruction. + template +- auto async(Func &&F) -> std::shared_future { ++ auto async(Func &&F) -> llvm::shared_future { + return asyncImpl(std::function(std::forward(F)), + nullptr); + } + + template + auto async(ThreadPoolTaskGroup &Group, Func &&F) +- -> std::shared_future { ++ -> llvm::shared_future { + return asyncImpl(std::function(std::forward(F)), + &Group); + } +@@ -113,11 +109,12 @@ + bool isWorkerThread() const; + + private: ++#if LLVM_ENABLE_THREADS + /// Helpers to create a promise and a callable wrapper of \p Task that sets + /// the result of the promise. Returns the callable and a future to access the + /// result. + template +- static std::pair, std::future> ++ static std::pair, llvm::future> + createTaskAndFuture(std::function Task) { + std::shared_ptr> Promise = + std::make_shared>(); +@@ -126,7 +123,7 @@ + [Promise = std::move(Promise), Task]() { Promise->set_value(Task()); }, + std::move(F)}; + } +- static std::pair, std::future> ++ static std::pair, llvm::future> + createTaskAndFuture(std::function Task) { + std::shared_ptr> Promise = + std::make_shared>(); +@@ -137,6 +134,7 @@ + }, + std::move(F)}; + } ++#endif + + /// Returns true if all tasks in the given group have finished (nullptr means + /// all tasks regardless of their group). QueueLock must be locked. +@@ -145,8 +143,8 @@ + /// Asynchronous submission of a task to the pool. The returned future can be + /// used to wait for the task to finish and is *non-blocking* on destruction. + template +- std::shared_future asyncImpl(std::function Task, +- ThreadPoolTaskGroup *Group) { ++ llvm::shared_future asyncImpl(std::function Task, ++ ThreadPoolTaskGroup *Group) { + + #if LLVM_ENABLE_THREADS + /// Wrap the Task in a std::function that sets the result of the +@@ -156,7 +154,7 @@ + int requestedThreads; + { + // Lock the queue and push the new task +- std::unique_lock LockGuard(QueueLock); ++ std::unique_lock LockGuard(QueueLock); + + // Don't allow enqueueing after disabling the pool + assert(EnableFlag && "Queuing a thread during ThreadPool destruction"); +@@ -169,8 +167,8 @@ + + #else // LLVM_ENABLE_THREADS Disabled + +- // Get a Future with launch::deferred execution using std::async +- auto Future = std::async(std::launch::deferred, std::move(Task)).share(); ++ // Get a Future with launch::deferred execution using llvm::async ++ auto Future = llvm::async(llvm::launch::deferred, std::move(Task)).share(); + // Wrap the future so that both ThreadPool::wait() can operate and the + // returned future can be sync'ed on. + Tasks.emplace_back(std::make_pair([Future]() { Future.get(); }, Group)); +@@ -195,11 +193,11 @@ + std::deque, ThreadPoolTaskGroup *>> Tasks; + + /// Locking and signaling for accessing the Tasks queue. +- std::mutex QueueLock; +- std::condition_variable QueueCondition; ++ llvm::mutex QueueLock; ++ llvm::condition_variable QueueCondition; + + /// Signaling for job completion (all tasks or all tasks in a group). +- std::condition_variable CompletionCondition; ++ llvm::condition_variable CompletionCondition; + + /// Keep track of the number of thread actually busy + unsigned ActiveThreads = 0; +diff '--color=auto' -r -u llvm/llvm/lib/CMakeLists.txt build/llvm-src/llvm/lib/CMakeLists.txt +--- llvm/llvm/lib/CMakeLists.txt 2023-12-29 16:36:23.232548969 +0100 ++++ build/llvm-src/llvm/lib/CMakeLists.txt 2024-01-02 03:15:13.630395186 +0100 +@@ -31,7 +31,9 @@ + add_subdirectory(Debuginfod) + add_subdirectory(DebugInfo) + add_subdirectory(DWP) ++if (NOT WASI) + add_subdirectory(ExecutionEngine) ++endif () + add_subdirectory(Target) + add_subdirectory(AsmParser) + add_subdirectory(LineEditor) +diff '--color=auto' -r -u llvm/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp build/llvm-src/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +--- llvm/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp 2023-12-29 16:36:23.293552327 +0100 ++++ build/llvm-src/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp 2024-01-02 03:15:13.631395241 +0100 +@@ -47,11 +47,12 @@ + #include "llvm/Support/DataExtractor.h" + #include "llvm/Support/Error.h" + #include "llvm/Support/Format.h" +-#include "llvm/Support/LEB128.h" + #include "llvm/Support/FormatVariadic.h" ++#include "llvm/Support/LEB128.h" + #include "llvm/Support/MemoryBuffer.h" + #include "llvm/Support/Path.h" + #include "llvm/Support/raw_ostream.h" ++#include "llvm/Support/thread.h" + #include + #include + #include +@@ -70,7 +71,6 @@ + using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind; + using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind; + +- + void fixupIndexV4(DWARFContext &C, DWARFUnitIndex &Index) { + using EntryType = DWARFUnitIndex::Entry::SectionContribution; + using EntryMap = DenseMap; +@@ -203,7 +203,6 @@ + return *Cache; + } + +- + std::unique_ptr + DWARFContext::DWARFContextState::parseMacroOrMacinfo(MacroSecType SectionType) { + auto Macro = std::make_unique(); +@@ -282,9 +281,8 @@ + std::string DWPName; + + public: +- ThreadUnsafeDWARFContextState(DWARFContext &DC, std::string &DWP) : +- DWARFContext::DWARFContextState(DC), +- DWPName(std::move(DWP)) {} ++ ThreadUnsafeDWARFContextState(DWARFContext &DC, std::string &DWP) ++ : DWARFContext::DWARFContextState(DC), DWPName(std::move(DWP)) {} + + DWARFUnitVector &getNormalUnits() override { + if (NormalUnits.empty()) { +@@ -328,8 +326,8 @@ + if (CUIndex) + return *CUIndex; + +- DataExtractor Data(D.getDWARFObj().getCUIndexSection(), +- D.isLittleEndian(), 0); ++ DataExtractor Data(D.getDWARFObj().getCUIndexSection(), D.isLittleEndian(), ++ 0); + CUIndex = std::make_unique(DW_SECT_INFO); + if (CUIndex->parse(Data)) + fixupIndex(D, *CUIndex); +@@ -339,8 +337,8 @@ + if (TUIndex) + return *TUIndex; + +- DataExtractor Data(D.getDWARFObj().getTUIndexSection(), +- D.isLittleEndian(), 0); ++ DataExtractor Data(D.getDWARFObj().getTUIndexSection(), D.isLittleEndian(), ++ 0); + TUIndex = std::make_unique(DW_SECT_EXT_TYPES); + bool isParseSuccessful = TUIndex->parse(Data); + // If we are parsing TU-index and for .debug_types section we don't need +@@ -364,8 +362,8 @@ + if (Abbrev) + return Abbrev.get(); + +- DataExtractor Data(D.getDWARFObj().getAbbrevSection(), +- D.isLittleEndian(), 0); ++ DataExtractor Data(D.getDWARFObj().getAbbrevSection(), D.isLittleEndian(), ++ 0); + Abbrev = std::make_unique(Data); + return Abbrev.get(); + } +@@ -394,8 +392,9 @@ + return Aranges.get(); + } + +- Expected +- getLineTableForUnit(DWARFUnit *U, function_ref RecoverableErrorHandler) override { ++ Expected getLineTableForUnit( ++ DWARFUnit *U, ++ function_ref RecoverableErrorHandler) override { + if (!Line) + Line = std::make_unique(); + +@@ -421,7 +420,6 @@ + U->isLittleEndian(), U->getAddressByteSize()); + return Line->getOrParseLineTable(Data, stmtOffset, U->getContext(), U, + RecoverableErrorHandler); +- + } + + void clearLineTableForUnit(DWARFUnit *U) override { +@@ -446,20 +444,19 @@ + const DWARFObject &DObj = D.getDWARFObj(); + const DWARFSection &DS = DObj.getFrameSection(); + +- // There's a "bug" in the DWARFv3 standard with respect to the target address +- // size within debug frame sections. While DWARF is supposed to be independent +- // of its container, FDEs have fields with size being "target address size", +- // which isn't specified in DWARF in general. It's only specified for CUs, but +- // .eh_frame can appear without a .debug_info section. Follow the example of +- // other tools (libdwarf) and extract this from the container (ObjectFile +- // provides this information). This problem is fixed in DWARFv4 +- // See this dwarf-discuss discussion for more details: ++ // There's a "bug" in the DWARFv3 standard with respect to the target ++ // address size within debug frame sections. While DWARF is supposed to be ++ // independent of its container, FDEs have fields with size being "target ++ // address size", which isn't specified in DWARF in general. It's only ++ // specified for CUs, but .eh_frame can appear without a .debug_info ++ // section. Follow the example of other tools (libdwarf) and extract this ++ // from the container (ObjectFile provides this information). This problem ++ // is fixed in DWARFv4 See this dwarf-discuss discussion for more details: + // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html + DWARFDataExtractor Data(DObj, DS, D.isLittleEndian(), + DObj.getAddressSize()); +- auto DF = +- std::make_unique(D.getArch(), /*IsEH=*/false, +- DS.Address); ++ auto DF = std::make_unique(D.getArch(), /*IsEH=*/false, ++ DS.Address); + if (Error E = DF->parse(Data)) + return std::move(E); + +@@ -475,9 +472,8 @@ + const DWARFSection &DS = DObj.getEHFrameSection(); + DWARFDataExtractor Data(DObj, DS, D.isLittleEndian(), + DObj.getAddressSize()); +- auto DF = +- std::make_unique(D.getArch(), /*IsEH=*/true, +- DS.Address); ++ auto DF = std::make_unique(D.getArch(), /*IsEH=*/true, ++ DS.Address); + if (Error E = DF->parse(Data)) + return std::move(E); + EHFrame.swap(DF); +@@ -513,20 +509,17 @@ + const DWARFObject &DObj = D.getDWARFObj(); + return getAccelTable(AppleNames, DObj, DObj.getAppleNamesSection(), + DObj.getStrSection(), D.isLittleEndian()); +- + } + const AppleAcceleratorTable &getAppleTypes() override { + const DWARFObject &DObj = D.getDWARFObj(); + return getAccelTable(AppleTypes, DObj, DObj.getAppleTypesSection(), + DObj.getStrSection(), D.isLittleEndian()); +- + } + const AppleAcceleratorTable &getAppleNamespaces() override { + const DWARFObject &DObj = D.getDWARFObj(); + return getAccelTable(AppleNamespaces, DObj, +- DObj.getAppleNamespacesSection(), +- DObj.getStrSection(), D.isLittleEndian()); +- ++ DObj.getAppleNamespacesSection(), DObj.getStrSection(), ++ D.isLittleEndian()); + } + const AppleAcceleratorTable &getAppleObjC() override { + const DWARFObject &DObj = D.getDWARFObj(); +@@ -534,8 +527,7 @@ + DObj.getStrSection(), D.isLittleEndian()); + } + +- std::shared_ptr +- getDWOContext(StringRef AbsolutePath) override { ++ std::shared_ptr getDWOContext(StringRef AbsolutePath) override { + if (auto S = DWP.lock()) { + DWARFContext *Ctxt = S->Context.get(); + return std::shared_ptr(std::move(S), Ctxt); +@@ -596,7 +588,7 @@ + const DenseMap &getNormalTypeUnitMap() { + if (!NormalTypeUnits) { + NormalTypeUnits.emplace(); +- for (const auto &U :D.normal_units()) { ++ for (const auto &U : D.normal_units()) { + if (DWARFTypeUnit *TU = dyn_cast(U.get())) + (*NormalTypeUnits)[TU->getTypeHash()] = TU; + } +@@ -607,7 +599,7 @@ + const DenseMap &getDWOTypeUnitMap() { + if (!DWOTypeUnits) { + DWOTypeUnits.emplace(); +- for (const auto &U :D.dwo_units()) { ++ for (const auto &U : D.dwo_units()) { + if (DWARFTypeUnit *TU = dyn_cast(U.get())) + (*DWOTypeUnits)[TU->getTypeHash()] = TU; + } +@@ -622,113 +614,112 @@ + else + return getNormalTypeUnitMap(); + } +- +- + }; + + class ThreadSafeState : public ThreadUnsafeDWARFContextState { +- std::recursive_mutex Mutex; ++ llvm::recursive_mutex Mutex; + + public: +- ThreadSafeState(DWARFContext &DC, std::string &DWP) : +- ThreadUnsafeDWARFContextState(DC, DWP) {} ++ ThreadSafeState(DWARFContext &DC, std::string &DWP) ++ : ThreadUnsafeDWARFContextState(DC, DWP) {} + + DWARFUnitVector &getNormalUnits() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getNormalUnits(); + } + DWARFUnitVector &getDWOUnits(bool Lazy) override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + // We need to not do lazy parsing when we need thread safety as + // DWARFUnitVector, in lazy mode, will slowly add things to itself and + // will cause problems in a multi-threaded environment. + return ThreadUnsafeDWARFContextState::getDWOUnits(false); + } + const DWARFUnitIndex &getCUIndex() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getCUIndex(); + } + const DWARFDebugAbbrev *getDebugAbbrevDWO() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getDebugAbbrevDWO(); + } + + const DWARFUnitIndex &getTUIndex() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getTUIndex(); + } + DWARFGdbIndex &getGdbIndex() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getGdbIndex(); + } + const DWARFDebugAbbrev *getDebugAbbrev() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getDebugAbbrev(); + } + const DWARFDebugLoc *getDebugLoc() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getDebugLoc(); + } + const DWARFDebugAranges *getDebugAranges() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getDebugAranges(); + } +- Expected +- getLineTableForUnit(DWARFUnit *U, function_ref RecoverableErrorHandler) override { +- std::unique_lock LockGuard(Mutex); +- return ThreadUnsafeDWARFContextState::getLineTableForUnit(U, RecoverableErrorHandler); ++ Expected getLineTableForUnit( ++ DWARFUnit *U, ++ function_ref RecoverableErrorHandler) override { ++ llvm::unique_lock LockGuard(Mutex); ++ return ThreadUnsafeDWARFContextState::getLineTableForUnit( ++ U, RecoverableErrorHandler); + } + void clearLineTableForUnit(DWARFUnit *U) override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::clearLineTableForUnit(U); + } + Expected getDebugFrame() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getDebugFrame(); + } + Expected getEHFrame() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getEHFrame(); + } + const DWARFDebugMacro *getDebugMacinfo() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getDebugMacinfo(); + } + const DWARFDebugMacro *getDebugMacinfoDWO() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getDebugMacinfoDWO(); + } + const DWARFDebugMacro *getDebugMacro() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getDebugMacro(); + } + const DWARFDebugMacro *getDebugMacroDWO() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getDebugMacroDWO(); + } + const DWARFDebugNames &getDebugNames() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getDebugNames(); + } + const AppleAcceleratorTable &getAppleNames() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getAppleNames(); + } + const AppleAcceleratorTable &getAppleTypes() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getAppleTypes(); + } + const AppleAcceleratorTable &getAppleNamespaces() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getAppleNamespaces(); + } + const AppleAcceleratorTable &getAppleObjC() override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getAppleObjC(); + } +- std::shared_ptr +- getDWOContext(StringRef AbsolutePath) override { +- std::unique_lock LockGuard(Mutex); ++ std::shared_ptr getDWOContext(StringRef AbsolutePath) override { ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getDWOContext(AbsolutePath); + } + +@@ -736,26 +727,23 @@ + + const DenseMap & + getTypeUnitMap(bool IsDWO) override { +- std::unique_lock LockGuard(Mutex); ++ llvm::unique_lock LockGuard(Mutex); + return ThreadUnsafeDWARFContextState::getTypeUnitMap(IsDWO); + } + }; + +- +- + DWARFContext::DWARFContext(std::unique_ptr DObj, + std::string DWPName, + std::function RecoverableErrorHandler, + std::function WarningHandler, + bool ThreadSafe) +- : DIContext(CK_DWARF), +- RecoverableErrorHandler(RecoverableErrorHandler), ++ : DIContext(CK_DWARF), RecoverableErrorHandler(RecoverableErrorHandler), + WarningHandler(WarningHandler), DObj(std::move(DObj)) { +- if (ThreadSafe) +- State = std::make_unique(*this, DWPName); +- else +- State = std::make_unique(*this, DWPName); +- } ++ if (ThreadSafe) ++ State = std::make_unique(*this, DWPName); ++ else ++ State = std::make_unique(*this, DWPName); ++} + + DWARFContext::~DWARFContext() = default; + +@@ -772,7 +760,7 @@ + return; + } + OS << "UUID: "; +- memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID)); ++ memcpy(&UUID, LC.Ptr + sizeof(LC.C), sizeof(UUID)); + OS.write_uuid(UUID); + Triple T = MachO->getArchTriple(); + OS << " (" << T.getArchName() << ')'; +@@ -945,7 +933,6 @@ + } + } + +- + static void dumpLoclistsSection(raw_ostream &OS, DIDumpOptions DumpOpts, + DWARFDataExtractor Data, const DWARFObject &Obj, + std::optional DumpOffset) { +@@ -1227,8 +1214,8 @@ + + if (shouldDump(Explicit, ".debug_addr", DIDT_ID_DebugAddr, + DObj->getAddrSection().Data)) { +- DWARFDataExtractor AddrData(*DObj, DObj->getAddrSection(), +- isLittleEndian(), 0); ++ DWARFDataExtractor AddrData(*DObj, DObj->getAddrSection(), isLittleEndian(), ++ 0); + dumpAddrSection(OS, AddrData, DumpOpts, getMaxVersion(), getCUAddrSize()); + } + +@@ -1341,8 +1328,7 @@ + DWARFUnitVector &DWOUnits = State->getDWOUnits(); + if (const auto &TUI = getTUIndex()) { + if (const auto *R = TUI.getFromHash(Hash)) +- return dyn_cast_or_null( +- DWOUnits.getUnitForIndexEntry(*R)); ++ return dyn_cast_or_null(DWOUnits.getUnitForIndexEntry(*R)); + return nullptr; + } + const DenseMap &Map = State->getTypeUnitMap(IsDWO); +@@ -1407,17 +1393,11 @@ + return Success; + } + +-const DWARFUnitIndex &DWARFContext::getCUIndex() { +- return State->getCUIndex(); +-} ++const DWARFUnitIndex &DWARFContext::getCUIndex() { return State->getCUIndex(); } + +-const DWARFUnitIndex &DWARFContext::getTUIndex() { +- return State->getTUIndex(); +-} ++const DWARFUnitIndex &DWARFContext::getTUIndex() { return State->getTUIndex(); } + +-DWARFGdbIndex &DWARFContext::getGdbIndex() { +- return State->getGdbIndex(); +-} ++DWARFGdbIndex &DWARFContext::getGdbIndex() { return State->getGdbIndex(); } + + const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { + return State->getDebugAbbrev(); +@@ -1459,7 +1439,6 @@ + return State->getDebugMacinfoDWO(); + } + +- + const DWARFDebugNames &DWARFContext::getDebugNames() { + return State->getDebugNames(); + } +@@ -2116,7 +2095,7 @@ + consumeError(NameOrErr.takeError()); + + ++SectionAmountMap[Name]; +- SectionNames.push_back({ Name, true }); ++ SectionNames.push_back({Name, true}); + + // Skip BSS and Virtual sections, they aren't interesting. + if (Section.isBSS() || Section.isVirtual()) +@@ -2351,17 +2330,19 @@ + + StringRef getAbbrevSection() const override { return AbbrevSection; } + const DWARFSection &getLocSection() const override { return LocSection; } +- const DWARFSection &getLoclistsSection() const override { return LoclistsSection; } +- StringRef getArangesSection() const override { return ArangesSection; } +- const DWARFSection &getFrameSection() const override { +- return FrameSection; ++ const DWARFSection &getLoclistsSection() const override { ++ return LoclistsSection; + } ++ StringRef getArangesSection() const override { return ArangesSection; } ++ const DWARFSection &getFrameSection() const override { return FrameSection; } + const DWARFSection &getEHFrameSection() const override { + return EHFrameSection; + } + const DWARFSection &getLineSection() const override { return LineSection; } + StringRef getStrSection() const override { return StrSection; } +- const DWARFSection &getRangesSection() const override { return RangesSection; } ++ const DWARFSection &getRangesSection() const override { ++ return RangesSection; ++ } + const DWARFSection &getRnglistsSection() const override { + return RnglistsSection; + } +@@ -2369,8 +2350,12 @@ + StringRef getMacroDWOSection() const override { return MacroDWOSection; } + StringRef getMacinfoSection() const override { return MacinfoSection; } + StringRef getMacinfoDWOSection() const override { return MacinfoDWOSection; } +- const DWARFSection &getPubnamesSection() const override { return PubnamesSection; } +- const DWARFSection &getPubtypesSection() const override { return PubtypesSection; } ++ const DWARFSection &getPubnamesSection() const override { ++ return PubnamesSection; ++ } ++ const DWARFSection &getPubtypesSection() const override { ++ return PubtypesSection; ++ } + const DWARFSection &getGnuPubnamesSection() const override { + return GnuPubnamesSection; + } +@@ -2389,9 +2374,7 @@ + const DWARFSection &getAppleObjCSection() const override { + return AppleObjCSection; + } +- const DWARFSection &getNamesSection() const override { +- return NamesSection; +- } ++ const DWARFSection &getNamesSection() const override { return NamesSection; } + + StringRef getFileName() const override { return FileName; } + uint8_t getAddressSize() const override { return AddressSize; } +@@ -2408,28 +2391,22 @@ + }; + } // namespace + +-std::unique_ptr +-DWARFContext::create(const object::ObjectFile &Obj, +- ProcessDebugRelocations RelocAction, +- const LoadedObjectInfo *L, std::string DWPName, +- std::function RecoverableErrorHandler, +- std::function WarningHandler, +- bool ThreadSafe) { ++std::unique_ptr DWARFContext::create( ++ const object::ObjectFile &Obj, ProcessDebugRelocations RelocAction, ++ const LoadedObjectInfo *L, std::string DWPName, ++ std::function RecoverableErrorHandler, ++ std::function WarningHandler, bool ThreadSafe) { + auto DObj = std::make_unique( + Obj, L, RecoverableErrorHandler, WarningHandler, RelocAction); +- return std::make_unique(std::move(DObj), +- std::move(DWPName), +- RecoverableErrorHandler, +- WarningHandler, ++ return std::make_unique(std::move(DObj), std::move(DWPName), ++ RecoverableErrorHandler, WarningHandler, + ThreadSafe); + } + +-std::unique_ptr +-DWARFContext::create(const StringMap> &Sections, +- uint8_t AddrSize, bool isLittleEndian, +- std::function RecoverableErrorHandler, +- std::function WarningHandler, +- bool ThreadSafe) { ++std::unique_ptr DWARFContext::create( ++ const StringMap> &Sections, uint8_t AddrSize, ++ bool isLittleEndian, std::function RecoverableErrorHandler, ++ std::function WarningHandler, bool ThreadSafe) { + auto DObj = + std::make_unique(Sections, AddrSize, isLittleEndian); + return std::make_unique( +diff '--color=auto' -r -u llvm/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp build/llvm-src/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp +--- llvm/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp 2023-12-29 16:36:23.295552437 +0100 ++++ build/llvm-src/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp 2024-01-02 03:15:13.631395241 +0100 +@@ -6,7 +6,6 @@ + // + //===----------------------------------------------------------------------===// + +-#include + #include + + #include "llvm/DebugInfo/DIContext.h" +@@ -83,7 +82,6 @@ + } + }; + +- + static DWARFDie GetParentDeclContextDIE(DWARFDie &Die) { + if (DWARFDie SpecDie = + Die.getAttributeValueAsReferencedDie(dwarf::DW_AT_specification)) { +@@ -171,7 +169,7 @@ + // templates + if (ParentName.front() == '<' && ParentName.back() == '>') + Name = "{" + ParentName.substr(1, ParentName.size() - 2).str() + "}" + +- "::" + Name; ++ "::" + Name; + else + Name = ParentName.str() + "::" + Name; + } +@@ -310,7 +308,6 @@ + const object::SectionedAddress SecAddress{ + StartAddress, object::SectionedAddress::UndefSection}; + +- + if (!CUI.LineTable->lookupAddressRange(SecAddress, RangeSize, RowVector)) { + // If we have a DW_TAG_subprogram but no line entries, fall back to using + // the DW_AT_decl_file an d DW_AT_decl_line if we have both attributes. +@@ -369,8 +366,9 @@ + if (RowAddress < FI.Range.start()) { + if (Log) { + *Log << "error: DIE has a start address whose LowPC is between the " +- "line table Row[" << RowIndex << "] with address " +- << HEX64(RowAddress) << " and the next one.\n"; ++ "line table Row[" ++ << RowIndex << "] with address " << HEX64(RowAddress) ++ << " and the next one.\n"; + Die.dump(*Log, 0, DIDumpOptions::getForSingleDIE()); + } + RowAddress = FI.Range.start(); +@@ -408,7 +406,7 @@ + // Skip multiple line entries for the same file and line. + auto LastLE = FI.OptLineTable->last(); + if (LastLE && LastLE->File == FileIdx && LastLE->Line == Row.Line) +- continue; ++ continue; + // Only push a row if it isn't an end sequence. End sequence markers are + // included for the last address in a function or the last contiguous + // address in a sequence. +@@ -519,7 +517,7 @@ + if (FI.Inline->Children.empty()) { + if (WarnIfEmpty && OS && !Gsym.isQuiet()) { + *OS << "warning: DIE contains inline function information that has " +- "no valid ranges, removing inline information:\n"; ++ "no valid ranges, removing inline information:\n"; + Die.dump(*OS, 0, DIDumpOptions::getForSingleDIE()); + } + FI.Inline = std::nullopt; +@@ -581,7 +579,7 @@ + pool.wait(); + + // Now convert all DWARF to GSYM in a thread pool. +- std::mutex LogMutex; ++ llvm::mutex LogMutex; + for (const auto &CU : DICtx.compile_units()) { + DWARFDie Die = getDie(*CU); + if (Die) { +@@ -589,11 +587,11 @@ + pool.async([this, CUI, &LogMutex, OS, Die]() mutable { + std::string ThreadLogStorage; + raw_string_ostream ThreadOS(ThreadLogStorage); +- handleDie(OS ? &ThreadOS: nullptr, CUI, Die); ++ handleDie(OS ? &ThreadOS : nullptr, CUI, Die); + ThreadOS.flush(); + if (OS && !ThreadLogStorage.empty()) { + // Print ThreadLogStorage lines into an actual stream under a lock +- std::lock_guard guard(LogMutex); ++ llvm::lock_guard guard(LogMutex); + *OS << ThreadLogStorage; + } + }); +@@ -622,14 +620,14 @@ + for (uint32_t I = 0; I < NumAddrs; ++I) { + auto FuncAddr = Gsym->getAddress(I); + if (!FuncAddr) +- return createStringError(std::errc::invalid_argument, +- "failed to extract address[%i]", I); ++ return createStringError(std::errc::invalid_argument, ++ "failed to extract address[%i]", I); + + auto FI = Gsym->getFunctionInfo(*FuncAddr); + if (!FI) +- return createStringError(std::errc::invalid_argument, +- "failed to extract function info for address 0x%" +- PRIu64, *FuncAddr); ++ return createStringError( ++ std::errc::invalid_argument, ++ "failed to extract function info for address 0x%" PRIu64, *FuncAddr); + + for (auto Addr = *FuncAddr; Addr < *FuncAddr + FI->size(); ++Addr) { + const object::SectionedAddress SectAddr{ +@@ -638,12 +636,10 @@ + if (!LR) + return LR.takeError(); + +- auto DwarfInlineInfos = +- DICtx.getInliningInfoForAddress(SectAddr, DLIS); ++ auto DwarfInlineInfos = DICtx.getInliningInfoForAddress(SectAddr, DLIS); + uint32_t NumDwarfInlineInfos = DwarfInlineInfos.getNumberOfFrames(); + if (NumDwarfInlineInfos == 0) { +- DwarfInlineInfos.addFrame( +- DICtx.getLineInfoForAddress(SectAddr, DLIS)); ++ DwarfInlineInfos.addFrame(DICtx.getLineInfoForAddress(SectAddr, DLIS)); + } + + // Check for 1 entry that has no file and line info +@@ -664,19 +660,17 @@ + << dii.FileName << ':' << dii.Line << '\n'; + } + Log << " " << LR->Locations.size() << " GSYM frames:\n"; +- for (size_t Idx = 0, count = LR->Locations.size(); +- Idx < count; ++Idx) { ++ for (size_t Idx = 0, count = LR->Locations.size(); Idx < count; ++Idx) { + const auto &gii = LR->Locations[Idx]; +- Log << " [" << Idx << "]: " << gii.Name << " @ " << gii.Dir +- << '/' << gii.Base << ':' << gii.Line << '\n'; ++ Log << " [" << Idx << "]: " << gii.Name << " @ " << gii.Dir << '/' ++ << gii.Base << ':' << gii.Line << '\n'; + } + DwarfInlineInfos = DICtx.getInliningInfoForAddress(SectAddr, DLIS); + Gsym->dump(Log, *FI); + continue; + } + +- for (size_t Idx = 0, count = LR->Locations.size(); Idx < count; +- ++Idx) { ++ for (size_t Idx = 0, count = LR->Locations.size(); Idx < count; ++Idx) { + const auto &gii = LR->Locations[Idx]; + if (Idx < NumDwarfInlineInfos) { + const auto &dii = DwarfInlineInfos.getFrame(Idx); +diff '--color=auto' -r -u llvm/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp build/llvm-src/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp +--- llvm/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp 2023-12-29 16:36:23.295552437 +0100 ++++ build/llvm-src/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp 2024-01-02 03:15:13.631395241 +0100 +@@ -38,7 +38,7 @@ + } + + uint32_t GsymCreator::insertFileEntry(FileEntry FE) { +- std::lock_guard Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + const auto NextIndex = Files.size(); + // Find FE in hash map and insert if not present. + auto R = FileEntryToIndex.insert(std::make_pair(FE, NextIndex)); +@@ -74,7 +74,7 @@ + } + + llvm::Error GsymCreator::encode(FileWriter &O) const { +- std::lock_guard Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + if (Funcs.empty()) + return createStringError(std::errc::invalid_argument, + "no functions to encode"); +@@ -186,7 +186,7 @@ + } + + llvm::Error GsymCreator::finalize(llvm::raw_ostream &OS) { +- std::lock_guard Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + if (Finalized) + return createStringError(std::errc::invalid_argument, "already finalized"); + Finalized = true; +@@ -227,7 +227,7 @@ + std::vector FinalizedFuncs; + FinalizedFuncs.reserve(Funcs.size()); + FinalizedFuncs.emplace_back(std::move(Funcs.front())); +- for (size_t Idx=1; Idx < NumBefore; ++Idx) { ++ for (size_t Idx = 1; Idx < NumBefore; ++Idx) { + FunctionInfo &Prev = FinalizedFuncs.back(); + FunctionInfo &Curr = Funcs[Idx]; + // Empty ranges won't intersect, but we still need to +@@ -248,9 +248,9 @@ + if (!Quiet) { + OS << "warning: same address range contains " + "different debug " +- << "info. Removing:\n" +- << Prev << "\nIn favor of this one:\n" +- << Curr << "\n"; ++ << "info. Removing:\n" ++ << Prev << "\nIn favor of this one:\n" ++ << Curr << "\n"; + } + } + // We want to swap the current entry with the previous since +@@ -261,13 +261,14 @@ + } else { + if (!Quiet) { // print warnings about overlaps + OS << "warning: function ranges overlap:\n" +- << Prev << "\n" +- << Curr << "\n"; ++ << Prev << "\n" ++ << Curr << "\n"; + } + FinalizedFuncs.emplace_back(std::move(Curr)); + } + } else { +- if (Prev.Range.size() == 0 && Curr.Range.contains(Prev.Range.start())) { ++ if (Prev.Range.size() == 0 && ++ Curr.Range.contains(Prev.Range.start())) { + // Symbols on macOS don't have address ranges, so if the range + // doesn't match and the size is zero, then we replace the empty + // symbol function info with the current one. +@@ -280,18 +281,18 @@ + std::swap(Funcs, FinalizedFuncs); + } + // If our last function info entry doesn't have a size and if we have valid +- // text ranges, we should set the size of the last entry since any search for +- // a high address might match our last entry. By fixing up this size, we can +- // help ensure we don't cause lookups to always return the last symbol that +- // has no size when doing lookups. ++ // text ranges, we should set the size of the last entry since any search ++ // for a high address might match our last entry. By fixing up this size, we ++ // can help ensure we don't cause lookups to always return the last symbol ++ // that has no size when doing lookups. + if (!Funcs.empty() && Funcs.back().Range.size() == 0 && ValidTextRanges) { +- if (auto Range = +- ValidTextRanges->getRangeThatContains(Funcs.back().Range.start())) { ++ if (auto Range = ValidTextRanges->getRangeThatContains( ++ Funcs.back().Range.start())) { + Funcs.back().Range = {Funcs.back().Range.start(), Range->end()}; + } + } + OS << "Pruned " << NumBefore - Funcs.size() << " functions, ended with " +- << Funcs.size() << " total\n"; ++ << Funcs.size() << " total\n"; + } + return Error::success(); + } +@@ -309,7 +310,7 @@ + + // The hash can be calculated outside the lock. + CachedHashStringRef CHStr(S); +- std::lock_guard Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + if (Copy) { + // We need to provide backing storage for the string if requested + // since StringTableBuilder stores references to strings. Any string +@@ -331,13 +332,13 @@ + } + + void GsymCreator::addFunctionInfo(FunctionInfo &&FI) { +- std::lock_guard Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + Funcs.emplace_back(std::move(FI)); + } + + void GsymCreator::forEachFunctionInfo( + std::function const &Callback) { +- std::lock_guard Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + for (auto &FI : Funcs) { + if (!Callback(FI)) + break; +@@ -346,7 +347,7 @@ + + void GsymCreator::forEachFunctionInfo( + std::function const &Callback) const { +- std::lock_guard Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + for (const auto &FI : Funcs) { + if (!Callback(FI)) + break; +@@ -354,7 +355,7 @@ + } + + size_t GsymCreator::getNumFunctionInfos() const { +- std::lock_guard Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + return Funcs.size(); + } + +@@ -392,10 +393,14 @@ + + uint64_t GsymCreator::getMaxAddressOffset() const { + switch (getAddressOffsetSize()) { +- case 1: return UINT8_MAX; +- case 2: return UINT16_MAX; +- case 4: return UINT32_MAX; +- case 8: return UINT64_MAX; ++ case 1: ++ return UINT8_MAX; ++ case 2: ++ return UINT16_MAX; ++ case 4: ++ return UINT32_MAX; ++ case 8: ++ return UINT64_MAX; + } + llvm_unreachable("invalid address offset"); + } +@@ -437,11 +442,12 @@ + void GsymCreator::fixupInlineInfo(const GsymCreator &SrcGC, InlineInfo &II) { + II.Name = copyString(SrcGC, II.Name); + II.CallFile = copyFile(SrcGC, II.CallFile); +- for (auto &ChildII: II.Children) ++ for (auto &ChildII : II.Children) + fixupInlineInfo(SrcGC, ChildII); + } + +-uint64_t GsymCreator::copyFunctionInfo(const GsymCreator &SrcGC, size_t FuncIdx) { ++uint64_t GsymCreator::copyFunctionInfo(const GsymCreator &SrcGC, ++ size_t FuncIdx) { + // To copy a function info we need to copy any files and strings over into + // this GsymCreator and then copy the function info and update the string + // table offsets to match the new offsets. +@@ -458,7 +464,7 @@ + // from SrcGC and must be converted to file indexes from this GsymCreator. + LineTable &DstLT = DstFI.OptLineTable.value(); + const size_t NumLines = DstLT.size(); +- for (size_t I=0; I Guard(Mutex); ++ llvm::lock_guard Guard(Mutex); + Funcs.emplace_back(DstFI); + return Funcs.back().cacheEncoding(); + } +@@ -537,10 +543,11 @@ + const uint64_t HeaderAndTableSize = GC->calculateHeaderAndTableSize(); + if (HeaderAndTableSize + SegmentFuncInfosSize >= SegmentSize) { + if (SegmentFuncInfosSize == 0) +- return createStringError(std::errc::invalid_argument, +- "a segment size of %" PRIu64 " is to small to " +- "fit any function infos, specify a larger value", +- SegmentSize); ++ return createStringError( ++ std::errc::invalid_argument, ++ "a segment size of %" PRIu64 " is to small to " ++ "fit any function infos, specify a larger value", ++ SegmentSize); + + break; + } +diff '--color=auto' -r -u llvm/llvm/lib/Debuginfod/Debuginfod.cpp build/llvm-src/llvm/lib/Debuginfod/Debuginfod.cpp +--- llvm/llvm/lib/Debuginfod/Debuginfod.cpp 2023-12-29 21:47:41.859673682 +0100 ++++ build/llvm-src/llvm/lib/Debuginfod/Debuginfod.cpp 2024-01-02 03:15:13.631395241 +0100 +@@ -38,11 +38,11 @@ + #include "llvm/Support/MemoryBuffer.h" + #include "llvm/Support/Path.h" + #include "llvm/Support/ThreadPool.h" ++#include "llvm/Support/thread.h" + #include "llvm/Support/xxhash.h" + + #include + #include +-#include + + namespace llvm { + +@@ -69,12 +69,12 @@ + } + + SmallVector getDefaultDebuginfodUrls() { +- std::shared_lock ReadGuard(UrlsMutex); ++ llvm::shared_lock ReadGuard(UrlsMutex); + if (!DebuginfodUrls) { + // Only read from the environment variable if the user hasn't already + // set the value + ReadGuard.unlock(); +- std::unique_lock WriteGuard(UrlsMutex); ++ llvm::unique_lock WriteGuard(UrlsMutex); + DebuginfodUrls = SmallVector(); + if (const char *DebuginfodUrlsEnv = std::getenv("DEBUGINFOD_URLS")) { + StringRef(DebuginfodUrlsEnv) +@@ -88,7 +88,7 @@ + + // Set the default debuginfod URL list, override the environment variable + void setDefaultDebuginfodUrls(const SmallVector &URLs) { +- std::unique_lock WriteGuard(UrlsMutex); ++ llvm::unique_lock WriteGuard(UrlsMutex); + DebuginfodUrls = URLs; + } + +@@ -310,7 +310,7 @@ + + void DebuginfodLog::push(DebuginfodLogEntry Entry) { + { +- std::lock_guard Guard(QueueMutex); ++ llvm::lock_guard Guard(QueueMutex); + LogEntryQueue.push(Entry); + } + QueueCondition.notify_one(); +@@ -318,11 +318,11 @@ + + DebuginfodLogEntry DebuginfodLog::pop() { + { +- std::unique_lock Guard(QueueMutex); ++ llvm::unique_lock Guard(QueueMutex); + // Wait for messages to be pushed into the queue. + QueueCondition.wait(Guard, [&] { return !LogEntryQueue.empty(); }); + } +- std::lock_guard Guard(QueueMutex); ++ llvm::lock_guard Guard(QueueMutex); + if (!LogEntryQueue.size()) + llvm_unreachable("Expected message in the queue."); + +@@ -340,7 +340,7 @@ + } + + Error DebuginfodCollection::update() { +- std::lock_guard Guard(UpdateMutex); ++ llvm::lock_guard Guard(UpdateMutex); + if (UpdateTimer.isRunning()) + UpdateTimer.stopTimer(); + UpdateTimer.clear(); +@@ -371,7 +371,7 @@ + while (true) { + if (Error Err = update()) + return Err; +- std::this_thread::sleep_for(Interval); ++ llvm::this_thread::sleep_for(Interval); + } + llvm_unreachable("updateForever loop should never end"); + } +@@ -396,7 +396,7 @@ + Error DebuginfodCollection::findBinaries(StringRef Path) { + std::error_code EC; + sys::fs::recursive_directory_iterator I(Twine(Path), EC), E; +- std::mutex IteratorMutex; ++ llvm::mutex IteratorMutex; + ThreadPoolTaskGroup IteratorGroup(Pool); + for (unsigned WorkerIndex = 0; WorkerIndex < Pool.getThreadCount(); + WorkerIndex++) { +@@ -405,7 +405,7 @@ + while (true) { + { + // Check if iteration is over or there is an error during iteration +- std::lock_guard Guard(IteratorMutex); ++ llvm::lock_guard Guard(IteratorMutex); + if (I == E || EC) + return; + // Grab a file path from the directory iterator and advance the +@@ -441,17 +441,17 @@ + + std::string IDString = buildIDToString(ID); + if (Object->hasDebugInfo()) { +- std::lock_guard DebugBinariesGuard(DebugBinariesMutex); ++ llvm::lock_guard DebugBinariesGuard(DebugBinariesMutex); + (void)DebugBinaries.try_emplace(IDString, std::move(FilePath)); + } else { +- std::lock_guard BinariesGuard(BinariesMutex); ++ llvm::lock_guard BinariesGuard(BinariesMutex); + (void)Binaries.try_emplace(IDString, std::move(FilePath)); + } + } + }); + } + IteratorGroup.wait(); +- std::unique_lock Guard(IteratorMutex); ++ llvm::unique_lock Guard(IteratorMutex); + if (EC) + return errorCodeToError(EC); + return Error::success(); +@@ -460,7 +460,7 @@ + Expected> + DebuginfodCollection::getBinaryPath(BuildIDRef ID) { + Log.push("getting binary path of ID " + buildIDToString(ID)); +- std::shared_lock Guard(BinariesMutex); ++ llvm::shared_lock Guard(BinariesMutex); + auto Loc = Binaries.find(buildIDToString(ID)); + if (Loc != Binaries.end()) { + std::string Path = Loc->getValue(); +@@ -472,7 +472,7 @@ + Expected> + DebuginfodCollection::getDebugBinaryPath(BuildIDRef ID) { + Log.push("getting debug binary path of ID " + buildIDToString(ID)); +- std::shared_lock Guard(DebugBinariesMutex); ++ llvm::shared_lock Guard(DebugBinariesMutex); + auto Loc = DebugBinaries.find(buildIDToString(ID)); + if (Loc != DebugBinaries.end()) { + std::string Path = Loc->getValue(); +diff '--color=auto' -r -u llvm/llvm/lib/DWARFLinker/DWARFLinker.cpp build/llvm-src/llvm/lib/DWARFLinker/DWARFLinker.cpp +--- llvm/llvm/lib/DWARFLinker/DWARFLinker.cpp 2023-12-29 21:47:41.855673461 +0100 ++++ build/llvm-src/llvm/lib/DWARFLinker/DWARFLinker.cpp 2024-01-02 03:15:13.631395241 +0100 +@@ -1357,9 +1357,9 @@ + // independently by the linker). + // - If address relocated in an inline_subprogram that happens at the + // beginning of its inlining function. +- // To avoid above cases and to not apply relocation twice (in applyValidRelocs +- // and here), read address attribute from InputDIE and apply Info.PCOffset +- // here. ++ // To avoid above cases and to not apply relocation twice (in ++ // applyValidRelocs and here), read address attribute from InputDIE and apply ++ // Info.PCOffset here. + + std::optional AddrAttribute = InputDIE.find(AttrSpec.Attr); + if (!AddrAttribute) +@@ -2040,8 +2040,7 @@ + } + + void DWARFLinker::DIECloner::emitDebugAddrSection( +- CompileUnit &Unit, +- const uint16_t DwarfVersion) const { ++ CompileUnit &Unit, const uint16_t DwarfVersion) const { + + if (LLVM_UNLIKELY(Linker.Options.Update)) + return; +@@ -2787,8 +2786,8 @@ + + // These variables manage the list of processed object files. + // The mutex and condition variable are to ensure that this is thread safe. +- std::mutex ProcessedFilesMutex; +- std::condition_variable ProcessedFilesConditionVariable; ++ llvm::mutex ProcessedFilesMutex; ++ llvm::condition_variable ProcessedFilesConditionVariable; + BitVector ProcessedFiles(NumObjects, false); + + // Analyzing the context info is particularly expensive so it is executed in +@@ -2912,7 +2911,7 @@ + for (unsigned I = 0, E = NumObjects; I != E; ++I) { + AnalyzeLambda(I); + +- std::unique_lock LockGuard(ProcessedFilesMutex); ++ llvm::unique_lock LockGuard(ProcessedFilesMutex); + ProcessedFiles.set(I); + ProcessedFilesConditionVariable.notify_one(); + } +@@ -2921,7 +2920,7 @@ + auto CloneAll = [&]() { + for (unsigned I = 0, E = NumObjects; I != E; ++I) { + { +- std::unique_lock LockGuard(ProcessedFilesMutex); ++ llvm::unique_lock LockGuard(ProcessedFilesMutex); + if (!ProcessedFiles[I]) { + ProcessedFilesConditionVariable.wait( + LockGuard, [&]() { return ProcessedFiles[I]; }); +@@ -3039,7 +3038,6 @@ + void DWARFLinker::verifyInput(const DWARFFile &File) { + assert(File.Dwarf); + +- + std::string Buffer; + raw_string_ostream OS(Buffer); + DIDumpOptions DumpOpts; +diff '--color=auto' -r -u llvm/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.cpp build/llvm-src/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.cpp +--- llvm/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.cpp 2023-12-29 21:47:41.856673516 +0100 ++++ build/llvm-src/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.cpp 2024-01-02 03:15:13.632395297 +0100 +@@ -437,7 +437,7 @@ + + void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc, + int64_t PcOffset) { +- std::lock_guard Guard(RangesMutex); ++ llvm::lock_guard Guard(RangesMutex); + + Ranges.insert({FuncLowPc, FuncHighPc}, PcOffset); + if (LowPc) +@@ -448,7 +448,7 @@ + } + + void CompileUnit::addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset) { +- std::lock_guard Guard(LabelsMutex); ++ llvm::lock_guard Guard(LabelsMutex); + Labels.insert({LabelLowPc, PcOffset}); } -diff --git a/llvm/lib/Support/LockFileManager.cpp b/llvm/lib/Support/LockFileManager.cpp -index b64d48302..ca5304b87 100644 ---- a/llvm/lib/Support/LockFileManager.cpp -+++ b/llvm/lib/Support/LockFileManager.cpp -@@ -116,9 +116,15 @@ bool LockFileManager::processStillExecuting(StringRef HostID, int PID) { - if (getHostID(StoredHostID)) - return true; // Conservatively assume it's executing on error. -+#ifndef __wasi__ - // Check whether the process is dead. If so, we're done. - if (StoredHostID == HostID && getsid(PID) == -1 && errno == ESRCH) - return false; -+#else -+ // Check whether the process is dead. If so, we're done. -+ if (StoredHostID == HostID && errno == ESRCH) -+ return true; -+#endif - #endif +diff '--color=auto' -r -u llvm/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.h build/llvm-src/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.h +--- llvm/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.h 2023-12-29 21:47:41.856673516 +0100 ++++ build/llvm-src/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.h 2024-01-02 03:15:13.632395297 +0100 +@@ -704,12 +704,12 @@ + /// associated with the PC offset to apply to the addresses to get + /// the linked address. + RangesTy Ranges; +- std::mutex RangesMutex; ++ llvm::mutex RangesMutex; - return true; -diff --git a/llvm/lib/Support/Unix/Memory.inc b/llvm/lib/Support/Unix/Memory.inc -index 69bd11643..925c792d1 100644 ---- a/llvm/lib/Support/Unix/Memory.inc -+++ b/llvm/lib/Support/Unix/Memory.inc -@@ -37,6 +37,7 @@ extern "C" void __clear_cache(void *, void *); - #endif + /// The DW_AT_low_pc of each DW_TAG_label. + using LabelMapTy = SmallDenseMap; + LabelMapTy Labels; +- std::mutex LabelsMutex; ++ llvm::mutex LabelsMutex; - static int getPosixProtectionFlags(unsigned Flags) { -+#ifndef __wasi__ - switch (Flags & llvm::sys::Memory::MF_RWE_MASK) { - case llvm::sys::Memory::MF_READ: - return PROT_READ; -@@ -65,6 +66,9 @@ static int getPosixProtectionFlags(unsigned Flags) { - } - // Provide a default return value as required by some compilers. - return PROT_NONE; -+#else -+ return 0; -+#endif - } + /// This field keeps current stage of overall compile unit processing. + std::atomic Stage; +diff '--color=auto' -r -u llvm/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.h build/llvm-src/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.h +--- llvm/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.h 2023-12-29 21:47:41.857673571 +0100 ++++ build/llvm-src/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.h 2024-01-02 03:15:13.632395297 +0100 +@@ -342,7 +342,7 @@ - namespace llvm { -@@ -77,6 +81,8 @@ MemoryBlock Memory::allocateMappedMemory(size_t NumBytes, - if (NumBytes == 0) - return MemoryBlock(); + /// Mapping the PCM filename to the DwoId. + StringMap ClangModules; +- std::mutex ClangModulesMutex; ++ llvm::mutex ClangModulesMutex; -+#ifndef __wasi__ -+ - // On platforms that have it, we can use MAP_ANON to get a memory-mapped - // page without file backing, but we need a fallback of opening /dev/zero - // for strictly POSIX platforms instead. -@@ -146,14 +152,19 @@ MemoryBlock Memory::allocateMappedMemory(size_t NumBytes, + /// Type unit. + std::unique_ptr ArtificialTypeUnit; +diff '--color=auto' -r -u llvm/llvm/lib/DWARFLinkerParallel/DWARFLinkerTypeUnit.h build/llvm-src/llvm/lib/DWARFLinkerParallel/DWARFLinkerTypeUnit.h +--- llvm/llvm/lib/DWARFLinkerParallel/DWARFLinkerTypeUnit.h 2023-12-29 21:47:41.857673571 +0100 ++++ build/llvm-src/llvm/lib/DWARFLinkerParallel/DWARFLinkerTypeUnit.h 2024-01-02 03:15:13.632395297 +0100 +@@ -67,7 +67,7 @@ + + /// Returns index for the specified \p String inside .debug_str_offsets. + uint64_t getDebugStrIndex(const StringEntry *String) override { +- std::unique_lock LockGuard(DebugStringIndexMapMutex); ++ llvm::unique_lock LockGuard(DebugStringIndexMapMutex); + return DebugStringIndexMap.getValueIndex(String); } - return Result; -+#else -+ return MemoryBlock(); -+#endif +@@ -129,7 +129,7 @@ + ArrayList AcceleratorRecords; + + /// Guard for DebugStringIndexMap. +- std::mutex DebugStringIndexMapMutex; ++ llvm::mutex DebugStringIndexMapMutex; + }; + + } // end of namespace dwarflinker_parallel +diff '--color=auto' -r -u llvm/llvm/lib/LTO/LTO.cpp build/llvm-src/llvm/lib/LTO/LTO.cpp +--- llvm/llvm/lib/LTO/LTO.cpp 2023-12-29 21:47:41.872674399 +0100 ++++ build/llvm-src/llvm/lib/LTO/LTO.cpp 2024-01-02 03:15:13.632395297 +0100 +@@ -232,7 +232,8 @@ + }; + + auto AddUsedThings = [&](GlobalValueSummary *GS) { +- if (!GS) return; ++ if (!GS) ++ return; + AddUnsigned(GS->getVisibility()); + AddUnsigned(GS->isLive()); + AddUnsigned(GS->canAutoHide()); +@@ -566,9 +567,7 @@ + return std::move(File); } - std::error_code Memory::releaseMappedMemory(MemoryBlock &M) { - if (M.Address == nullptr || M.AllocatedSize == 0) - return std::error_code(); +-StringRef InputFile::getName() const { +- return Mods[0].getModuleIdentifier(); +-} ++StringRef InputFile::getName() const { return Mods[0].getModuleIdentifier(); } -+#ifndef __wasi__ - if (0 != ::munmap(M.Address, M.AllocatedSize)) - return std::error_code(errno, std::generic_category()); -+#endif + BitcodeModule &InputFile::getSingleBitcodeModule() { + assert(Mods.size() == 1 && "Expect only one bitcode module"); +@@ -900,8 +899,8 @@ + if (Res.FinalDefinitionInLinkageUnit) { + GV->setDSOLocal(true); + if (GV->hasDLLImportStorageClass()) +- GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes:: +- DefaultStorageClass); ++ GV->setDLLStorageClass( ++ GlobalValue::DLLStorageClassTypes::DefaultStorageClass); + } + } else if (auto *AS = + dyn_cast_if_present(Msym)) { +@@ -1346,7 +1345,7 @@ + #undef HANDLE_LIBCALL + }; - M.Address = nullptr; - M.AllocatedSize = 0; -@@ -193,7 +204,11 @@ std::error_code Memory::protectMappedMemory(const MemoryBlock &M, - } - #endif +-ArrayRef LTO::getRuntimeLibcallSymbols() { ++ArrayRef LTO::getRuntimeLibcallSymbols() { + return ArrayRef(libcallRoutineNames); + } -+#ifndef __wasi__ - int Result = ::mprotect((void *)Start, End - Start, Protect); -+#else -+ int Result = -1; -+#endif +@@ -1412,7 +1411,7 @@ + std::set CfiFunctionDecls; - if (Result != 0) - return std::error_code(errno, std::generic_category()); -diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc -index 68ca58fda..c294b2e8c 100644 ---- a/llvm/lib/Support/Unix/Path.inc -+++ b/llvm/lib/Support/Unix/Path.inc -@@ -32,7 +32,9 @@ - #endif + std::optional Err; +- std::mutex ErrMu; ++ llvm::mutex ErrMu; - #include -+#ifndef __wasi__ - #include -+#endif - #include + bool ShouldEmitIndexFiles; - #ifdef __APPLE__ -@@ -75,10 +77,12 @@ extern char **environ; - #include - #if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && \ - !defined(__linux__) && !defined(__FreeBSD_kernel__) && !defined(_AIX) -+#ifndef __wasi__ - #include - #define STATVFS statvfs - #define FSTATVFS fstatvfs - #define STATVFS_F_FRSIZE(vfs) vfs.f_frsize -+#endif - #else - #if defined(__OpenBSD__) || defined(__FreeBSD__) - #include -@@ -118,6 +122,10 @@ typedef uint_t uint; - #define STATVFS_F_FLAG(vfs) (vfs).f_flags - #endif +@@ -1507,7 +1506,7 @@ + AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList, + ResolvedODR, DefinedGlobals, ModuleMap); + if (E) { +- std::unique_lock L(ErrMu); ++ llvm::unique_lock L(ErrMu); + if (Err) + Err = joinErrors(std::move(*Err), std::move(E)); + else +diff '--color=auto' -r -u llvm/llvm/lib/Support/CrashRecoveryContext.cpp build/llvm-src/llvm/lib/Support/CrashRecoveryContext.cpp +--- llvm/llvm/lib/Support/CrashRecoveryContext.cpp 2023-12-29 16:36:23.355555740 +0100 ++++ build/llvm-src/llvm/lib/Support/CrashRecoveryContext.cpp 2024-01-02 03:15:13.632395297 +0100 +@@ -7,6 +7,53 @@ + //===----------------------------------------------------------------------===// + #include "llvm/Support/CrashRecoveryContext.h" ++ +#ifdef __wasi__ -+#include "wasi_shim.h" -+#endif + - using namespace llvm; ++namespace llvm { ++CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() = default; ++CrashRecoveryContext::CrashRecoveryContext() { ++ (void)Impl; ++ (void)head; ++} ++CrashRecoveryContext::~CrashRecoveryContext() {} ++ ++void CrashRecoveryContext::registerCleanup( ++ CrashRecoveryContextCleanup *cleanup) { ++ cleanup->cleanupFired = true; ++ cleanup->recoverResources(); ++ delete cleanup; ++} ++ ++void CrashRecoveryContext::unregisterCleanup( ++ CrashRecoveryContextCleanup *cleanup) {} ++ ++void CrashRecoveryContext::Enable() {} ++void CrashRecoveryContext::Disable() {} ++CrashRecoveryContext *CrashRecoveryContext::GetCurrent() { return nullptr; } ++bool CrashRecoveryContext::isRecoveringFromCrash() { return false; } ++ ++bool CrashRecoveryContext::RunSafely(function_ref Fn) { ++ Fn(); ++ return true; ++} ++ ++bool CrashRecoveryContext::RunSafelyOnThread(function_ref Fn, ++ unsigned RequestedStackSize) { ++ return RunSafely(Fn); ++} ++ ++[[noreturn]] void CrashRecoveryContext::HandleExit(int RetCode) { ++ exit(RetCode); ++} ++ ++bool CrashRecoveryContext::isCrash(int RetCode) { return false; } ++bool CrashRecoveryContext::throwIfCrash(int RetCode) { return false; } ++ ++} // namespace llvm ++ ++#else ++ + #include "llvm/Config/llvm-config.h" + #include "llvm/Support/ErrorHandling.h" + #include "llvm/Support/ExitCodes.h" +@@ -118,7 +165,7 @@ + } + IsRecoveringFromCrash = PC; - namespace llvm { -@@ -334,7 +342,7 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) { - char link_path[PATH_MAX]; - if (realpath(DLInfo.dli_fname, link_path)) - return link_path; --#else -+#elif !defined(__wasi__) - #error GetMainExecutable is not implemented on this host yet. - #endif - return ""; -@@ -355,6 +363,7 @@ UniqueID file_status::getUniqueID() const { - uint32_t file_status::getLinkCount() const { return fs_st_nlinks; } +- CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; ++ CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *)Impl; + delete CRCI; + } - ErrorOr disk_space(const Twine &Path) { -+#ifndef __wasi__ - struct STATVFS Vfs; - if (::STATVFS(const_cast(Path.str().c_str()), &Vfs)) - return std::error_code(errno, std::generic_category()); -@@ -363,6 +372,12 @@ ErrorOr disk_space(const Twine &Path) { - SpaceInfo.capacity = static_cast(Vfs.f_blocks) * FrSize; - SpaceInfo.free = static_cast(Vfs.f_bfree) * FrSize; - SpaceInfo.available = static_cast(Vfs.f_bavail) * FrSize; -+#else -+ space_info SpaceInfo; -+ SpaceInfo.capacity = 0; -+ SpaceInfo.free = 0; -+ SpaceInfo.available = 0; -+#endif - return SpaceInfo; +@@ -154,8 +201,8 @@ + uninstallExceptionOrSignalHandlers(); + } + +-void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup) +-{ ++void CrashRecoveryContext::registerCleanup( ++ CrashRecoveryContextCleanup *cleanup) { + if (!cleanup) + return; + if (head) +@@ -164,16 +211,15 @@ + head = cleanup; } -@@ -381,6 +396,7 @@ std::error_code current_path(SmallVectorImpl &result) { +-void +-CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) { ++void CrashRecoveryContext::unregisterCleanup( ++ CrashRecoveryContextCleanup *cleanup) { + if (!cleanup) + return; + if (cleanup == head) { + head = cleanup->next; + if (head) + head->prev = nullptr; +- } +- else { ++ } else { + cleanup->prev->next = cleanup->next; + if (cleanup->next) + cleanup->next->prev = cleanup->prev; +@@ -263,16 +309,14 @@ - result.resize_for_overwrite(PATH_MAX); + #include "llvm/Support/Windows/WindowsSupport.h" -+#ifndef __wasi__ - while (true) { - if (::getcwd(result.data(), result.size()) == nullptr) { - // See if there was a real error. -@@ -393,6 +409,7 @@ std::error_code current_path(SmallVectorImpl &result) { - } else - break; +-static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) +-{ ++static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) { + // DBG_PRINTEXCEPTION_WIDE_C is not properly defined on all supported + // compilers and platforms, so we define it manually. + constexpr ULONG DbgPrintExceptionWideC = 0x4001000AL; +- switch (ExceptionInfo->ExceptionRecord->ExceptionCode) +- { ++ switch (ExceptionInfo->ExceptionRecord->ExceptionCode) { + case DBG_PRINTEXCEPTION_C: + case DbgPrintExceptionWideC: +- case 0x406D1388: // set debugger thread name ++ case 0x406D1388: // set debugger thread name + return EXCEPTION_CONTINUE_EXECUTION; } -+#endif - result.truncate(strlen(result.data())); - return std::error_code(); -@@ -402,8 +419,10 @@ std::error_code set_current_path(const Twine &path) { - SmallString<128> path_storage; - StringRef p = path.toNullTerminatedStringRef(path_storage); +@@ -307,7 +351,7 @@ + // CrashRecoveryContext at all. So we make use of a thread-local + // exception table. The handles contained in here will either be + // non-NULL, valid VEH handles, or NULL. +-static LLVM_THREAD_LOCAL const void* sCurrentExceptionHandle; ++static LLVM_THREAD_LOCAL const void *sCurrentExceptionHandle; -+#ifndef __wasi__ - if (::chdir(p.begin()) == -1) - return std::error_code(errno, std::generic_category()); -+#endif + static void installExceptionOrSignalHandlers() { + // We can set up vectored exception handling now. We will install our +@@ -344,8 +388,8 @@ - return std::error_code(); - } -@@ -476,6 +495,7 @@ std::error_code remove(const Twine &path, bool IgnoreNonExisting) { - return std::error_code(); - } + #include -+#ifndef __wasi__ - static bool is_local_impl(struct STATVFS &Vfs) { - #if defined(__linux__) || defined(__GNU__) - #ifndef NFS_SUPER_MAGIC -@@ -559,22 +579,33 @@ static bool is_local_impl(struct STATVFS &Vfs) { - return !!(STATVFS_F_FLAG(Vfs) & MNT_LOCAL); - #endif +-static const int Signals[] = +- { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP }; ++static const int Signals[] = {SIGABRT, SIGBUS, SIGFPE, ++ SIGILL, SIGSEGV, SIGTRAP}; + static const unsigned NumSignals = std::size(Signals); + static struct sigaction PrevActions[NumSignals]; + +@@ -502,7 +546,7 @@ + + static void RunSafelyOnThread_Dispatch(void *UserData) { + RunSafelyOnThreadInfo *Info = +- reinterpret_cast(UserData); ++ reinterpret_cast(UserData); + + if (Info->UseBackgroundPriority) + setThreadBackgroundPriority(); +@@ -512,7 +556,7 @@ + bool CrashRecoveryContext::RunSafelyOnThread(function_ref Fn, + unsigned RequestedStackSize) { + bool UseBackgroundPriority = hasThreadBackgroundPriority(); +- RunSafelyOnThreadInfo Info = { Fn, this, UseBackgroundPriority, false }; ++ RunSafelyOnThreadInfo Info = {Fn, this, UseBackgroundPriority, false}; + llvm::thread Thread(RequestedStackSize == 0 + ? std::nullopt + : std::optional(RequestedStackSize), +@@ -523,3 +567,5 @@ + CRC->setSwitchedThread(); + return Info.Result; } ++ +#endif +diff '--color=auto' -r -u llvm/llvm/lib/Support/FileCollector.cpp build/llvm-src/llvm/lib/Support/FileCollector.cpp +--- llvm/llvm/lib/Support/FileCollector.cpp 2023-12-29 16:36:23.356555794 +0100 ++++ build/llvm-src/llvm/lib/Support/FileCollector.cpp 2024-01-02 03:15:13.632395297 +0100 +@@ -19,7 +19,7 @@ + FileCollectorBase::~FileCollectorBase() = default; - std::error_code is_local(const Twine &Path, bool &Result) { -+#ifndef __wasi__ - struct STATVFS Vfs; - if (::STATVFS(const_cast(Path.str().c_str()), &Vfs)) - return std::error_code(errno, std::generic_category()); + void FileCollectorBase::addFile(const Twine &File) { +- std::lock_guard lock(Mutex); ++ llvm::lock_guard lock(Mutex); + std::string FileStr = File.str(); + if (markAsSeen(FileStr)) + addFileImpl(FileStr); +@@ -181,7 +181,7 @@ + return Err; + } - Result = is_local_impl(Vfs); -+#else -+ Result = 1; -+#endif -+ - return std::error_code(); +- std::lock_guard lock(Mutex); ++ llvm::lock_guard lock(Mutex); + + for (auto &entry : VFSWriter.getMappings()) { + // Get the status of the original file/directory. +@@ -236,7 +236,7 @@ } - std::error_code is_local(int FD, bool &Result) { -+#ifndef __wasi__ - struct STATVFS Vfs; - if (::FSTATVFS(FD, &Vfs)) - return std::error_code(errno, std::generic_category()); + std::error_code FileCollector::writeMapping(StringRef MappingFile) { +- std::lock_guard lock(Mutex); ++ llvm::lock_guard lock(Mutex); - Result = is_local_impl(Vfs); -+#else -+ Result = 1; -+#endif -+ - return std::error_code(); - } + VFSWriter.setOverlayDir(OverlayRoot); + VFSWriter.setCaseSensitivity(isCaseSensitivePath(OverlayRoot)); +diff '--color=auto' -r -u llvm/llvm/lib/Support/LockFileManager.cpp build/llvm-src/llvm/lib/Support/LockFileManager.cpp +--- llvm/llvm/lib/Support/LockFileManager.cpp 2023-12-29 16:36:23.357555849 +0100 ++++ build/llvm-src/llvm/lib/Support/LockFileManager.cpp 2024-01-02 03:15:13.633395352 +0100 +@@ -16,6 +16,7 @@ + #include "llvm/Support/Process.h" + #include "llvm/Support/Signals.h" + #include "llvm/Support/raw_ostream.h" ++#include "llvm/Support/thread.h" + #include + #include + #include +@@ -24,7 +25,6 @@ + #include + #include + #include +-#include + #include -@@ -673,6 +704,7 @@ static void expandTildeExpr(SmallVectorImpl &Path) { - return; + #ifdef _WIN32 +@@ -34,7 +34,9 @@ + #include + #endif + +-#if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > 1050) ++#if defined(__APPLE__) && \ ++ defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \ ++ (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > 1050) + #define USE_OSX_GETHOSTUUID 1 + #else + #define USE_OSX_GETHOSTUUID 0 +@@ -136,9 +138,10 @@ + class RemoveUniqueLockFileOnSignal { + StringRef Filename; + bool RemoveImmediately; ++ + public: + RemoveUniqueLockFileOnSignal(StringRef Name) +- : Filename(Name), RemoveImmediately(true) { ++ : Filename(Name), RemoveImmediately(true) { + sys::RemoveFileOnSignal(Filename, nullptr); } -+#ifndef __wasi__ - // This is a string of the form ~username/, look up this user's entry in the - // password database. - std::unique_ptr Buf; -@@ -694,6 +726,7 @@ static void expandTildeExpr(SmallVectorImpl &Path) { - Path.clear(); - Path.append(Entry->pw_dir, Entry->pw_dir + strlen(Entry->pw_dir)); - llvm::sys::path::append(Path, Storage); -+#endif - } +@@ -157,8 +160,7 @@ - void expand_tilde(const Twine &path, SmallVectorImpl &dest) { -@@ -770,25 +803,33 @@ std::error_code status(int FD, file_status &Result) { - } + } // end anonymous namespace - unsigned getUmask() { -+#ifndef __wasi__ - // Chose arbitary new mask and reset the umask to the old mask. - // umask(2) never fails so ignore the return of the second call. - unsigned Mask = ::umask(0); - (void)::umask(Mask); - return Mask; -+#else -+ return 0; -+#endif - } +-LockFileManager::LockFileManager(StringRef FileName) +-{ ++LockFileManager::LockFileManager(StringRef FileName) { + this->FileName = FileName; + if (std::error_code EC = sys::fs::make_absolute(this->FileName)) { + std::string S("failed to obtain absolute path for "); +@@ -215,8 +217,7 @@ - std::error_code setPermissions(const Twine &Path, perms Permissions) { - SmallString<128> PathStorage; - StringRef P = Path.toNullTerminatedStringRef(PathStorage); + while (true) { + // Create a link from the lock file name. If this succeeds, we're done. +- std::error_code EC = +- sys::fs::create_link(UniqueLockFileName, LockFileName); ++ std::error_code EC = sys::fs::create_link(UniqueLockFileName, LockFileName); + if (!EC) { + RemoveUniqueFile.lockAcquired(); + return; +@@ -316,7 +317,7 @@ + std::uniform_int_distribution Distribution(1, + WaitMultiplier); + unsigned long WaitDurationMS = MinWaitDurationMS * Distribution(Engine); +- std::this_thread::sleep_for(std::chrono::milliseconds(WaitDurationMS)); ++ llvm::this_thread::sleep_for(std::chrono::milliseconds(WaitDurationMS)); -+#ifndef __wasi__ - if (::chmod(P.begin(), Permissions)) - return std::error_code(errno, std::generic_category()); -+#endif - return std::error_code(); - } + if (sys::fs::access(LockFileName.c_str(), sys::fs::AccessMode::Exist) == + errc::no_such_file_or_directory) { +diff '--color=auto' -r -u llvm/llvm/lib/Support/ManagedStatic.cpp build/llvm-src/llvm/lib/Support/ManagedStatic.cpp +--- llvm/llvm/lib/Support/ManagedStatic.cpp 2023-12-29 16:36:23.357555849 +0100 ++++ build/llvm-src/llvm/lib/Support/ManagedStatic.cpp 2024-01-02 03:15:13.633395352 +0100 +@@ -13,22 +13,23 @@ + #include "llvm/Support/ManagedStatic.h" + #include "llvm/Config/config.h" + #include "llvm/Support/Threading.h" ++#include "llvm/Support/thread.h" + #include +-#include ++ + using namespace llvm; - std::error_code setPermissions(int FD, perms Permissions) { -+#ifndef __wasi__ - if (::fchmod(FD, Permissions)) - return std::error_code(errno, std::generic_category()); -+#endif - return std::error_code(); + static const ManagedStaticBase *StaticList = nullptr; + +-static std::recursive_mutex *getManagedStaticMutex() { +- static std::recursive_mutex m; ++static llvm::recursive_mutex *getManagedStaticMutex() { ++ static llvm::recursive_mutex m; + return &m; } -@@ -829,6 +870,11 @@ std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime, + void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), +- void (*Deleter)(void*)) const { ++ void (*Deleter)(void *)) const { + assert(Creator); + if (llvm_is_multithreaded()) { +- std::lock_guard Lock(*getManagedStaticMutex()); ++ llvm::lock_guard Lock(*getManagedStaticMutex()); - std::error_code mapped_file_region::init(int FD, uint64_t Offset, - mapmode Mode) { -+#ifdef __wasi__ -+ // Fail. -+ errno = EINVAL; -+ return std::error_code(errno, std::generic_category()); -+#else - assert(Size != 0); + if (!Ptr.load(std::memory_order_relaxed)) { + void *Tmp = Creator(); +diff '--color=auto' -r -u llvm/llvm/lib/Support/Parallel.cpp build/llvm-src/llvm/lib/Support/Parallel.cpp +--- llvm/llvm/lib/Support/Parallel.cpp 2023-12-29 16:36:23.357555849 +0100 ++++ build/llvm-src/llvm/lib/Support/Parallel.cpp 2024-01-02 03:15:13.633395352 +0100 +@@ -10,11 +10,10 @@ + #include "llvm/Config/llvm-config.h" + #include "llvm/Support/ManagedStatic.h" + #include "llvm/Support/Threading.h" ++#include "llvm/Support/thread.h" - int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE; -@@ -860,6 +906,7 @@ std::error_code mapped_file_region::init(int FD, uint64_t Offset, - if (Mapping == MAP_FAILED) - return std::error_code(errno, std::generic_category()); - return std::error_code(); -+#endif - } + #include + #include +-#include +-#include + #include - mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length, -@@ -872,11 +919,14 @@ mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length, - } + llvm::ThreadPoolStrategy llvm::parallel::strategy; +@@ -55,11 +54,11 @@ + // can take a while. + Threads.reserve(ThreadCount); + Threads.resize(1); +- std::lock_guard Lock(Mutex); ++ llvm::lock_guard Lock(Mutex); + // Use operator[] before creating the thread to avoid data race in .size() + // in “safe libc++” mode. + auto &Thread0 = Threads[0]; +- Thread0 = std::thread([this, S] { ++ Thread0 = llvm::thread([this, S] { + for (unsigned I = 1; I < ThreadCount; ++I) { + Threads.emplace_back([=] { work(S, I); }); + if (Stop) +@@ -72,7 +71,7 @@ - void mapped_file_region::unmapImpl() { -+#ifndef __wasi__ - if (Mapping) - ::munmap(Mapping, Size); -+#endif - } + void stop() { + { +- std::lock_guard Lock(Mutex); ++ llvm::lock_guard Lock(Mutex); + if (Stop) + return; + Stop = true; +@@ -83,8 +82,8 @@ - void mapped_file_region::dontNeedImpl() { -+#ifndef __wasi__ - assert(Mode == mapped_file_region::readonly); - if (!Mapping) - return; -@@ -887,6 +937,7 @@ void mapped_file_region::dontNeedImpl() { + ~ThreadPoolExecutor() override { + stop(); +- std::thread::id CurrentThreadId = std::this_thread::get_id(); +- for (std::thread &T : Threads) ++ llvm::thread::id CurrentThreadId = llvm::this_thread::get_id(); ++ for (llvm::thread &T : Threads) + if (T.get_id() == CurrentThreadId) + T.detach(); + else +@@ -100,7 +99,7 @@ + + void add(std::function F, bool Sequential = false) override { + { +- std::lock_guard Lock(Mutex); ++ llvm::lock_guard Lock(Mutex); + if (Sequential) + WorkQueueSequential.emplace_front(std::move(F)); + else +@@ -122,7 +121,7 @@ + threadIndex = ThreadID; + S.apply_thread_strategy(ThreadID); + while (true) { +- std::unique_lock Lock(Mutex); ++ llvm::unique_lock Lock(Mutex); + Cond.wait(Lock, [&] { + return Stop || hasGeneralTasks() || hasSequentialTasks(); + }); +@@ -148,10 +147,10 @@ + std::atomic SequentialQueueIsLocked{false}; + std::deque> WorkQueue; + std::deque> WorkQueueSequential; +- std::mutex Mutex; +- std::condition_variable Cond; +- std::promise ThreadsCreated; +- std::vector Threads; ++ llvm::mutex Mutex; ++ llvm::condition_variable Cond; ++ llvm::promise ThreadsCreated; ++ std::vector Threads; + unsigned ThreadCount; + }; + +@@ -195,11 +194,12 @@ + TaskGroup::TaskGroup() + #if LLVM_ENABLE_THREADS + : Parallel((parallel::strategy.ThreadsRequested != 1) && +- (threadIndex == UINT_MAX)) {} ++ (threadIndex == UINT_MAX)){} #else - ::madvise(Mapping, Size, MADV_DONTNEED); +- : Parallel(false) {} ++ : Parallel(false) { ++} #endif -+#endif +-TaskGroup::~TaskGroup() { ++ TaskGroup::~TaskGroup() { + // We must ensure that all the workloads have finished before decrementing the + // instances count. + L.sync(); +diff '--color=auto' -r -u llvm/llvm/lib/Support/Process.cpp build/llvm-src/llvm/lib/Support/Process.cpp +--- llvm/llvm/lib/Support/Process.cpp 2023-12-29 16:36:23.358555904 +0100 ++++ build/llvm-src/llvm/lib/Support/Process.cpp 2024-01-02 03:15:13.633395352 +0100 +@@ -67,24 +67,18 @@ + return FoundPath; } - int mapped_file_region::alignment() { return Process::getPageSizeEstimate(); } -@@ -920,7 +971,7 @@ static file_type direntType(dirent *Entry) { +- + #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m" + +-#define ALLCOLORS(FGBG,BOLD) {\ +- COLOR(FGBG, "0", BOLD),\ +- COLOR(FGBG, "1", BOLD),\ +- COLOR(FGBG, "2", BOLD),\ +- COLOR(FGBG, "3", BOLD),\ +- COLOR(FGBG, "4", BOLD),\ +- COLOR(FGBG, "5", BOLD),\ +- COLOR(FGBG, "6", BOLD),\ +- COLOR(FGBG, "7", BOLD)\ ++#define ALLCOLORS(FGBG, BOLD) \ ++ { \ ++ COLOR(FGBG, "0", BOLD), COLOR(FGBG, "1", BOLD), COLOR(FGBG, "2", BOLD), \ ++ COLOR(FGBG, "3", BOLD), COLOR(FGBG, "4", BOLD), \ ++ COLOR(FGBG, "5", BOLD), COLOR(FGBG, "6", BOLD), COLOR(FGBG, "7", BOLD) \ + } + + static const char colorcodes[2][2][8][10] = { +- { ALLCOLORS("3",""), ALLCOLORS("3","1;") }, +- { ALLCOLORS("4",""), ALLCOLORS("4","1;") } +-}; ++ {ALLCOLORS("3", ""), ALLCOLORS("3", "1;")}, ++ {ALLCOLORS("4", ""), ALLCOLORS("4", "1;")}}; + + // A CMake option controls wheter we emit core dumps by default. An application + // may disable core dumps by calling Process::PreventCoreFiles(). +diff '--color=auto' -r -u llvm/llvm/lib/Support/TimeProfiler.cpp build/llvm-src/llvm/lib/Support/TimeProfiler.cpp +--- llvm/llvm/lib/Support/TimeProfiler.cpp 2023-12-29 16:36:23.359555960 +0100 ++++ build/llvm-src/llvm/lib/Support/TimeProfiler.cpp 2024-01-02 03:15:13.633395352 +0100 +@@ -17,6 +17,7 @@ + #include "llvm/Support/Path.h" + #include "llvm/Support/Process.h" + #include "llvm/Support/Threading.h" ++#include "llvm/Support/thread.h" + #include + #include + #include +@@ -37,7 +38,7 @@ + using std::chrono::time_point_cast; + + struct TimeTraceProfilerInstances { +- std::mutex Lock; ++ llvm::mutex Lock; + std::vector List; + }; + +@@ -148,7 +149,7 @@ + void write(raw_pwrite_stream &OS) { + // Acquire Mutex as reading ThreadTimeTraceProfilerInstances. + auto &Instances = getTimeTraceProfilerInstances(); +- std::lock_guard Lock(Instances.Lock); ++ llvm::lock_guard Lock(Instances.Lock); + assert(Stack.empty() && + "All profiler sections should be ended when calling write"); + assert(llvm::all_of(Instances.List, +@@ -300,7 +301,7 @@ + TimeTraceProfilerInstance = nullptr; + + auto &Instances = getTimeTraceProfilerInstances(); +- std::lock_guard Lock(Instances.Lock); ++ llvm::lock_guard Lock(Instances.Lock); + for (auto *TTP : Instances.List) + delete TTP; + Instances.List.clear(); +@@ -310,7 +311,7 @@ + // This doesn't remove the instance, just moves the pointer to global vector. + void llvm::timeTraceProfilerFinishThread() { + auto &Instances = getTimeTraceProfilerInstances(); +- std::lock_guard Lock(Instances.Lock); ++ llvm::lock_guard Lock(Instances.Lock); + Instances.List.push_back(TimeTraceProfilerInstance); + TimeTraceProfilerInstance = nullptr; + } +diff '--color=auto' -r -u llvm/llvm/lib/Support/Unix/Path.inc build/llvm-src/llvm/lib/Support/Unix/Path.inc +--- llvm/llvm/lib/Support/Unix/Path.inc 2023-12-29 21:47:41.896675722 +0100 ++++ build/llvm-src/llvm/lib/Support/Unix/Path.inc 2024-01-02 03:15:13.633395352 +0100 +@@ -191,7 +191,9 @@ + /// GetMainExecutable - Return the path to the main executable, given the + /// value of argv[0] from program startup. + std::string getMainExecutable(const char *argv0, void *MainAddr) { +-#if defined(__APPLE__) ++#if defined(__wasi__) ++ return argv0; ++#elif defined(__APPLE__) + // On OS X the executable path is saved to the stack by dyld. Reading it + // from there is much faster than calling dladdr, especially for large + // binaries with symbols. +@@ -505,7 +507,7 @@ + #elif defined(__Fuchsia__) + // Fuchsia doesn't yet support remote filesystem mounts. + return true; +-#elif defined(__EMSCRIPTEN__) ++#elif defined(__EMSCRIPTEN__) || defined(__wasi__) + // Emscripten doesn't currently support remote filesystem mounts. + return true; + #elif defined(__HAIKU__) +@@ -920,7 +922,7 @@ // Note that while glibc provides a macro to see if this is supported, // _DIRENT_HAVE_D_TYPE, it's not defined on BSD/Mac, so we test for the // d_type-to-mode_t conversion macro instead. @@ -527,17 +4180,17 @@ index 68ca58fda..c294b2e8c 100644 return typeForMode(DTTOIF(Entry->d_type)); #else // Other platforms such as Solaris require a stat() to get the type. -@@ -1215,6 +1266,9 @@ Expected readNativeFileSlice(file_t FD, MutableArrayRef Buf, +@@ -1215,6 +1217,9 @@ } std::error_code tryLockFile(int FD, std::chrono::milliseconds Timeout) { -+#ifdef __wasi__ ++#if defined(__wasi__) + return std::error_code(); +#else auto Start = std::chrono::steady_clock::now(); auto End = Start + Timeout; do { -@@ -1232,9 +1286,13 @@ std::error_code tryLockFile(int FD, std::chrono::milliseconds Timeout) { +@@ -1232,9 +1237,13 @@ usleep(1000); } while (std::chrono::steady_clock::now() < End); return make_error_code(errc::no_lock_available); @@ -545,13 +4198,13 @@ index 68ca58fda..c294b2e8c 100644 } std::error_code lockFile(int FD) { -+#ifdef __wasi__ ++#if defined(__wasi__) + return std::error_code(); +#else struct flock Lock; memset(&Lock, 0, sizeof(Lock)); Lock.l_type = F_WRLCK; -@@ -1245,9 +1303,13 @@ std::error_code lockFile(int FD) { +@@ -1245,9 +1254,13 @@ return std::error_code(); int Error = errno; return std::error_code(Error, std::generic_category()); @@ -559,13 +4212,13 @@ index 68ca58fda..c294b2e8c 100644 } std::error_code unlockFile(int FD) { -+#ifdef __wasi__ ++#if defined(__wasi__) + return std::error_code(); +#else struct flock Lock; Lock.l_type = F_UNLCK; Lock.l_whence = SEEK_SET; -@@ -1256,6 +1318,7 @@ std::error_code unlockFile(int FD) { +@@ -1256,6 +1269,7 @@ if (::fcntl(FD, F_SETLK, &Lock) != -1) return std::error_code(); return std::error_code(errno, std::generic_category()); @@ -573,132 +4226,28 @@ index 68ca58fda..c294b2e8c 100644 } std::error_code closeFile(file_t &F) { -@@ -1327,10 +1390,12 @@ std::error_code real_path(const Twine &path, SmallVectorImpl &dest, - } - - std::error_code changeFileOwnership(int FD, uint32_t Owner, uint32_t Group) { -+#ifndef __wasi__ - auto FChown = [&]() { return ::fchown(FD, Owner, Group); }; - // Retry if fchown call fails due to interruption. - if ((sys::RetryAfterSignal(-1, FChown)) < 0) - return std::error_code(errno, std::generic_category()); -+#endif - return std::error_code(); - } - -@@ -1341,6 +1406,7 @@ namespace path { - bool home_directory(SmallVectorImpl &result) { - std::unique_ptr Buf; - char *RequestedDir = getenv("HOME"); -+#ifndef __wasi__ - if (!RequestedDir) { - long BufSize = sysconf(_SC_GETPW_R_SIZE_MAX); - if (BufSize <= 0) -@@ -1352,6 +1418,7 @@ bool home_directory(SmallVectorImpl &result) { - if (pw && pw->pw_dir) - RequestedDir = pw->pw_dir; - } -+#endif - if (!RequestedDir) - return false; - -diff --git a/llvm/lib/Support/Unix/Process.inc b/llvm/lib/Support/Unix/Process.inc -index 2babf0794..a8380a36b 100644 ---- a/llvm/lib/Support/Unix/Process.inc -+++ b/llvm/lib/Support/Unix/Process.inc -@@ -202,6 +202,7 @@ private: - } // namespace - - std::error_code Process::FixupStandardFileDescriptors() { -+#ifndef __wasi__ - int NullFD = -1; - FDCloser FDC(NullFD); - const int StandardFDs[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}; -@@ -232,10 +233,12 @@ std::error_code Process::FixupStandardFileDescriptors() { - else if (dup2(NullFD, StandardFD) < 0) - return std::error_code(errno, std::generic_category()); - } -+#endif - return std::error_code(); - } - - std::error_code Process::SafelyCloseFileDescriptor(int FD) { -+#ifndef __wasi__ - // Create a signal set filled with *all* signals. - sigset_t FullSet, SavedSet; - if (sigfillset(&FullSet) < 0 || sigfillset(&SavedSet) < 0) -@@ -248,6 +251,7 @@ std::error_code Process::SafelyCloseFileDescriptor(int FD) { - #else - if (sigprocmask(SIG_SETMASK, &FullSet, &SavedSet) < 0) - return std::error_code(errno, std::generic_category()); -+#endif - #endif - // Attempt to close the file descriptor. - // We need to save the error, if one occurs, because our subsequent call to -@@ -257,11 +261,13 @@ std::error_code Process::SafelyCloseFileDescriptor(int FD) { - ErrnoFromClose = errno; - // Restore the signal mask back to what we saved earlier. - int EC = 0; -+#ifndef __wasi__ - #if LLVM_ENABLE_THREADS - EC = pthread_sigmask(SIG_SETMASK, &SavedSet, nullptr); - #else - if (sigprocmask(SIG_SETMASK, &SavedSet, nullptr) < 0) - EC = errno; -+#endif - #endif - // The error code from close takes precedence over the one from - // pthread_sigmask. -diff --git a/llvm/lib/Support/Unix/Program.inc b/llvm/lib/Support/Unix/Program.inc -index 9466d0f0b..b78b3e57d 100644 ---- a/llvm/lib/Support/Unix/Program.inc -+++ b/llvm/lib/Support/Unix/Program.inc -@@ -95,6 +95,7 @@ ErrorOr sys::findProgramByName(StringRef Name, - } - - static bool RedirectIO(std::optional Path, int FD, std::string *ErrMsg) { -+#ifndef __wasi__ - if (!Path) // Noop - return false; - std::string File; -@@ -120,6 +121,9 @@ static bool RedirectIO(std::optional Path, int FD, std::string *ErrMs - } - close(InFD); // Close the original FD - return false; -+#else -+ return true; -+#endif - } - - #ifdef HAVE_POSIX_SPAWN -@@ -175,6 +179,7 @@ static bool Execute(ProcessInfo &PI, StringRef Program, +diff '--color=auto' -r -u llvm/llvm/lib/Support/Unix/Program.inc build/llvm-src/llvm/lib/Support/Unix/Program.inc +--- llvm/llvm/lib/Support/Unix/Program.inc 2023-12-29 21:47:41.897675778 +0100 ++++ build/llvm-src/llvm/lib/Support/Unix/Program.inc 2024-01-02 03:15:13.633395352 +0100 +@@ -177,6 +177,9 @@ ArrayRef> Redirects, unsigned MemoryLimit, std::string *ErrMsg, BitVector *AffinityMask) { -+#ifndef __wasi__ ++#ifdef __wasi__ ++ return false; ++#else if (!llvm::sys::fs::exists(Program)) { if (ErrMsg) *ErrMsg = std::string("Executable \"") + Program.str() + -@@ -335,6 +340,9 @@ static bool Execute(ProcessInfo &PI, StringRef Program, +@@ -337,6 +340,7 @@ PI.Process = child; return true; -+#else -+ return false; +#endif } namespace llvm { -@@ -342,7 +350,7 @@ namespace sys { - - #if defined(_AIX) - static pid_t(wait4)(pid_t pid, int *status, int options, struct rusage *usage); --#elif !defined(__Fuchsia__) -+#elif !defined(__Fuchsia__) && !defined(__wasi__) - using ::wait4; - #endif - -@@ -388,6 +396,7 @@ ProcessInfo llvm::sys::Wait(const ProcessInfo &PI, +@@ -390,6 +394,7 @@ std::string *ErrMsg, std::optional *ProcStat, bool Polling) { @@ -706,7 +4255,7 @@ index 9466d0f0b..b78b3e57d 100644 struct sigaction Act, Old; assert(PI.Pid && "invalid pid to wait on, process not started?"); -@@ -504,6 +513,11 @@ ProcessInfo llvm::sys::Wait(const ProcessInfo &PI, +@@ -506,6 +511,11 @@ // signal during execution as opposed to failing to execute. WaitResult.ReturnCode = -2; } @@ -718,141 +4267,112 @@ index 9466d0f0b..b78b3e57d 100644 return WaitResult; } -diff --git a/llvm/lib/Support/Unix/Signals.inc b/llvm/lib/Support/Unix/Signals.inc -index 792b0fd66..e35a13977 100644 ---- a/llvm/lib/Support/Unix/Signals.inc -+++ b/llvm/lib/Support/Unix/Signals.inc -@@ -206,6 +206,7 @@ struct FilesToRemoveCleanup { - - static StringRef Argv0; - -+#ifndef __wasi__ - /// Signals that represent requested termination. There's no bug or failure, or - /// if there is, it's not our direct responsibility. For whatever reason, our - /// continued execution is no longer desirable. -@@ -285,8 +286,10 @@ static void CreateSigAltStack() { - #else - static void CreateSigAltStack() {} - #endif -+#endif - - static void RegisterHandlers() { // Not signal-safe. -+#ifndef __wasi__ - // The mutex prevents other threads from registering handlers while we're - // doing it. We also have to protect the handlers and their count because - // a signal handler could fire while we're registeting handlers. -@@ -335,15 +338,18 @@ static void RegisterHandlers() { // Not signal-safe. - registerHandler(SIGPIPE, SignalKind::IsKill); - for (auto S : InfoSigs) - registerHandler(S, SignalKind::IsInfo); -+#endif - } +diff '--color=auto' -r -u llvm/llvm/lib/Transforms/CMakeLists.txt build/llvm-src/llvm/lib/Transforms/CMakeLists.txt +--- llvm/llvm/lib/Transforms/CMakeLists.txt 2023-12-29 16:36:23.625570600 +0100 ++++ build/llvm-src/llvm/lib/Transforms/CMakeLists.txt 2024-01-02 03:15:13.633395352 +0100 +@@ -5,7 +5,9 @@ + add_subdirectory(Scalar) + add_subdirectory(IPO) + add_subdirectory(Vectorize) ++if (NOT WASI) + add_subdirectory(Hello) ++endif () + add_subdirectory(ObjCARC) + add_subdirectory(Coroutines) + add_subdirectory(CFGuard) +diff '--color=auto' -r -u llvm/llvm/lib/Transforms/Instrumentation/InstrOrderFile.cpp build/llvm-src/llvm/lib/Transforms/Instrumentation/InstrOrderFile.cpp +--- llvm/llvm/lib/Transforms/Instrumentation/InstrOrderFile.cpp 2023-12-29 16:36:23.644571646 +0100 ++++ build/llvm-src/llvm/lib/Transforms/Instrumentation/InstrOrderFile.cpp 2024-01-02 03:15:13.633395352 +0100 +@@ -29,8 +29,7 @@ - void sys::unregisterHandlers() { -+#ifndef __wasi__ - // Restore all of the signal handlers to how they were before we showed up. - for (unsigned i = 0, e = NumRegisteredSignals.load(); i != e; ++i) { - sigaction(RegisteredSignalInfo[i].SigNo, &RegisteredSignalInfo[i].SA, - nullptr); - --NumRegisteredSignals; - } -+#endif - } + static cl::opt ClOrderFileWriteMapping( + "orderfile-write-mapping", cl::init(""), +- cl::desc( +- "Dump functions and their MD5 hash to deobfuscate profile data"), ++ cl::desc("Dump functions and their MD5 hash to deobfuscate profile data"), + cl::Hidden); - /// Process the FilesToRemove list. -@@ -354,15 +360,19 @@ static void RemoveFilesToRemove() { - void sys::CleanupOnSignal(uintptr_t Context) { - int Sig = (int)Context; + namespace { +@@ -40,7 +39,7 @@ + // fixed-size buffer that saves the MD5 hash of the function. We need + // a global variable to save the index into the buffer. -+#ifndef __wasi__ - if (llvm::is_contained(InfoSigs, Sig)) { - InfoSignalHandler(Sig); - return; - } -+#endif +-std::mutex MappingMutex; ++llvm::mutex MappingMutex; - RemoveFilesToRemove(); + struct InstrOrderFile { + private: +@@ -68,14 +67,16 @@ -+#ifndef __wasi__ - if (llvm::is_contained(IntSigs, Sig) || Sig == SIGPIPE) - return; -+#endif + // Create the global variables. + std::string SymbolName = INSTR_PROF_ORDERFILE_BUFFER_NAME_STR; +- OrderFileBuffer = new GlobalVariable(M, BufferTy, false, GlobalValue::LinkOnceODRLinkage, ++ OrderFileBuffer = ++ new GlobalVariable(M, BufferTy, false, GlobalValue::LinkOnceODRLinkage, + Constant::getNullValue(BufferTy), SymbolName); + Triple TT = Triple(M.getTargetTriple()); + OrderFileBuffer->setSection( + getInstrProfSectionName(IPSK_orderfile, TT.getObjectFormat())); - llvm::sys::RunSignalHandlers(); - } -@@ -375,6 +385,7 @@ static void SignalHandler(int Sig) { - // instead of recursing in the signal handler. - sys::unregisterHandlers(); + std::string IndexName = INSTR_PROF_ORDERFILE_BUFFER_IDX_NAME_STR; +- BufferIdx = new GlobalVariable(M, IdxTy, false, GlobalValue::LinkOnceODRLinkage, ++ BufferIdx = ++ new GlobalVariable(M, IdxTy, false, GlobalValue::LinkOnceODRLinkage, + Constant::getNullValue(IdxTy), IndexName); -+#ifndef __wasi__ - // Unmask all potentially blocked kill signals. - sigset_t SigMask; - sigfillset(&SigMask); -@@ -398,6 +409,7 @@ static void SignalHandler(int Sig) { - return; + std::string BitMapName = "bitmap_0"; +@@ -87,18 +88,19 @@ + // update the buffer. + void generateCodeSequence(Module &M, Function &F, int FuncId) { + if (!ClOrderFileWriteMapping.empty()) { +- std::lock_guard LogLock(MappingMutex); ++ llvm::lock_guard LogLock(MappingMutex); + std::error_code EC; + llvm::raw_fd_ostream OS(ClOrderFileWriteMapping, EC, + llvm::sys::fs::OF_Append); + if (EC) { +- report_fatal_error(Twine("Failed to open ") + ClOrderFileWriteMapping + +- " to save mapping file for order file instrumentation\n"); ++ report_fatal_error( ++ Twine("Failed to open ") + ClOrderFileWriteMapping + ++ " to save mapping file for order file instrumentation\n"); + } else { + std::stringstream stream; + stream << std::hex << MD5Hash(F.getName()); +- std::string singleLine = "MD5 " + stream.str() + " " + +- std::string(F.getName()) + '\n'; ++ std::string singleLine = ++ "MD5 " + stream.str() + " " + std::string(F.getName()) + '\n'; + OS << singleLine; + } } +@@ -140,8 +142,9 @@ + Value *BufferGEPIdx[] = {ConstantInt::get(Int32Ty, 0), WrappedIdx}; + Value *BufferAddr = + updateB.CreateGEP(BufferTy, OrderFileBuffer, BufferGEPIdx, ""); +- updateB.CreateStore(ConstantInt::get(Type::getInt64Ty(Ctx), MD5Hash(F.getName())), +- BufferAddr); ++ updateB.CreateStore( ++ ConstantInt::get(Type::getInt64Ty(Ctx), MD5Hash(F.getName())), ++ BufferAddr); + updateB.CreateBr(OrigEntry); } -+#endif - // Otherwise if it is a fault (like SEGV) run any handler. - llvm::sys::RunSignalHandlers(); -diff --git a/llvm/lib/Support/Unix/Unix.h b/llvm/lib/Support/Unix/Unix.h -index 1599241a3..8b1cd3da5 100644 ---- a/llvm/lib/Support/Unix/Unix.h -+++ b/llvm/lib/Support/Unix/Unix.h -@@ -30,7 +30,9 @@ - #include - #include - #include -+#ifndef __wasi__ - #include -+#endif - - #ifdef HAVE_UNISTD_H - #include -diff --git a/llvm/lib/Support/Unix/Watchdog.inc b/llvm/lib/Support/Unix/Watchdog.inc -index b33e52d88..3ef7f572f 100644 ---- a/llvm/lib/Support/Unix/Watchdog.inc -+++ b/llvm/lib/Support/Unix/Watchdog.inc -@@ -19,15 +19,19 @@ - namespace llvm { - namespace sys { - Watchdog::Watchdog(unsigned int seconds) { -+#ifndef __wasi__ - #ifdef HAVE_UNISTD_H - alarm(seconds); - #endif -+#endif - } +@@ -162,8 +165,8 @@ + }; // End of InstrOrderFile struct + } // End anonymous namespace - Watchdog::~Watchdog() { -+#ifndef __wasi__ - #ifdef HAVE_UNISTD_H - alarm(0); - #endif -+#endif - } - } // namespace sys - } // namespace llvm -diff --git a/llvm/lib/Transforms/CMakeLists.txt b/llvm/lib/Transforms/CMakeLists.txt -index 84a7e3414..1f7952955 100644 ---- a/llvm/lib/Transforms/CMakeLists.txt -+++ b/llvm/lib/Transforms/CMakeLists.txt -@@ -5,7 +5,9 @@ add_subdirectory(InstCombine) - add_subdirectory(Scalar) - add_subdirectory(IPO) - add_subdirectory(Vectorize) -+if(NOT WASI) - add_subdirectory(Hello) -+endif() - add_subdirectory(ObjCARC) - add_subdirectory(Coroutines) - add_subdirectory(CFGuard) -diff --git a/llvm/tools/CMakeLists.txt b/llvm/tools/CMakeLists.txt -index c6116ac81..c3eab659e 100644 ---- a/llvm/tools/CMakeLists.txt -+++ b/llvm/tools/CMakeLists.txt -@@ -28,12 +28,14 @@ endif() +-PreservedAnalyses +-InstrOrderFilePass::run(Module &M, ModuleAnalysisManager &AM) { ++PreservedAnalyses InstrOrderFilePass::run(Module &M, ++ ModuleAnalysisManager &AM) { + if (InstrOrderFile().run(M)) + return PreservedAnalyses::none(); + return PreservedAnalyses::all(); +diff '--color=auto' -r -u llvm/llvm/tools/CMakeLists.txt build/llvm-src/llvm/tools/CMakeLists.txt +--- llvm/llvm/tools/CMakeLists.txt 2023-12-29 16:36:26.139708973 +0100 ++++ build/llvm-src/llvm/tools/CMakeLists.txt 2024-01-02 03:15:13.633395352 +0100 +@@ -28,18 +28,23 @@ # Add LTO, llvm-ar, llvm-config, and llvm-profdata before clang, ExternalProject # requires targets specified in DEPENDS to exist before the call to # ExternalProject_Add. @@ -863,19 +4383,24 @@ index c6116ac81..c3eab659e 100644 add_llvm_tool_subdirectory(llvm-config) add_llvm_tool_subdirectory(llvm-lto) add_llvm_tool_subdirectory(llvm-profdata) -+endif() ++endif () # Projects supported via LLVM_EXTERNAL_*_SOURCE_DIR need to be explicitly # specified. -@@ -45,9 +47,11 @@ add_llvm_external_project(mlir) - add_llvm_external_project(flang) - add_llvm_external_project(bolt) - + add_llvm_external_project(clang) + add_llvm_external_project(lld) ++ +if (NOT WASI) - # Automatically add remaining sub-directories containing a 'CMakeLists.txt' - # file as external projects. + add_llvm_external_project(lldb) ++ + add_llvm_external_project(mlir) + # Flang depends on mlir, so place it afterward + add_llvm_external_project(flang) +@@ -50,6 +55,7 @@ add_llvm_implicit_projects() -+endif() add_llvm_external_project(polly) ++endif () + # Add subprojects specified using LLVM_EXTERNAL_PROJECTS + foreach(p ${LLVM_EXTERNAL_PROJECTS}) diff --git a/llvm_version_major.sh b/llvm_version_major.sh new file mode 100755 index 0000000..1a9e368 --- /dev/null +++ b/llvm_version_major.sh @@ -0,0 +1,4 @@ +#/usr/bin/env bash +LLVM_PROJ_DIR=${1:-./src/llvm-project} +MAJOR=`grep "set(LLVM_VERSION_MAJOR" $LLVM_PROJ_DIR/llvm/CMakeLists.txt | awk '{print substr($2, 1, length($2) - 1)}'` +echo $MAJOR diff --git a/test.sh b/test.sh index 4dd18a5..d43a472 100755 --- a/test.sh +++ b/test.sh @@ -2,18 +2,22 @@ set -xe -DIR=build/test +ROOT=$PWD/build/output +CPPROOT=$ROOT/cpp +PYROOT=$ROOT/python -$(which wasmtime) run --dir $PWD/build/output/python::/ \ +$(which wasmtime) run --dir $PYROOT::/ \ --env PYTHONPATH=/lib/python-3.13 \ - $PWD/build/output/python/bin/python3.wasm \ + $PYROOT/bin/python3.13.wasm \ -c "import json; print(json.dumps('hello'))" +DIR=build/test + rm -rf $DIR mkdir -p $DIR -cp -r build/output/cpp/* $DIR +cp -r $CPPROOT/* $DIR -cat > $DIR/root/main.cc << EOF +cat > $DIR/main.cc << EOF #include #include #include @@ -32,15 +36,16 @@ int main(int argc, char **argv) { } EOF -wasmtime --dir $DIR/root/::/ \ - $DIR/clang -cc1 \ - -I /include/c++/v1/ -I /clang-include/ -I /include/ \ +wasmtime --dir $DIR::/ \ + $DIR/bin/clang++ -cc1 -isysroot / \ + "-resource-dir" "lib/clang/18" -I /include/c++/v1 "-isysroot" "/" \ + "-internal-isystem" "lib/clang/18/include" "-internal-isystem" "/include/wasm32-wasi" "-internal-isystem" "/include" \ -O2 -emit-obj main.cc -o main.wasm -wasmtime --dir $DIR/root::/ \ - $DIR/wasm-ld \ - -L /lib/wasm32-wasi/ \ - -lc -lclang_rt.builtins-wasm32 /lib/wasm32-wasi/crt1.o \ +wasmtime --dir $DIR::/ \ + $DIR/bin/wasm-ld \ + -L /lib/wasm32-wasi/ /lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a \ + -lc /lib/wasm32-wasi/crt1.o \ -lc++ -lc++abi main.wasm -o main -wasmtime $DIR/root/main a b c d <<< 13845 +wasmtime $DIR/main a b c d <<< 13845 diff --git a/wasi-libc b/wasi-libc new file mode 160000 index 0000000..5a69318 --- /dev/null +++ b/wasi-libc @@ -0,0 +1 @@ +Subproject commit 5a693184e947b7a30087b91d4afc1a5d06eec885 diff --git a/wasi-libc-polyfill.c b/wasi-libc-polyfill.c new file mode 100644 index 0000000..4a0ba58 --- /dev/null +++ b/wasi-libc-polyfill.c @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +uid_t getuid(void) { return 1000; } +int getsid(int pid) { + if (pid == 0 || pid == getpid()) + return getpid(); + return -1; +} +int gethostname(char *name, size_t len) { + strncpy(name, "wasi", len); + return 0; +} +char *realpath(const char *path, char *resolved_path) { + if (resolved_path == NULL) { + resolved_path = (char *)malloc(PATH_MAX); + } + if (resolved_path != NULL) { + strcpy(resolved_path, path); + } + return resolved_path; +} + +static int fillstatvfs(struct statvfs *buf) { + memset(buf, 0, sizeof(*buf)); + buf->f_frsize = 512; + buf->f_bfree = 1024 * 1024; + buf->f_blocks = buf->f_bfree * 2; + buf->f_bavail = buf->f_bfree; + return 0; +} + +int statvfs(const char *path, struct statvfs *buf) { return fillstatvfs(buf); } +int fstatvfs(int fd, struct statvfs *buf) { return fillstatvfs(buf); } + +mode_t umask(mode_t mask) { return 0; } +int chmod(const char *pathname, mode_t mode) { return 0; } +int fchmod(int fd, mode_t mode) { return 0; } +int fchmodat(int fd, const char *path, mode_t mode, int flags) { return 0; } +int fchown(int fd, uid_t owner, gid_t group) { return 0; } +unsigned alarm(unsigned seconds) { return 0; } +int socket(int domain, int type, int protocol) { + errno = EACCES; + return -1; +} +int bind(int fd, const struct sockaddr *addr, socklen_t len) { + errno = EBADF; + return -1; +} +int connect(int fd, const struct sockaddr *addr, socklen_t len) { + errno = EBADF; + return -1; +} +int listen(int fd, int n) { + errno = EBADF; + return -1; +} + +int sigemptyset(sigset_t *set) { + memset(set, 0, sizeof(*set)); + return 0; +} +int sigfillset(sigset_t *set) { + memset(set, -1U, sizeof(*set)); + return 0; +} +int sigaddset(sigset_t *set, int sig) { + set->__val[0] |= (1 << sig); + return 0; +} +int sigdelset(sigset_t *set, int sig) { + set->__val[0] &= ~(1 << sig); + return 0; +} +int sigismember(const sigset_t *set, int sig) { + return (set->__val[0] & ~(1 << sig)) ? 1 : 0; +} + +// LLVM wants to install signal handlers, so failing here causes it not to +// produce any output. Pretend we succeeded and do nothing instead. +int sigprocmask(int how, const sigset_t *set, sigset_t *oset) { + if (oset) { + sigemptyset(oset); + } + return 0; +} + +int sigaction(int sig, const struct sigaction *restrict act, + struct sigaction *restrict oact) { + if (oact) { + oact->sa_handler = SIG_DFL; + } + return 0; +} + +int posix_madvise(void *addr, size_t len, int advice) { return 0; } + +int getpwnam_r(const char *restrict name, struct passwd *restrict pwd, + char buf[], size_t buflen, struct passwd **restrict result) { + *result = NULL; + return ENOENT; +} +int getpwuid_r(uid_t uid, struct passwd *restrict pwd, char buf[], + size_t buflen, struct passwd **restrict result) { + *result = NULL; + return ENOENT; +} + +int mprotect(void *addr, size_t len, int prot) { + if (prot == (PROT_READ | PROT_WRITE)) { + return 0; + } + errno = ENOTSUP; + return -1; +} + +int getrlimit(int resource, struct rlimit *rlp) { + errno = EINVAL; + return -1; +} + +int setrlimit(int resource, const struct rlimit *rlp) { + errno = EINVAL; + return -1; +} + +int dup(int oldfd) { + errno = EBADF; + return -1; +} + +int dup2(int oldfd, int newfd) { + errno = EBADF; + return -1; +} + +// For Python +#undef h_errno +int h_errno = HOST_NOT_FOUND; +int *__h_errno_location(void) { return &h_errno; } +struct servent *getservbyname(const char *name, const char *proto) { + return NULL; +} +struct servent *getservbyport(int port, const char *proto) { + return NULL; +} +struct hostent *gethostbyname(const char *name) { + return NULL; +} +struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type) { + return NULL; +} +const char *hstrerror(int err) { return "host not found"; } +struct protoent *getprotobyname(const char *name) { + return NULL; +} diff --git a/wasi-libc.patch b/wasi-libc.patch new file mode 100644 index 0000000..1ebfef6 --- /dev/null +++ b/wasi-libc.patch @@ -0,0 +1,491 @@ +Only in build/wasi-libc/: build +diff '--color=auto' -r -u wasi-libc/libc-bottom-half/headers/public/__typedef_sigset_t.h build/wasi-libc/libc-bottom-half/headers/public/__typedef_sigset_t.h +--- wasi-libc/libc-bottom-half/headers/public/__typedef_sigset_t.h 2024-01-02 01:42:49.525707686 +0100 ++++ build/wasi-libc/libc-bottom-half/headers/public/__typedef_sigset_t.h 2024-01-02 21:50:58.155242189 +0100 +@@ -2,6 +2,6 @@ + #define __wasilibc___typedef_sigset_t_h + + /* TODO: This is just a placeholder for now. Keep this in sync with musl. */ +-typedef unsigned char sigset_t; ++typedef struct { unsigned long long __val[1]; } sigset_t; + + #endif +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/arch/wasm32/bits/signal.h build/wasi-libc/libc-top-half/musl/arch/wasm32/bits/signal.h +--- wasi-libc/libc-top-half/musl/arch/wasm32/bits/signal.h 2023-12-31 09:15:54.869499142 +0100 ++++ build/wasi-libc/libc-top-half/musl/arch/wasm32/bits/signal.h 2024-01-02 21:50:58.156242245 +0100 +@@ -1,5 +1,3 @@ +-#ifdef _WASI_EMULATED_SIGNAL +- + #define SIGHUP 1 + #define SIGINT 2 + #define SIGQUIT 3 +@@ -37,4 +35,11 @@ + + #define _NSIG 65 + +-#endif ++#define SA_NOCLDSTOP 1 ++#define SA_NOCLDWAIT 2 ++#define SA_SIGINFO 4 ++#define SA_ONSTACK 0x08000000 ++#define SA_RESTART 0x10000000 ++#define SA_NODEFER 0x40000000 ++#define SA_RESETHAND 0x80000000 ++#define SA_RESTORER 0x04000000 +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/dirent.h build/wasi-libc/libc-top-half/musl/include/dirent.h +--- wasi-libc/libc-top-half/musl/include/dirent.h 2023-12-29 15:38:38.782537729 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/dirent.h 2024-01-02 21:50:58.156242245 +0100 +@@ -56,7 +56,9 @@ + #define DT_BLK 6 + #define DT_REG 8 + #define DT_LNK 10 ++#endif + #define DT_SOCK 12 ++#ifdef __wasilibc_unmodified_upstream /* Use alternate WASI libc headers */ + #define DT_WHT 14 + #define IFTODT(x) ((x)>>12 & 017) + #define DTTOIF(x) ((x)<<12) +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/fcntl.h build/wasi-libc/libc-top-half/musl/include/fcntl.h +--- wasi-libc/libc-top-half/musl/include/fcntl.h 2024-01-02 01:42:49.526707741 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/fcntl.h 2024-01-02 21:50:58.156242245 +0100 +@@ -55,11 +55,13 @@ + #define F_OFD_SETLKW 38 + + #define F_DUPFD_CLOEXEC 1030 ++#endif + + #define F_RDLCK 0 + #define F_WRLCK 1 + #define F_UNLCK 2 + ++#ifdef __wasilibc_unmodified_upstream /* Use alternate WASI libc headers */ + #define FD_CLOEXEC 1 + + #define AT_FDCWD (-100) +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/signal.h build/wasi-libc/libc-top-half/musl/include/signal.h +--- wasi-libc/libc-top-half/musl/include/signal.h 2023-12-29 15:38:38.785537895 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/signal.h 2024-01-02 21:50:58.156242245 +0100 +@@ -1,7 +1,3 @@ +-#ifndef _WASI_EMULATED_SIGNAL +-#error "wasm lacks signal support; to enable minimal signal emulation, \ +-compile with -D_WASI_EMULATED_SIGNAL and link with -lwasi-emulated-signal" +-#else + #ifndef _SIGNAL_H + #define _SIGNAL_H + +@@ -15,7 +11,6 @@ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ + || defined(_BSD_SOURCE) + +-#ifdef __wasilibc_unmodified_upstream /* WASI has no ucontext support */ + #ifdef _GNU_SOURCE + #define __ucontext ucontext + #endif +@@ -49,15 +44,12 @@ + typedef struct sigaltstack stack_t; + #endif + +-#endif +- + #include + + #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ + || defined(_BSD_SOURCE) + +-#ifdef __wasilibc_unmodified_upstream /* WASI has no sigaction */ + #define SIG_HOLD ((void (*)(int)) 2) + + #define FPE_INTDIV 1 +@@ -188,6 +180,8 @@ + #define SA_UNSUPPORTED 0x00000400 + #define SA_EXPOSE_TAGBITS 0x00000800 + ++#ifdef __wasilibc_unmodified_upstream /* WASI has no realtime signals */ ++ + struct sigevent { + union sigval sigev_value; + int sigev_signo; +@@ -210,9 +204,7 @@ + #define SIGEV_NONE 1 + #define SIGEV_THREAD 2 + #define SIGEV_THREAD_ID 4 +-#endif + +-#ifdef __wasilibc_unmodified_upstream /* WASI has no realtime signals */ + int __libc_current_sigrtmin(void); + int __libc_current_sigrtmax(void); + +@@ -222,6 +214,7 @@ + + #ifdef __wasilibc_unmodified_upstream /* WASI has no signals */ + int kill(pid_t, int); ++#endif + + int sigemptyset(sigset_t *); + int sigfillset(sigset_t *); +@@ -230,8 +223,11 @@ + int sigismember(const sigset_t *, int); + + int sigprocmask(int, const sigset_t *__restrict, sigset_t *__restrict); ++#ifdef __wasilibc_unmodified_upstream /* WASI has no signals */ + int sigsuspend(const sigset_t *); ++#endif + int sigaction(int, const struct sigaction *__restrict, struct sigaction *__restrict); ++#ifdef __wasilibc_unmodified_upstream /* WASI has no signals */ + int sigpending(sigset_t *); + int sigwait(const sigset_t *__restrict, int *__restrict); + int sigwaitinfo(const sigset_t *__restrict, siginfo_t *__restrict); +@@ -336,4 +332,3 @@ + #endif + + #endif +-#endif +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/stdlib.h build/wasi-libc/libc-top-half/musl/include/stdlib.h +--- wasi-libc/libc-top-half/musl/include/stdlib.h 2023-12-29 15:38:38.785537895 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/stdlib.h 2024-01-02 21:50:58.156242245 +0100 +@@ -128,9 +128,7 @@ + + #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ + || defined(_BSD_SOURCE) +-#ifdef __wasilibc_unmodified_upstream /* WASI has no absolute paths */ + char *realpath (const char *__restrict, char *__restrict); +-#endif + long int random (void); + void srandom (unsigned int); + char *initstate (unsigned int, char *, size_t); +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/sys/mman.h build/wasi-libc/libc-top-half/musl/include/sys/mman.h +--- wasi-libc/libc-top-half/musl/include/sys/mman.h 2023-12-29 15:38:38.785537895 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/sys/mman.h 2024-01-02 22:02:33.744824119 +0100 +@@ -1,7 +1,3 @@ +-#ifndef _WASI_EMULATED_MMAN +-#error "WASI lacks a true mmap; to enable minimal mmap emulation, \ +-compile with -D_WASI_EMULATED_MMAN and link with -lwasi-emulated-mman" +-#else + #ifndef _SYS_MMAN_H + #define _SYS_MMAN_H + #ifdef __cplusplus +@@ -130,13 +126,6 @@ + int mlockall (int); + int munlockall (void); + +-#ifdef _GNU_SOURCE +-void *mremap (void *, size_t, size_t, int, ...); +-int remap_file_pages (void *, size_t, int, size_t, int); +-int memfd_create (const char *, unsigned); +-int mlock2 (const void *, size_t, unsigned); +-#endif +- + #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) + int madvise (void *, size_t, int); + int mincore (void *, size_t, unsigned char *); +@@ -154,4 +143,3 @@ + } + #endif + #endif +-#endif +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/sys/resource.h build/wasi-libc/libc-top-half/musl/include/sys/resource.h +--- wasi-libc/libc-top-half/musl/include/sys/resource.h 2023-12-29 15:38:38.786537951 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/sys/resource.h 2024-01-02 21:50:58.156242245 +0100 +@@ -1,8 +1,3 @@ +-#ifndef _WASI_EMULATED_PROCESS_CLOCKS +-#error WASI lacks process-associated clocks; to enable emulation of the `getrusage` function using \ +-the wall clock, which isn't sensitive to whether the program is running or suspended, \ +-compile with -D_WASI_EMULATED_PROCESS_CLOCKS and link with -lwasi-emulated-process-clocks +-#else + #ifndef _SYS_RESOURCE_H + #define _SYS_RESOURCE_H + +@@ -22,7 +17,6 @@ + #include + #include + +-#ifdef __wasilibc_unmodified_upstream /* Use alternate WASI libc headers */ + typedef unsigned long long rlim_t; + + struct rlimit { +@@ -56,8 +50,10 @@ + int setrlimit (int, const struct rlimit *); + int getrusage (int, struct rusage *); + ++#ifdef __wasilibc_unmodified_upstream + int getpriority (int, id_t); + int setpriority (int, id_t, int); ++#endif + + #ifdef _GNU_SOURCE + int prlimit(pid_t, int, const struct rlimit *, struct rlimit *); +@@ -110,9 +106,6 @@ + #define rlimit64 rlimit + #define rlim64_t rlim_t + #endif +-#else +-#include <__header_sys_resource.h> +-#endif + + #if _REDIR_TIME64 + __REDIR(getrusage, __getrusage_time64); +@@ -123,4 +116,3 @@ + #endif + + #endif +-#endif +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/sys/socket.h build/wasi-libc/libc-top-half/musl/include/sys/socket.h +--- wasi-libc/libc-top-half/musl/include/sys/socket.h 2023-12-29 15:38:38.786537951 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/sys/socket.h 2024-01-02 21:50:58.156242245 +0100 +@@ -88,6 +88,7 @@ + #define SHUT_RD 0 + #define SHUT_WR 1 + #define SHUT_RDWR 2 ++#endif + + #ifndef SOCK_STREAM + #define SOCK_STREAM 1 +@@ -336,6 +337,7 @@ + + #define SOMAXCONN 128 + ++#ifdef __wasilibc_unmodified_upstream /* Use alternate WASI libc headers */ + #define MSG_OOB 0x0001 + #define MSG_PEEK 0x0002 + #define MSG_DONTROUTE 0x0004 +@@ -395,18 +397,16 @@ + #include <__struct_sockaddr_storage.h> + #endif + +-#ifdef __wasilibc_unmodified_upstream /* WASI has no socket/socketpair */ + int socket (int, int, int); ++#ifdef __wasilibc_unmodified_upstream /* WASI has no socket/socketpair */ + int socketpair (int, int, int, int [2]); + #endif + + int shutdown (int, int); + +-#ifdef __wasilibc_unmodified_upstream /* WASI has no bind/connect/listen/accept */ + int bind (int, const struct sockaddr *, socklen_t); + int connect (int, const struct sockaddr *, socklen_t); + int listen (int, int); +-#endif + + int accept (int, struct sockaddr *__restrict, socklen_t *__restrict); + int accept4(int, struct sockaddr *__restrict, socklen_t *__restrict, int); +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/sys/stat.h build/wasi-libc/libc-top-half/musl/include/sys/stat.h +--- wasi-libc/libc-top-half/musl/include/sys/stat.h 2023-12-29 15:38:38.786537951 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/sys/stat.h 2024-01-02 21:50:58.156242245 +0100 +@@ -78,14 +78,10 @@ + int fstat(int, struct stat *); + int lstat(const char *__restrict, struct stat *__restrict); + int fstatat(int, const char *__restrict, struct stat *__restrict, int); +-#ifdef __wasilibc_unmodified_upstream /* WASI has no chmod */ + int chmod(const char *, mode_t); + int fchmod(int, mode_t); + int fchmodat(int, const char *, mode_t, int); +-#endif +-#ifdef __wasilibc_unmodified_upstream /* WASI has no umask */ + mode_t umask(mode_t); +-#endif + int mkdir(const char *, mode_t); + #ifdef __wasilibc_unmodified_upstream /* WASI has no fifo */ + int mkfifo(const char *, mode_t); +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/sys/times.h build/wasi-libc/libc-top-half/musl/include/sys/times.h +--- wasi-libc/libc-top-half/musl/include/sys/times.h 2023-12-29 15:38:38.786537951 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/sys/times.h 2024-01-02 21:50:58.156242245 +0100 +@@ -1,8 +1,3 @@ +-#ifndef _WASI_EMULATED_PROCESS_CLOCKS +-#error WASI lacks process-associated clocks; to enable emulation of the `times` function using \ +-the wall clock, which isn't sensitive to whether the program is running or suspended, \ +-compile with -D_WASI_EMULATED_PROCESS_CLOCKS and link with -lwasi-emulated-process-clocks +-#else + #ifndef _SYS_TIMES_H + #define _SYS_TIMES_H + +@@ -31,4 +26,3 @@ + #endif + + #endif +-#endif +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/sys/un.h build/wasi-libc/libc-top-half/musl/include/sys/un.h +--- wasi-libc/libc-top-half/musl/include/sys/un.h 2023-12-29 15:38:38.786537951 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/sys/un.h 2024-01-02 21:50:58.156242245 +0100 +@@ -14,14 +14,10 @@ + + #include + +-#ifdef __wasilibc_unmodified_upstream /* WASI has no UNIX-domain sockets */ + struct sockaddr_un { + sa_family_t sun_family; + char sun_path[108]; + }; +-#else +-#include <__struct_sockaddr_un.h> +-#endif + + #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) + #ifdef __wasilibc_unmodified_upstream /* Declare strlen with the same attributes as uses */ +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/time.h build/wasi-libc/libc-top-half/musl/include/time.h +--- wasi-libc/libc-top-half/musl/include/time.h 2023-12-29 15:38:38.787538006 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/time.h 2024-01-02 21:50:58.157242300 +0100 +@@ -60,16 +60,7 @@ + #include <__header_time.h> + #endif + +-#if defined(__wasilibc_unmodified_upstream) || defined(_WASI_EMULATED_PROCESS_CLOCKS) + clock_t clock (void); +-#else +-__attribute__((__deprecated__( +-"WASI lacks process-associated clocks; to enable emulation of the `clock` function using " +-"the wall clock, which isn't sensitive to whether the program is running or suspended, " +-"compile with -D_WASI_EMULATED_PROCESS_CLOCKS and link with -lwasi-emulated-process-clocks" +-))) +-clock_t clock (void); +-#endif + time_t time (time_t *); + double difftime (time_t, time_t); + time_t mktime (struct tm *); +diff '--color=auto' -r -u wasi-libc/libc-top-half/musl/include/unistd.h build/wasi-libc/libc-top-half/musl/include/unistd.h +--- wasi-libc/libc-top-half/musl/include/unistd.h 2023-12-29 15:38:38.787538006 +0100 ++++ build/wasi-libc/libc-top-half/musl/include/unistd.h 2024-01-02 21:50:58.157242300 +0100 +@@ -51,11 +51,9 @@ + #endif + int close(int); + int posix_close(int, int); +-#ifdef __wasilibc_unmodified_upstream /* WASI has no dup */ + int dup(int); + int dup2(int, int); + int dup3(int, int, int); +-#endif + off_t lseek(int, off_t, int); + #ifdef __wasilibc_unmodified_upstream /* Optimize the readonly case of lseek */ + #else +@@ -93,12 +91,10 @@ + ssize_t pread(int, void *, size_t, off_t); + ssize_t pwrite(int, const void *, size_t, off_t); + +-#ifdef __wasilibc_unmodified_upstream /* WASI has no chown */ + int chown(const char *, uid_t, gid_t); + int fchown(int, uid_t, gid_t); + int lchown(const char *, uid_t, gid_t); + int fchownat(int, const char *, uid_t, gid_t, int); +-#endif + + int link(const char *, const char *); + int linkat(int, const char *, int, const char *, int); +@@ -128,9 +124,7 @@ + int chdir(const char *); + char *getcwd(char *, size_t); + +-#ifdef __wasilibc_unmodified_upstream /* WASI has no signals */ + unsigned alarm(unsigned); +-#endif + unsigned sleep(unsigned); + #ifdef __wasilibc_unmodified_upstream /* WASI has no pause */ + int pause(void); +@@ -149,24 +143,15 @@ + #endif + _Noreturn void _exit(int); + +-#if defined(__wasilibc_unmodified_upstream) || defined(_WASI_EMULATED_GETPID) +-pid_t getpid(void); +-#else +-__attribute__((__deprecated__( +-"WASI lacks process identifiers; to enable emulation of the `getpid` function using " +-"a placeholder value, which doesn't reflect the host PID of the program, " +-"compile with -D_WASI_EMULATED_GETPID and link with -lwasi-emulated-getpid" +-))) + pid_t getpid(void); +-#endif + #ifdef __wasilibc_unmodified_upstream /* WASI has no getpid etc. */ + pid_t getppid(void); + pid_t getpgrp(void); + pid_t getpgid(pid_t); + int setpgid(pid_t, pid_t); + pid_t setsid(void); +-pid_t getsid(pid_t); + #endif ++pid_t getsid(pid_t); + #ifdef __wasilibc_unmodified_upstream /* WASI has no ttyname */ + char *ttyname(int); + int ttyname_r(int, char *, size_t); +@@ -177,8 +162,8 @@ + int tcsetpgrp(int, pid_t); + #endif + +-#ifdef __wasilibc_unmodified_upstream /* WASI has no getuid etc. */ + uid_t getuid(void); ++#ifdef __wasilibc_unmodified_upstream /* WASI has no getuid etc. */ + uid_t geteuid(void); + gid_t getgid(void); + gid_t getegid(void); +diff '--color=auto' -r -u wasi-libc/Makefile build/wasi-libc/Makefile +--- wasi-libc/Makefile 2023-12-29 15:38:38.761536567 +0100 ++++ build/wasi-libc/Makefile 2024-01-02 21:50:58.157242300 +0100 +@@ -389,6 +389,8 @@ + LIBDL_OBJS = $(call objs,$(LIBDL_SOURCES)) + LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES)) + ++LIBC_OBJS += $(OBJDIR)/wasi-libc-polyfill.o ++ + # These variables describe the locations of various files and + # directories in the generated sysroot tree. + SYSROOT_LIB := $(SYSROOT)/lib/$(TARGET_TRIPLE) +@@ -420,14 +422,9 @@ + "sys/user.h" \ + "sys/kd.h" "sys/vt.h" "sys/soundcard.h" "sys/sem.h" \ + "sys/shm.h" "sys/msg.h" "sys/ipc.h" "sys/ptrace.h" \ +- "sys/statfs.h" \ + "bits/kd.h" "bits/vt.h" "bits/soundcard.h" "bits/sem.h" \ + "bits/shm.h" "bits/msg.h" "bits/ipc.h" "bits/ptrace.h" \ +- "bits/statfs.h" \ +- "sys/vfs.h" \ +- "sys/statvfs.h" \ + "syslog.h" "sys/syslog.h" \ +- "wait.h" "sys/wait.h" \ + "ucontext.h" "sys/ucontext.h" \ + "paths.h" \ + "utmp.h" "utmpx.h" \ +@@ -448,9 +445,8 @@ + "elf.h" "link.h" "bits/link.h" \ + "scsi/scsi.h" "scsi/scsi_ioctl.h" "scsi/sg.h" \ + "sys/auxv.h" \ +- "pwd.h" "shadow.h" "grp.h" \ ++ "shadow.h" "grp.h" \ + "mntent.h" \ +- "netdb.h" \ + "resolv.h" \ + "pty.h" \ + "setjmp.h" \ +@@ -532,7 +528,8 @@ + + $(OBJDIR)/libdl.so.a: $(LIBDL_SO_OBJS) + +-$(SYSROOT_LIB)/libc.a: $(LIBC_OBJS) ++$(SYSROOT_LIB)/libc.a: $(LIBC_OBJS) $(LIBWASI_EMULATED_SIGNAL_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS) $(LIBWASI_EMULATED_MMAN_OBJS) $(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS) $(LIBWASI_EMULATED_GETPID_OBJS) ++ + + $(SYSROOT_LIB)/libc-printscan-long-double.a: $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS) + +@@ -705,13 +702,6 @@ + # The build succeeded! The generated sysroot is in $(SYSROOT). + # + +-# 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 +- + DEFINED_SYMBOLS = $(SYSROOT_SHARE)/defined-symbols.txt + UNDEFINED_SYMBOLS = $(SYSROOT_SHARE)/undefined-symbols.txt + +@@ -814,7 +804,7 @@ + + install: finish + mkdir -p "$(INSTALL_DIR)" +- cp -r "$(SYSROOT)/lib" "$(SYSROOT)/share" "$(SYSROOT)/include" "$(INSTALL_DIR)" ++ cp -r "$(SYSROOT)/lib" "$(SYSROOT)/include" "$(INSTALL_DIR)" + + clean: + $(RM) -r "$(OBJDIR)" +Only in build/wasi-libc/: sysroot +Only in build/wasi-libc/: wasi-libc-polyfill.c diff --git a/wasi_shim.cpp b/wasi_shim.cpp deleted file mode 100644 index e7d0659..0000000 --- a/wasi_shim.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#include "wasi_shim.h" - -#include -#include -#include -#include -#include -#include -#include - -// This file commits all sorts of crimes. - -extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso_handle); -extern "C" int __cxa_thread_atexit(void (*func)(void *), void *arg, - void *dso_handle) { - return __cxa_atexit(func, arg, dso_handle); -} - -extern "C" int getpid() { return 12345; } -extern "C" int gethostname(char *name, size_t len) { - strncpy(name, "wasi", len); - return 0; -} -extern "C" char *realpath(const char *path, char *resolved_path) throw() { - if (resolved_path == nullptr) { - resolved_path = (char *)malloc(PATH_MAX); - } - if (resolved_path != nullptr) { - strcpy(resolved_path, path); - } - return resolved_path; -} - -static constexpr size_t kThreadKeyCapacity = 1024; -static std::pair - thread_key_storage[kThreadKeyCapacity]; -static size_t thread_key_size; - -__attribute__((destructor)) void cleanup_thread_key() { - for (size_t _ = 0; _ < 100; _++) { - bool run = false; - for (size_t i = 0; i < thread_key_size; i++) { - auto &kv = thread_key_storage[i]; - if (kv.first) { - auto v = kv.first; - kv.first = nullptr; - kv.second(v); - run = true; - } - } - if (!run) { - return; - } - } -} - -extern "C" int pthread_key_create(pthread_key_t *key, - void (*destructor)(void *)) { - if (thread_key_size == kThreadKeyCapacity) { - return EAGAIN; - } - *key = thread_key_size++; - if (!destructor) { - destructor = +[](void *) {}; - } - thread_key_storage[*key] = std::make_pair(nullptr, destructor); - return 0; -} -extern "C" int pthread_key_delete(pthread_key_t key) { - thread_key_storage[key].first = nullptr; - return 0; -} -extern "C" int pthread_setspecific(pthread_key_t key, const void *value) { - thread_key_storage[key].first = const_cast(value); - return 0; -} - -extern "C" void *pthread_getspecific(pthread_key_t key) { - return thread_key_storage[key].first; -} - -extern "C" int pthread_detach(pthread_t th) { return 0; } - -extern "C" int pthread_create(pthread_t *__restrict__ thread, - const pthread_attr_t *__restrict__ attr, - void *(*start_routine)(void *), - void *__restrict__ arg) { - return EAGAIN; -} - -namespace std { -inline namespace __2 { -__shared_mutex_base::__shared_mutex_base() {} -recursive_mutex::recursive_mutex() {} -recursive_mutex::~recursive_mutex() {} -void recursive_mutex::lock() {} -void recursive_mutex::unlock() noexcept {} -mutex::~mutex() noexcept {} -void mutex::lock() {} -void mutex::unlock() noexcept {} -condition_variable::~condition_variable() {} -void condition_variable::wait( - std::__2::unique_lock &) noexcept {} -void condition_variable::notify_all() noexcept {} -} // namespace __2 -} // namespace std - -#include "llvm/libcxx/src/future.cpp" -#include "llvm/libcxx/src/thread.cpp" diff --git a/wasi_shim.h b/wasi_shim.h index 48551d0..63731dd 100644 --- a/wasi_shim.h +++ b/wasi_shim.h @@ -1,2 +1,25 @@ +#include +#include extern "C" char *realpath(const char *path, char *resolved_path) throw(); +extern "C" int getsid(int pid); + +#define MNT_LOCAL 1 + +// fake statvfs for LLVM that always returns a FS with half a gig used and free. +struct statvfs { + unsigned f_blocks; + unsigned f_bfree; + unsigned f_bavail; + unsigned f_frsize; + unsigned f_flags; +}; + +extern "C" int statvfs(const char *path, struct statvfs *buf); +extern "C" int fstatvfs(int fd, struct statvfs *buf); + +extern "C" mode_t umask(mode_t mask); +extern "C" int chmod(const char *pathname, mode_t mode); +extern "C" int fchmod(int fd, mode_t mode); +extern "C" int fchown(int fd, uid_t owner, gid_t group); +extern "C" unsigned alarm(unsigned seconds);