Skip to content

Instantly share code, notes, and snippets.

View MaskRay's full-sized avatar
🏠
Working from home. Need food

Fangrui Song MaskRay

🏠
Working from home. Need food
View GitHub Profile
@MaskRay
MaskRay / CMakeLists.txt
Last active August 13, 2024 22:56
cminify: shorten function/variable/type names in a C source file
cmake_minimum_required(VERSION 3.14)
project(cminify LANGUAGES C CXX)
add_executable(cminify "")
set(DEFAULT_CMAKE_BUILD_TYPE Release)
set_property(TARGET cminify PROPERTY CXX_STANDARD 17)
set_property(TARGET cminify PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET cminify PROPERTY CXX_EXTENSIONS OFF)
find_package(Clang REQUIRED)
@MaskRay
MaskRay / b.c
Created November 6, 2022 21:43
-fgnu89-inline / C99 / C++ inline modes
//__attribute__((gnu_inline))
EXTERN inline int foo(int a) { return a * a; }
int bar(int a) { return foo(a+1); }
@MaskRay
MaskRay / bench-lld.sh
Last active January 8, 2023 00:25
lld with different malloc implementations
# Build malloc implementations.
mkdir -p /tmp/p
pushd /tmp/p
if [[ ! -d jemalloc ]]; then
git clone https://github.com/jemalloc/jemalloc
(cd jemalloc
./autogen.sh
mkdir -p out/release && cd out/release
../../configure && make -j $(nproc))
fi
@MaskRay
MaskRay / lld-copy-fewer-members.patch
Created February 6, 2022 05:12
[PATCH] [ELF] Symbol::replace: copy fewer members
From 7592fc59796cb41a4e7fe0d171b11d4198718a5b Mon Sep 17 00:00:00 2001
From: Fangrui Song
Date: Sat, 5 Feb 2022 16:48:23 -0800
Subject: [PATCH] [ELF] Symbol::replace: copy fewer members
Symbol::replace copies the whole struct and then restores some members.
This patch changes it to copy the needed members.
---
lld/ELF/InputFiles.cpp | 2 +
@MaskRay
MaskRay / parallelForEach.patch
Created January 17, 2022 19:27
[Support] Add parallelForEach with fixed TaskSize and use it in lld/ELF's Writer
From 7a28e9d1cc99e997ca8e2d463776aaa7e8c1d6da Mon Sep 17 00:00:00 2001
From: Fangrui Song <>
Date: Mon, 17 Jan 2022 11:25:51 -0800
Subject: [PATCH] [Support] Add parallelForEach with fixed TaskSize and use it
in lld/ELF's Writer
---
lld/ELF/OutputSections.cpp | 2 +-
llvm/include/llvm/Support/Parallel.h | 21 +++++++++++++++++++++
2 files changed, 22 insertions(+), 1 deletion(-)
@MaskRay
MaskRay / gist:4f274c978df684c870aec0254f844487
Created January 14, 2022 01:07
ld.lld: poor man's concurrent hash map
From 088ff7744ff15adc524f21431a5206d6f82f493b Mon Sep 17 00:00:00 2001
From: Fangrui Song
Date: Thu, 13 Jan 2022 17:05:28 -0800
Subject: [PATCH] DO NOT SUBMIT! Poor man's concurrent hash map
---
lld/ELF/SyntheticSections.cpp | 25 +++++++++----------------
lld/ELF/SyntheticSections.h | 2 +-
lld/include/lld/Common/SpinMutex.h | 28 ++++++++++++++++++++++++++++
3 files changed, 38 insertions(+), 17 deletions(-)
@MaskRay
MaskRay / implement-an-elf-linker.md
Last active December 1, 2025 20:24
Implement an ELF linker
theme class highlighter fonts
default
text-center
MaskRay
sans serif mono
sans-serif
serif
monospace
@MaskRay
MaskRay / loc.md
Last active September 11, 2024 03:55
Very coarse estimate of compiler complexity/LOC

It is by no means accurate and is comparing apples and oranges in many dimensions, e.g.

  • Targeting LLVM IR, C, assembly have varying difficulty.
  • Some may include runtime while some may not.
  • Different compilers are written in different languages. Languages have different expressiveness. Different paradigms have varying expressiveness.
  • LLVM has many non-default passes which are not used by regular compilation. I try to use fine-grained directories but still some unneeded files are included.

Nevertheless, here is the result (auxiliary files like shell/Makefile/documentation are ignored):

  • gcc (2207000+): tokei gcc libcpp -e ada -e d -e go -e objc -e objcp -e '*test*'
diff --git i/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp w/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index 7ce9e25da342..37ff9af84679 100644
--- i/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ w/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -458,2 +458,3 @@ static void GetTls(uptr *addr, uptr *size) {
*addr += ThreadDescriptorSize();
+ Printf("+++GetTls: %p %zd\n", *addr, *size);
#elif SANITIZER_GLIBC && defined(__aarch64__)
@@ -462,2 +463,3 @@ static void GetTls(uptr *addr, uptr *size) {
*size = g_tls_size + ThreadDescriptorSize();
@MaskRay
MaskRay / protected.c
Created June 18, 2021 01:45
STV_PROTECTED
__attribute__((visibility("protected")))
void *func_addr() { return (void *)func_addr; }
__attribute__((visibility("protected"))) void undef_func();
void *undef_func_addr() { return (void *)undef_func; }
__attribute__((visibility("protected"))) int var;
int load() { return var; }
__attribute__((visibility("protected"))) int inlinable(int x) { return x + x; }