Archived
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
60 lines
3.1 KiB
Bash
Executable File
60 lines
3.1 KiB
Bash
Executable File
#!/bin/sh
|
|
# Every kernel in libr4d that indexes a VGPR array with an index the compiler could not prove
|
|
# uniform -- and what that costs.
|
|
#
|
|
# BUILD=/path/to/build ./waterfall.sh
|
|
#
|
|
# WHY THIS IS ITS OWN INSTRUMENT. A divergent VGPR index is not an addressing mode on gfx12. The
|
|
# compiler emits `v_movrels_b32` inside an exec-masked loop that re-runs once per distinct index in
|
|
# the wave -- a waterfall -- and nothing about the source says so. The shape that produces one is
|
|
#
|
|
# for (e = lane; e < n_expert; e += kWave) v[n_own++] = ...;
|
|
# ... later ... for (i = 0; i < n_own; ++i) ... v[i] ...
|
|
#
|
|
# where `n_own` is derived from `lane`, so every `v[i]` after it indexes divergently and becomes a
|
|
# waterfall costing tens of thousands of cycles over a wave. The fix is to unroll to the
|
|
# compile-time bound so the indices are constants. res.sh cannot see any of this -- such a kernel
|
|
# has no scratch, a modest VGPR count and an unremarkable instruction count. Only the disassembly
|
|
# says it.
|
|
#
|
|
# The count is STATIC, so read it as "look here", not as a cost. A `v_movrels` with a genuinely
|
|
# uniform index is cheap and appears here too; what makes it a waterfall is the `s_cbranch_execnz`
|
|
# 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"
|
|
kernels=0
|
|
for o in $objs; do
|
|
tu=$(basename "$o" .hip.o)
|
|
# 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" > "$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
|
|
}
|