On targets with non-default program address space (e.g., Harvard architectures), clang crashes (example backtrace below) when emitting Objective-C method metadata, because the address of the method IMP cannot be bitcast to i8*. It similarly crashes at messenger callsite with a failed bitcast.
Define the _imp field instead as i8 addrspace(1)* (or whatever the target's program address space is). And in getMessageSendInfo(), create signatureType by specifying the program address space.
Add a regression test using the AVR target. Test failed previously and passes now. Checked codegen of the test for x86_64-apple-darwin19.6.0 and saw no difference, as expected.
Backtrace (when emitting method metadata):
Assertion failed: (CastInst::castIsValid(Instruction::BitCast, C, DstTy) && "Invalid constantexpr bitcast!"), function getBitCast, file /Users/matt/src/llvm/llvm/lib/IR/Constants.cpp, line 2224. ... 7 libsystem_c.dylib 0x00007fff6ec09ac6 err + 0 8 clang-14 0x0000000101800518 llvm::ConstantExpr::getBitCast(llvm::Constant*, llvm::Type*, bool) + 120 9 clang-14 0x000000010394ca70 clang::CodeGen::ConstantAggregateBuilderBase::addBitCast(llvm::Constant*, llvm::Type*) + 48 10 clang-14 0x0000000103999ac2 (anonymous namespace)::CGObjCNonFragileABIMac::emitMethodConstant(clang::CodeGen::ConstantArrayBuilder&, clang::ObjCMethodDecl const*, bool) + 402 11 clang-14 0x0000000103999102 (anonymous namespace)::CGObjCNonFragileABIMac::emitMethodList(llvm::Twine, (anonymous namespace)::(anonymous namespace)::MethodListType, llvm::ArrayRef<clang::ObjCMethodDecl const*>) + 1074 12 clang-14 0x000000010399a4c0 (anonymous namespace)::CGObjCNonFragileABIMac::BuildClassRoTInitializer(unsigned int, unsigned int, unsigned int, clang::ObjCImplementationDecl const*) + 1376 13 clang-14 0x0000000103990b9b (anonymous namespace)::CGObjCNonFragileABIMac::GenerateClass(clang::ObjCImplementationDecl const*) + 1995 14 clang-14 0x0000000103bb87d1 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) + 2065 15 clang-14 0x0000000103db5bf2 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) + 146 16 clang-14 0x0000000103b61014 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) + 228 17 clang-14 0x00000001070d7425 clang::ParseAST(clang::Sema&, bool, bool) + 533
clang-format: please reformat the code