[swift-dev] "For" loop iterator and "enum"

Joe Groff jgroff at apple.com
Fri Oct 27 16:33:43 CDT 2017



> On Oct 24, 2017, at 8:32 AM, Sam Ding via swift-dev <swift-dev at swift.org> wrote:
> 
> Hi,
> 
> It is found that "for" loop iterate and "enum" does not work on s390x.
> Here is a sample code:
> enum IntKey : Int {
> case a = 3
> case b = 4 // Implicitly 4
> case c = 1
> }
> for ( a1, a2) in [(IntKey.a, 1)] {
> print ("+++++ test \n")
> }
> 
> The for loop inifinitly iterates on s390x. Note that it works fine if replacing the enum with normal tuple in the above code.
> When debugging this code, after one iteration, the logic gets a "nil" in "IndexingIterator.next()" as follows:
> ....
> frame #0: 0x000003fffd5b4088 libswiftCore.so`IndexingIterator.next(self=0x000003fffffff460) at Collection.swift:411
> 408 @inline(__always)
> 409 public mutating func next() -> Elements.Element? {
> 410 if _position == _elements.endIndex {
> -> 411 return nil
> 412 }
> 413 let element = _elements[_position]
> ..
> Then comes "Enum.cpp:211" (swift::swift_storeEnumTagSinglePayload) and no difference on both s390x and x86_64.
> However, It continues goes into the loop body on s390x, and terminates on x86_64, respectively.
> 
> Have anyones know well this implementation and give us a hint for the problems?
> 
> Thanks,
> 

At a binary level, Swift is going to represent the result of the next() operation as an value of the element type wrapped in an Optional, `(IntKey, Int)?`, and the compiler is going to recognize that it can represent the `nil` case by using unused representations of IntKey. Our code for doing enum layout was originally written in a very little-endian-centric way, and if s390x is big-endian, there's a possibility that the code in IRGen or the runtime that encodes the `nil` result is not correct, or that the runtime and IRGen do not agree on the representation of `nil`. That could lead to the infinite loop you're seeing.

-Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20171027/fcffda12/attachment.html>


More information about the swift-dev mailing list