Skip to content

[AVR] Fix IRGen function emission to respect LLVM DataLayout program address space. #78810

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

carlos4242
Copy link
Contributor

[AVR] Fix IRGen function emission to respect LLVM DataLayout program address space.

[IRGen/LLVM] Fix broken IRGen emission not respecting LLVM Datalayout

IRGen crashes when attempting to lower even the most basic Swift programs on AVR. I have encountered many problems similar to this over the years and it's always to do with program address space. (See forum discussion: https://forums.swift.org/t/avr-irgen-traps-on-lowering-invalid-cast-assertion-thrown-by-llvm/76883).

This seems to work for at least basic IRGen functionality on AVR. We may need more PRs in future for AVR but this is a start at least.

This is the approach I took on my fork, changing the structure of IRGenModule a bit to separate out FunctionPtrTy, if this is too intrusive for other platforms, I'm happy to use another approach, whatever seems sensible. I'm not attached to any one approach.


Note: AVR uses pointers with a different address space for program (flash) memory on the microcontrollers to distinguish RAM from Flash memory. Because AVR uses 16-bit pointers, there are not enough addresses to keep the two types of memory separate otherwise. This is well established in LLVM now (it has been in mainstream LLVM for years now) and all other AVR front ends (Rust, C, TinyGo) depend on it.

Resolves #78380

@kubamracek @rauhul @phausler

Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

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

Seems harmless enough

@kubamracek
Copy link
Contributor

Could I ask for just a commit message adjustment? It currently implies this is a test-only change, but in reality it's not.

@carlos4242
Copy link
Contributor Author

Sure thing. I'll fix that. Cheers!

@carlos4242 carlos4242 force-pushed the avr-irgen-functions-respect-llvm-datalayout branch from eec7e74 to 7df40ff Compare January 25, 2025 11:31
@carlos4242 carlos4242 changed the title [AVR] Added test for basic IRGen function. [AVR] Fix IRGen function emission to respect LLVM DataLayout program address space. Jan 25, 2025
@carlos4242
Copy link
Contributor Author

Apologies for that @kubamracek ... a mistake with my commit messages when doing an interactive rebase to squash commits. Now it's a bit easier to see what it's doing properly.

@kubamracek
Copy link
Contributor

@swift-ci please test

…ding with the correct fixups for program memory on AVR.
@carlos4242
Copy link
Contributor Author

While waiting for review I came up with another unit test and pushed it, IR emission is working properly right down to object files/assembly, but it requires -O... that's OK because you really can't use non-optimised microcontroller code, it won't fit on the chip!

@carlos4242
Copy link
Contributor Author

@kubamracek @eeckstein hi guys, I'm not sure what state this is in? Is it fit to merge? If you'd rather pick it up after WWDC I understand. I left it for a few months so people could give their feedback but I'm feeling like I might need to do something to move it on?

Carl

@eeckstein
Copy link
Contributor

@kubamracek any objections merging this PR?

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.

[AVR] swift frontend crashes when trying to emit LLVM IR
4 participants