From f406c4dafcf68d5477c16aca7b5c14b85cc23175 Mon Sep 17 00:00:00 2001 From: orca Date: Tue, 27 Oct 2026 17:18:32 +0100 Subject: [PATCH] fix(isa): fail closed, and count a back-edge, not any exec branch Both instruments could report a clean tree after inspecting nothing. res.sh and waterfall.sh skipped an object whose code object would not extract (`|| continue`), skipped a disassembly that failed, and then printed a header and a short table and exited 0. waterfall.sh additionally counted every s_cbranch_exec* after a movrel -- a forward branch is not the loop that re-runs the movrel -- and carried that flag across kernels, because flush() reset n and loops but not seen. Now: no .hip.o under BUILD, a code object that will not extract, a llvm-readelf/llvm-objdump that fails, or not one kernel inspected all exit 1. A HIP translation unit with no kernels carries no .hip_fatbin and is skipped, not failed -- one exists in this tree (r4d_ar_entry), which is why "every object must extract" was not an option. The parse is waterfall.awk, and a branch counts only when its target label was already defined above it, i.e. is a back-edge. Measured over every kernel this tree builds (761 movrels): 199 loops become 101, 19 rows change, and 12 of them are r4d_ar_twoshot_Nrank_exact reporting 7 where it has no back-edge at all: r4d_ar_twoshot_Nrank_exact 7 -> 0 (x12) r4d_hc_bf16 6 -> 1 xprefetch 4 -> 3 r4d_ple_gate_bf16 3 -> 0 r4d_ngram_ids 2 -> 1 spechub 9 -> 8, 2 -> 0 r4d_encoder_bf16 1 -> 0 --- libr4d/isa/res.sh | 31 +++++++++++++++++++++++++------ libr4d/isa/waterfall.awk | 28 ++++++++++++++++++++++++++++ libr4d/isa/waterfall.sh | 39 ++++++++++++++++++++++++++------------- 3 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 libr4d/isa/waterfall.awk diff --git a/libr4d/isa/res.sh b/libr4d/isa/res.sh index 6e9bf45d..f8474b43 100755 --- a/libr4d/isa/res.sh +++ b/libr4d/isa/res.sh @@ -8,16 +8,35 @@ # source does not show, and scratch without spills is a private array the compiler could not keep # in registers. WAVES is the other one: on gfx1201 a SIMD has 1536 VGPRs in wave32 allocated in # groups of 24, so 96 VGPRs is 16 waves, 97 is 12 and 256 is 5 -- and LDS can cut it further, which -# this column does NOT model. The parse is rows.awk, which selftest.sh checks at those boundaries. +# this column does NOT model. +# +# It fails rather than printing an empty table: no .hip.o under BUILD, a code object that will not +# extract, a note read that fails, or not one kernel in the whole tree all exit 1. An instrument +# that reports "nothing to see" and exits 0 reads like a clean tree. : "${ROCM:=/opt/rocm}" : "${BUILD:=build}" : "${ISA_OUT:=/tmp/r4disa}" -D="$ISA_OUT/res"; rm -rf "$D"; mkdir -p "$D" here=$(cd "$(dirname "$0")" && pwd) -for o in $(find "$BUILD" -name '*.hip.o'); do +D="$ISA_OUT/res"; rm -rf "$D"; mkdir -p "$D" +objs=$(find "$BUILD" -name '*.hip.o' 2>/dev/null | sort) +[ -n "$objs" ] || { + echo "res.sh: no .hip.o under $BUILD" >&2 + echo "res.sh: BUILD wants a configured build tree with device kernels (RAD_WITH_HIP=ON)" >&2 + exit 1 +} +for o in $objs; do n=$(basename "$o" .hip.o) - ISA_OUT="$D" "$here/co.sh" "$o" >/dev/null 2>&1 || continue - "$ROCM/llvm/bin/llvm-readelf" --notes "$D/$n.co" 2>/dev/null > "$D/$n.md" + # A HIP translation unit with no kernels carries no .hip_fatbin; that is not a broken object. + "$ROCM/llvm/bin/llvm-readelf" -S "$o" 2>/dev/null | grep -q '\.hip_fatbin' || continue + ISA_OUT="$D" "$here/co.sh" "$o" >/dev/null || { + echo "res.sh: no gfx1201 code object in $o" >&2; exit 1; } + "$ROCM/llvm/bin/llvm-readelf" --notes "$D/$n.co" > "$D/$n.md" || { + echo "res.sh: llvm-readelf failed on $D/$n.co" >&2; exit 1; } done printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' vgpr sgpr lds scratch spill waves wg kernel - awk -f "$here/rows.awk" "$D"/*.md | sort -k5,5nr -k4,4nr -k1,1nr +rows=$(awk -f "$here/rows.awk" "$D"/*.md | sort -k5,5nr -k4,4nr -k1,1nr) || exit 1 +[ -n "$rows" ] || { + echo "res.sh: no kernel metadata in $D (every object was host-only, or the ISA_ARCH is absent)" >&2 + exit 1 +} +printf '%s\n' "$rows" diff --git a/libr4d/isa/waterfall.awk b/libr4d/isa/waterfall.awk new file mode 100644 index 00000000..34cca42c --- /dev/null +++ b/libr4d/isa/waterfall.awk @@ -0,0 +1,28 @@ +# movrel count per kernel, plus how many of them sit in a real loop. +# +# A `movrel` is a divergent VGPR index the compiler had to serialize; the loop that re-runs it once +# per distinct index is a `s_cbranch_execnz` back-edge around it. Counting every exec branch after +# a movrel (as this did) also counts forward branches, which are not loops, and carried the flag +# from one kernel into the next, so a kernel with no movrel at all could report loops. +# +# A branch is a back-edge when its target label has already been defined above it: llvm-objdump +# emits the blocks in address order, so "seen already" is "lower address". Forward targets are +# skipped. +function flush( ) { + if (n > 0) printf "%-22s %6d %6d %s\n", tu, n, loops, k + n = 0; loops = 0; seen = 0 +} +/^[0-9a-f]+ <.*>:$/ { + if ($2 ~ /^:$/) lab[$2] = 1 + else { flush(); k = $2 } + next +} +/movrel/ { n++; seen = 1 } +/s_cbranch_exec(nz|z)/ { + # The operand is the target label (`L667`), or a bare address followed by the label in brackets + # when the disassembler was told to symbolize; the definition line is `0000 :`. + key = ($3 ~ /^:" + if (seen && (key in lab)) loops++ + seen = 0 +} +END { flush() } diff --git a/libr4d/isa/waterfall.sh b/libr4d/isa/waterfall.sh index 197d7991..ec8aed86 100755 --- a/libr4d/isa/waterfall.sh +++ b/libr4d/isa/waterfall.sh @@ -22,25 +22,38 @@ # back-edge around it, which the second column reports. And a waterfall in a cold kernel is worth # nothing: most of what this finds is far below the noise of a step. Check the kernel against a # trace before touching it. +# +# It fails rather than printing a short table: no .hip.o under BUILD, a code object that will not +# extract, a disassembly that fails, or not one kernel seen all exit 1. An instrument that reports +# "no waterfalls" because it looked at nothing reads like a clean tree. : "${ROCM:=/opt/rocm}" : "${BUILD:=build}" : "${ISA_OUT:=/tmp/r4disa}" here=$(cd "$(dirname "$0")" && pwd) mkdir -p "$ISA_OUT" +objs=$(find "$BUILD" -name '*.hip.o' 2>/dev/null | sort) +[ -n "$objs" ] || { + echo "waterfall.sh: no .hip.o under $BUILD" >&2 + echo "waterfall.sh: BUILD wants a configured build tree with device kernels (RAD_WITH_HIP=ON)" >&2 + exit 1 +} printf "%-22s %6s %6s %s\n" "translation unit" "movrel" "loops" "kernel" -for o in $(find "$BUILD" -name '*.hip.o' | sort); do +kernels=0 +for o in $objs; do tu=$(basename "$o" .hip.o) - "$here/co.sh" "$o" >/dev/null 2>&1 || continue + # A HIP translation unit with no kernels carries no .hip_fatbin; that is not a broken object. + "$ROCM/llvm/bin/llvm-readelf" -S "$o" 2>/dev/null | grep -q '\.hip_fatbin' || continue + "$here/co.sh" "$o" >/dev/null || { + echo "waterfall.sh: no gfx1201 code object in $o" >&2; exit 1; } "$ROCM/llvm/bin/llvm-objdump" -d --symbolize-operands --no-show-raw-insn \ - "$ISA_OUT/$tu.co" 2>/dev/null > "$ISA_OUT/$tu.scan.s" || continue - awk -v tu="$tu" ' - function flush( ) { - if (n > 0) printf "%-22s %6d %6d %s\n", tu, n, loops, k - n = 0; loops = 0 - } - /^[0-9a-f]+ <.*>:$/ { if ($2 !~ /^:$/) { flush(); k = $2 } } - /movrel/ { n++; seen = 1 } - /s_cbranch_exec(nz|z)/ { if (seen) { loops++; seen = 0 } } - END { flush() } - ' "$ISA_OUT/$tu.scan.s" + "$ISA_OUT/$tu.co" > "$ISA_OUT/$tu.scan.s" 2>/dev/null || { + echo "waterfall.sh: llvm-objdump failed on $ISA_OUT/$tu.co" >&2; exit 1; } + awk -f "$here/waterfall.awk" -v tu="$tu" "$ISA_OUT/$tu.scan.s" || exit 1 + # Empty output is a real answer here -- no kernel has a movrel -- so the check is that kernels + # were disassembled at all. + kernels=$((kernels + $(grep -cE '^[0-9a-f]+ <[^L]' "$ISA_OUT/$tu.scan.s" || true))) done +[ "$kernels" -gt 0 ] || { + echo "waterfall.sh: no kernel disassembled (every object was host-only, or ISA_ARCH is absent)" >&2 + exit 1 +}