Files
arti b313de480b m7.1: keep narrowing base casts out of the barrier
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.
2026-11-14 13:57:48 +00:00

157 lines
4.7 KiB
C

/* M7.1 `-O2` bit-pointer gate driver.
*
* The TMS34010 pointer model uses bit addresses: `p += n` must scale n by 8
* exactly once. The M6 pipeline scaled before and after opt; InstCombine
* canonicalized an already-bit `inttoptr(add(ptrtoint p, n*8))` back into a
* byte GEP when its consumers were integer/icmp-only, and the post-opt
* `--scale` multiplied that n*8 index by 8 again.
*
* The shapes below are the ones that exposed the bug:
* - `p += n; *p = ...` with a volatile n, checking the byte and that the
* double-scaled byte (n*8) was not touched;
* - a runtime pointer difference;
* - the natural overlapping backward-copy shape from the conformance shim's
* memmove (`d += n; s += n; while (n--) *--d = *--s;`);
* - the 64-bit pointer-int shape
* (`(unsigned long long)(unsigned)p + 24` and the signed `long long`
* variant), whose low 32-bit word is the 24-bit addend and must not
* become 192;
* - narrowing pointer-int casts (`(unsigned short)/(unsigned char)` on the
* base), which must keep the `and 0xffff`/`and 0xff` and must NOT be
* barriered into `p + 24`.
*
* Markers are at 0x018A8000 + n*0x20; the copy result bytes are at
* 0x018A9000 + n*8, both fixed writable RAM addresses (the M4/M6 pattern).
* pointer-check.lua compares them with the expected values.
*/
typedef unsigned int u32;
#define MARK(n) (*(volatile u32 *)(0x018A8000u + (u32)(n) * 0x20u))
#define OUTB(n) (*(volatile unsigned char *)(0x018A9000u + (u32)(n) * 8u))
#define BUF 128
char a[BUF];
char b[BUF];
/* The exact natural shape the shim's memmove now uses. The optimizer turns
* `d += n` into a bit pointer; the barrier must keep it from being scaled a
* second time. */
static void __attribute__((noinline)) copy_back(char *dst, const char *src, int n)
{
char *d = dst;
const char *s = src;
d += n;
s += n;
while (n--)
*--d = *--s;
}
/* The 64-bit pointer-int shape: clang widens the ptrtoint to i64, adds the bit
* count, and inttoptrs the result. The pointer is 32-bit, so the low word of
* the addend is the bit offset (24 bits = 3 bytes), not 192 bits. */
static int __attribute__((noinline)) late64(char *p, char *q)
{
return (char *)((unsigned long long)(unsigned int)p + 24) == q;
}
/* The signed-widening form of the same i64 shape. */
static int __attribute__((noinline)) late64s(char *p, char *q)
{
return (char *)((long long)(int)p + 24) == q;
}
/* A narrowing cast on the base changes the value: these must keep the
* `(p & 0xffff)/(p & 0xff)` mask, not be barriered into `p + 24`. */
static unsigned __attribute__((noinline)) narrow16(char *p)
{
return (unsigned)(char *)((unsigned short)(unsigned int)p + 24);
}
static unsigned __attribute__((noinline)) narrow8(char *p)
{
return (unsigned)(char *)((unsigned char)(unsigned int)p + 24);
}
void pointer_run(void)
{
volatile int move_n = 8;
volatile int step_n = 3;
volatile int diff_n = 5;
int n, i;
char *p;
for (i = 0; i < BUF; i++) {
a[i] = 0;
b[i] = 0;
}
for (i = 0; i < 8; i++)
a[i] = (char)('a' + i);
/* Overlapping backward copy: a+1 <- a, 8 bytes. The 8-byte copy starts
* "abcdefgh", so a[1..8] becomes "abcdefgh" and a[0] keeps 'a':
* "aabcdefg" as a string, with a[8] = 'h'. A double scale would take
* the source from a+64 and leave the destination zeroed. */
n = move_n;
copy_back(a + 1, a, n);
MARK(0) = (a[0] == 'a') && (a[1] == 'a') && (a[2] == 'b') &&
(a[3] == 'c') && (a[4] == 'd') && (a[5] == 'e') &&
(a[6] == 'f') && (a[7] == 'g');
MARK(1) = (a[8] == 'h') && (a[9] == 0) && (a[65] == 0);
for (i = 0; i < 9; i++)
OUTB(i) = (unsigned char)a[i];
/* `p += n; *p = ...` with a runtime n. */
n = step_n;
p = b;
p += n;
*p = 'Q';
MARK(2) = (u32)(unsigned char)b[3];
MARK(3) = (u32)(unsigned char)b[0];
MARK(4) = (u32)(unsigned char)b[24];
MARK(5) = (u32)(unsigned char)b[127];
/* The pointer difference is in bytes; the integer difference is bits. */
{
char *q = b;
int m = diff_n;
q += m;
MARK(6) = (u32)(q - b);
MARK(7) = (u32)q - (u32)b;
}
/* A few exact byte checks in the forward direction as well. */
MARK(8) = ((u32)(unsigned char)b[3] == 'Q') &&
((u32)(unsigned char)b[4] == 0) &&
((u32)(unsigned char)b[2] == 0);
/* The i64 pointer-int cast: c + 24 bits is c + 3 bytes. A missed
* barrier over-scales to c + 24 bytes and this is false. */
{
char c[8];
volatile int off = 3;
c[0] = 0;
c[1] = 0;
c[2] = 0;
c[3] = 0;
MARK(9) = (u32)late64(c, c + off);
MARK(12) = (u32)late64s(c, c + off);
}
/* p is a runtime pointer; the mask must stay in the address. */
{
char *volatile vp = (char *)0x018A9000u;
MARK(10) = (u32)(narrow16(vp) ==
((0x018A9000u & 0xFFFFu) + 24u));
MARK(11) = (u32)(narrow8(vp) ==
((0x018A9000u & 0xFFu) + 24u));
}
}