[swift-dev] Reducing array abstraction

Chris Lattner clattner at nondot.org
Sun Oct 8 17:14:45 CDT 2017


> On Oct 8, 2017, at 11:57 AM, Michael Gottesman via swift-dev <swift-dev at swift.org> wrote:
> 
> 
>> On Oct 6, 2017, at 11:06 PM, Chris Lattner via swift-dev <swift-dev at swift.org> wrote:
>> 
>> This question is somewhere between swift-dev and swift-users, not sure where best to post this.  
>> 
>> I’m working on a project that wants to get very low-abstraction penalty array operations, particularly with varargs.  Given the currently language limitations (no fixed size arrays), the best I’ve been able to come up with is something like this, where “lowlevelAPI” contains the heavy lifting (and is assumed to be opaque), and the “safeAPI” wrappers exist merely to provide a convenient safe wrapper for clients:
>> 
>> <array_abstraction.swift>
>> 
>> Given whole module optimization of the program, we should in principle, be able to optimize this down to the equivalent of an LLVM array alloca in the clients, a few stores to it, then passing the pointers to the LLVM alloca into lowlevelAPI.  However, Swift is not doing this, not even with:
>> 
>> $ swiftc array_abstraction.swift -emit-sil -o - -O 
>> 
>> In this trivial case (with constant initializers) it does do the “array outlining” optimization,
> 
> What do you mean by the array outlining optimization specifically?

I mean the "Outline global variable” thing that IPO/GlobalOpt.cpp does.  I have no idea why it is called that.

> We definitely already have a heap->stack for classes in the guise of the StackPromotion optimization is that what you are talking about with the "array outlining" optimization? (outlining to me is referring to specifically code outlining). IIRC Erik (+CC) do special work to make it work for fixed size array. I would ask why that optimization is not kicking in for varargs. Perhaps, we could add a special recognition that the given array will not escape through a varargs? Or provide some way of saying, trust me this doesn't escape.

This is actually pretty straight-forward to fit into the existing optimizer, and I’m working on a proto patch for it in my spare time this weekend.  The problem is that it will only work on non-Apple systems because of the way that BridgeSupport model’s the ObjC interop goop. IMO, SIL and the stdlib have the wrong division of labor here.  I’ll follow up with something more concrete when I have a chance.

> In terms of what Slava was talking about with copy-on-escape. That can be implemented (assuming a sane runtime ; p) by initializing any COW data structure with a count of 2. Then you are guaranteed to know that any write use or escape from the COW data structure will cause a copy. Once we have guaranteed, this comes for free since any guaranteed parameter must be copied before a mutable use.
> 
> I do think that you will run into issues with escaping around C APIs though.

C APIs cannot escape an array unless they have access to the refcount.  Beyond the SIL representational problem with bridging, what I’m getting at seems like a straight-forward extension to ArrayElementValuePropagation.cpp.

-Chris



More information about the swift-dev mailing list