I'm not 100% sure if it's working or not as I'm not sure of the expected behaviour?
Rebased on cdfeefc68b689b90653b20136a53c0858adc154c the compiler still seems to be creating a heap based object behind the scenes I think?
EDIT: I tested carefully to make sure it was parsed as library and I still think I''m seeing it.
I built tests as described in the GitHub issue: Fixed-size, constant, global array of Ints not promoted to StaticArray storage on some targets · Issue #74549 · swiftlang/swift · GitHub
This library code...
let pots = [41,52,99,1,4,4]
public enum ValidState {
case yes
case no
case undefined
}
public func checkPot(i: UInt8, testValue: UInt8) -> ValidState {
guard i < pots.count else { return .undefined }
return pots[Int(i)] < testValue ? .yes : .no
}
...is compiled with the new compiler, including PR [embedded] Enable read-only static array promotion in embedded mode by kubamracek · Pull Request #74551 · swiftlang/swift · GitHub and I see this function for the array initialiser:
; Function Attrs: nounwind
define protected void @"$s5sats24pots_WZ"(ptr nocapture readnone %0) addrspace(1) #0 {
%2 = tail call noalias addrspace(1) ptr @swift_allocObject(ptr nonnull @"$ss23_ContiguousArrayStorageCys5UInt8VGN", i16 14, i16 poison) #1
%3 = getelementptr inbounds %Ts28__ContiguousArrayStorageBaseC, ptr %2, i16 0, i32 1
store i16 6, ptr %3, align 2
%4 = getelementptr inbounds %Ts28__ContiguousArrayStorageBaseC, ptr %2, i16 0, i32 1, i32 0, i32 1
store i16 12, ptr %4, align 2
%5 = getelementptr inbounds i8, ptr %2, i16 8
store i8 41, ptr %5, align 1
%6 = getelementptr inbounds i8, ptr %2, i16 9
store i8 52, ptr %6, align 1
%7 = getelementptr inbounds i8, ptr %2, i16 10
store i8 99, ptr %7, align 1
%8 = getelementptr inbounds i8, ptr %2, i16 11
store i8 1, ptr %8, align 1
%9 = getelementptr inbounds i8, ptr %2, i16 12
store i8 4, ptr %9, align 1
%10 = getelementptr inbounds i8, ptr %2, i16 13
store i8 4, ptr %10, align 1
store ptr %2, ptr @"$s5sats24potsSays5UInt8VGvp", align 2
ret void
}
...which looks like it's still storing the array guts on the heap I think?
I'm happy to build a debug compiler and step through the optimisation to try and work out where it's not going to plan if that's useful? I might just need a few pointers how best to do that?
p.s. The compile command is...
"/Users/carl/Library/Developer/Xcode/DerivedData/Swift_For_Arduino-ekjmfkrcyudnlpemyalpewpvtamj/Build/Products/Debug/Swift For Arduino.app/Contents/XPCServices/BuildEngine.xpc/Contents/Resources/swift/swiftc" -target avr-atmel-linux-gnueabihf -emit-bc -O -enforce-exclusivity=unchecked -parse-as-library -import-underlying-module -I . -O -no-link-objc-runtime -Xfrontend -disable-reflection-metadata -Xfrontend -disable-stack-protector -enable-experimental-feature Embedded -nostdimport -I "/Users/carl/Library/Developer/Xcode/DerivedData/Swift_For_Arduino-ekjmfkrcyudnlpemyalpewpvtamj/Build/Products/Debug/Swift For Arduino.app/Contents/XPCServices/BuildEngine.xpc/Contents/Resources/uSwift-AVR/Embedded" -I "/Users/carl/Library/Developer/Xcode/DerivedData/Swift_For_Arduino-ekjmfkrcyudnlpemyalpewpvtamj/Build/Products/Debug/Swift For Arduino.app/Contents/XPCServices/BuildEngine.xpc/Contents/Resources/uSwiftShims/Embedded" -I "/Users/carl/Library/Developer/Xcode/DerivedData/Swift_For_Arduino-ekjmfkrcyudnlpemyalpewpvtamj/Build/Products/Debug/Swift For Arduino.app/Contents/XPCServices/BuildEngine.xpc/Contents/Resources/gpl-tools-avr/lib/avr-libgcc/include" -I "/Users/carl/Library/Developer/Xcode/DerivedData/Swift_For_Arduino-ekjmfkrcyudnlpemyalpewpvtamj/Build/Products/Debug/Swift For Arduino.app/Contents/XPCServices/BuildEngine.xpc/Contents/Resources/gpl-tools-avr/lib/avr-libc/include" -Xfrontend -disable-implicit-concurrency-module-import -Xfrontend -disable-implicit-string-processing-module-import -I"/Users/carl/Documents/SwiftForArduino/Exports"/Embedded -I"/Users/carl/Library/Application Support/SwiftForArduino/Extensions/Modules"/Embedded -I"/Users/carl/Library/Application Support/SwiftForArduino/S4A/206/Modules"/Embedded -Xcc -DAVR_LIBC_DEFINED -Xcc -DLIBC_DEFINED -Xcc -D__AVR_ATmega328P__ -Xcc -DF_CPU=16000000 -DAVR_LIBC_DEFINED_SWIFT $(cat build/packages-swift-includes-list.txt) -module-name sats2 -whole-module-optimization -num-threads 4 -output-file-map build/outputMap.json constants.swift