I am not convinced that this is not possible, just because a project from the early 2000s failed, and Knuth said it was a bad idea.
I am not talking about a general optimizer of poorly written arbitrary programs. Rather an optimizer for parts of the code written with a certain paradigm. Just like BigQuery does with SQL.
(Thank you for sticking with the thread)