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

[AVR] swift frontend crashes when trying to emit LLVM IR #78380

Open
carlos4242 opened this issue Dec 30, 2024 · 1 comment · May be fixed by #78810
Open

[AVR] swift frontend crashes when trying to emit LLVM IR #78380

carlos4242 opened this issue Dec 30, 2024 · 1 comment · May be fixed by #78810
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels

Comments

@carlos4242
Copy link
Contributor

Description

In attempting to move to a working swift compiler, able to produce AVR object files there are a number of obstacles. While the basic front end functions now support AVR and the standard library has been upgraded to support 16-bit platforms, a pre-requisite for Swift on AVR... we still have not managed to produce AVR object files.

There appear to be some (separate) issues with the LLVM back end for AVR being used in the current Swift trunk, which I will investigate separately after we have Swift reliably producing bitcode/llvm-IR on the AVR platform. Hopefully that will just resolve as more recent AVR bugfixes make their way through into the Swift llvm-project branches. We can re-assess later.

Until IRGen works for AVR, we will definitely never be able to produce object files for AVR.

At the moment, even the most basic files trap in IRGen, due to issues with invalid casts. The root cause is the Swift front end not respecting the LLVM program address space in the AVR back end data layout. On the AVR platform, regular RAM pointers live in address space 0 but function/program pointers all live in address space 1, which physically corresponds to flash memory instead of RAM on the AVR microcontroller chips... due to the limitations of 16-bit pointer addressing, flash memory and RAM cannot be separated just by address location, as they are on ARM Cortex M0 platforms for example. So I believe the proper fix for the Swift front end is to respect LLVM data layout program address space in various places.

Modifying it everywhere would be a large job and is probably not necessary, for example most metadata emission does not apply to embedded Swift.

Reproduction

// RUN: %swift-frontend -emit-ir %s -target avr-none-none-elf \
// RUN:   -wmo -enable-experimental-feature Embedded

import Swift

If the above file is saved as test.swift and attempted compile to IR with...

swift-frontend -emit-ir -target avr-none-none-elf -enable-experimental-feature Embedded -wmo test.swift

...the assertion is immediately seen.

Assertion failed: (castIsValid(op, S, Ty) && "Invalid cast!"), function Create, file Instructions.cpp, line 2974

Stack dump

0.	Program arguments: /Users/carl/Documents/Code/swift/build/Ninja-RelWithDebInfoAssert/swift-macosx-arm64/bin/swift-frontend -frontend -emit-ir a.swift -target avr-none-none-elf -disable-objc-interop -color-diagnostics -enable-experimental-feature Embedded -empty-abi-descriptor -resource-dir /Users/carl/Documents/Code/swift/build/Ninja-RelWithDebInfoAssert/swift-macosx-arm64/lib/swift -module-name a -in-process-plugin-server-path /Users/carl/Documents/Code/swift/build/Ninja-RelWithDebInfoAssert/swift-macosx-arm64/lib/swift/host/libSwiftInProcPluginServer.dylib -plugin-path /Users/carl/Documents/Code/swift/build/Ninja-RelWithDebInfoAssert/swift-macosx-arm64/lib/swift/host/plugins -plugin-path /Users/carl/Documents/Code/swift/build/Ninja-RelWithDebInfoAssert/swift-macosx-arm64/local/lib/swift/host/plugins -o -
1.	Swift version 6.2-dev (LLVM 9f6c3d784782c34, Swift 55189bae8e55169)
2.	Compiling with effective version 5.10
3.	While evaluating request IRGenRequest(IR Generation for module a)
4.	While emitting IR SIL function "@swift_once".
 for <<debugloc at "<compiler-generated>":0:0>>Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000106932f90 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001069314b4 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000106933608 SignalHandler(int) + 332
3  libsystem_platform.dylib 0x000000019e0db584 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000019e0aac20 pthread_kill + 288
5  libsystem_c.dylib        0x000000019dfb7a20 abort + 180
6  libsystem_c.dylib        0x000000019dfb6d10 err + 0
7  swift-frontend           0x000000010850701c llvm::CastInst::CreateZExtOrBitCast(llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::InsertPosition) (.cold.1) + 0
8  swift-frontend           0x0000000106760edc llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::InsertPosition) + 772
9  swift-frontend           0x0000000100ae3474 llvm::IRBuilderBase::CreateBitCast(llvm::Value*, llvm::Type*, llvm::Twine const&) + 124
10 swift-frontend           0x0000000100affc2c swift::irgen::IRGenFunction::coerceValue(llvm::Value*, llvm::Type*, llvm::DataLayout const&) + 180
11 swift-frontend           0x0000000100b0898c swift::irgen::emitForeignParameter(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::ForeignFunctionInfo, unsigned int, swift::SILType, swift::irgen::LoadableTypeInfo const&, swift::irgen::Explosion&, bool) + 764
12 swift-frontend           0x0000000100ce2890 (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 4072
13 swift-frontend           0x0000000100ce1324 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 1816
14 swift-frontend           0x0000000100b61a5c swift::irgen::IRGenerator::emitGlobalTopLevel(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>> const&) + 752
15 swift-frontend           0x0000000100c786b0 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 2212
16 swift-frontend           0x0000000100ce0784 swift::GeneratedModule swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)17>::callDerived<0ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul>) const + 176
17 swift-frontend           0x0000000100c818fc swift::IRGenRequest::OutputType swift::Evaluator::getResultUncached<swift::IRGenRequest, swift::IRGenRequest::OutputType swift::evaluateOrFatal<swift::IRGenRequest>(swift::Evaluator&, swift::IRGenRequest)::'lambda'()>(swift::IRGenRequest const&, swift::IRGenRequest::OutputType swift::evaluateOrFatal<swift::IRGenRequest>(swift::Evaluator&, swift::IRGenRequest)::'lambda'()) + 196
18 swift-frontend           0x0000000100c79768 swift::performIRGeneration(swift::ModuleDecl*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>, llvm::GlobalVariable**) + 1420
19 swift-frontend           0x00000001007254d0 generateIR(swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, llvm::GlobalVariable*&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>) + 272
20 swift-frontend           0x0000000100721b04 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 1336
21 swift-frontend           0x00000001007212b8 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 768
22 swift-frontend           0x000000010072db3c withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
23 swift-frontend           0x0000000100722c08 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 700
24 swift-frontend           0x0000000100722464 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2180
25 swift-frontend           0x00000001004d7384 swift::mainEntry(int, char const**) + 3104
26 dyld                     0x000000019dd220e0 start + 2360

Expected behavior

This file should compile to trivial IR with an empty main method body.

Environment

Swift version 6.2-dev (LLVM 9f6c3d784782c34, Swift 55189ba)

Additional information

Initially reported here:
https://forums.swift.org/t/avr-irgen-traps-on-lowering-invalid-cast-assertion-thrown-by-llvm/76883

@carlos4242 carlos4242 added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Dec 30, 2024
@carlos4242
Copy link
Contributor Author

This should be fixed by #78381

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels
Projects
None yet
1 participant