swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.51k stars 10.35k forks source link

Bug compiling embedded Swift project with `-no-allocations` flag enabled with swift-mmio #76857

Open xtremekforever opened 2 weeks ago

xtremekforever commented 2 weeks ago

Description

I understand that the -no-allocations flag is not fully baked yet or complete, but for giggles I enabled it on my Swift for Embedded project for an STM32C011 and found that swift-mmio does not compile properly with an error:

<unknown>:0: error: cannot use allocating type '__SwiftNativeNSArrayWithContiguousStorage' in -no-allocations mode
in function __SwiftNativeNSArrayWithContiguousStorage.__allocating_init()

Reproduction

To reproduce this it requires setting up a Swift SDK to provide the -no-allocations flag and other flags to swiftpm so it can properly cross compile for the target. I have an example repo here that does just this:

https://github.com/xtremekforever/swift-stm32c011-examples/tree/feature/no-allocations/blink-mmio

To replicate the issue, grab the repo at this branch, then compile the project with the make command:

$ https://github.com/xtremekforever/swift-stm32c011-examples.git -b feature/no-allocations
$ cd swift-stm32c011-examples/blink-mmio
$ make

After building for a while, you should see the error eventually when building swift-mmio for the target:

/opt/swift-6.0.1/usr/bin/swift-frontend -frontend -c ~/swift-stm32c011-examples/blink-mmio/.build/checkouts/swift-mmio/Sources/MMIO/BitField.swift ~/swift-stm32c011-examples/blink-mmio/.build/checkouts/swift-mmio/Sources/MMIO/BitFieldProjectable.swift ~/swift-stm32c011-examples/blink-mmio/.build/checkouts/swift-mmio/Sources/MMIO/MMIOInterposer.swift ~/swift-stm32c011-examples/blink-mmio/.build/checkouts/swift-mmio/Sources/MMIO/MMIOMacros.swift ~/swift-stm32c011-examples/blink-mmio/.build/checkouts/swift-mmio/Sources/MMIO/Register.swift ~/swift-stm32c011-examples/blink-mmio/.build/checkouts/swift-mmio/Sources/MMIO/RegisterArray.swift ~/swift-stm32c011-examples/blink-mmio/.build/checkouts/swift-mmio/Sources/MMIO/RegisterProtocol.swift ~/swift-stm32c011-examples/blink-mmio/.build/checkouts/swift-mmio/Sources/MMIO/RegisterStorage.swift ~/swift-stm32c011-examples/blink-mmio/.build/checkouts/swift-mmio/Sources/MMIO/RegisterValue.swift -supplementary-output-file-map /tmp/TemporaryDirectory.FHSkzt/supplementaryOutputs-1 -target armv6m-none-none-eabi -disable-objc-interop -sdk ~/swift-stm32c011-examples/swift-sdks/stm32c0xx.artifactbundle -I ~/swift-stm32c011-examples/blink-mmio/.build/armv6m-none-none-eabi/release/Modules -color-diagnostics -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path ~/swift-stm32c011-examples/blink-mmio/.build/armv6m-none-none-eabi/release/ModuleCache -suppress-warnings -swift-version 5 -Osize -D SWIFT_PACKAGE -disable-stack-protector -function-sections -load-plugin-executable ~/swift-stm32c011-examples/blink-mmio/.build/x86_64-unknown-linux-gnu/release/MMIOMacros-tool#MMIOMacros -no-allocations -enable-experimental-feature Embedded -enable-experimental-feature Embedded -empty-abi-descriptor -resource-dir /opt/swift-6.0.1/usr/lib/swift -file-compilation-dir ~/swift-stm32c011-examples/blink-mmio -Xcc -fmodule-map-file=~/swift-stm32c011-examples/blink-mmio/.build/checkouts/swift-mmio/Sources/MMIOVolatile/module.modulemap -Xcc --sysroot -Xcc ~/swift-stm32c011-examples/swift-sdks/stm32c0xx.artifactbundle -Xcc -mcpu=cortex-m0plus -Xcc -mthumb -Xcc -mfloat-abi=soft -Xcc -ffreestanding -Xcc -ffunction-sections -Xcc -fshort-enums -Xcc -g -module-name MMIO -package-name swift_mmio -plugin-path /opt/swift-6.0.1/usr/lib/swift/host/plugins -plugin-path /opt/swift-6.0.1/usr/local/lib/swift/host/plugins -enable-default-cmo -parse-as-library -num-threads 12 -o ~/swift-stm32c011-examples/blink-mmio/.build/armv6m-none-none-eabi/release/MMIO.build/BitField.swift.o -o ~/swift-stm32c011-examples/blink-mmio/.build/armv6m-none-none-eabi/release/MMIO.build/BitFieldProjectable.swift.o -o ~/swift-stm32c011-examples/blink-mmio/.build/armv6m-none-none-eabi/release/MMIO.build/MMIOInterposer.swift.o -o ~/swift-stm32c011-examples/blink-mmio/.build/armv6m-none-none-eabi/release/MMIO.build/MMIOMacros.swift.o -o ~/swift-stm32c011-examples/blink-mmio/.build/armv6m-none-none-eabi/release/MMIO.build/Register.swift.o -o ~/swift-stm32c011-examples/blink-mmio/.build/armv6m-none-none-eabi/release/MMIO.build/RegisterArray.swift.o -o ~/swift-stm32c011-examples/blink-mmio/.build/armv6m-none-none-eabi/release/MMIO.build/RegisterProtocol.swift.o -o ~/swift-stm32c011-examples/blink-mmio/.build/armv6m-none-none-eabi/release/MMIO.build/RegisterStorage.swift.o -o ~/swift-stm32c011-examples/blink-mmio/.build/armv6m-none-none-eabi/release/MMIO.build/RegisterValue.swift.o
<unknown>:0: error: cannot use allocating type '__SwiftNativeNSArrayWithContiguousStorage' in -no-allocations mode
in function __SwiftNativeNSArrayWithContiguousStorage.__allocating_init()
make: *** [Makefile:22: build] Error 1

Expected behavior

Since swift-mmio does not allocate any heap memory by design, I would expect that the -no-allocations flag does not throw an error that a heap operation is being performed.

Environment

Swift version 6.1-dev (LLVM 89ccf4b8a46135a, Swift 6a5ae8d5df144dd) Target: x86_64-unknown-linux-gnu

Additional information

I also tested this with the Swift 6.0.1 release with the same result.

kubamracek commented 2 weeks ago

The error is "legit" because Swift MMIO in this case does/did actually use an array, reported here: https://github.com/apple/swift-mmio/issues/126

The thing to improve/fix is the quality of the diagnostic. It needs to point to the real location of the problem.