Skip to content

Instantly share code, notes, and snippets.

@Filiprogrammer
Created June 19, 2026 19:35
Show Gist options
  • Select an option

  • Save Filiprogrammer/117de8217efe1bba5670b4c4c2a1a00c to your computer and use it in GitHub Desktop.

Select an option

Save Filiprogrammer/117de8217efe1bba5670b4c4c2a1a00c to your computer and use it in GitHub Desktop.
Diff the LLVM-IR between two Git commits of a Rust crate
#!/usr/bin/env bash
set -e
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <commit_A> <commit_B>"
exit 1
fi
COMMIT_A=$1
COMMIT_B=$2
RUST_COMMIT_HASH=$(rustc --version --verbose | sed '3q;d' | cut -d' ' -f2)
echo "Comparing LLVM-IR between $COMMIT_A..$COMMIT_B"
echo "Rust toolchain commit hash: $RUST_COMMIT_HASH"
mkdir -p /tmp/git-llvm-diff
TREE_A="/tmp/git-llvm-diff/${RUST_COMMIT_HASH}_${COMMIT_A}"
TREE_B="/tmp/git-llvm-diff/${RUST_COMMIT_HASH}_${COMMIT_B}"
if [ ! -d "$TREE_A" ]; then
mkdir "$TREE_A"
git archive "$COMMIT_A" | tar -x -C "$TREE_A"
fi
if [ ! -d "$TREE_B" ]; then
mkdir "$TREE_B"
git archive "$COMMIT_B" | tar -x -C "$TREE_B"
fi
get_ll_mapping() {
local tree=$1
cd "$tree"
RUSTFLAGS="-C lto=off --emit=llvm-ir --emit=asm -Z location-detail=none" cargo build --release --all-targets --benches --message-format=json | jq -r 'select(.reason == "compiler-artifact" and .executable != null) | "\(.target.name)\t\(.executable).ll"'
}
echo "Building COMMIT_A..."
declare -A LL_FILES_A
while IFS=$'\t' read -r target_name ll_path; do
LL_FILES_A["$target_name"]="$ll_path"
done < <(get_ll_mapping "$TREE_A")
echo "Building COMMIT_B..."
declare -A LL_FILES_B
while IFS=$'\t' read -r target_name ll_path; do
LL_FILES_B["$target_name"]="$ll_path"
done < <(get_ll_mapping "$TREE_B")
echo "Diffing LLVM-IR..."
for target in "${!LL_FILES_A[@]}"; do
file_a="${LL_FILES_A[$target]}"
file_b="${LL_FILES_B[$target]}"
if [ -n "$file_b" ]; then
printf "\033[1;34m========== llvm-diff: $target: ==========\033[0m\n"
if [ -f "$file_a" ] && [ -f "$file_b" ]; then
llvm-diff "$file_a" "$file_b" 2>&1 || true
else
echo "Warning: .ll file missing for $target"
echo "Checked: $file_a"
echo " and $file_b"
fi
echo ""
else
echo "Target '$target' exists in Commit A but not in Commit B."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment