Skip to content

[AVR] Initial AVR support #74818

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
merged 1 commit into from
Jul 8, 2024
Merged

Conversation

carlos4242
Copy link
Contributor

This adds support for the AVR target to Swift. It is a preliminary draft PR for discussion.
@rauhul @phausler @kubamracek

Note: names like "Embedded" I created many years ago, when it was only us using this code. This would probably not make sense now, but it gives you ideas of what we have been using and is a kick off point for discussion.

(See also #74817)

@harlanhaskins
Copy link
Contributor

Would AVR need its own platform name or would the architecture conditional suffice? Embedded Swift is usually platform-agnostic (if we managed to compile a tiny Linux kernel for AVR, we would write Swift for this OS via #if os(Linux) && arch(avr))

@rauhul
Copy link
Member

rauhul commented Jun 29, 2024

Would AVR need its own platform name or would the architecture conditional suffice? Embedded Swift is usually platform-agnostic (if we managed to compile a tiny Linux kernel for AVR, we would write Swift for this OS via #if os(Linux) && arch(avr))

It should not, the standard conditionals should suffice. I think when building for these targets we will use one or more triple from the following set: avr-{unknown, apple, atmel, swift}-none-{elf, macho?}.

The vendor part will be interesting to figure out.

@carlos4242
Copy link
Contributor Author

carlos4242 commented Jun 30, 2024

@rauhul @harlanhaskins ... they sound like good ideas.

Would AVR need its own platform name or would the architecture conditional suffice? Embedded Swift is usually platform-agnostic (if we managed to compile a tiny Linux kernel for AVR, we would write Swift for this OS via #if os(Linux) && arch(avr))

It should not, the standard conditionals should suffice. I think when building for these targets we will use one or more triple from the following set: avr-{unknown, apple, atmel, swift}-none-{elf, macho?}.

The vendor part will be interesting to figure out.

To be honest, when I made these changes, I was partly copying other people's PRs where they were adding architectures. All I really wanted was to be able to add -target avr ... to the swiftc command line and have it work, i.e. still compile at least as far as bitcode, passing the target triple down to llvm. I think the llvm back end probably pretty much ignores everything after the architecture, because it's not relevant on that architecture, you're always building bare metal.

I'd be completely happy with avr-none-none or avr-unknown-unknown as target triples. What do you guys think are the minimal changes to get that to work? I can strip out everything else.

(Subarch and vendor are irrelevant on AVR.)

@harlanhaskins
Copy link
Contributor

I think all you really need is to make sure the compiler recognizes AVR as an architecture, and that AVR implies 16 bit. Then you just need to tell the embedded seam routines to build a swift standard library for 16 bit AVR, and then clients will just be able to compile for AVR, import the standard library, and manually link/search for Arduino headers

@harlanhaskins
Copy link
Contributor

If we did want specific support for Arduino as a named platform, along with searching for those headers and writing an overlay, for example, that could be done. I suspect that should be done outside of this PR though, and I'm curious if @rauhul has more opinions on how to do that.

@carlos4242
Copy link
Contributor Author

carlos4242 commented Jul 1, 2024

I think all you really need is to make sure the compiler recognizes AVR as an architecture, and that AVR implies 16 bit. Then you just need to tell the embedded seam routines to build a swift standard library for 16 bit AVR, and then clients will just be able to compile for AVR, import the standard library, and manually link/search for Arduino headers

Actually we are quite separate from the Arduino platform. Most code written for our platform doesn’t use Arduino headers or libraries at all. We quite often use avr libc which is a much older and more widely used set of libraries. But again it’s really optional. A lot of us are writing pure Swift code with our own pure Swift hardware libraries. The only thing that might be needed to build valid binaries are some custom linker scripts. But that’s only if we want to go as far as having Swift do the full link. For now I think a great first step would be just compiling object files. In fact probably just getting to the Bitcode stage would be enough. In our own tooling we only ever use Swift to compile to Bitcode and then let other tools take over from there.

@kubamracek
Copy link
Contributor

I suggest we define a first goal to just accept AVR as a target CPU in the compiler and be able to start the compilation. For that we will need some parts of this PR, plus some more (hopefully trivial) changes. I think we should drop from this PR everything that's not the minimal set of changes to get there. (Or open a new PR and leave this as a reference.)

Specifically, I think we'll need:

  • SwiftSetIfArchBitness.cmake change from this PR
  • SupportedConditionalCompilationArches change in LangOptions.cp from this PR
  • Enabling the AVR LLVM target in standard LLVM builds for Swift in build-presets.ini (look for llvm-targets-to-build)
  • A lit test that compiles something trivial in -parse-stdlib mode for AVR.
  • Optionally, if this this ends up needing a lot more work then let's make that a separate follow PR:
    • Enabling the build of the embedded stdlib for AVR in stdlib/public/CMakeLists.txt + a lit test compiling in non -parse-stdlib mode

@carlos4242
Copy link
Contributor Author

I'm a little bit guessing with build-presets.ini as it's a bit opaque to me. Feel free to tweak anything. I didn't have time to write tests yet as it's after midnight and I have to get up for work tomorrow. I'll try to pick it up again tomorrow or Thursday.

@kubamracek
Copy link
Contributor

@swift-ci please smoke test

@kubamracek
Copy link
Contributor

Actually, we'll also need to fix the default set of llvm-targets-to-build in utils/build_swift/build_swift/driver_arguments.py.

@carlos4242
Copy link
Contributor Author

I'd appreciate some help/advice how to fix the broken test? https://ci.swift.org/job/swift-PR-macos-smoke-test/14213/consoleFull#-1588501433d6fdb6cb-f376-4f2e-8bce-d31c7304698b

It looks like it's not emitting the expected fixit and is instead suggesting avr as a possible fixit, which I think is not what we would like (I don't mind if swift suggests arm, or x86 or riscv or avr... so long as these more rare architectures are further down the list). Could someone suggest how I might fix this please?

@rauhul
Copy link
Member

rauhul commented Jul 3, 2024

I'd appreciate some help/advice how to fix the broken test? https://ci.swift.org/job/swift-PR-macos-smoke-test/14213/consoleFull#-1588501433d6fdb6cb-f376-4f2e-8bce-d31c7304698b

It looks like it's not emitting the expected fixit and is instead suggesting avr as a possible fixit, which I think is not what we would like (I don't mind if swift suggests arm, or x86 or riscv or avr... so long as these more rare architectures are further down the list). Could someone suggest how I might fix this please?

It's an interesting question, I'm not sure what architecture the fixit should suggest. It appears to suggest bad options in either case (arm or avr) and it seems like suggesting an arch based Levenshtein distance would be a much better UX.

@DougGregor do you happen to have opinions on behavior of the target conditional fixits with the addition of the AVR architecture? For reference the lit errors are below:

/Users/ec2-user/jenkins/workspace/swift-PR-macos-smoke-test/branch-main/swift/test/Parse/ConditionalCompilation/basicParseErrors.swift:90:10: error: unexpected note produced: did you mean 'avr'?
#if arch(leg) // expected-warning {{unknown architecture for build configuration 'arch'}} expected-note{{did you mean 'arm'?}} {{10-13=arm}}
         ^
/Users/ec2-user/jenkins/workspace/swift-PR-macos-smoke-test/branch-main/swift/test/Parse/ConditionalCompilation/basicParseErrors.swift:126:11: error: unexpected note produced: did you mean 'avr'?
#if !arch(tecture) && !os(ystem) && !_endian(ness)
          ^
/Users/ec2-user/jenkins/workspace/swift-PR-macos-smoke-test/branch-main/swift/test/Parse/ConditionalCompilation/basicParseErrors.swift:144:4: error: expected note not produced
// expected-note@-5 {{did you mean 'arm'?}} {{26-29=arm}}
~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/ec2-user/jenkins/workspace/swift-PR-macos-smoke-test/branch-main/swift/test/Parse/ConditionalCompilation/basicParseErrors.swift:145:23: error: incorrect message found
// expected-note@-6 {{did you mean 'i386'?}} {{26-29=i386}}
                      ^~~~~~~~~~~~~~~~~~~~
                      did you mean 'avr'?

@DougGregor
Copy link
Member

DougGregor commented Jul 3, 2024

It's an interesting question, I'm not sure what architecture the fixit should suggest. It appears to suggest bad options in either case (arm or avr) and it seems like suggesting an arch based Levenshtein distance would be a much better UX.

@DougGregor do you happen to have opinions on behavior of the target conditional fixits with the addition of the AVR architecture? For reference the lit errors are below:

It is using Levenshtein distance. I think the test here is unfortunate and the easiest thing to fix. The edit distance between leg and arm is 3, as it is with avr, which is why we're seeing the test failure here.

I recommend updating the test to be more realistic: replace leg and tecture with arn, which has an edit distance of 1 to arm and is therefore more robust against adding new architectures.

@rauhul
Copy link
Member

rauhul commented Jul 3, 2024

It's an interesting question, I'm not sure what architecture the fixit should suggest. It appears to suggest bad options in either case (arm or avr) and it seems like suggesting an arch based Levenshtein distance would be a much better UX.
@DougGregor do you happen to have opinions on behavior of the target conditional fixits with the addition of the AVR architecture? For reference the lit errors are below:

It is using Levenshtein distance. I think the test here is unfortunate and the easiest thing to fix. The edit distance between leg and arm is 3, as it is with avr, which is why we're seeing the test failure here.

I recommend updating the test to be more realistic: replace leg with arn, which has an edit distance of 1 and is therefore more robust against adding new architectures.

Serves me right for not reading the implementation before typing, thanks for the correction! I think updating the test with arn is a really good idea.

@kubamracek
Copy link
Contributor

@swift-ci please smoke test

@carlos4242
Copy link
Contributor Author

carlos4242 commented Jul 3, 2024 via email

@carlos4242
Copy link
Contributor Author

carlos4242 commented Jul 3, 2024 via email

@carlos4242
Copy link
Contributor Author

carlos4242 commented Jul 3, 2024 via email

@carlos4242
Copy link
Contributor Author

@rauhul @kubamracek I think I fixed the broken unit test, at least it works locally, I also added a very basic parse test that should check the architecture exists and a couple of basic features, modelled on wasm. And suggested a tiny documentation tweak. I hope this is useful.

@rauhul
Copy link
Member

rauhul commented Jul 5, 2024

@swift-ci please smoke test

@DougGregor
Copy link
Member

@swift-ci please smoke test

2 similar comments
@DougGregor
Copy link
Member

@swift-ci please smoke test

@DougGregor
Copy link
Member

@swift-ci please smoke test

@DougGregor
Copy link
Member

@swift-ci please smoke test Linux

@carlos4242
Copy link
Contributor Author

All tests passed do you want to review it now?

@kubamracek
Copy link
Contributor

Other than the small note, this looks good to me to go in. If you don't mind, could you wait with merging until let's say Tuesday to see if there's any more review comments from others (it's a Friday after a holiday in the US)?

@kubamracek
Copy link
Contributor

Oh and one more thing: Because we went back and forth on some of the changes, I'd suggest squashing the commits before merging and also expanding on the commit message to make it more clear that this isn't actually yet a meaningful AVR support, but just the first step towards it.

@carlos4242
Copy link
Contributor Author

carlos4242 commented Jul 6, 2024 via email

@carlos4242
Copy link
Contributor Author

carlos4242 commented Jul 6, 2024 via email

- Add simple support for the AVR architecture, as a supported conditional compilation value, and added to the default llvm targets to build.

(Later PRs will fix support for 16-bit pointers, which is broken in places, and any fixes needed to get the standard library to build.)

(Note: AVR as a target is expected to always be compiled with -enable-experimental-feature Embedded.)
@carlos4242 carlos4242 changed the title Simple AVR support [AVR] Initial AVR support Jul 6, 2024
@carlos4242
Copy link
Contributor Author

OK @kubamracek. I think I've addressed all your concerns. This seems like a good first PR now.

@carlos4242 carlos4242 requested a review from kubamracek July 6, 2024 17:25
@kubamracek
Copy link
Contributor

@swift-ci please test

Copy link
Member

@rauhul rauhul left a comment

Choose a reason for hiding this comment

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

Step 1 ✅

@carlos4242
Copy link
Contributor Author

Hi @kubamracek @rauhul should we tag anyone else who needs to review this? p.s. I don't think I have permission to merge it anyway, so someone else will have to do that when it's time.

@DougGregor
Copy link
Member

Let's go ahead and merge this. Thank you @carlos4242!

@DougGregor DougGregor merged commit fbc4e67 into swiftlang:main Jul 8, 2024
5 checks passed
@kubamracek
Copy link
Contributor

🎉

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.

6 participants