Compiler matmul optimization prevents constant folding?

Yes, although I would avoid getting too dependent on constant-folding since this is the worst way to perform this calculation when constant-folding is not possible. I’d recommend instead calculating I + M + M*M as a global constant and apply that directly.