I recently realised I'm getting huge, pointless code bloat in my libraries from reflection metadata. I never use reflection on my platform (and have removed all stdlib support for it).
I stumbled across the flag "-Xfrontend" "-disable-reflection-metadata" and added that to my build, however, now I'm getting a crash in IRGen...
Assertion failed: (castIsValid(op, S, Ty) && "Invalid cast!"), function Create, file /Users/carlpeto/compilers/avr-swift/llvm-project/llvm/lib/IR/Instructions.cpp, line 2741.
Stack dump:
0. Program arguments: /Users/carlpeto/compilers/avr-swift/build/Ninja-ReleaseAssert+swift-DebugAssert/swift-macosx-x86_64/bin/swift -frontend -emit-bc CoreOperators.swift CoreAliases.swift RawRepresentable.swift LiteralProtocols.swift TopLevelFunctions.swift CoreProtocols.swift CoreFloatingPoint.swift CoreBinaryFloatingPoint.swift Float.swift Float16.swift CoreFloatingPointFunctions.swift Optional.swift Bridging.swift CoreNumericProtocols.swift BinaryInteger.swift CoreIntegers.swift ErrorType.swift Bool.swift Integers.swift Ranges.swift Sequence.swift Stride.swift Slice.swift Collection.swift BidirectionalCollection.swift RandomAccessCollection.swift ClosedRange.swift MutableCollection.swift Hash.swift Pointer.swift UnsafeBufferPointer.swift UnsafeRawBufferPointer.swift UnsafeRawPointer.swift Indices.swift Existential.swift Algorithm.swift FixedWidth.swift IntegerMath.swift CTypes.swift UnsafePointer.swift ObjectIdentifier.swift CollectionAlgorithms.swift WriteBackMutableSlice.swift Random.swift RangeReplaceableCollection.swift MemoryLayout.swift Tuple.swift SequenceAlgorithms.swift LifetimeManager.swift Repeat.swift EmptyCollection.swift CollectionOfOne.swift StringLiterals.swift StaticString.swift Unicode.swift UnicodeScalar.swift UnicodeEncoding.swift UTF8.swift UTF16.swift ValidUTF8Buffer.swift UnicodeParser.swift UIntBuffer.swift UTFEncoding.swift UTF32.swift ArrayType.swift ArrayBufferProtocol.swift ArrayLiterals.swift ArrayShared.swift ContiguousArray.swift SliceBuffer.swift ArraySlice.swift Array.swift ArrayBody.swift ArrayCast.swift AnyHashable.swift ManagedBuffer.swift AVRArrayBuffer.swift Reverse.swift Map.swift Zip.swift LazySequence.swift LazyCollection.swift Filter.swift FlatMap.swift Flatten.swift DropWhile.swift Volatile.swift Integer-16.swift IntegerMath-16.swift Progmem.swift -supplementary-output-file-map /var/folders/1q/fy3cg3j92fgb11hj50dtzf900000gn/T/supplementaryOutputs-4fb296 -disable-objc-attr-requires-foundation-module -target avr-atmel-linux-gnueabihf -disable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -color-diagnostics -I uSwiftShims -I /Applications/Swift For Arduino.app/Contents/XPCServices/S4ABuildEngine.xpc/Contents/Resources/gpl-tools-avr/lib/avr-libgcc/include -I /Applications/Swift For Arduino.app/Contents/XPCServices/S4ABuildEngine.xpc/Contents/Resources/gpl-tools-avr/lib/avr-libc/include -I AVR -enable-library-evolution -nostdimport -parse-stdlib -Osize -D AVR_LIBC_DEFINED_SWIFT -disable-reflection-metadata -Xcc -DAVR_LIBC_DEFINED -DLIBC_DEFINED -parse-as-library -module-name Swift -num-threads 4 -o bin/AVR/CoreOperators.bc -o bin/AVR/CoreAliases.bc -o bin/AVR/RawRepresentable.bc -o bin/AVR/LiteralProtocols.bc -o bin/AVR/TopLevelFunctions.bc -o bin/AVR/CoreProtocols.bc -o bin/AVR/CoreFloatingPoint.bc -o bin/AVR/CoreBinaryFloatingPoint.bc -o bin/AVR/Float.bc -o bin/AVR/Float16.bc -o bin/AVR/CoreFloatingPointFunctions.bc -o bin/AVR/Optional.bc -o bin/AVR/Bridging.bc -o bin/AVR/CoreNumericProtocols.bc -o bin/AVR/BinaryInteger.bc -o bin/AVR/CoreIntegers.bc -o bin/AVR/ErrorType.bc -o bin/AVR/Bool.bc -o bin/AVR/Integers.bc -o bin/AVR/Ranges.bc -o bin/AVR/Sequence.bc -o bin/AVR/Stride.bc -o bin/AVR/Slice.bc -o bin/AVR/Collection.bc -o bin/AVR/BidirectionalCollection.bc -o bin/AVR/RandomAccessCollection.bc -o bin/AVR/ClosedRange.bc -o bin/AVR/MutableCollection.bc -o bin/AVR/Hash.bc -o bin/AVR/Pointer.bc -o bin/AVR/UnsafeBufferPointer.bc -o bin/AVR/UnsafeRawBufferPointer.bc -o bin/AVR/UnsafeRawPointer.bc -o bin/AVR/Indices.bc -o bin/AVR/Existential.bc -o bin/AVR/Algorithm.bc -o bin/AVR/FixedWidth.bc -o bin/AVR/IntegerMath.bc -o bin/AVR/CTypes.bc -o bin/AVR/UnsafePointer.bc -o bin/AVR/ObjectIdentifier.bc -o bin/AVR/CollectionAlgorithms.bc -o bin/AVR/WriteBackMutableSlice.bc -o bin/AVR/Random.bc -o bin/AVR/RangeReplaceableCollection.bc -o bin/AVR/MemoryLayout.bc -o bin/AVR/Tuple.bc -o bin/AVR/SequenceAlgorithms.bc -o bin/AVR/LifetimeManager.bc -o bin/AVR/Repeat.bc -o bin/AVR/EmptyCollection.bc -o bin/AVR/CollectionOfOne.bc -o bin/AVR/StringLiterals.bc -o bin/AVR/StaticString.bc -o bin/AVR/Unicode.bc -o bin/AVR/UnicodeScalar.bc -o bin/AVR/UnicodeEncoding.bc -o bin/AVR/UTF8.bc -o bin/AVR/UTF16.bc -o bin/AVR/ValidUTF8Buffer.bc -o bin/AVR/UnicodeParser.bc -o bin/AVR/UIntBuffer.bc -o bin/AVR/UTFEncoding.bc -o bin/AVR/UTF32.bc -o bin/AVR/ArrayType.bc -o bin/AVR/ArrayBufferProtocol.bc -o bin/AVR/ArrayLiterals.bc -o bin/AVR/ArrayShared.bc -o bin/AVR/ContiguousArray.bc -o bin/AVR/SliceBuffer.bc -o bin/AVR/ArraySlice.bc -o bin/AVR/Array.bc -o bin/AVR/ArrayBody.bc -o bin/AVR/ArrayCast.bc -o bin/AVR/AnyHashable.bc -o bin/AVR/ManagedBuffer.bc -o bin/AVR/AVRArrayBuffer.bc -o bin/AVR/Reverse.bc -o bin/AVR/Map.bc -o bin/AVR/Zip.bc -o bin/AVR/LazySequence.bc -o bin/AVR/LazyCollection.bc -o bin/AVR/Filter.bc -o bin/AVR/FlatMap.bc -o bin/AVR/Flatten.bc -o bin/AVR/DropWhile.bc -o bin/AVR/Volatile.bc -o bin/AVR/Integer-16.bc -o bin/AVR/IntegerMath-16.bc -o bin/AVR/Progmem.bc
1. Swift version 5.3-dev (LLVM 9bbbe0cda8, Swift 3675d8bbac)
2. While evaluating request IRGenWholeModuleRequest(IR Generation for module Swift)
3. While emitting IR SIL function "@$sSxsE5_step5after4from2bySiSg5index_x5valuetAeF_xAGt_x6StrideQztFZ".
for '_step(after:from:by:)' (at Stride.swift:28:10)
0 swift 0x0000000106841c95 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1 swift 0x0000000106840c48 llvm::sys::RunSignalHandlers() + 248
2 swift 0x0000000106842296 SignalHandler(int) + 262
3 libsystem_platform.dylib 0x00007fff6f0565fd _sigtramp + 29
4 swift 0x00000001092f8f86 cmark_strbuf__initbuf + 1266817
5 libsystem_c.dylib 0x00007fff6ef2c808 abort + 120
6 libsystem_c.dylib 0x00007fff6ef2bac6 err + 0
7 swift 0x0000000107bebb23 llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::Instruction*) (.cold.14) + 35
8 swift 0x0000000106682f69 llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::Instruction*) + 1145
9 swift 0x0000000100a29844 llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter>::CreateSExtOrBitCast(llvm::Value*, llvm::Type*, llvm::Twine const&) + 196
10 swift 0x0000000100a28907 emitMetadataAccessByMangledName(swift::irgen::IRGenFunction&, swift::CanType, swift::irgen::DynamicMetadataRequest)::$_10::operator()() const + 1655
11 swift 0x0000000100a2789e emitMetadataAccessByMangledName(swift::irgen::IRGenFunction&, swift::CanType, swift::irgen::DynamicMetadataRequest) + 1246
12 swift 0x0000000100a189e6 emitCallToTypeMetadataAccessFunction(swift::irgen::IRGenFunction&, swift::CanType, swift::irgen::DynamicMetadataRequest) + 358
13 swift 0x0000000100a186d1 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType, swift::irgen::DynamicMetadataRequest) + 481
14 swift 0x0000000100a249ba (anonymous namespace)::EmitTypeMetadataRef::visitTupleType(swift::CanTypeWrapper<swift::TupleType>, swift::irgen::DynamicMetadataRequest)::'lambda'(swift::CanType, swift::irgen::DynamicMetadataRequest)::operator()(swift::CanType, swift::irgen::DynamicMetadataRequest) const + 106
15 swift 0x0000000100a2493c swift::irgen::MetadataResponse llvm::function_ref<swift::irgen::MetadataResponse (swift::CanType, swift::irgen::DynamicMetadataRequest)>::callback_fn<(anonymous namespace)::EmitTypeMetadataRef::visitTupleType(swift::CanTypeWrapper<swift::TupleType>, swift::irgen::DynamicMetadataRequest)::'lambda'(swift::CanType, swift::irgen::DynamicMetadataRequest)>(long, swift::CanType, swift::irgen::DynamicMetadataRequest) + 140
16 swift 0x0000000100a24438 llvm::function_ref<swift::irgen::MetadataResponse (swift::CanType, swift::irgen::DynamicMetadataRequest)>::operator()(swift::CanType, swift::irgen::DynamicMetadataRequest) const + 152
17 swift 0x0000000100a244b2 emitTupleTypeMetadataRef(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::TupleType>, swift::irgen::DynamicMetadataRequest, bool, llvm::function_ref<swift::irgen::MetadataResponse (swift::CanType, swift::irgen::DynamicMetadataRequest)>)::$_9::operator()(swift::CanType) const + 98
18 swift 0x0000000100a237f1 emitTupleTypeMetadataRef(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::TupleType>, swift::irgen::DynamicMetadataRequest, bool, llvm::function_ref<swift::irgen::MetadataResponse (swift::CanType, swift::irgen::DynamicMetadataRequest)>) + 369
19 swift 0x0000000100a21d7e (anonymous namespace)::EmitTypeMetadataRef::visitTupleType(swift::CanTypeWrapper<swift::TupleType>, swift::irgen::DynamicMetadataRequest) + 318
20 swift 0x0000000100a205e9 swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRef, swift::irgen::MetadataResponse, swift::irgen::DynamicMetadataRequest>::visit(swift::CanType, swift::irgen::DynamicMetadataRequest) + 1737
21 swift 0x0000000100a18867 emitDirectTypeMetadataRef(swift::irgen::IRGenFunction&, swift::CanType, swift::irgen::DynamicMetadataRequest) + 135
22 swift 0x0000000100a18672 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType, swift::irgen::DynamicMetadataRequest) + 386
23 swift 0x0000000100a18f7c swift::irgen::IRGenFunction::emitTypeMetadataRefForLayout(swift::SILType, swift::irgen::DynamicMetadataRequest) + 492
24 swift 0x00000001007e8b63 swift::irgen::IRGenFunction::emitValueWitnessTableRef(swift::SILType, swift::irgen::DynamicMetadataRequest, llvm::Value**) + 451
25 swift 0x00000001007e8995 swift::irgen::IRGenFunction::emitValueWitnessTableRef(swift::SILType, llvm::Value**) + 101
26 swift 0x000000010082196a swift::irgen::IRGenFunction::emitValueWitnessValue(swift::SILType, swift::irgen::ValueWitness) + 186
27 swift 0x000000010082241a swift::irgen::emitLoadOfSize(swift::irgen::IRGenFunction&, swift::SILType) + 42
28 swift 0x000000010082239b swift::irgen::IRGenFunction::emitDynamicAlloca(swift::SILType, llvm::Twine const&) + 59
29 swift 0x000000010089cbfc swift::irgen::WitnessSizedTypeInfo<(anonymous namespace)::NonFixedTupleTypeInfo>::allocateStack(swift::irgen::IRGenFunction&, swift::SILType, llvm::Twine const&) const + 92
30 swift 0x0000000100989530 (anonymous namespace)::IRGenSILFunction::visitAllocStackInst(swift::AllocStackInst*) + 256
31 swift 0x00000001009875d7 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 135
32 swift 0x000000010097c0e7 (anonymous namespace)::IRGenSILFunction::visitSILBasicBlock(swift::SILBasicBlock*) + 1415
33 swift 0x000000010097484f (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 1695
34 swift 0x0000000100974132 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 210
35 swift 0x00000001007059bf swift::irgen::IRGenerator::emitGlobalTopLevel(llvm::StringSet<llvm::MallocAllocator>*) + 1503
36 swift 0x00000001008d54ed performParallelIRGeneration(swift::IRGenOptions const&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, int, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, llvm::StringSet<llvm::MallocAllocator>*) + 1213
37 swift 0x00000001008d4e9e swift::IRGenWholeModuleRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 334
38 swift 0x000000010097378c swift::GeneratedModule swift::SimpleRequest<swift::IRGenWholeModuleRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)1>::callDerived<0ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul>) const + 156
39 swift 0x000000010097362e swift::SimpleRequest<swift::IRGenWholeModuleRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::IRGenWholeModuleRequest const&, swift::Evaluator&) + 46
40 swift 0x00000001008f87dd llvm::Expected<swift::IRGenWholeModuleRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenWholeModuleRequest>(swift::IRGenWholeModuleRequest const&) + 413
41 swift 0x00000001008d4cee llvm::Expected<swift::IRGenWholeModuleRequest::OutputType> swift::Evaluator::operator()<swift::IRGenWholeModuleRequest, (void*)0>(swift::IRGenWholeModuleRequest const&) + 78
42 swift 0x00000001008d4a3a swift::performIRGeneration(swift::IRGenOptions const&, swift::ModuleDecl*, 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**, llvm::StringSet<llvm::MallocAllocator>*) + 410
43 swift 0x000000010018a8ec generateIR(swift::IRGenOptions 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> > >, llvm::StringSet<llvm::MallocAllocator>&) + 588
44 swift 0x0000000100189c9d performCompileStepsPostSILGen(swift::CompilerInstance&, swift::CompilerInvocation const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 2125
45 swift 0x000000010016dbe4 performCompileStepsPostSema(swift::CompilerInvocation const&, swift::CompilerInstance&, int&, swift::FrontendObserver*) + 548
46 swift 0x0000000100162df5 performCompile(swift::CompilerInstance&, swift::CompilerInvocation const&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 1525
47 swift 0x0000000100161952 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2226
48 swift 0x0000000100004427 run_driver(llvm::StringRef, llvm::ArrayRef<char const*>) + 263
49 swift 0x00000001000036e4 main + 2084
50 libdyld.dylib 0x00007fff6ee5dcc9 start + 1
51 libdyld.dylib 0x0000000000000130 start + 18446603338655605864
<unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)
Process 49672 exited with status = 254 (0x000000fe)
SIL function being emitted...
// static Strideable._step(after:from:by:)
sil @$sSxsE5_step5after4from2bySiSg5index_x5valuetAeF_xAGt_x6StrideQztFZ : $@convention(method) <Self where Self : Strideable> (Optional<Int>, @in_guaranteed Self, @in_guaranteed Self, @in_guaranteed Self.Stride, @thick Self.Type) -> (Optional<Int>, @out Self) {
// %0 "$return_value" // user: %14
// %1 "current" // user: %8
// %2 "current" // user: %10
// %3 "start"
// %4 "distance" // users: %14, %11
// %5 "self"
bb0(%0 : $*Self, %1 : $Optional<Int>, %2 : $*Self, %3 : $*Self, %4 : $*Self.Stride, %5 : $@thick Self.Type):
%6 = alloc_stack $(index: Optional<Int>, value: Self), let, name "current", argno 1 // users: %16, %15, %9, %7
%7 = tuple_element_addr %6 : $*(index: Optional<Int>, value: Self), 0 // user: %8
store %1 to %7 : $*Optional<Int> // id: %8
%9 = tuple_element_addr %6 : $*(index: Optional<Int>, value: Self), 1 // users: %14, %10
copy_addr %2 to [initialization] %9 : $*Self // id: %10
debug_value_addr %4 : $*Self.Stride, let, name "distance", argno 3 // id: %11
%12 = enum $Optional<Int>, #Optional.none!enumelt // user: %17
%13 = witness_method $Self, #Strideable.advanced : <Self where Self : Strideable> (Self) -> (Self.Stride) -> Self : $@convention(witness_method: Strideable) <τ_0_0 where τ_0_0 : Strideable> (@in_guaranteed τ_0_0.Stride, @in_guaranteed τ_0_0) -> @out τ_0_0 // user: %14
%14 = apply %13<Self>(%0, %4, %9) : $@convention(witness_method: Strideable) <τ_0_0 where τ_0_0 : Strideable> (@in_guaranteed τ_0_0.Stride, @in_guaranteed τ_0_0) -> @out τ_0_0
destroy_addr %6 : $*(index: Optional<Int>, value: Self) // id: %15
dealloc_stack %6 : $*(index: Optional<Int>, value: Self) // id: %16
return %12 : $Optional<Int> // id: %17
} // end sil function '$sSxsE5_step5after4from2bySiSg5index_x5valuetAeF_xAGt_x6StrideQztFZ'
This SIL instruction...
%6 = alloc_stack $(index: Optional<Int>, value: Self), let, name "current", argno 1 // users: %16, %15, %9, %7
Can anyone advise what I'm doing wrong? I sort of expected this just to stop emitting reflection metadata only?