3 very important higher level structures that comes up that don't exist in TB but instead can be inferred are: loops, tensors and indices some numbers just refer to slots in some buffer, it means that they're helpful in identifying the dimensions of a tensor or buffer
#include <stddef.h>
typedef size_t index_t;
#define FOR_N(i, n) for (index_t i = 0, n_ = (n); i < n_; i++)
void foo(float* restrict c, float* restrict a, float* restrict b) {
// let's walk different refinements of the types
// TB pointers are opaque
// c = [? * ?] #restrict #aligned(4)
//
// but since all stores were floats
// c = [? * float] #restrict #aligned(4)
//
// we also know that both i & j span 0..3 so
// c = [4 * 4 * float] #restrict #aligned(4)
//
FOR_N(i, 4) {
FOR_N(j, 4) {
float x = 0.0f;
FOR_N(k, 4) x += a[i + k*4] * b[k + j*4];
c[i + j*4] = x;
}
}
}the idea is that a refined pointer is no longer referring to a buffer, it's a tensor in a sense, from there we can reorder things [4 * 4 * float] is something with indices we could pay attention to if we wanted, we could flip the indices c[i + j4] => c[j + i4] and represent that as a new tensor type ideally you'd be able to make type casts between the old version and the new one but also ideally you could avoid ever needing to but much like we have optimizations around reducing casts, if we see a type we'd rather be we could force things to match that type and then just organize the right casts which eventually propagate Dis A. Robbery — Yesterday at 8:21 PM a more common reordering might be cache blocking regardless if we can represent the refinements like this then we can avoid losing information since it would be represented in complex annotations like if i do a cast like this (unsigned int) (int) x i don't lose any info, it's just represented differently and thus (unsigned int) x is a valid optimization of that [4i * 4j * float] => [4j * 4i * float] => [4i * 4j * float] i can also reverse optimizations based on this which is something that a lot of aggressive optimizations simply can't do much like there exists lowering im calling this shit highering