Fix the fix-round regression: the base matcher stripped any zext/trunc, so `(unsigned short)(unsigned)p + 24` lost its 16-bit truncation and became `p + 24` at both -O0 and -O2. stripWideningCasts now strips only zext/sext and truncs to at least the 32-bit pointer width; a trunc below i32 stops the match, leaving the shape unbarriered with its `and 0xffff`/`and 0xff` intact (the base commit's value). Allow widening sext on the base as well, closing the signed 64-bit pointer-int shape: `(char *)((long long)(int)p + 24)` now becomes `@__tms_bitptr(i32 24, ptr p)` instead of `getelementptr i8, p, 192`. Use the fixed 32-bit TMS34010 pointer width rather than an absent module DataLayout's 64-bit default, which misjudged a trunc to i32. Add @narrow16/@narrow8/@narrow16and/@late64s to bitptr.ll (narrow shapes must not emit a call), negative tests for the mismatched declaration, body and non-call use fatal paths, a late64s M0 check with the late positive scoped to @late itself, and narrow16/narrow8/late64s runtime markers plus IR checks in the M7 gate. Update the pointer-o2.sh header.
147 lines
6.2 KiB
Bash
Executable File
147 lines
6.2 KiB
Bash
Executable File
#!/bin/sh
|
|
# M7.1 `-O2` bit-pointer gate.
|
|
#
|
|
# scripts/tests/m7/pointer-o2.sh
|
|
#
|
|
# Compiles scripts/tests/m7/pointer-o2.c at the default -O2 through
|
|
# scripts/tms-clang (clang -> --bitptr -> opt -> --scale -> --lower-bitptr ->
|
|
# instcombine), assembles it with llc, concatenates the M7 entry, then links and
|
|
# loads the COFF with the 1987 GSPA/GSPLNK tools, the dwt loader and MAME (the
|
|
# scripts/m4/run.sh harness).
|
|
#
|
|
# The C driver exercises the shapes the M6 pipeline miscompiled or the M7.1
|
|
# barrier must not over-apply to:
|
|
# - `p += n; *p = ...` with a runtime n;
|
|
# - the natural overlapping backward copy `d += n; s += n;
|
|
# while (n--) *--d = *--s;` (the shim's memmove body before the workaround
|
|
# was removed);
|
|
# - 64-bit pointer-int casts, `(unsigned long long)(unsigned)p + 24` and the
|
|
# signed `(long long)(int)p + 24`, whose low word is 24 (never 192);
|
|
# - narrowing base casts `(unsigned short)/(unsigned char)p + 24`, which must
|
|
# keep the mask (`(p & 0xffff)/(p & 0xff) + 24`), not become `p + 24`.
|
|
#
|
|
# pointer-check.lua checks the marker words and the exact copy bytes. The
|
|
# script also checks that the lowered copy has exactly one `shl i32 ..., 3` and
|
|
# one `SLL 3` (no second `shl`/`SLL 6`), that no @__tms_bitptr call remains,
|
|
# that narrow16/narrow8 keep their `and`, and that late64/late64s carry the
|
|
# 24-bit addend rather than 192.
|
|
#
|
|
# Overridable: LLVM_BIN TMS_CLANG TMS_OPT TMS_BITADDR TMS_LLC MAME DOSBOX
|
|
# DOSBOX_CONF SDK_IMG M7_POINTER_WORK M7_POINTER_COFF
|
|
# LOADER_AT LOADER_END MAME_TIMEOUT DOSBOX_TIMEOUT
|
|
set -eu
|
|
|
|
ROOT=$(cd "$(dirname "$0")/../../.." && pwd)
|
|
DIR=$ROOT/scripts/tests/m7
|
|
WORK=${M7_POINTER_WORK:-$ROOT/build/m7-pointer-o2}
|
|
|
|
fail() {
|
|
echo "m7 pointer: FAIL: $*" >&2
|
|
exit 1
|
|
}
|
|
|
|
rm -rf "$WORK"
|
|
mkdir -p "$WORK"
|
|
|
|
echo "== compile at -O2 and run through the M4 assembler/linker/MAME harness"
|
|
M4_WORK="$WORK" \
|
|
M4_SDK="$WORK/sdk" \
|
|
M4_COFF="${M7_POINTER_COFF:-$WORK/m7-pointer.coff}" \
|
|
M4_SOURCES="$DIR/pointer-o2.c" \
|
|
M4_ENTRY="$DIR/pointer-entry.asm" \
|
|
M4_CHECK_LUA="$DIR/pointer-check.lua" \
|
|
M4_CLANG_OPT=-O2 \
|
|
M4_LLC_OPT=-O0 \
|
|
"$ROOT/scripts/m4/run.sh" > "$WORK/run.log" 2>&1 \
|
|
|| { tail -60 "$WORK/run.log" >&2; fail "compile/link/MAME harness failed"; }
|
|
|
|
grep -q '^M4 MAME: PASS' "$WORK/mame.log" \
|
|
|| { tail -40 "$WORK/run.log" >&2; fail "M4 harness link/MAME check failed"; }
|
|
|
|
grep -q '^M7 POINTER MAME: PASS' "$WORK/mame.log" \
|
|
|| { grep -E '^M7 pointer|^M4 MAME' "$WORK/mame.log" >&2 || true; \
|
|
tail -40 "$WORK/run.log" >&2; \
|
|
fail "pointer markers/bytes did not match in MAME"; }
|
|
grep -q '^M4 MAME: PASS' "$WORK/mame.log" \
|
|
|| fail "the M4 harness marker did not pass"
|
|
|
|
echo "== MAME evidence"
|
|
grep -E '^M7 pointer' "$WORK/mame.log" | sed 's/^/ /'
|
|
|
|
IR=$WORK/asm/pointer-o2.ll
|
|
ASM=$WORK/asm/pointer-o2.s
|
|
[ -f "$IR" ] || fail "missing IR: $IR"
|
|
[ -f "$ASM" ] || fail "missing asm: $ASM"
|
|
|
|
echo "== one-scale IR evidence"
|
|
if grep -q '__tms_bitptr' "$IR"; then
|
|
fail "$IR still contains @__tms_bitptr (the barrier was not lowered)"
|
|
fi
|
|
# Isolate copy_back: the red IR has two `shl i32 ..., 3` instructions
|
|
# (`shl %n, 3` and `shl %bit.off, 3`); the fixed IR has exactly one.
|
|
awk '
|
|
/^define .*@copy_back\(/ { in_fn = 1 }
|
|
in_fn { print }
|
|
in_fn && /^}/ { exit }
|
|
' "$IR" > "$WORK/copy_back.ll"
|
|
grep -q '@copy_back' "$WORK/copy_back.ll" \
|
|
|| fail "IR has no copy_back definition"
|
|
SHLS=$(grep -c 'shl i32' "$WORK/copy_back.ll" || true)
|
|
[ "$SHLS" -eq 1 ] || { grep -n 'shl i32' "$WORK/copy_back.ll" >&2 || true; \
|
|
fail "copy_back has $SHLS 'shl i32', want exactly one"; }
|
|
grep -Eq 'shl i32 .*, 3$' "$WORK/copy_back.ll" \
|
|
|| { cat "$WORK/copy_back.ll" >&2; \
|
|
fail "copy_back's single shl is not by 3"; }
|
|
if grep -Eq 'shl i32 .*, (6|[1-9][0-9]+)$' "$WORK/copy_back.ll"; then
|
|
cat "$WORK/copy_back.ll" >&2
|
|
fail "copy_back bit offset is still multi-scaled in $IR"
|
|
fi
|
|
grep -n 'shl i32' "$WORK/copy_back.ll" | sed 's/^/ /'
|
|
|
|
# The barrier must not over-apply to a narrowing base cast, and the i64 forms
|
|
# must carry the 24-bit low word, not 192.
|
|
echo "== barrier-shape IR evidence"
|
|
grep -A8 'define.*@narrow16(' "$IR" | grep -Eq 'and i32 .*, 65535' \
|
|
|| { grep -A8 'define.*@narrow16(' "$IR" >&2 || true; \
|
|
fail "narrow16 lost its (p & 0xffff) mask"; }
|
|
grep -A8 'define.*@narrow8(' "$IR" | grep -Eq 'and i32 .*, 255' \
|
|
|| { grep -A8 'define.*@narrow8(' "$IR" >&2 || true; \
|
|
fail "narrow8 lost its (p & 0xff) mask"; }
|
|
for fn in late64 late64s; do
|
|
grep -A8 "define.*@$fn(" "$IR" \
|
|
| grep -Eq 'getelementptr i8, ptr %p, i32 24|add i32 %bit[.a-z0-9]*, 24' \
|
|
|| { grep -A8 "define.*@$fn(" "$IR" >&2 || true; \
|
|
fail "$fn does not carry the 24-bit addend"; }
|
|
if grep -A8 "define.*@$fn(" "$IR" | grep -Eq 'i32 192'; then
|
|
grep -A8 "define.*@$fn(" "$IR" >&2
|
|
fail "$fn over-scaled to 192"
|
|
fi
|
|
done
|
|
grep -A8 'define.*@narrow16(' "$IR" | sed 's/^/ /'
|
|
grep -A8 'define.*@narrow8(' "$IR" | sed 's/^/ /'
|
|
|
|
# Isolate the (noinline) copy_back function in the assembly and require a
|
|
# single SLL 3 and no SLL 6 in it.
|
|
awk '
|
|
/^_copy_back[^:]*:/ { in_fn = 1 }
|
|
in_fn && /^_[A-Za-z0-9_.$]*:/ && !/^_copy_back/ { in_fn = 0 }
|
|
in_fn { print }
|
|
' "$ASM" > "$WORK/copy_back.s"
|
|
grep -q '^_copy_back' "$WORK/copy_back.s" \
|
|
|| fail "assembly has no separate copy_back function"
|
|
# Tight patterns: `SLL 3` must not match `SLL 31`, and any other SLL in
|
|
# copy_back is a second scale.
|
|
SLL3=$(grep -cE '^[[:space:]]*SLL[[:space:]]+3([^0-9]|$)' "$WORK/copy_back.s" || true)
|
|
SLL6=$(grep -cE '^[[:space:]]*SLL[[:space:]]+6([^0-9]|$)' "$WORK/copy_back.s" || true)
|
|
SLLANY=$(grep -cE '^[[:space:]]*SLL[[:space:]]' "$WORK/copy_back.s" || true)
|
|
[ "$SLL3" -eq 1 ] || { cat "$WORK/copy_back.s" >&2; \
|
|
fail "copy_back has $SLL3 'SLL 3', want exactly one"; }
|
|
[ "$SLL6" -eq 0 ] || { cat "$WORK/copy_back.s" >&2; \
|
|
fail "copy_back still has an 'SLL 6' double scale"; }
|
|
[ "$SLLANY" -eq 1 ] || { cat "$WORK/copy_back.s" >&2; \
|
|
fail "copy_back has $SLLANY SLL instructions, want one"; }
|
|
echo "== one-scale asm evidence"
|
|
grep -E '^[[:space:]]*SLL' "$WORK/copy_back.s" | sed 's/^/ /'
|
|
|
|
echo "M7 POINTER: PASS"
|