[swift-dev] Reducing array abstraction

Michael Gottesman mgottesman at apple.com
Sun Oct 8 13:57:41 CDT 2017


> 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?

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.

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.

> but this is no where near the abstraction level it could be.
> 
> Is there a better way to achieve this, and if not, is there any planned work (perhaps aligned with the ABI stability efforts) to improve vararg array performance to be able to avoid heap abstractions?  Any individual call to a vararg array is a known length, so it is a perfect candidate for a stack allocation.
> 
> -Chris
> 
> 
> _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev



More information about the swift-dev mailing list