[PATCH] D115530: [GlobalOpt][Evaluator] Rewrite global ctor evaluation (fixes PR51879)

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 10 08:48:13 PST 2021


nikic created this revision.
nikic added reviewers: aeubanks, jroelofs, efriedma.
Herald added subscribers: ormris, hiraditya.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Global ctor evaluation currently models memory as a map from `Constant *` to `Constant *`. For this to be correct, it is required that there is only a single `Constant *` referencing a given memory location. The Evaluator tries to ensure this by imposing certain limitations that could result in ambiguities (by limiting types, casts and GEP formats), but ultimately still fails, as can be seen in https://github.com/llvm/llvm-bugzilla-archive/issues/51879. The approach is fundamentally fragile and will get more so with opaque pointers.

My original thought was to instead store memory for each global as an offset => value representation. However, we also need to make sure that we can actually rematerialize the modified global initializer into a Constant in the end, which may not be possible if we allow arbitrary writes.

What this patch does instead is to represent globals as a `MutableValue`, which is either a `Constant *` or a `MutableAggregate *`. The mutable aggregate exists to allow efficient mutation of individual aggregate elements, as mutating an element on a `Constant` would require interning a new constant. When a write to the `Constant *` is made, it is converted into a `MutableAggregate *` as needed.

I believe this should make the evaluator more robust, compatible with opaque pointers, and a bit simpler as well.


https://reviews.llvm.org/D115530

Files:
  llvm/include/llvm/Transforms/Utils/Evaluator.h
  llvm/lib/Transforms/IPO/GlobalOpt.cpp
  llvm/lib/Transforms/Utils/Evaluator.cpp
  llvm/test/Transforms/GlobalOpt/pr51879.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115530.393507.patch
Type: text/x-patch
Size: 25545 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211210/d8602ac8/attachment.bin>


More information about the llvm-commits mailing list