-
Notifications
You must be signed in to change notification settings - Fork 14
[WIP] M1 #2
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
base: main
Are you sure you want to change the base?
[WIP] M1 #2
Conversation
|
Hi @zero9178 thank you for the project again! I tried one more time with your fix of #1 , there is still one problem related with Mach-O codegen: Bitcode (although this may not be the issue) |
|
Commenting out both |
|
That assertion seems very concerning and might need some changes in either LLVM or the general approach of how the GC plugin works. The linker errors are probably due to the use of some #ifdef __APPLE__
#define MANGLING "_"
#else
#define MANGLING ""
#endif
#define BUILTIN(name, symbol, _, Type, ...) extern Type name asm(MANGLING symbol); |
|
I have posted https://reviews.llvm.org/D132708 to fix the first assert. |
|
I've been thinking quite a bit about the testability of this PR and how to best review it once it's not WIP anymore and I got some questions and possible suggestions. This PR tries to port two things at the moment. First, it adds the Darwin toolchain which should allow setting target specific options and enable linking executables via calls to ld64, and second, it ports the LLVM codegen to work on Arm. Since I am assuming you're working on an Arm Mac machine, is it possible (via Rosetta or so) to split the Darwin toolchain into a separate reviewable and testable patch so that x86_64 Mac would work? The codegen support for arm64 could then be added later in a separate PR. Luckily it's ABI is not a lot different to Linuxs arm ABI making it easier to collaborate on since I don't have any Mac machines to work with. |
This patch has landed in LLVM now and I have also bumped the version to build Pylir with (and that is used in CI) to include it. |
|
After a few fixes it now is able to generate object files without problem. However, the generated Mach-O files cannot run, because the Apple DYLD fails at the |
|
generated mach-o for just one line |
|
Can you try if the Additionally you could then try compiling a python source file to an object file with |
Yup, it fails for the same error |
|
My next guess would be something to do with PIC or similar. Clang also defaults to PIC [0] so possibly this is required by the platform (I haven't checked for Darwin, but Windows eg. mandates it as well). I don't think that will fix the issue however, as the cmake script currently always passes |
|
Finally found the culprit, quoting stack overflow: https://stackoverflow.com/questions/73382860/why-cant-i-assemble-absolute-addresses-in-the-text-section-on-arm64-macos/73382861#73382861
Only need one line of change: Note this is likely a solution that sacrifices some binary safety. |
now running on macOS M1!
| #elifdef __APPLE__ | ||
| # define PYLIR_WEAK_VAR(variable, alternative) \ | ||
| variable __attribute__((weak)); \ | ||
| __asm__(".set _" #variable ", _" #alternative) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't know if this is indeed the right way to do this...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole weak attribute thing is a bit of a hack in case one wants to use LTO. I don't think LTO is that worthwhile of a use case however, so its no problem if this does not work.
Are there any tests that actually fail if this were to be a noop?
src/pylir/LLVM/PylirGC.cpp
Outdated
|
|
||
| for (auto& iter : callSiteInfos) | ||
| { | ||
| os.emitValueToAlignment(4); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that is needed, since the code that reads this structure uses memcpy to copy these into aligned memory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this line caused errors from the linker, I think Apple ARM64 cannot do unaligned access at all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh I see. So the linker can't handle relocations if they're not aligned properly. I did not consider that and only thought it was about the reading code (which circumvents that restriction via memcpy).
I am wondering now however why the value 4? I'd expect it to require alignment equal to pointerSize.
The code reading the stackmap: https://github.com/zero9178/Pylir/blob/6ec52b054ef4960931ead397557e9c31d80c214d/src/pylir/Runtime/Stack.cpp#L73
will also need adjustments to align curr to alignof(void*) before reading programCounter.
I hope/am guessing that is the reason the Linux builds failed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ARM64 is 4 byte aligned so I just used 4 lol, should be the current platform alignment.
I don't currently have b/w to look at the stackmap reader though, have tons of stuff on hand
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries! I think I should be able to push to your branch as well so I'll just do it once I got time as well to make sure there are no regressions so you can continue working on the mac specifics.
|
Now that the thing is working, next step is to clean this mess up and possibly set up CI |
|
Current lit-test result: |
|
Amazing work thank you! |
|
A few more things after looking at the |
|
bbd50cb should fix the tests failing due to X86 not being compiled into your LLVM |
# Conflicts: # src/pylir/LLVM/PylirGC.cpp
|
Hello @ProfFan! I recently did some progress in this area and added support for MacOS that at least works on x86 machines here 110d2e3 If you have time, I'd be curiouss if all these changes are enough for the compiler to work on the M1 chip. |
|
There is a linking error with Catch2 and fmtlib that I haven't got time to debug: |
|
That is a very odd and confusing error. Especially since it is about fmt version 9 (based on the namespace), when the repo currently vendors version 7.1.3. Nevertheless, thank you for checking! |
|
Maybe it's because your CMake did not specify the vendor version and it is finding my system lib? |
No description provided.