(Disclaimer: I donāt know anything about MergeFunctions; Iām just assuming functionality from the name)
Ā
How does MergeFunctions compare to performing identical code folding (ICF) in the linker?
On Jan 31, 2019, at 12:24 PM, Shoaib Meenai <sme...@fb.com> wrote:(Disclaimer: I donāt know anything about MergeFunctions; Iām just assuming functionality from the name)ĀHow does MergeFunctions compare to performing identical code folding (ICF) in the linker?
The linker has access to a narrower set of informations, so it can
only merge byte-by-byte identical functions, while, in theory, you can
imagine that mergefunc could discover equivalences (through VN), and
understand that, e.g.
```
func pat(i32 %blah) {
%0 = sub %blah, 1
ret %0
}
```
and
```
func tinky(i32 %blah) {
%0 = add %blah, 0
ret %0
}
```
are equivalent. Some things to keep in mind:
1) My example is relatively silly, but some slightly more elaborate
patterns might show up in real code
2) I think llvm does enough canonicalization that we should be able to
catch these anyways
3) I don't think the pass as-is in tree does any equivalence finding,
but that's a potential advantage of running this analysis as IR pass.
Thanks,
--
Davide
_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
On Jan 31, 2019, at 4:40 PM, Aditya K via llvm-dev <llvm...@lists.llvm.org> wrote:Hi Nikita,Glad to hear that Rust code can benefit a lot from this.Ā ĀI have put patches to enable merge-similar functions with thinLTO.ĀThis is more powerful than existing merge-functions pass and all we need to do is port these patches to trunk llvm. I'd be happy to help with this effort.
-AdityaFrom:Ā Nikita Popov <nikit...@gmail.com>
Sent:Ā Thursday, January 31, 2019 3:46 PM
To:Ā Vedant Kumar
Cc:Ā llvm-dev; Reid Kleckner; Aditya K; white...@whitequark.org; Teresa Johnson; Duncan P. N. Exon Smith; Jessica Paquette
Subject:Ā Re: Status of the function merging pass?ĀOn Thu, Jan 31, 2019 at 8:52 PM Vedant Kumar <v...@apple.com> wrote:Hi,I'm interested in finding ways to reduce code size. LLVM's MergeFunctions pass seems like a promising option, and I'm curious about its status in tree.Enabling MergeFunctions gives a 1% code size reduction across the entire iOS shared cache (a collection of a few hundred system-critical DSO's). The numbers are even more compelling for Swift code. In fact, the swift compiler enables MergeFunctions by default when optimizing, along with an even more aggressive merging pass which handles equivalence-modulo-constant-uses (https://github.com/apple/swift/blob/master/lib/LLVMPasses/LLVMMergeFunctions.cpp).Is anyone actively working on enabling MergeFunctions in LLVM's default pipelines? Is there a roadmap for doing so?ISTM that preventing miscompiles when merging functions is a serious, unsolved problem. I.e., it's hard for the MergeFunctions pass to be *really sure* that two functions are a) really identical and b) safe to merge.Is there a systematic solution at the IR-level, given that the semantics of IR are subject to change? Is extensive testing the only solution? Or is this intractable, and the only safe approach is to perform merging post-regalloc (or, at some late point when equivalence is easier to determine)?In Rust we've been running with MergeFunctions enabled by default for a while now, and have recently also enabled the use of aliases instead of thunks. Apart from some initial bugs we didn't encounter any significant issues (one minor issue with NVPTX not supporting aliases and having CC restrictions).As Rust tends to be quite heavy on monomorphization, MergeFuncs can give significant binary size reductions. I don't have any comprehensive numbers, but from checking this on a pet project just now, it reduces final artifact size by 13% and I've seen some similar numbers in the ~10% range quoted before.So, at least for Rust's use case this pass seems to be both quite robust and useful :)Regards,Nikita
Adding back llvmdev; sorry about that.Ā Recently had to switch mail clients, and Outlookās reply-all button is broken.
Ā
-Eli
Ā
From: Eli Friedman
Sent: Thursday, January 31, 2019 5:35 PM
To: 'JF Bastien' <jfba...@apple.com>; Aditya K <hira...@msn.com>; Vedant Kumar <v...@apple.com>
Subject: RE: [EXT] Re: [llvm-dev] Status of the function merging pass?
Ā
Specifically on the question of making sure MergeFuncs doesnāt regress, Iām not sure what you think we can do.
Ā
We have certain extension points which allow changes to IR in general without individually verifying each pass: metadata, and function attributes.Ā Merging can just discard metadata, and refuse to merge functions with mismatched attributes.Ā Similarly, we can add intrinsics which donāt have special semantics (essentially, intrinsics which can be treated as equivalent to function calls); we can also generally ignore those.
Ā
If anything else about the IR changes, itās necessary to individually verify each pass to make sure they donāt make any invalid assumptions.Ā Weāll inevitably make mistakes in that verification because our transformation passes arenāt proof-verified.Ā There isnāt really anything about that which is specific to mergefuncs in particular.
Ā
There are maybe a few changes we could make that would make MergeFuncs more resistant to certain classes of IR changes.Ā For example, we could require that all instructions store all information which isnāt an operand or metadata in a way that would allow MergeFuncs to retrieve it as an opaque blob.Ā Or MergeFuncs could bail out if it sees an instruction with an unknown opcode. But itās impossible to write an pass that will never need to be updated.
Ā
-Eli
On Jan 31, 2019, at 5:37 PM, Eli Friedman <efri...@quicinc.com> wrote:Adding back llvmdev; sorry about that.Ā Recently had to switch mail clients, and Outlookās reply-all button is broken.Ā-EliĀFrom:Ā Eli FriedmanĀ
Sent:Ā Thursday, January 31, 2019 5:35 PM
To:Ā 'JF Bastien' <jfba...@apple.com>; Aditya K <hira...@msn.com>; Vedant Kumar <v...@apple.com>
Subject:Ā RE: [EXT] Re: [llvm-dev] Status of the function merging pass?ĀSpecifically on the question of making sure MergeFuncs doesnāt regress, Iām not sure what you think we can do.
We have certain extension points which allow changes to IR in general without individually verifying each pass: metadata, and function attributes.Ā Merging can just discard metadata, and refuse to merge functions with mismatched attributes.Ā Similarly, we can add intrinsics which donāt have special semantics (essentially, intrinsics which can be treated as equivalent to function calls); we can also generally ignore those.
If anything else about the IR changes, itās necessary to individually verify each pass to make sure they donāt make any invalid assumptions.Ā Weāll inevitably make mistakes in that verification because our transformation passes arenāt proof-verified.Ā There isnāt really anything about that which is specific to mergefuncs in particular.
There are maybe a few changes we could make that would make MergeFuncs more resistant to certain classes of IR changes.Ā For example, we could require that all instructions store all information which isnāt an operand or metadata in a way that would allow MergeFuncs to retrieve it as an opaque blob.Ā Or MergeFuncs could bail out if it sees an instruction with an unknown opcode. But itās impossible to write an pass that will never need to be updated.
On Jan 31, 2019, at 4:40 PM, Aditya K <hira...@msn.com> wrote:
Hi Nikita,Glad to hear that Rust code can benefit a lot from this.Ā ĀI have put patches to enable merge-similar functions with thinLTO.
This is more powerful than existing merge-functions pass and all we need to do is port these patches to trunk llvm. I'd be happy to help with this effort.
On Jan 31, 2019, at 4:54 PM, JF Bastien <jfba...@apple.com> wrote:On Jan 31, 2019, at 4:40 PM, Aditya K via llvm-dev <llvm...@lists.llvm.org> wrote:Hi Nikita,Glad to hear that Rust code can benefit a lot from this.Ā ĀI have put patches to enable merge-similar functions with thinLTO.ĀThis is more powerful than existing merge-functions pass and all we need to do is port these patches to trunk llvm. I'd be happy to help with this effort.I still donāt understand why we should ditch mergefuncs instead of incrementally improving it. Iād like to understand whatās actually changing incrementally, and first fix the fundamental flaw mergefuncs has (as I discuss below).To answer Vedantās question: I think the fundamental problem with mergefuncs is that it needs to understand IR perfectly, for equality comparison, hashing, and āfuzzyā matching. Any solution thatās on-by-default should address this issue: when we change IR we cannot allow mergefuncs to suddenly be wrong in a subtle way. For example, when we added cmpxchg āfailureā order, mergefuncs needed to know about it, otherwise it could merge functions which differed only in failure ordering and suddenly generate code that was *wrong* in an incredibly hard to diagnose manner.
Once thatās addressed, I think mergefuncs can be improved in a few ways.First it can be run early to remove exact duplicates. This will speed up build times. I had an intern, Jason, work on mergefuncs a few years ago and he measured speedups when compiling Chrome just though an early run.
Then mergefuncs should be improved to do fuzzy matching, where it determines that functions are similar enough that they can be variants of each other with an extra argument passed in to specialize each āflavorā. Jason had posted a patch for this back then as well, and it yielded some gains on Chromeās binary size. He hadnāt explored the full breadth of specializations (do you just find differences in constants, or branch around entire code blocks, etc). Thereās extra science to perform around different optimization levels.The fuzzy matching should only be run later, and some science should be put in determining how it interacts with inlining.Vedant, Iām happy to chat in person next time weāre in the same building :-)
On Jan 31, 2019, at 4:40 PM, Aditya K <hira...@msn.com> wrote:Hi Nikita,Glad to hear that Rust code can benefit a lot from this.Ā ĀI have put patches to enable merge-similar functions with thinLTO.