Eric_Wing
(Eric Wing)
November 15, 2016, 9:31pm
1
Hi, I have encountered an internal Swift compiler assertion/bug when
using Swift on ARM (Raspberry Pi).
The assertion is:
Swift compiler assertion failure: Assertion `LastArgWritten >=
adjustedArg.size()' failed
It seems to be related to calling a bridged C function with a C struct
parametert.
I posted on the swift-arm group. hpux735 said this is deeper into the
compiler internals than he knows about so we should see if we can get
the attention of some experts on this because we don't know how to
proceed.
I filed the bug here:
opened 07:58PM - 20 Sep 16 UTC
closed 12:45AM - 24 Jan 18 UTC
bug
Compiler
arm
compiler crash
3.0Regression
armv7
| | |
|------------------|-----------------|…
|Previous ID | SR-2706 |
|Radar | None |
|Original Reporter | ewmailing (JIRA User) |
|Type | Bug |
|Status | Closed |
|Resolution | Duplicate |
Attachment: [Download](https://user-images.githubusercontent.com/2727770/164961611-f17e1d47-367e-4c17-87e8-33edf57104c9.gz)
<details>
<summary>Environment</summary>
Raspberry Pi 2 (32-bit armv7) running Raspbian Jessie
Built Swift 3 from source. (A bunch of components failed to build, but the Swift compiler got built.)
I've tested this code on Mac, iOS, Linux x86_64, and Android. The code compiles and works for all these other platforms. So this bug seems to be arm specific. (iOS and Android use x86_64 platforms to cross-compile, whereas I am directly compiling on the arm device to generate arm binaries which is the big difference there.)
</details>
<details>
<summary>Additional Detail from JIRA</summary>
| | |
|------------------|-----------------|
|Votes | 1 |
|Component/s | Compiler |
|Labels | Bug, 3.0Regression, CompilerCrash, arm, armv7 |
|Assignee | @compnerd |
|Priority | Medium |
md5: 8f2abfe8f8520f432ecc0169bea57a0a
</details>
**Issue Description:**
I'm trying to use Swift 3 on a Raspberry Pi 2 (armv7). On code that used to compile in Swift 2.3, and code that currently compiles correctly on Swift 3 for Mac/Xcode 8, Linux x86_64, and for Android, I'm getting a compiler assertion failure and stack dump.
=======
``` java
swift: /home/pi/Source/SWIFT/swift/lib/IRGen/GenCall.cpp:1886: void swift::irgen::CallEmission::setArgs(swift::irgen::Explosion &, swift::irgen::WitnessMetadata *): Assertion `LastArgWritten >= adjustedArg.size()' failed.
Stack dump:
0. Program arguments: /home/pi/swift3/usr/bin/swift -frontend -c -DBLURRR_PLATFORM_LINUX -DBLURRR_PLATFORM_LINUX=1 -DBLURRR_PLATFORM_RASPBERRY_PI -DBLURRR_PLATFORM_RASPBERRY_PI=1 -DBLURRR_PLATFORM_RASPBIAN -DBLURRR_PLATFORM_RASPBIAN=1 -I/home/pi/Source/Blurrr/Release/BlurrrSDKRaspberryPi/Libraries/SDK/Raspbian/C/include/BlurrrCore -I/home/pi/Source/Blurrr/Release/BlurrrSDKRaspberryPi/Libraries/SDK/Raspbian/C/include/ALmixer -I/home/pi/Source/Blurrr/Release/BlurrrSDKRaspberryPi/Libraries/SDK/Raspbian/C/include/AL -I/home/pi/Source/Blurrr/Release/BlurrrSDKRaspberryPi/Libraries/SDK/Raspbian/C/include/chipmunk -I/home/pi/Source/Blurrr/Release/BlurrrSDKRaspberryPi/Libraries/SDK/Raspbian/C/include/SDL_ttf -I/home/pi/Source/Blurrr/Release/BlurrrSDKRaspberryPi/Libraries/SDK/Raspbian/C/include/SDL_image -I/home/pi/Source/Blurrr/Release/BlurrrSDKRaspberryPi/Libraries/SDK/Raspbian/C/include/SDL2 -I/home/pi/Source/Blurrr/Examples/flappyblurrrswift/source -target armv7-unknown-linux-gnueabihf -primary-file /home/pi/Source/Blurrr/Examples/flappyblurrrswift/source/BlurrrMain.swift /home/pi/Source/Blurrr/Examples/flappyblurrrswift/source/main.swift /home/pi/Source/Blurrr/Examples/flappyblurrrswift/source/BlurrrMain.swift -import-objc-header /home/pi/Source/Blurrr/Examples/flappyblurrrswift/source/MySwiftBridgingHeader.h -emit-module -module-name FlappyBlurrrSwift -o CMakeFiles/FlappyBlurrrSwift.dir/source/BlurrrMain.swift.o
1. While emitting IR SIL function @_TF17FlappyBlurrrSwift27Flappy_UpdateGroundPositionFTVs6UInt329base_timeS0_12current_timeS0__T_ for 'Flappy_UpdateGroundPosition' at /home/pi/Source/Blurrr/Examples/flappyblurrrswift/source/BlurrrMain.swift:1137:1
CMakeFiles/FlappyBlurrrSwift.dir/build.make:78: recipe for target 'CMakeFiles/FlappyBlurrrSwift.dir/source/BlurrrMain.swift.o' failed
make[2]: *** [CMakeFiles/FlappyBlurrrSwift.dir/source/BlurrrMain.swift.o] Aborted
CMakeFiles/Makefile2:102: recipe for target 'CMakeFiles/FlappyBlurrrSwift.dir/all' failed
make[1]: *** [CMakeFiles/FlappyBlurrrSwift.dir/all] Error 2
Makefile:149: recipe for target 'all' failed
make: *** [all] Error 2
```
\_\_\_\_\_\_
I think Swift is having trouble with C-bindings. As far as I can tell, the compiler is breaking with stuff related to a type called cpVect defined as:
``` java
typedef struct cpVect{cpFloat x,y;} cpVect;
```
My code typically used a convenience function provided by the library defined as:
``` java
/// Convenience constructor for cpVect structs.
static inline cpVect cpv(const cpFloat x, const cpFloat y)
{
cpVect v = {x, y};
return v;
}
```
The simple pattern that seems to trigger this seems to come down to:
``` java
let pos_vec = cpv(0.0, 0.0);
cpBodySetPosition(body, pos_vec); // also tried explicit nil for body to try to suggest that it is the cpVect causing the problem
```
~~I'm attaching my slightly modified file trying to isolate the problem. It is line 1178 that trips up the compiler. (But if I comment this out, there are other instances of the code that trigger this.)~~
~~The full source code is in BlurrrMain.swift which can be found here.~~
~~bitbucket.org/blurrr/flappyblurrrswift/src~~
~~The project has a bunch of dependencies. I'm developing a cross-platform SDK that makes this easy to build. I'm happy to give you access to the SDK if it helps you debug this.~~
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
# Update:
# \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
I have new/additional information for this bug.
I have created a minimal, reproducible test case that can demonstrate this bug. It seems to be related to using a bridged C struct and function in Swift.
So in Swift, I have a simple function like which will cause the compiler to hit the assertion failure:
``` java
func BlurrrMain() -> Int32
{
let body_vec: fakeVect = fakeVect(x:1.1, y:2.2);
fakeSetPosition(body_vec);
return 0;
}
```
In C, I have a header like so:
``` java
#ifndef MYCTYPES_H
#define MYCTYPES_H
struct fakeVect
{
float x;
float y;
};
typedef struct fakeVect fakeVect;
void fakeSetPosition(fakeVect vec);
#endif
```
I make sure to include this header in my bridging header.
Compiling this on Pi is sufficient enough to trigger the Swift compiler assertion.
I know this code builds and works for Mac, iOS, Linux x86_64, and Android. I’m only having this problem with the Pi.
As a reminder, the assertion says:
``` java
swift: /home/pi/Source/SWIFT3ARM/build-swift/swift/lib/IRGen/GenCall.cpp:1864: void swift::irgen::CallEmission::setArgs(swift::irgen::Explosion &, swift::irgen::WitnessMetadata *): Assertion `LastArgWritten >= adjustedArg.size()' failed.
```
In this case, LastArgWritten is 2 and adustedArg.size() is 3
I am attaching a reproducible project called SwiftArmCompileBug.tar.gz.
There is a build.sh script to help build it, but if you want to jump directly to the failing part, the command is the 3rd one which is:
``` java
swift -frontend -c -target armv7-unknown-linux-gnueabihf -primary-file BlurrrMain.swift main.swift BlurrrMain.swift -import-objc-header MySwiftBridgingHeader.h -emit-module -module-name MyBlurrrProject -o BlurrrMain.swift.o
```
It also contains the simple reproducible test case with a build.sh script.
Thanks,
Eric