Meta-programming in general have been used for this kind of thing for a long time. I often see macros as a way to "add your own user defined compiler stage." There are many examples of lisp macros that try to optimize code.
of C++ templates being used to generate optimized shader code depending on the compile target.
To be a shill for my favorite hobby lang for a bit: Nim is interesting on this topic. Nim has lisp style macros, and liberally uses high level rewrite passes to generate optimized code. For example, Nim has the concept of an
inline iterator, which gives you convenient python style iterator syntax, but rewrites the code into a more optimal for loop.
Async / Await in Nim is entirely built as a macro library. But, as interesting as those examples are, any language with a macro system can accomplish something similar in theory.
What is interesting about Nim is that it is purely a "compiler front end" to use compiler terminology. Nim does not produce assembly language. It produces C code that can then be fed into any C compiler. This means the output is fairly high level, and while it's not "pretty" or "elegant", it's inspectable! You can read it and understand what is going on.