Skip to content

Assertion failure while emitting a partial apply forwarder on arm64e #75742

@asl

Description

@asl

Description

It seems that partial apply emission forwarder is getting confused by ptrauth wrapper globals. In particular, *FunctionPointer::getFunctionType() does the following:

llvm::FunctionType *FunctionPointer::getFunctionType() const {
  // Static async function pointers can read the type off the secondary value
  // (the function definition.
  if (SecondaryValue) {
    assert(kind == FunctionPointer::Kind::AsyncFunctionPointer);
    return cast<llvm::Function>(SecondaryValue)->getFunctionType();
  }

  if (awaitSignature) {
    return cast<llvm::FunctionType>(awaitSignature);
  }

  // Read the function type off the global or else from the Signature.
  if (auto *constant = dyn_cast<llvm::Constant>(Value)) {
    auto *gv = dyn_cast<llvm::GlobalValue>(Value);
    if (!gv) {
      return Sig.getType();
    }

    if (useSignature) { // Because of various casting (e.g thin_to_thick) the
                      // signature of the function Value might mismatch
                      // (e.g no context argument).
      return Sig.getType();
    }

    return cast<llvm::FunctionType>(gv->getValueType());
  }

  return Sig.getType();
}

However, in this particular case the pointer is created to ptrauth wrapper global:

@afun2.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @afun2, i32 0, i64 0, i64 55253 }, section "llvm.ptrauth", align 8

and the cast fails.

Do we want to peel off the wrapper globals? I guess the same problem will be with ptrauth constant expressions used intead of these in LLVM mainline.

Reproduction

import Builtin
import Swift

sil @afun2 : $@convention(thin) (Int) -> (@error Error)

sil @convert_thin_test2 : $@convention(thin) (Int) -> @callee_guaranteed () -> (@error Error) {
bb(%0 : $Int):
  %f = function_ref @afun2 : $@convention(thin) (Int) -> (@error Error)
  %c = convert_function %f : $@convention(thin) (Int) -> (@error Error) to $@convention(thin) @noescape (Int) -> (@error Error)
  %pa = partial_apply [callee_guaranteed] %c(%0) : $@convention(thin) @noescape (Int) -> (@error Error)
  return %pa : $@callee_guaranteed () -> (@error Error)
}

Compile like:

swift-frontend  -sdk '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.2.sdk'  repro.sil -o - -emit-ir -Xllvm -sil-disable-pass=OnoneSimplification -target arm64e-apple-ios11.0

Stack dump

Assertion failed: (isa<To>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file Casting.h, line 578.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: ./swift-frontend -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.2.sdk repro.sil -o repro.ll -emit-ir -Xllvm -sil-disable-pass=OnoneSimplification -target arm64e-apple-ios11.0
1.	Swift version 6.0-dev (LLVM 73a346c9caf32cc, Swift f5a6fa612ffd7b7)
2.	Compiling with effective version 5.10
3.	While evaluating request IRGenRequest(IR Generation for module repro)
4.	While emitting IR SIL function "@convert_thin_test2".
 for repro.sil:11:6
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           0x0000000107d16750 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107d14f40 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107d16d98 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001a337aa24 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a334bc28 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a3259ae8 abort + 180
6  libsystem_c.dylib        0x00000001a3258e44 err + 0
7  swift-frontend           0x0000000107e01ae0 swift::irgen::FunctionPointer::getFunctionType() const (.cold.6) + 0
8  swift-frontend           0x0000000102d8d10c swift::irgen::PointerAuthInfo::getCorrespondingCodeAuthInfo() const + 0
9  swift-frontend           0x0000000102d8dec8 swift::irgen::IRBuilder::CreateCallOrInvoke(swift::irgen::FunctionPointer const&, llvm::ArrayRef<llvm::Value*>, llvm::BasicBlock*, llvm::BasicBlock*) + 220
10 swift-frontend           0x0000000102d887e8 swift::irgen::IRBuilder::CreateCall(swift::irgen::FunctionPointer const&, llvm::ArrayRef<llvm::Value*>) + 64
11 swift-frontend           0x0000000102e43f08 emitPartialApplicationForwarder(swift::irgen::IRGenModule&, std::__1::optional<swift::irgen::FunctionPointer> const&, bool, swift::irgen::Signature const&, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::SubstitutionMap, swift::irgen::HeapLayout const*, llvm::ArrayRef<swift::ParameterConvention>) + 7348
12 swift-frontend           0x0000000102e41804 swift::irgen::emitFunctionPartialApplication(swift::irgen::IRGenFunction&, swift::SILFunction&, swift::irgen::FunctionPointer const&, llvm::Value*, swift::irgen::Explosion&, llvm::ArrayRef<swift::SILParameterInfo>, swift::SubstitutionMap, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::irgen::Explosion&, bool) + 3620
13 swift-frontend           0x0000000102f54f60 (anonymous namespace)::IRGenSILFunction::visitSILBasicBlock(swift::SILBasicBlock*) + 69540
14 swift-frontend           0x0000000102f43078 (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 8476
15 swift-frontend           0x0000000102f409e0 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 1548
16 swift-frontend           0x0000000102de9154 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
17 swift-frontend           0x0000000102ef5f58 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 2164
18 swift-frontend           0x0000000102f3ff4c 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
19 swift-frontend           0x0000000102efe4b8 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
20 swift-frontend           0x0000000102ef6fb0 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**) + 1416
21 swift-frontend           0x0000000102a0c53c 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
22 swift-frontend           0x0000000102a08f8c 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*) + 1376
23 swift-frontend           0x0000000102a08638 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1216
24 swift-frontend           0x0000000102a1443c withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
25 swift-frontend           0x0000000102a0a2bc performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 700
26 swift-frontend           0x0000000102a099d0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2404
27 swift-frontend           0x00000001027f7cc8 swift::mainEntry(int, char const**) + 3144
28 dyld                     0x00000001a2ff3f28 start + 2236
fish: Job 1, './swift-frontend  -sdk '/Applic…' terminated by signal SIGABRT (Abort)

Expected behavior

Compilation succeeded

Environment

Swift version 6.0-dev (LLVM 73a346c9caf32cc, Swift f5a6fa6)
Target: arm64-apple-macosx13.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    IRGenLLVM IR generationbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of softwaretriage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions