savi-lang / savi

A fast language for programmers who are passionate about their craft.
BSD 3-Clause "New" or "Revised" License
156 stars 12 forks source link

Update to LLVM 14.0.3 and re-enable full optimizations. #321

Closed jemc closed 2 years ago

jemc commented 2 years ago

Update to LLVM 14.0.3.

In LLVM 13.0.0 we had observed a bug in which, when using the new LLVM PassBuilder, some functions were being generated without return instructions, causing the instruction pointer to just blunder off the end of one function definition into the next one in the executable binary's memory layout, likely violating stack/argument assumptions of that function, ultimately leading to some form of memory corruption.

This is why we had disabled use of those optimizations temporarily in an earlier PR (#203), though it was only diagnosed in detail this morning.

We update to the latest available LLVM release (14.0.3) to avoid this issue and also to keep up with the times.

Note that in this update to 14.0.3 an issue was discovered in Pony with the use of NoAlias attributes on allocation functions for those functions that allocate an object with a finalizer. These attributes were technically incorrect, and a new optimization present in 14.0.3 now can optimize away code that shouldn't be removed if it trusts the erroneous use of that attribute. See https://github.com/ponylang/ponyc/pull/4055

This bug had not been observed directly in Savi because we do not yet support allocations with finalizers. But the attributes are updated here nonetheless, as it's easy to fix this correctness issue now while it's at top of mind.


Go back to using the new LLVM PassBuilder for optimization.

This reverts PR #203, in which we temporarily disabled these optimizations due to an LLVM bug. We no longer observe this bug after upgrading LLVM from 13.0.0 to 14.0.3 (see above).

We also move the emission of LLVM IR to ensure it happens after optimizations (in the new place where they are, after linking to the runtime bitcode and thus allowing link time optimization with it).