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 / README.md
Last active July 20, 2026 03:37
Simplified BranchProbabilityInfo model

Simplified BranchProbabilityInfo demo

bpi.cpp reimplements the estimated (loop-aware) heuristic of LLVM's BranchProbabilityInfo — the only heuristic that is a pure function of the CFG + loop structure. See the header comment in bpi.cpp for what is intentionally omitted (instruction-level heuristics, computeUnlikelySuccessors).

Loops come from a nested cycle forest built with Tao Wei's single-pass DFS (the algorithm llvm::CycleInfo uses), visiting successors LIFO to match LLVM's iterative DFS order. This handles reducible and irreducible loops and gives each

@MaskRay
MaskRay / havlak.cc
Last active July 18, 2026 21:39
A new algorithm for identifying loops in decompilation
// Havlak-Tarjan loop-nesting-forest algorithm, self-contained.
//
// Two passes over the CFG: (1) a DFS spanning tree recording preorder and finish
// times, then (2) candidate headers in reverse preorder, each flood-filling its
// loop body backward through predecessors, with a UNION-FIND collapse of the
// already-formed inner loops. Each loop tracks its ENTRY set, so absorbing an
// irreducible multi-entry subloop re-examines the predecessors of ALL its
// entries -- pulling in the outer nodes that reach it through a non-header
// entry. It builds the same loop-nesting forest as the single-pass wei.cpp and
// as llvm/include/llvm/ADT/GenericCycleImpl.h.
@MaskRay
MaskRay / main.cpp
Created July 3, 2026 19:30
Squarebound
// ============================================================================
// Squarebound · 方格征途
// —— 致童年:在五子棋盘上,用黑白棋子与四色标识摆出的战棋梦。
//
// 绿子=主角 蓝/红/黄=英雄 白子正面=我方小兵 白子反面=敌方小兵 黑子=障碍
// 15x15 棋盘(五子棋规格,含星位)。英雄血量以二进制棋子展示(致敬)。
//
// 操作:左键 选择/移动/攻击 右键 待命/取消 空格 结束回合(敌方回合按住加速)
// Shift+F1~F9 存档 F1~F9 读档 Esc 取消选择/返回标题
// ============================================================================
@MaskRay
MaskRay / alan-modra.txt
Last active February 8, 2026 02:45
Silly questions "What's the code review style of XXX?" and Claude Code's answers
Alan Modra - Code Review Style
===============================
Role: Long-time GNU binutils maintainer, GNU ld (BFD linker), PowerPC backend
Deep institutional knowledge
- Decades of binutils/ld experience
- Knows the history behind obscure design decisions
- Understands BFD internals better than anyone
@MaskRay
MaskRay / wavl.cc
Last active December 15, 2025 02:03
Weak AVL tree
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <numeric>
#include <random>
#include <vector>
using namespace std;
@MaskRay
MaskRay / be_prefix_varint.c
Last active February 3, 2026 19:16
variants of Little Endian Base 128 where the length information is in the first byte
#include <assert.h>
#include <inttypes.h>
#include <stdbit.h>
#include <stdio.h>
#include <string.h>
/*
Big-endian prefix varint encoding for 64-bit integers.
The first byte determines the total length using most significant bits.
@MaskRay
MaskRay / eh_frame_results.csv
Last active October 24, 2025 07:13
Analyze .eh_frame section size distribution across executables and shared libraries on a Linux system
File Total_VM_KB EH_Frame_VM_KB EH_Frame_Ratio
/usr/bin/s390x-linux-gnu-lto-dump-14 26112.0 1863.68 7.1373
/usr/bin/pnmcrop 18.7 0.34765625 1.8591
/usr/bin/uclampset 17.9 1.23 6.8715
/usr/bin/atktopbm 6.36 0.18359375 2.8867
/usr/bin/getent 26.1 1.3 4.9808
/usr/bin/systemd-firstboot 52.1 1.2 2.3033
/usr/bin/busctl 82.9 1.79 2.1592
/usr/bin/arm-linux-gnueabi-g++ 1853.44 113.0 6.0968
/usr/bin/loadunimap 22.2 0.765625 3.4488
@MaskRay
MaskRay / suffixvarint.c
Created October 6, 2025 05:54
unsigned 70-bit suffixvarint (little-endian)
#include <assert.h>
#include <stdbit.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/*
little-endian suffix varint
xxxxxxx1: 7 value bits, 1 byte
@MaskRay
MaskRay / bench.rb
Last active April 24, 2026 02:40
https://maskray.me/blog/2025-08-31-benchmarking-compression-programs program, distributed under the terms of both the MIT license and the Apache License (Version 2.0)
#!/usr/bin/env ruby
# This program downloads and builds several compression utilities, benchmarks their compression and decompression
# performance on a specific input file including memory consumption, and finally generates HTML charts.
require 'benchmark'
require 'digest'
require 'etc'
require 'fileutils'
require 'json'
require 'net/http'
require 'optparse'
@MaskRay
MaskRay / bad.log
Created August 23, 2025 18:21
journalctl-b.log, good vs bad (system freeze)
Aug 23 10:58:15 blitz kernel: Linux version 6.16.1-arch1-1 (linux@archlinux) (gcc (GCC) 15.2.1 20250813, GNU ld (GNU Binutils) 2.45.0) #1 SMP PREEMPT_DYNAMIC Fri, 15 Aug 2025 16:04:43 +0000
Aug 23 10:58:15 blitz kernel: Command line: BOOT_IMAGE=/vmlinuz-linux root=/dev/nvme0n1p3
Aug 23 10:58:15 blitz kernel: x86/CPU: Model not found in latest microcode list
Aug 23 10:58:15 blitz kernel: x86/split lock detection: #AC: crashing the kernel on kernel split_locks and warning on user-space split_locks
Aug 23 10:58:15 blitz kernel: BIOS-provided physical RAM map:
Aug 23 10:58:15 blitz kernel: BIOS-e820: [mem 0x0000000000000000-0x000000000009efff] usable
Aug 23 10:58:15 blitz kernel: BIOS-e820: [mem 0x000000000009f000-0x00000000000fffff] reserved
Aug 23 10:58:15 blitz kernel: BIOS-e820: [mem 0x0000000000100000-0x000000005df8afff] usable
Aug 23 10:58:15 blitz kernel: BIOS-e820: [mem 0x000000005df8b000-0x000000005e88afff] reserved
Aug 23 10:58:15 blitz kernel: BIOS-e820: [mem 0x000000005e88b000-0x00000000606befff] usab