# No longer able to type check expressions with Xcode 10.2 (caused by new SIMD API?)

**URL:** <https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873>\
**Category:** Using Swift\
**Tags:** bug, compiler, simd, regression\
**Created:** [April 9, 2019, 4:16pm UTC](https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873 "2019-04-09T16:16:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [April 9, 2019, 4:16pm UTC](https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873/1 "2019-04-09T16:16:29Z")

</div>

I know for certain that the three lines with `i`, `j` and `k` in **test.swift** did type check (quickly) prior to upgrading to Xcode 10.2 and macOS 10.14.4. But the compiler is now unable to type check them in reasonable time (`i` takes forever).

**test.swift:**

```swift
import simd

func test(_ p0: float2, _ p1: float2, _ p2: float2, _ p3: float2) {
    let i = 3*(-p0 + 3*p1) - (3*p2 + p3)
    let j = 6*(p0 - 2*p1 + p2)
    let k = 3*(p1 - p0)
    print(i, j, k)
}
test(float2(0.1, 1.2), float2(2.3, 3.4), float2(4.5, 5.6), float2(6.7, 7.8))

```

* * *

```swift
$ swiftc --version
Apple Swift version 5.0 (swiftlang-1001.0.69.5 clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0
$ time swiftc test.swift 
test.swift:4:28: error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
    let i = 3*(-p0 + 3*p1) - (3*p2 + p3)
            ~~~~~~~~~~~~~~~^~~~~~~~~~~~~

real	0m17.296s
user	0m16.647s
sys	0m0.613s

```

* * *

And a naive workaround like wrapping parts of the expressions in `float2(…)` will make this tiny program compile, but in 12 seconds!  
**testB.swift:**

```swift
import simd

func test(_ p0: float2, _ p1: float2, _ p2: float2, _ p3: float2) {
    let i = 3*float2(-p0 + float2(3*p1)) - float2(3*p2 + p3)
    let j = float2(6*(p0 - 2*p1 + p2))
    let k = float2(3*(p1 - p0))
    print(i, j, k)
}
test(float2(0.1, 1.2), float2(2.3, 3.4), float2(4.5, 5.6), float2(6.7, 7.8))

```

* * *

```swift
$ time swiftc testB.swift 

real	0m12.477s
user	0m11.975s
sys	0m0.490s

```

* * *

Is this because float2 is now a typealias for the new generic `SIMD2<Float>` (cc @scanon )?

We have had to go through and modify a lot of our code because of this, and the build time has increased dramatically, especially in release builds.

* * *

There are of course better workaround/ways to break up the expressions. For example, in order to get it to compile it suffices to break up the expression for `i` into two parts `iA` and `iB`:  
**testC.swift:**

```swift
import simd

func test(_ p0: float2, _ p1: float2, _ p2: float2, _ p3: float2) {
    let iA = 3*(-p0 + 3*p1)
    let iB = (3*p2 + p3)
    let i = iA - iB
    let j = 6*(p0 - 2*p1 + p2)
    let k = 3*(p1 - p0)
    print(i, j, k)
    print(i)
}
test(float2(0.1, 1.2), float2(2.3, 3.4), float2(4.5, 5.6), float2(6.7, 7.8))

```

But testC.swift still takes a long time to compile, 7 seconds, for 10 lines of code!  
That's much longer than it took Xcode 10.1 (and macOS 10.14.3) to compile an entire medium sized project that included exactly the original expression for `i` from test.swift above, and lots and lots of other similar expressions.

This is a serious regression. We have to jump through hoops in order to try and reduce the ridiculously long compile times that now arises whenever we are writing code that uses SIMD types, this was not necessary before.

---

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [April 12, 2019, 6:53am UTC](https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873/2 "2019-04-12T06:53:42Z")

</div>

This regression is still present in the most recent dev snapshot (2019-04-10).  
Filed [SR-10461](https://bugs.swift.org/browse/SR-10461).

---

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [April 12, 2019, 8:36am UTC](https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873/3 "2019-04-12T08:36:37Z")

</div>

I did some more testing and the following demonstration program seems to suggest that this regression is caused (at least in part) by the fact that `float2` is now generic (a typealias for `SIMD2<Float>`).

It would be interesting to know if the behavior of the following program was the same in Xcode 10.1 and macOS 10.14.3. If it wasn't, then the compile time regression in the OP is more general, and not something that only has to do with the SIMD types.

* * *

```swift
//-----------------------------------------------------------------------------
// This program is meant to demonstrate the difference in the compiler's
// ability to type check the expression returned by the function foo,
// depending on wether Point is generic or not.
//
// So please comment out A and B to test both of them. A will compile
// quickly while B will be unable to type check within reasonable time,
// at least when I try it out with the def toolchain of Xcode 10.2 as
// well as with the most recent dev snapshot (2019-04-10).
//-----------------------------------------------------------------------------

typealias Point = Point2D_Float // A
//typealias Point = Point2D<Float> // B

//-----------------------------------------------------------------------------
// Note that:
// A ~ How it was prior to Xcode 10.2, when float2 was not generic.
// B ~ How it is in Xcode 10.2, when float2 is generic (SIMD2<Float>).
//-----------------------------------------------------------------------------

func foo(_ a: Point, _ b: Point, _ c: Point, _ d: Point) -> Point {
    return 2*(3*a - b) + (4*c - d) // Typechecks quickly if A but sloooow if B.
}

struct Point2D_Float {
    var x, y : Float
    init(_ x: Float, _ y: Float) { (self.x, self.y) = (x, y) }
    
    static func +(lhs: Point2D_Float, rhs: Point2D_Float) -> Point2D_Float {
        return Point2D_Float(lhs.x + rhs.x, lhs.y + rhs.y)
    }
    static func -(lhs: Point2D_Float, rhs: Point2D_Float) -> Point2D_Float {
        return Point2D_Float(lhs.x - rhs.x, lhs.y - rhs.y)
    }
    static func *(lhs: Float, rhs: Point2D_Float) -> Point2D_Float {
        return Point2D_Float(lhs * rhs.x, lhs * rhs.y)
    }
}

struct Point2D<Scalar: BinaryFloatingPoint> {
    var x, y : Scalar
    init(_ x: Scalar, _ y: Scalar) { (self.x, self.y) = (x, y) }

    static func +(lhs: Point2D, rhs: Point2D) -> Point2D {
        return Point2D(lhs.x + rhs.x, lhs.y + rhs.y)
    }
    static func -(lhs: Point2D, rhs: Point2D) -> Point2D {
        return Point2D(lhs.x - rhs.x, lhs.y - rhs.y)
    }
    static func *(lhs: Scalar, rhs: Point2D) -> Point2D {
        return Point2D(lhs * rhs.x, lhs * rhs.y)
    }
}

let result = foo(Point(1, 2), Point(3, 4), Point(5, 6), Point(7, 8))
print(result)

```

---

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://forums.swift.org/u/Martin)\
**Post date:** [April 12, 2019, 9:19am UTC](https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873/4 "2019-04-12T09:19:40Z")

</div>

I have compiled your code with Xcode 10.1 (Swift 4.2) on a MacBook (Early 2016, 1.2 GHz Intel Core m5) with macOS 10.14.4. Version A compiles quickly, as expected:

```
# Version A
$ time swiftc main.swift 

real	0m0.207s
user	0m0.108s
sys	0m0.043s

```

Version B compiles as well, but much slower:

```
# Version B
$ time swiftc main.swift 

real	0m5.538s
user	0m4.979s
sys	0m0.447s

```

Interestingly, version B (but not version A) fails to compile in Xcode 10.1 as well if an `import simd` is added to the program:

```
# Version B with `import simd`
$ time swiftc main.swift 
main.swift:26:24: error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
    return 2*(3*a - b) + (4*c - d) // Typechecks quickly if A but sloooow if B.
           ~~~~~~~~~~~~^~~~~~~~~~~

real	0m14.559s
user	0m13.381s
sys	0m1.093s

```

---

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [April 19, 2019, 2:13pm UTC](https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873/5 "2019-04-19T14:13:13Z")

</div>

The code example in the OP is still too complex to type check, even for Xcode 10.2.1:

```swift
import simd

func test(_ p0: float2, _ p1: float2, _ p2: float2, _ p3: float2) {
    let i = 3*(-p0 + 3*p1) - (3*p2 + p3)
    let j = 6*(p0 - 2*p1 + p2)
    let k = 3*(p1 - p0)
    print(i, j, k)
}

```

* * *

```swift
swiftc --version
Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)
Target: x86_64-apple-darwin18.5.0
$ time swiftc test.swift
test.swift:4:28: error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
    let i = 3*(-p0 + 3*p1) - (3*p2 + p3)
            ~~~~~~~~~~~~~~~^~~~~~~~~~~~~

real	0m20.778s
user	0m18.693s
sys	0m1.627s

```

:´ /

(As previously noted, this is: [SR-10461](https://bugs.swift.org/browse/SR-10461).)

---

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [April 19, 2019, 5:10pm UTC](https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873/6 "2019-04-19T17:10:17Z")

</div>

> [@Martin](#):
>
> Interestingly, version B (but not version A) fails to compile in Xcode 10.1 as well if an `import simd` is added to the program:
> 
> ```swift
> # Version B with `import simd`
> $ time swiftc main.swift 
> main.swift:26:24: error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
> return 2*(3*a - b) + (4*c - d) // Typechecks quickly if A but sloooow if B.
> ~~~~~~~~~~~~^~~~~~~~~~~
> 
> real	0m14.559s
> user	0m13.381s
> sys	0m1.093s
> 
> ```

Thanks for running [that program](https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873/3) on Xcode 10.1 (and macOS 10.14.4), interesting!

That makes one wonder what the results would be on a machine with macOS 10.14.3.  
That is: Is version B not slow when compiled with Xcode 10.1 on macOS 10.14.3?

Perhaps the slow down in type checking has something to do with parts of Swift being in the OS as of macOS 10.14.4?

---

<div class="post-metadata">

**Author:** ![yxckjhasdkjh](https://avatars.discourse-cdn.com/v4/letter/y/278dde/32.png) [@yxckjhasdkjh](https://forums.swift.org/u/yxckjhasdkjh)\
**Post date:** [April 25, 2019, 6:32pm UTC](https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873/7 "2019-04-25T18:32:07Z")

</div>

I'm trying to update our code to Swift 5 and unfortunately, I'm also seeing regressions where the code can't be compiled anymore ☹ This is annoying and will end up costing a lot of time again (last time when we updated from Swift 4 to 4.2, we also ran into this problem).

We're not importing simd, but we do use a lot of operator overloads.

---

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [April 27, 2019, 6:52am UTC](https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873/8 "2019-04-27T06:52:07Z")

</div>

I agree, this and the long standing [auto-balancing brackets bug](https://forums.swift.org/t/auto-balancing-brackets-bug/17136) takes away a lot of the joy from my work days ...

It would be great if someone could look into [SR-10461](https://bugs.swift.org/browse/SR-10461), keeping in mind that it's not specific to the new SIMD types, but rather that it has to do with operators and generics. I think this build time regression, which is still in Xcode 10.2.1, needs some attention and priority.

cc @tkremenek

---

<div class="post-metadata">

**Author:** ![yxckjhasdkjh](https://avatars.discourse-cdn.com/v4/letter/y/278dde/32.png) [@yxckjhasdkjh](https://forums.swift.org/u/yxckjhasdkjh)\
**Post date:** [April 30, 2019, 4:21pm UTC](https://forums.swift.org/t/no-longer-able-to-type-check-expressions-with-xcode-10-2-caused-by-new-simd-api/22873/9 "2019-04-30T16:21:01Z")

</div>

We could fix the compilation failures by breaking up some expressions, but compilation on CI now takes about twice as long and we had to increase the memory in the docker image because it was otherwise failing to compile (3GB wasn't enough).

So far, upgrading to Swift 5 seems like a step backwards.
