swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.3k stars 10.34k forks source link

[Platform request] Is there any plan to open-source the xsOS(Apple VisionOS) PlatformKind support in Swift compiler #68034

Open dreampiggy opened 1 year ago

dreampiggy commented 1 year ago

Motivation

Current Swift compiler on release/5.9.0 branch, does not contains any xrOS(Apple VisionOS) support code. However, in Swift compiler itself, it contains the support code for each platform for this syntax:

@available(xrOS, 1.0, *)
func foobar() {}

And it will encode the swift::PlatformKind into the serialized AST (.swiftmodule file)

Which means, if the open-source Swift compiler does not contains the support, when loading an Apple's provided swiftmodule (like libswiftFoundation.swiftmodule), will hit the assertion, even if we don't compile for that xrOS platform.

See: https://github.com/apple/swift/blob/d4ee7bffa1f9ff67b5ca717683a316f3a7acb425/lib/AST/PlatformKind.cpp#L104

Compiler assertion here:

PlatformKind
UNREACHABLE executed at /Users/foobar/29263/swift-source/swift/lib/AST/PlatformKind.cpp:104!
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
1.  Apple Swift version 5.9-dev
2.  Compiling with the current language version
3.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for module Demo)
4.  While silgen emitFunction SIL function "@$s3Demo13ColorResourceV23__derived_struct_equalsySbAC_ACtFZ".
 for '__derived_struct_equals(_:_:)' (in module 'Demo')
5.  While evaluating request TypeCheckFunctionBodyRequest(Demo.(file).ColorResource.__derived_struct_equals)
6.  While type-checking statement at [<invalid loc> - <invalid loc>]
7.  While type-checking statement at [<invalid loc> - <invalid loc>]
8.  While type-checking expression at [<invalid loc> - <invalid loc>]
9.  While type-checking-target starting at <invalid loc>
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           0x0000000109ccfec0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109cceed0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109cd054c SignalHandler(int) + 344
3  libsystem_platform.dylib 0x000000018a662a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018a633c28 pthread_kill + 288
5  libsystem_c.dylib        0x000000018a541ae8 abort + 180
6  swift-frontend           0x0000000109c22f14 llvm::install_out_of_memory_new_handler() + 0
7  swift-frontend           0x00000001063290a0 swift::targetPlatform(swift::LangOptions const&) + 0
8  swift-frontend           0x00000001060fc908 swift::DeclAttributes::getUnavailable(swift::ASTContext const&) const + 104
9  swift-frontend           0x0000000105d12718 swift::constraints::ConstraintSystem::isDeclUnavailable(swift::Decl const*, swift::constraints::ConstraintLocator*) const + 40
10 swift-frontend           0x0000000105c4569c (anonymous namespace)::favorCallOverloads(swift::ApplyExpr*, swift::constraints::ConstraintSystem&, llvm::function_ref<bool (swift::ValueDecl*, swift::Type)>, std::__1::function<bool (swift::ValueDecl*)>) + 480
11 swift-frontend           0x0000000105c45134 (anonymous namespace)::ConstraintOptimizer::walkToExprPre(swift::Expr*) + 416
12 swift-frontend           0x00000001060f3ad8 (anonymous namespace)::Traversal::doIt(swift::Expr*) + 44
13 swift-frontend           0x00000001060f3aa0 swift::Expr::walk(swift::ASTWalker&) + 32
14 swift-frontend           0x0000000105c36388 swift::constraints::ConstraintSystem::optimizeConstraints(swift::Expr*) + 1660
15 swift-frontend           0x0000000105c3344c swift::constraints::ConstraintSystem::generateConstraints(swift::Expr*, swift::DeclContext*, bool) + 216
16 swift-frontend           0x0000000105c32f18 swift::constraints::ConstraintSystem::generateConstraints(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) + 1360
17 swift-frontend           0x0000000105c8c8ec swift::constraints::ConstraintSystem::solveImpl(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) + 388
18 swift-frontend           0x0000000105c8c218 swift::constraints::ConstraintSystem::solve(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) + 136
19 swift-frontend           0x0000000105de3b08 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 304
20 swift-frontend           0x0000000105de3980 swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 184
21 swift-frontend           0x0000000105de3854 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::constraints::ContextualTypeInfo, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 84
22 swift-frontend           0x0000000105de50cc swift::TypeChecker::typeCheckCondition(swift::Expr*&, swift::DeclContext*) + 112
23 swift-frontend           0x0000000105ea8584 swift::TypeChecker::typeCheckStmtConditionElement(swift::StmtConditionElement&, bool&, swift::DeclContext*) + 340
24 swift-frontend           0x0000000105eafc2c typeCheckConditionForStatement(swift::LabeledConditionalStmt*, swift::DeclContext*) + 80
25 swift-frontend           0x0000000105eac8b8 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 1452
26 swift-frontend           0x0000000105eac2d8 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::Stmt>(swift::Stmt*&) + 136
27 swift-frontend           0x0000000105ea9888 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 172
28 swift-frontend           0x0000000105eac394 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 136
29 swift-frontend           0x0000000105eaadac bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
30 swift-frontend           0x0000000105eaa47c swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1012
31 swift-frontend           0x00000001062144e4 llvm::Expected<swift::TypeCheckFunctionBodyRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest>(swift::TypeCheckFunctionBodyRequest const&) + 284
32 swift-frontend           0x0000000106214314 llvm::Expected<swift::TypeCheckFunctionBodyRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckFunctionBodyRequest, (void*)0>(swift::TypeCheckFunctionBodyRequest const&) + 104
33 swift-frontend           0x000000010615fb50 swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType) + 36
34 swift-frontend           0x0000000105384cfc swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 580
35 swift-frontend           0x0000000105306c34 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 6268
36 swift-frontend           0x000000010530b4a8 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 4976
37 swift-frontend           0x00000001053d8380 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 132
38 swift-frontend           0x000000010530dd18 llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 380
39 swift-frontend           0x000000010530b6bc swift::performASTLowering(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) + 128
40 swift-frontend           0x0000000104df47e0 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1096
41 swift-frontend           0x0000000104e0349c withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
42 swift-frontend           0x0000000104df6310 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 620
43 swift-frontend           0x0000000104df5894 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 1832
44 swift-frontend           0x0000000104c62d50 swift::mainEntry(int, char const**) + 3440
45 dyld                     0x000000018a2dbf28 start + 2236

Solution

Provide the PlatformKind::xrOS support in Swift compiler, which at least should be able to TypeCheck or LoadAST from the swiftmodule provided by Apple

For linker (ld-64) support for xsOS, which is not needed because it's not part of open-source Swift toolchain.

Alternatives considered

Should we just treat this assertions as warning instead ? But I think the assertions is the basic ensure of the compiler's behavior before the final distribution to the developers.

IamYJLee commented 11 months ago

@dreampiggy When I build the code that's been tagged for the Swift 5.9 release and subsequently run the Swift compatibility test, I encounter the same error. Do you have any ongoing activities?

dreampiggy commented 11 months ago

I modify the Swift compiler source code and write the PlatformKind::visionOS for enum case 13, PlatformKind::visionSimulator for enum case 14, and compile the internal project successfully.

IamYJLee commented 11 months ago

I modify the Swift compiler source code and write the PlatformKind::visionOS for enum case 13, PlatformKind::visionSimulator for enum case 14, and compile the internal project successfully.

@dreampiggy I added PlatformKind::visionOS, PlatformKind::visionSimulator and handled it with the same process as the PlatformKind::none case. What do you think?

Errors like #67857 and #68034 no longer occur.

dreampiggy commented 11 months ago

@IamYJLee Another quick fix is to avoid the availability attr and availability expr, just fix the issue when compile for target (like iOS) with swiftmodule which contains visionOS in the AST.

@avaiable (iOS 10, visionOS 1, *)
class Foobar {}

=>

@avaiable (iOS 10, *)
class Foobar {}
if #available(iOS 10, visionOS 1, *) {}

=>

if #available(iOS 10, *) {}
IamYJLee commented 10 months ago

@IamYJLee Another quick fix is to avoid the availability attr and availability expr, just fix the issue when compile for target (like iOS) with swiftmodule which contains visionOS in the AST.

  • Available Atrribute
@avaiable (iOS 10, visionOS 1, *)
class Foobar {}

=>

@avaiable (iOS 10, *)
class Foobar {}
  • Available Expression (Swift inline function which embed AST of exprs)
if #available(iOS 10, visionOS 1, *) {}

=>

if #available(iOS 10, *) {}

Thank you so much.

I added PlatformKind::visionOS, PlatformKind::visionSimulator and handled it with the same process as the > PlatformKind::none

There is no problem in an arm64 host environment, but in an x86_64 host environment, another crash occurs in the swift compatibility test, so it is not the correct solution.

dreampiggy commented 10 months ago

@IamYJLee Any better idea about the fix before Apple release the full xros target support ? Which should be a big work beyond our effort.

We just use the open-source compiler on the AST consuming and static analysis domain. So this hack is suitable for us.

dreampiggy commented 8 months ago

Since the LLVM part is merged-in. I think we can re-call this issue.

https://github.com/llvm/llvm-project/pull/77707 https://github.com/llvm/llvm-project/pull/78373 https://github.com/llvm/llvm-project/pull/78392