[PATCH] D117503: [GlobalOpt] Generalize malloc-to-global for any allocation function

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 17 12:58:57 PST 2022


nikic accepted this revision.
nikic added a comment.
This revision is now accepted and ready to land.

LGTM

In D117503#3249054 <https://reviews.llvm.org/D117503#3249054>, @reames wrote:

> A couple of side notes:
>
> With both the new and old code, we might be loosing some ability to prune loads from uninitialized state of the allocation.  We could consider using lifetime markers at the point of the original allocation to preserve this.  I'm a little leery of introducing them though as their semantics on globals are a bit vague.  Thankfully, I think this is a minor opt quality issue at worst, and I don't have any motivating examples to justify exploring this further.

Yeah, definitely don't want lifetime intrinsics on globals!

> It would be interesting to explore whether we could prove the memory was allocated once.  This might actually be easier for a nullable global than a non-null one.  (e.g. for the idiomatic pattern if (!g) g = malloc())  I don't have a strong motivating example for this, but it seems like we're leaving something on the floor here.  If we do the init once thing, we also need to be careful about data section size and profitability.  What's profitable for undef and zero (e.g. effecting bss size), and what's profitably for other init constants (e.g. effecting .data sizes) might not be the same - i.e. we have to account for load time initialization costs.

This seems like a more general optimization -- what we currently do is optimize stored-once globals (one store, multiple reads) and arbitrary initialization in global ctors. We could try to determine that some initialization code can only be reached once (e.g. by detecting a "global initialization flag" pattern) and then treat that like global ctor initialization. Don't think this is worth bothering with at this point though.



================
Comment at: llvm/test/Transforms/GlobalOpt/calloc-promote.ll:3
+; RUN: opt -passes=globalopt -S < %s | FileCheck %s
+; RUN: opt -passes=globalopt -S < %s | FileCheck %s
+
----------------
Duplicate run line


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117503/new/

https://reviews.llvm.org/D117503



More information about the llvm-commits mailing list