[swift-dev] Reducing array abstraction
Michael Gottesman
mgottesman at apple.com
Sun Oct 8 14:41:35 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?
>
> 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.
*We definitely already have a heap->stack for classes in the guise of the StackPromotion optimization. Is that the sort of thing that you are talking about when you say the "array outlining" optimization? (outlining to me is referring to specifically code outlining). IIRC Erik (+CC) did special work to make heap->stack work for fixed size Swift.Array that do not escape. But my memory may be incorrect. If my memory is correct, I would ask why that optimization is not kicking in for varargs. I imagine it is an issue around the optimization assuming that the array escapes. 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? I imagine that a special case would work here since IIRC we can know in the optimizer via the AST that we are dealing with a varargs parameter. Things sort of follow from there.
(Sorry for the half written reply. Needed Coffee).
>
> 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 <mailto:swift-dev at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-dev <https://lists.swift.org/mailman/listinfo/swift-dev>
>
> _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org <mailto:swift-dev at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-dev <https://lists.swift.org/mailman/listinfo/swift-dev>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20171008/a6b1b845/attachment.html>
More information about the swift-dev
mailing list