Skip to content

[AVR] standard library support for AVR #75127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

carlos4242
Copy link
Contributor

Standard library support for AVR

  • when compiling embedded cross compile target standard libraries, include AVR
  • add 16-bit pointer as a conditional compilation condition and get the void pointer size right for gyb sources

@kubamracek this is the first attempt to get the standard library compiling

@kubamracek
Copy link
Contributor

@swift-ci please smoke test

@rauhul rauhul mentioned this pull request Jul 10, 2024
9 tasks
@kubamracek
Copy link
Contributor

@swift-ci please smoke test

@kubamracek kubamracek requested review from phausler and lorentey July 15, 2024 15:58
@carlos4242
Copy link
Contributor Author

OK @kubamracek, I addressed a couple of your suggestions, they were all good ideas. I also sent up a small patch on CTypes.swift. Without this, the clang importer imports some int types incorrectly on 16-bit. It's the old thing of what an unsigned int is (16-bit on 16-bit platforms). I'm happy to put this into another PR if you'd prefer.

The last couple of days I've been flexing the muscles of the compiler with this patched standard library and trying to build our core hardware library, which is a good test! This fix came out of that work, along with a load of other cherry-picks from other compiler work I've needed in the past... none of which are suitable for this PR! They will come later, if required.

There are question marks around the synchronisation stuff here. that might be a bit of a hack to get things compiling and I'm not sure it's right.

The big question is around what to do with String. As discussed elsewhere!

@carlos4242 carlos4242 requested review from kubamracek and rauhul July 15, 2024 23:32
@carlos4242
Copy link
Contributor Author

carlos4242 commented Jul 15, 2024 via email

@kubamracek
Copy link
Contributor

@swift-ci please test

@kubamracek
Copy link
Contributor

I've reviewed the whole PR, I don't have any more comments besides the ones I just added, with those addressed, I think this PR is ready to go, assuming it passes tests. But let's please also give @phausler a chance to review. Great work, @carlos4242 !

- when compiling embedded cross compile target standard libraries, include AVR
- add 16-bit pointer as a conditional compilation condition and get the void pointer size right for gyb sources
- attempt to fix clang importer not importing __swift_intptr_t correctly on 16 bit platforms
- changed the unit test target to avr-none-none-elf to match the cmake build

[AVR] got the standard library compiling in a somewhat restricted form:

General
- updated the Embedded Runtime
- tweaked CTypes.swift to fix clang import on 16 bit platforms

Strings
- as discussed in https://forums.swift.org/t/stringguts-stringobject-internals-how-to-layout-on-16-bit-platforms/73130, I went for just using the same basic layout in 16 bit as 32 bit but with 16 bit pointers/ints... the conversation is ongoing, I think something more efficient is possible but at least this compiles and will probably work (inefficiently)

Unicode
- the huge arrays of unicode stuff in UnicodeStubs would not compile, so I skipped it for AVR for now.

Synchronization
- disabled building the Synchronization library on AVR for now. It's arguable if it adds value on this platform anyway.
@carlos4242 carlos4242 force-pushed the embedded-avr-cross-compile-stdlib branch from 58c09af to 3689427 Compare July 16, 2024 11:30
@carlos4242 carlos4242 requested a review from kubamracek July 16, 2024 11:32
@carlos4242
Copy link
Contributor Author

Hi @kubamracek @rauhul, the test currently failing is abi/macOS/arm64/stdlib-asserts.swift. It looks like this test does something like check the standard library symbols to make sure no unexpected new ones have appeared and no old ones have disappeared?

The error output from lit is...

--- /Users/carl/Documents/Code/swift/swift/test/abi/macOS/arm64/../../Inputs/macOS/arm64/stdlib/baseline-asserts	2024-07-16 12:30:02
+++ /Users/carl/Documents/Code/swift/build/Ninja-ReleaseAssert/swift-macosx-arm64/test-macosx-arm64/abi/macOS/arm64/Output/stdlib-asserts.swift.tmp/symbols	2024-07-16 13:17:21
@@ -9387,6 +9387,7 @@
 _$ss32_diagnoseUnexpectedEnumCaseValue4type03rawE0s5NeverOxm_q_tr0_lF
 _$ss32_makeSwiftNSFastEnumerationStateSo01_bcdE0ayF
 _$ss32_stdlib_binary_CFStringGetLengthySiyXlF
+_$ss32_swift_stdlib_atomicFetchOrInt166object7operands0F0VSpyAEG_AEtF
 _$ss32_swift_stdlib_atomicFetchOrInt326object7operands0F0VSpyAEG_AEtF
 _$ss32_swift_stdlib_atomicFetchOrInt646object7operands0F0VSpyAEG_AEtF
 _$ss33ExpressibleByUnicodeScalarLiteralMp
@@ -9400,10 +9401,13 @@
 _$ss33_ExpressibleByBuiltinFloatLiteralTL
 _$ss33_isBridgedNonVerbatimToObjectiveCySbxmlF
 _$ss33_stdlib_binary_CFStringCreateCopyyyXlyXlF
+_$ss33_swift_stdlib_atomicFetchAddInt166object7operands0F0VSpyAEG_AEtF
 _$ss33_swift_stdlib_atomicFetchAddInt326object7operands0F0VSpyAEG_AEtF
 _$ss33_swift_stdlib_atomicFetchAddInt646object7operands0F0VSpyAEG_AEtF
+_$ss33_swift_stdlib_atomicFetchAndInt166object7operands0F0VSpyAEG_AEtF
 _$ss33_swift_stdlib_atomicFetchAndInt326object7operands0F0VSpyAEG_AEtF
 _$ss33_swift_stdlib_atomicFetchAndInt646object7operands0F0VSpyAEG_AEtF
+_$ss33_swift_stdlib_atomicFetchXorInt166object7operands0F0VSpyAEG_AEtF
 _$ss33_swift_stdlib_atomicFetchXorInt326object7operands0F0VSpyAEG_AEtF
 _$ss33_swift_stdlib_atomicFetchXorInt646object7operands0F0VSpyAEG_AEtF
 _$ss34CustomPlaygroundDisplayConvertibleMp

I'm thinking, perhaps it makes sense that these not be present on the standard library on MacOS?

I think the change that broke this is probably adding 16 bits to this section in AtomicInt.swift.gyb...

%   for bits in [ 16, 32, 64 ]:

// Warning: no overflow checking.
@usableFromInline // used by SwiftPrivate._stdlib_AtomicInt
internal func _swift_stdlib_atomicFetch${operation}Int${bits}(
  object target: UnsafeMutablePointer<Int${bits}>,
  operand: Int${bits}) -> Int${bits} {

  let value = Builtin.atomicrmw_${operation.lower()}_seqcst_Int${bits}(
    target._rawValue, operand._value)

  return Int${bits}(value)
}

%   end

There's two or three approaches we can take. Firstly, simplest, I can just revert this change? I've already removed all the other work on atomics and the synchronisation library from this PR.

Secondly, I can change the above so that it only runs for a number of bits suitable for the platform, so that 16 bit is only emitted for the AVR standard library?

Or lastly, if this should be present, presumably I can change the test to pass on MacOS and add ss32_swift_stdlib_atomicFetchOrInt166object7operands0F0VSpyAEG_AEtF etc. to the expected symbols from the standard library.

What do you think is the best approach? If you have no opinion, I'll probably just revert the original change that broke this test, as it's the simplest approach.

@kubamracek
Copy link
Contributor

This ABI/API addition seems pretty reasonable to me, but neither me nor @rauhul can sign off on that, that's more of a question for @lorentey and maybe @phausler. In any case, I think it should go in as a separate change, so what I'd suggest is to drop that change for now, and let's see what @lorentey and @phausler say -- if the change is useful then let's have a separate PR for it.

@kubamracek
Copy link
Contributor

@swift-ci please test

@kubamracek
Copy link
Contributor

FAIL: Swift(macosx-x86_64) :: embedded/avr/testStdlibFunctioning.swift (9815 of 18049)
******************** TEST 'Swift(macosx-x86_64) :: embedded/avr/testStdlibFunctioning.swift' FAILED ********************
Script:
--
: 'RUN: at line 1';   ...
--
Exit Code: 134

Command Output (stderr):
--
<unknown>:0: warning: no target microcontroller specified on command line, cannot link standard libraries, please pass -mmcu=<mcu name>
Assertion failed: (castIsValid(op, S, Ty) && "Invalid cast!"), function Create, file Instructions.cpp, line 3427.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: ...
1.	Apple Swift version 6.0-dev (LLVM 4ee06774aa7ace9, Swift 42aa1164f579cce)
2.	Compiling with effective version 4.1.50
3.	While evaluating request IRGenRequest(IR Generation for module testStdlibFunctioning)
4.	While emitting IR SIL function "@swift_once".

I suggest we change the test back to only do -typecheck instead of -emit-ir. And then work on fixing this separately.

@kubamracek kubamracek marked this pull request as ready for review July 17, 2024 04:51
@carlos4242
Copy link
Contributor Author

carlos4242 commented Jul 17, 2024 via email

@carlos4242
Copy link
Contributor Author

FAIL: Swift(macosx-x86_64) :: embedded/avr/testStdlibFunctioning.swift (9815 of 18049)
******************** TEST 'Swift(macosx-x86_64) :: embedded/avr/testStdlibFunctioning.swift' FAILED ********************
Script:
--
: 'RUN: at line 1';   ...
--
Exit Code: 134

Command Output (stderr):
--
<unknown>:0: warning: no target microcontroller specified on command line, cannot link standard libraries, please pass -mmcu=<mcu name>
Assertion failed: (castIsValid(op, S, Ty) && "Invalid cast!"), function Create, file Instructions.cpp, line 3427.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: ...
1.	Apple Swift version 6.0-dev (LLVM 4ee06774aa7ace9, Swift 42aa1164f579cce)
2.	Compiling with effective version 4.1.50
3.	While evaluating request IRGenRequest(IR Generation for module testStdlibFunctioning)
4.	While emitting IR SIL function "@swift_once".

I suggest we change the test back to only do -typecheck instead of -emit-ir. And then work on fixing this separately.

OK done.

@kubamracek
Copy link
Contributor

@swift-ci please test

Copy link
Contributor

@phausler phausler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall the standard library side looks pretty good to me. The one place where I am not sure 100% is the string side of things - I will try to drudge up some additional reviews for ya

@kubamracek
Copy link
Contributor

@swift-ci please test Linux platform

@ahoppen ahoppen removed their request for review July 17, 2024 19:42
@carlos4242
Copy link
Contributor Author

@swift-ci please test Linux platform

I must admit I can't understand the logs from the linux test run failure. Are they real test failures do you think?

@phausler
Copy link
Contributor

@swift-ci please test linux

@phausler
Copy link
Contributor

The linux failures don't seem to be something that would be caused by this set of changes from what I can tell. It did not introduce a --parallel option, nor did it remove that.

@carlos4242
Copy link
Contributor Author

carlos4242 commented Jul 17, 2024 via email

@kubamracek
Copy link
Contributor

@swift-ci please test Linux platform

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me.

@DougGregor
Copy link
Member

Lots of green checks, I'll go ahead and merge

@DougGregor DougGregor merged commit ac0f574 into swiftlang:main Jul 18, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants