Skip to content
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

Figure out what's needed to support rustc #15

Closed
elfprince13 opened this issue Dec 10, 2020 · 16 comments
Closed

Figure out what's needed to support rustc #15

elfprince13 opened this issue Dec 10, 2020 · 16 comments
Labels
enhancement New feature or request help wanted Extra attention is needed wontfix This will not be worked on

Comments

@elfprince13
Copy link

Rust seems like an attractive language to support on TI Calculators (and other (e)Z80 platforms):

  • It's a "systems" language like C and C++
  • It benefits from many years of research in programming language design to enable writing code that is safer and more robust than similar C/C++ code.
  • There appears to be an active community for embedded rust.
  • rustc emits LLVM IR, so it seems like we should be able to try and hook things up!

Therefore, I'm opening this issue to try and get the discussion moving forward!

@tari
Copy link

tari commented Dec 10, 2020

It's documented in the rustc dev guide: https://rustc-dev-guide.rust-lang.org/building/new-target.html

@elfprince13
Copy link
Author

Thanks @tari!

@jacobly0 - do you have a thread somewhere tracking progress your backends, and what should be expected to work/not work? I'm assuming that if TI Planet's Project Builder now supports C++17 / clang, things are in a decent shape?

@adriweb
Copy link

adriweb commented Dec 10, 2020 via email

jacobly0 pushed a commit that referenced this issue Dec 11, 2020
CXXDeductionGuideDecl with a local typedef has its own copy of the
TypedefDecl with the CXXDeductionGuideDecl as the DeclContext of that
TypedefDecl.
```
      template <typename T> struct A {
        typedef T U;
        A(U, T);
      };
      A a{(int)0, (int)0};
```
Related discussion on cfe-dev:
http://lists.llvm.org/pipermail/cfe-dev/2020-November/067252.html

Without this fix, when we import the CXXDeductionGuideDecl (via
VisitFunctionDecl) then before creating the Decl we must import the
FunctionType. However, the first parameter's type is the afore mentioned
local typedef. So, we then start importing the TypedefDecl whose
DeclContext is the CXXDeductionGuideDecl itself. The infinite loop is
formed.
```
 #0 clang::ASTNodeImporter::VisitCXXDeductionGuideDecl(clang::CXXDeductionGuideDecl*) clang/lib/AST/ASTImporter.cpp:3543:0
 #1 clang::declvisitor::Base<std::add_pointer, clang::ASTNodeImporter, llvm::Expected<clang::Decl*> >::Visit(clang::Decl*) /home/egbomrt/WORK/llvm5/build/debug/tools/clang/include/clang/AST/DeclNodes.inc:405:0
 #2 clang::ASTImporter::ImportImpl(clang::Decl*) clang/lib/AST/ASTImporter.cpp:8038:0
 #3 clang::ASTImporter::Import(clang::Decl*) clang/lib/AST/ASTImporter.cpp:8200:0
 #4 clang::ASTImporter::ImportContext(clang::DeclContext*) clang/lib/AST/ASTImporter.cpp:8297:0
 #5 clang::ASTNodeImporter::ImportDeclContext(clang::Decl*, clang::DeclContext*&, clang::DeclContext*&) clang/lib/AST/ASTImporter.cpp:1852:0
 #6 clang::ASTNodeImporter::ImportDeclParts(clang::NamedDecl*, clang::DeclContext*&, clang::DeclContext*&, clang::DeclarationName&, clang::NamedDecl*&, clang::SourceLocation&) clang/lib/AST/ASTImporter.cpp:1628:0
 #7 clang::ASTNodeImporter::VisitTypedefNameDecl(clang::TypedefNameDecl*, bool) clang/lib/AST/ASTImporter.cpp:2419:0
 #8 clang::ASTNodeImporter::VisitTypedefDecl(clang::TypedefDecl*) clang/lib/AST/ASTImporter.cpp:2500:0
 #9 clang::declvisitor::Base<std::add_pointer, clang::ASTNodeImporter, llvm::Expected<clang::Decl*> >::Visit(clang::Decl*) /home/egbomrt/WORK/llvm5/build/debug/tools/clang/include/clang/AST/DeclNodes.inc:315:0
 #10 clang::ASTImporter::ImportImpl(clang::Decl*) clang/lib/AST/ASTImporter.cpp:8038:0
 #11 clang::ASTImporter::Import(clang::Decl*) clang/lib/AST/ASTImporter.cpp:8200:0
 #12 llvm::Expected<clang::TypedefNameDecl*> clang::ASTNodeImporter::import<clang::TypedefNameDecl>(clang::TypedefNameDecl*) clang/lib/AST/ASTImporter.cpp:165:0
 #13 clang::ASTNodeImporter::VisitTypedefType(clang::TypedefType const*) clang/lib/AST/ASTImporter.cpp:1304:0
 #14 clang::TypeVisitor<clang::ASTNodeImporter, llvm::Expected<clang::QualType> >::Visit(clang::Type const*) /home/egbomrt/WORK/llvm5/build/debug/tools/clang/include/clang/AST/TypeNodes.inc:74:0
 #15 clang::ASTImporter::Import(clang::QualType) clang/lib/AST/ASTImporter.cpp:8071:0
 #16 llvm::Expected<clang::QualType> clang::ASTNodeImporter::import<clang::QualType>(clang::QualType const&) clang/lib/AST/ASTImporter.cpp:179:0
 #17 clang::ASTNodeImporter::VisitFunctionProtoType(clang::FunctionProtoType const*) clang/lib/AST/ASTImporter.cpp:1244:0
 #18 clang::TypeVisitor<clang::ASTNodeImporter, llvm::Expected<clang::QualType> >::Visit(clang::Type const*) /home/egbomrt/WORK/llvm5/build/debug/tools/clang/include/clang/AST/TypeNodes.inc:47:0
 #19 clang::ASTImporter::Import(clang::QualType) clang/lib/AST/ASTImporter.cpp:8071:0
 #20 llvm::Expected<clang::QualType> clang::ASTNodeImporter::import<clang::QualType>(clang::QualType const&) clang/lib/AST/ASTImporter.cpp:179:0
 #21 clang::QualType clang::ASTNodeImporter::importChecked<clang::QualType>(llvm::Error&, clang::QualType const&) clang/lib/AST/ASTImporter.cpp:198:0
 #22 clang::ASTNodeImporter::VisitFunctionDecl(clang::FunctionDecl*) clang/lib/AST/ASTImporter.cpp:3313:0
 #23 clang::ASTNodeImporter::VisitCXXDeductionGuideDecl(clang::CXXDeductionGuideDecl*) clang/lib/AST/ASTImporter.cpp:3543:0
```

The fix is to first create the TypedefDecl and only then start to import
the DeclContext.
Basically, we could do this during the import of all other Decls (not
just for typedefs). But it seems, there is only one another AST
construct that has a similar cycle: a struct defined as a function
parameter:
```
int struct_in_proto(struct data_t{int a;int b;} *d);

```
In that case, however, we had decided to return simply with an error
back then because that seemed to be a very rare construct.

Differential Revision: https://reviews.llvm.org/D92209
jacobly0 pushed a commit that referenced this issue Feb 28, 2021
The Select insn in BPF is expensive as BPF backend
needs to resolve with conditionals.  This patch set
the getCmpSelInstrCost() to SCEVCheapExpansionBudget
for Select insn to prevent some Select insn related
optimizations.

This change is motivated during bcc code review for
   iovisor/bcc#3270
where IndVarSimplifyPass eventually caused generating
the following asm code:
  ;       for (i = 0; (i < VIRTIO_MAX_SGS) && (i < num); i++) {
      14:       16 05 40 00 00 00 00 00 if w5 == 0 goto +64 <LBB0_6>
      15:       bc 51 00 00 00 00 00 00 w1 = w5
      16:       04 01 00 00 ff ff ff ff w1 += -1
      17:       67 05 00 00 20 00 00 00 r5 <<= 32
      18:       77 05 00 00 20 00 00 00 r5 >>= 32
      19:       a6 01 01 00 05 00 00 00 if w1 < 5 goto +1 <LBB0_4>
      20:       b7 05 00 00 06 00 00 00 r5 = 6
  00000000000000a8 <LBB0_4>:
      21:       b7 02 00 00 00 00 00 00 r2 = 0
      22:       b7 01 00 00 00 00 00 00 r1 = 0
  ;       for (i = 0; (i < VIRTIO_MAX_SGS) && (i < num); i++) {
      23:       7b 1a e0 ff 00 00 00 00 *(u64 *)(r10 - 32) = r1
      24:       7b 5a c0 ff 00 00 00 00 *(u64 *)(r10 - 64) = r5
Note that insn #15 has w1 = w5 and w1 is refined later but r5(w5) is
eventually saved on stack at insn #24 for later use. This cause
later verifier failures.

With this change, IndVarSimplifyPass won't do the above
transformation any more.

Differential Revision: https://reviews.llvm.org/D97479
@mrk-its
Copy link

mrk-its commented May 27, 2021

You may try to compile rust code as described here: http://forum.6502.org/viewtopic.php?p=84048#p84048

6502 backend is very incomplete yet, but I was able to produce working executable this way. The trick is to compile rust code to llvm-ir first (.ll file) and then link it using your llvm fork.

@Serentty
Copy link

I was able to build a crate using Cargo and then use this method to have Clang compile and link all of the IR files. Unfortunately this means I still don't have access to the core library. There's also the issue that all I can figure out how to get is assembly in GAS syntax which I don't know of any Z80 assembler for. Still, it's fascinating to read through the generated code.

@elfprince13
Copy link
Author

@Serentty - which method did you try? The one linked by @tari sounds like it should be able to produce a cross-compiler that can handle core too?

@Serentty
Copy link

I tried that. I followed along with the changes made by @mrk-its to make a Rust compiler for the 6502 in his form, but for the Z80 instead. But unfortunately, I just ended up getting LLVM to segfault when I tried that. So I just wrote a custom JSON target specification (telling it that it was ARM, but changing all the data sizes to match the Z80), got it to generate IR, and then fed that into Clang.

@dtomvan
Copy link

dtomvan commented Aug 28, 2021

I tried compiling core from the rust stdlib for the ti-84: here

However, llc outputs this:

tools/llc: /usr/lib/libtinfo.so.6: no version information available (required by tools/llc)
LLVM ERROR: unable to translate instruction: ret (in function: _ZN4core6result19Result$LT$T$C$E$GT$6unwrap17h902768c9e7854d83E)
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.      Program arguments: tools/llc incremental/ti_84_poc-a6bfe995eb836153.ll -o ./incremental/ti_84_poc.s
1.      Running pass 'Function Pass Manager' on module 'incremental/ti_84_poc-a6bfe995eb836153.ll'.
2.      Running pass 'IRTranslator' on function '@"_ZN4core6result19Result$LT$T$C$E$GT$6unwrap17h902768c9e7854d83E"'
 #0 0x000000000166c653 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (tools/llc+0x166c653)
 #1 0x000000000166a57e llvm::sys::RunSignalHandlers() (tools/llc+0x166a57e)
 #2 0x000000000166caff SignalHandler(int) (tools/llc+0x166caff)
 #3 0x00007feb2d964870 __restore_rt (/usr/lib/libpthread.so.0+0x13870)
 #4 0x00007feb2d5c4d22 raise (/usr/lib/libc.so.6+0x3cd22)
 #5 0x00007feb2d5ae862 abort (/usr/lib/libc.so.6+0x26862)
 #6 0x00000000015e9919 (tools/llc+0x15e9919)
 #7 0x00000000015e9937 (tools/llc+0x15e9937)
 #8 0x00000000018c4a4a (tools/llc+0x18c4a4a)
 #9 0x00000000018d5e88 llvm::IRTranslator::runOnMachineFunction(llvm::MachineFunction&) (tools/llc+0x18d5e88)
#10 0x0000000000bbc26e llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (tools/llc+0xbbc26e)
#11 0x0000000000f68f5d llvm::FPPassManager::runOnFunction(llvm::Function&) (tools/llc+0xf68f5d)
#12 0x0000000000f6ecf3 llvm::FPPassManager::runOnModule(llvm::Module&) (tools/llc+0xf6ecf3)
#13 0x0000000000f6959f llvm::legacy::PassManagerImpl::run(llvm::Module&) (tools/llc+0xf6959f)
#14 0x000000000081e683 main (tools/llc+0x81e683)
#15 0x00007feb2d5afb25 __libc_start_main (/usr/lib/libc.so.6+0x27b25)
#16 0x0000000000819eae _start (tools/llc+0x819eae)
/tmp/tomvd/fsio/CHns2yiOPZ.sh: line 5: 67714 Aborted                 (core dumped) tools/llc $(find incremental -name ${CARGO_MAKE_PROJECT_NAME}-*.ll) -o ./incremental/${CARGO_MAKE_PROJECT_NAME}.s

@dtomvan
Copy link

dtomvan commented Aug 28, 2021

The person I forked this from to try out called it "a proof of concept" but then I start using the core library (I updated the compiler to the latest commit, see my fork) and it does not work properly. LLVM seems to not understand this ret instruction.

@adriweb
Copy link

adriweb commented Aug 28, 2021 via email

@dtomvan
Copy link

dtomvan commented Aug 28, 2021

Which compiler are you talking about exactly? rustc or llc

Edit: maybe related to this? rust-lang/rust#58567

@jacobly0
Copy link
Owner

That error means that the calling convention code didn't support however the function was trying to return.

@sbechet
Copy link

sbechet commented Sep 20, 2021

Hello,

maybe it can help as an example: rust-lang/rust#88321

@jacobly0
Copy link
Owner

Closing because I don't have plans to work on this, feel free to continue discussing or reopen if someone wants to have this issue assigned to them.

@jacobly0 jacobly0 added enhancement New feature or request wontfix This will not be worked on help wanted Extra attention is needed labels Jan 31, 2022
@TrevorCow
Copy link

I've been trying my hand at this and so far I've started with rust 1.65 as a base, since it has the most similar LLVM version to this project, and then merged this z80 branch into that one and rebuilt rust from src. All the steps work up until trying to transpile the LLVM bytecode into ASM (I think that's what it's doing? I'm still new to working with low-level LLVM forgive me if I'm misnaming or misunderstanding something). Wondering if I could get someone help that knows more about LLVM. It seems it's failing at translating a call instruction that has a non-primitive type.

Here is an example of the LLVM bytecode I'm trying to compile:

; ModuleID = 'calculator_snake-bf64b38161ffb5f6.3wbn5zfgu27wxvpa.rcgu.o'
source_filename = "3wbn5zfgu27wxvpa"
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
target triple = "ez80"

%"calculator_snake::ffi::types::ti_uint" = type { [3 x i8] }

@alloc1 = private unnamed_addr constant <{ [21 x i8] }> <{ [21 x i8] c"Running Screen Test \00" }>, align 1
@alloc2 = private unnamed_addr constant <{ [38 x i8] }> <{ [38 x i8] c"Press Enter to progress to next test \00" }>, align 1

; Function Attrs: nounwind
define dso_local void @main() unnamed_addr #0 {
start:
  %0 = tail call %"calculator_snake::ffi::types::ti_uint" @os_PutStrFull(ptr nonnull @alloc1) #1
  tail call void @os_NewLine() #1
  %1 = tail call %"calculator_snake::ffi::types::ti_uint" @os_PutStrFull(ptr nonnull @alloc2) #1
  br label %bb6

bb6:                                              ; preds = %bb6, %start
  %2 = tail call noundef zeroext i16 @os_GetKey() #1, !range !1
  %.not = icmp eq i16 %2, 5
  br i1 %.not, label %bb10, label %bb6

bb10:                                             ; preds = %bb6
  tail call void @os_ClrLCD() #1
  ret void
}

; Function Attrs: nounwind
declare dso_local %"calculator_snake::ffi::types::ti_uint" @os_PutStrFull(ptr) unnamed_addr #0

; Function Attrs: nounwind
declare dso_local void @os_NewLine() unnamed_addr #0

; Function Attrs: nounwind
declare dso_local noundef zeroext i16 @os_GetKey() unnamed_addr #0

; Function Attrs: nounwind
declare dso_local void @os_ClrLCD() unnamed_addr #0

attributes #0 = { nounwind "target-cpu"="generic" }
attributes #1 = { nounwind }

!llvm.module.flags = !{!0}

!0 = !{i32 1, !"LTOPostLink", i32 1}
!1 = !{i16 1, i16 252}

Changing the

  %0 = tail call %"calculator_snake::ffi::types::ti_uint" @os_PutStrFull(ptr nonnull @alloc1) #1
  tail call void @os_NewLine() #1
  %1 = tail call %"calculator_snake::ffi::types::ti_uint" @os_PutStrFull(ptr nonnull @alloc2) #1

to

  %0 = tail call i32 @os_PutStrFull(ptr nonnull @alloc1) #1
  tail call void @os_NewLine() #1
  %1 = tail call i32 @os_PutStrFull(ptr nonnull @alloc2) #1

Compiles successfully (of course being actually incorrect)

Here is the full stack trace of the failed compile:

LLVM ERROR: unable to translate instruction: call (in function: main)
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: C:/Dev/CLionProjects/rust_z80_llvm/build/x86_64-pc-windows-gnu/llvm/bin/llc ./atest_incremental/test_screen-6da35421ec774f08.ll -o ./atest_incremental/test_screen.s
1.      Running pass 'Function Pass Manager' on module './atest_incremental/test_screen-6da35421ec774f08.ll'.
2.      Running pass 'IRTranslator' on function '@main'
Exception Code: 0xC000001D
 #0 0x00007ff722058e70 HandleAbort.cold (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x2a08e70)
 #1 0x00007ffb89d2e0ac (C:\Windows\System32\msvcrt.dll+0x2e0ac)
 #2 0x00007ffb89d3255b (C:\Windows\System32\msvcrt.dll+0x3255b)
 #3 0x00007ff722058d65 llvm::report_fatal_error(llvm::Twine const&, bool) (.cold) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x2a08d65)
 #4 0x00007ff71f94917d reportTranslationError(llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::OptimizationRemarkEmitter&, llvm::OptimizationRemarkMissed&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x2f917d)
 #5 0x00007ff720550a35 llvm::IRTranslator::runOnMachineFunction(llvm::MachineFunction&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0xf00a35)
 #6 0x00007ff720e30cc8 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x17e0cc8)
 #7 0x00007ff7206a72aa llvm::FPPassManager::runOnFunction(llvm::Function&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x10572aa)
 #8 0x00007ff7206a6f53 llvm::FPPassManager::runOnModule(llvm::Module&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x1056f53)
 #9 0x00007ff72125c9c5 llvm::legacy::PassManagerImpl::run(llvm::Module&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x1c0c9c5)
#10 0x00007ff71f71975d compileModule(char**, llvm::LLVMContext&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0xc975d)
#11 0x00007ff721fbcf7a main (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x296cf7a)
#12 0x00007ff71f6512ee __tmainCRTStartup C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:268:8
#13 0x00007ff71f651406 .l_start C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:190:3
#14 0x00007ffb8adc257d (C:\Windows\System32\KERNEL32.DLL+0x1257d)
#15 0x00007ffb8baeaa58 (C:\Windows\SYSTEM32\ntdll.dll+0x5aa58)

Any help would be greatly appreciated!

@boondocklabs
Copy link

I've been trying my hand at this and so far I've started with rust 1.65 as a base, since it has the most similar LLVM version to this project, and then merged this z80 branch into that one and rebuilt rust from src. All the steps work up until trying to transpile the LLVM bytecode into ASM (I think that's what it's doing? I'm still new to working with low-level LLVM forgive me if I'm misnaming or misunderstanding something). Wondering if I could get someone help that knows more about LLVM. It seems it's failing at translating a call instruction that has a non-primitive type.

Here is an example of the LLVM bytecode I'm trying to compile:

; ModuleID = 'calculator_snake-bf64b38161ffb5f6.3wbn5zfgu27wxvpa.rcgu.o'
source_filename = "3wbn5zfgu27wxvpa"
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
target triple = "ez80"

%"calculator_snake::ffi::types::ti_uint" = type { [3 x i8] }

@alloc1 = private unnamed_addr constant <{ [21 x i8] }> <{ [21 x i8] c"Running Screen Test \00" }>, align 1
@alloc2 = private unnamed_addr constant <{ [38 x i8] }> <{ [38 x i8] c"Press Enter to progress to next test \00" }>, align 1

; Function Attrs: nounwind
define dso_local void @main() unnamed_addr #0 {
start:
  %0 = tail call %"calculator_snake::ffi::types::ti_uint" @os_PutStrFull(ptr nonnull @alloc1) #1
  tail call void @os_NewLine() #1
  %1 = tail call %"calculator_snake::ffi::types::ti_uint" @os_PutStrFull(ptr nonnull @alloc2) #1
  br label %bb6

bb6:                                              ; preds = %bb6, %start
  %2 = tail call noundef zeroext i16 @os_GetKey() #1, !range !1
  %.not = icmp eq i16 %2, 5
  br i1 %.not, label %bb10, label %bb6

bb10:                                             ; preds = %bb6
  tail call void @os_ClrLCD() #1
  ret void
}

; Function Attrs: nounwind
declare dso_local %"calculator_snake::ffi::types::ti_uint" @os_PutStrFull(ptr) unnamed_addr #0

; Function Attrs: nounwind
declare dso_local void @os_NewLine() unnamed_addr #0

; Function Attrs: nounwind
declare dso_local noundef zeroext i16 @os_GetKey() unnamed_addr #0

; Function Attrs: nounwind
declare dso_local void @os_ClrLCD() unnamed_addr #0

attributes #0 = { nounwind "target-cpu"="generic" }
attributes #1 = { nounwind }

!llvm.module.flags = !{!0}

!0 = !{i32 1, !"LTOPostLink", i32 1}
!1 = !{i16 1, i16 252}

Changing the

  %0 = tail call %"calculator_snake::ffi::types::ti_uint" @os_PutStrFull(ptr nonnull @alloc1) #1
  tail call void @os_NewLine() #1
  %1 = tail call %"calculator_snake::ffi::types::ti_uint" @os_PutStrFull(ptr nonnull @alloc2) #1

to

  %0 = tail call i32 @os_PutStrFull(ptr nonnull @alloc1) #1
  tail call void @os_NewLine() #1
  %1 = tail call i32 @os_PutStrFull(ptr nonnull @alloc2) #1

Compiles successfully (of course being actually incorrect)

Here is the full stack trace of the failed compile:

LLVM ERROR: unable to translate instruction: call (in function: main)
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: C:/Dev/CLionProjects/rust_z80_llvm/build/x86_64-pc-windows-gnu/llvm/bin/llc ./atest_incremental/test_screen-6da35421ec774f08.ll -o ./atest_incremental/test_screen.s
1.      Running pass 'Function Pass Manager' on module './atest_incremental/test_screen-6da35421ec774f08.ll'.
2.      Running pass 'IRTranslator' on function '@main'
Exception Code: 0xC000001D
 #0 0x00007ff722058e70 HandleAbort.cold (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x2a08e70)
 #1 0x00007ffb89d2e0ac (C:\Windows\System32\msvcrt.dll+0x2e0ac)
 #2 0x00007ffb89d3255b (C:\Windows\System32\msvcrt.dll+0x3255b)
 #3 0x00007ff722058d65 llvm::report_fatal_error(llvm::Twine const&, bool) (.cold) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x2a08d65)
 #4 0x00007ff71f94917d reportTranslationError(llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::OptimizationRemarkEmitter&, llvm::OptimizationRemarkMissed&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x2f917d)
 #5 0x00007ff720550a35 llvm::IRTranslator::runOnMachineFunction(llvm::MachineFunction&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0xf00a35)
 #6 0x00007ff720e30cc8 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x17e0cc8)
 #7 0x00007ff7206a72aa llvm::FPPassManager::runOnFunction(llvm::Function&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x10572aa)
 #8 0x00007ff7206a6f53 llvm::FPPassManager::runOnModule(llvm::Module&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x1056f53)
 #9 0x00007ff72125c9c5 llvm::legacy::PassManagerImpl::run(llvm::Module&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x1c0c9c5)
#10 0x00007ff71f71975d compileModule(char**, llvm::LLVMContext&) (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0xc975d)
#11 0x00007ff721fbcf7a main (C:\Dev\CLionProjects\rust_z80_llvm\build\x86_64-pc-windows-gnu\llvm\bin\llc.exe+0x296cf7a)
#12 0x00007ff71f6512ee __tmainCRTStartup C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:268:8
#13 0x00007ff71f651406 .l_start C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:190:3
#14 0x00007ffb8adc257d (C:\Windows\System32\KERNEL32.DLL+0x1257d)
#15 0x00007ffb8baeaa58 (C:\Windows\SYSTEM32\ntdll.dll+0x5aa58)

Any help would be greatly appreciated!

Have you made any progress on this? I pulled out my RC2014 today and went to see if there had been any progress on Rust support for the z80

jacobly0 pushed a commit that referenced this issue Aug 17, 2024
A case for this transformation, https://gcc.godbolt.org/z/nhYcWq1WE
Fold
  mov     w8, llvm#56952
  movk    w8, #15, lsl #16
  ldrb    w0, [x0, x8]
into
  add     x0, x0, 1036288
  ldrb    w0, [x0, 3704]

Only LDRBBroX is supported for the first time.
Fix llvm#71917

Note: This PR is try relanding the commit 32878c2 with fix crash for PR79756
  this crash is exposes when there is MOVKWi instruction in the head of a block,
but without MOVZWi
jacobly0 pushed a commit that referenced this issue Oct 9, 2024
…ext is not fully initialized (llvm#110481)

As this comment around target initialization implies:
```
  // This can be NULL if we don't know anything about the architecture or if
  // the target for an architecture isn't enabled in the llvm/clang that we
  // built
```

There are cases where we might fail to call `InitBuiltinTypes` when
creating the backing `ASTContext` for a `TypeSystemClang`. If that
happens, the builtins `QualType`s, e.g., `VoidPtrTy`/`IntTy`/etc., are
not initialized and dereferencing them as we do in
`GetBuiltinTypeForEncodingAndBitSize` (and other places) will lead to
nullptr-dereferences. Example backtrace:
```
(lldb) run
Assertion failed: (!isNull() && "Cannot retrieve a NULL type pointer"), function getCommonPtr, file Type.h, line 958.
Process 2680 stopped
* thread #15, name = '<lldb.process.internal-state(pid=2712)>', stop reason = hit program assert
    frame #4: 0x000000010cdf3cdc liblldb.20.0.0git.dylib`DWARFASTParserClang::ExtractIntFromFormValue(lldb_private::CompilerType const&, lldb_private::plugin::dwarf::DWARFFormValue const&) const (.cold.1) + 
liblldb.20.0.0git.dylib`DWARFASTParserClang::ParseObjCMethod(lldb_private::ObjCLanguage::MethodName const&, lldb_private::plugin::dwarf::DWARFDIE const&, lldb_private::CompilerType, ParsedDWARFTypeAttributes
, bool) (.cold.1):
->  0x10cdf3cdc <+0>:  stp    x29, x30, [sp, #-0x10]!
    0x10cdf3ce0 <+4>:  mov    x29, sp
    0x10cdf3ce4 <+8>:  adrp   x0, 545
    0x10cdf3ce8 <+12>: add    x0, x0, #0xa25 ; "ParseObjCMethod"
Target 0: (lldb) stopped.
(lldb) bt
* thread #15, name = '<lldb.process.internal-state(pid=2712)>', stop reason = hit program assert
    frame #0: 0x0000000180d08600 libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x0000000180d40f50 libsystem_pthread.dylib`pthread_kill + 288
    frame #2: 0x0000000180c4d908 libsystem_c.dylib`abort + 128
    frame #3: 0x0000000180c4cc1c libsystem_c.dylib`__assert_rtn + 284
  * frame #4: 0x000000010cdf3cdc liblldb.20.0.0git.dylib`DWARFASTParserClang::ExtractIntFromFormValue(lldb_private::CompilerType const&, lldb_private::plugin::dwarf::DWARFFormValue const&) const (.cold.1) + 
    frame #5: 0x0000000109d30acc liblldb.20.0.0git.dylib`lldb_private::TypeSystemClang::GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding, unsigned long) + 1188
    frame #6: 0x0000000109aaaed4 liblldb.20.0.0git.dylib`DynamicLoaderMacOS::NotifyBreakpointHit(void*, lldb_private::StoppointCallbackContext*, unsigned long long, unsigned long long) + 384
```

This patch adds a one-time user-visible warning for when we fail to
initialize the AST to indicate that initialization went wrong for the
given target. Additionally, we add checks for whether one of the
`ASTContext` `QualType`s is invalid before dereferencing any builtin
types.

The warning would look as follows:
```
(lldb) target create "a.out"
Current executable set to 'a.out' (arm64).
(lldb) b main
warning: Failed to initialize builtin ASTContext types for target 'some-unknown-triple'. Printing variables may behave unexpectedly.
Breakpoint 1: where = a.out`main + 8 at stepping.cpp:5:14, address = 0x0000000100003f90
```

rdar://134869779
jacobly0 pushed a commit that referenced this issue Nov 20, 2024
…onger cause a crash (llvm#116569)

This PR fixes a bug introduced by llvm#110199, which causes any half float
argument to crash the compiler on MIPS64.

Currently compiling this bit of code with `llc -mtriple=mips64`: 
```
define void @half_args(half %a) nounwind {
entry:
        ret void
}
```

Crashes with the following log:
```
LLVM ERROR: unable to allocate function argument #0
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: llc -mtriple=mips64
1.	Running pass 'Function Pass Manager' on module '<stdin>'.
2.	Running pass 'MIPS DAG->DAG Pattern Instruction Selection' on function '@half_args'
 #0 0x000055a3a4013df8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x32d0df8)
 #1 0x000055a3a401199e llvm::sys::RunSignalHandlers() (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x32ce99e)
 #2 0x000055a3a40144a8 SignalHandler(int) Signals.cpp:0:0
 #3 0x00007f00bde558c0 __restore_rt libc_sigaction.c:0:0
 #4 0x00007f00bdea462c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x00007f00bde55822 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #6 0x00007f00bde3e4af abort ./stdlib/abort.c:81:7
 #7 0x000055a3a3f80e3c llvm::report_fatal_error(llvm::Twine const&, bool) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x323de3c)
 #8 0x000055a3a2e20dfa (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x20dddfa)
 #9 0x000055a3a2a34e20 llvm::MipsTargetLowering::LowerFormalArguments(llvm::SDValue, unsigned int, bool, llvm::SmallVectorImpl<llvm::ISD::InputArg> const&, llvm::SDLoc const&, llvm::SelectionDAG&, llvm::SmallVectorImpl<llvm::SDValue>&) const MipsISelLowering.cpp:0:0
#10 0x000055a3a3d896a9 llvm::SelectionDAGISel::LowerArguments(llvm::Function const&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30466a9)
#11 0x000055a3a3e0b3ec llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30c83ec)
#12 0x000055a3a3e09e21 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30c6e21)
#13 0x000055a3a2aae1ca llvm::MipsDAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) MipsISelDAGToDAG.cpp:0:0
#14 0x000055a3a3e07706 llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30c4706)
#15 0x000055a3a3051ed6 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x230eed6)
#16 0x000055a3a35a3ec9 llvm::FPPassManager::runOnFunction(llvm::Function&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x2860ec9)
#17 0x000055a3a35ac3b2 llvm::FPPassManager::runOnModule(llvm::Module&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x28693b2)
#18 0x000055a3a35a499c llvm::legacy::PassManagerImpl::run(llvm::Module&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x286199c)
#19 0x000055a3a262abbb main (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x18e7bbb)
#20 0x00007f00bde3fc4c __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#21 0x00007f00bde3fd05 call_init ./csu/../csu/libc-start.c:128:20
#22 0x00007f00bde3fd05 __libc_start_main@GLIBC_2.2.5 ./csu/../csu/libc-start.c:347:5
#23 0x000055a3a2624921 _start /builddir/glibc-2.39/csu/../sysdeps/x86_64/start.S:117:0
```

This is caused by the fact that after the change, `f16`s are no longer
lowered as `f32`s in calls.

Two possible fixes are available:
- Update calling conventions to properly support passing `f16` as
integers.
- Update `useFPRegsForHalfType()` to return `true` so that `f16` are
still kept in `f32` registers, as before llvm#110199.

This PR implements the first solution to not introduce any more ABI
changes as llvm#110199 already did.

As of what is the correct ABI for halfs, I don't think there is a
correct answer. GCC doesn't support halfs on MIPS, and I couldn't find
any information on old MIPS ABI manuals either.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

10 participants