mojo
mojo copied to clipboard
[BUG] [mojo-lang]: Compiler crash passing dynamic vectors as parameters and returning them
Bug description
This causes a compiler crash:
fn adds_one[v: DynamicVector[Int], new_val: Int]() -> DynamicVector[Int]:
var out = DynamicVector[Int](len(v)+1)
for i in range(len(v)):
out[i] = v[i]
out[len(v)] = new_val
out.size = v.size + 1
return out
fn do_work[v: DynamicVector[Int], new_val: Int]() -> DynamicVector[Int]:
alias f = adds_one[v, new_val]()
return f
fn main():
alias a = DynamicVector[Int]()
alias b = do_work[a,100]()
alias c = do_work[b,200]() # expected print: 100 200, but getting an error
It's only on the line with alias c
that causes the crash:
Stack dump:
0. Program arguments: mojo /tmp/wow.mojo
#0 0x000000010055d4a8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1000d14a8)
#1 0x000000010055b648 llvm::sys::RunSignalHandlers() (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1000cf648)
#2 0x000000010055db40 SignalHandler(int) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1000d1b40)
#3 0x000000018a291a24 (/usr/lib/system/libsystem_platform.dylib+0x18046da24)
#4 0x0000000102143b58 std::__1::__function::__func<std::__1::enable_if<!std::is_same_v<M::MemRefAttr, mlir::Attribute> || !std::is_convertible_v<std::__1::pair<mlir::Attribute, mlir::WalkResult>, std::__1::optional<std::__1::pair<mlir::Attribute, mlir::WalkResult>>>, void>::type mlir::AttrTypeReplacer::addReplacement<M::InterpreterState::internalizeMemory(llvm::MutableArrayRef<mlir::Attribute>)::$_5, M::MemRefAttr, mlir::Attribute, std::__1::pair<mlir::Attribute, mlir::WalkResult>>(M::InterpreterState::internalizeMemory(llvm::MutableArrayRef<mlir::Attribute>)::$_5&&)::'lambda'(mlir::Attribute), std::__1::allocator<std::__1::enable_if<!std::is_same_v<M::MemRefAttr, mlir::Attribute> || !std::is_convertible_v<std::__1::pair<mlir::Attribute, mlir::WalkResult>, std::__1::optional<std::__1::pair<mlir::Attribute, mlir::WalkResult>>>, void>::type mlir::AttrTypeReplacer::addReplacement<M::InterpreterState::internalizeMemory(llvm::MutableArrayRef<mlir::Attribute>)::$_5, M::MemRefAttr, mlir::Attribute, std::__1::pair<mlir::Attribute, mlir::WalkResult>>(M::InterpreterState::internalizeMemory(llvm::MutableArrayRef<mlir::Attribute>)::$_5&&)::'lambda'(mlir::Attribute)>, std::__1::optional<std::__1::pair<mlir::Attribute, mlir::WalkResult>> (mlir::Attribute)>::operator()(mlir::Attribute&&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101cb7b58)
#5 0x0000000100a0d354 mlir::Attribute mlir::AttrTypeReplacer::replaceImpl<mlir::Attribute, std::__1::vector<std::__1::function<std::__1::optional<std::__1::pair<mlir::Attribute, mlir::WalkResult>> (mlir::Attribute)>, std::__1::allocator<std::__1::function<std::__1::optional<std::__1::pair<mlir::Attribute, mlir::WalkResult>> (mlir::Attribute)>>>>(mlir::Attribute, std::__1::vector<std::__1::function<std::__1::optional<std::__1::pair<mlir::Attribute, mlir::WalkResult>> (mlir::Attribute)>, std::__1::allocator<std::__1::function<std::__1::optional<std::__1::pair<mlir::Attribute, mlir::WalkResult>> (mlir::Attribute)>>>&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x100581354)
#6 0x0000000100a0e994 void updateSubElementImpl<mlir::Attribute>(mlir::Attribute, mlir::AttrTypeReplacer&, llvm::SmallVectorImpl<mlir::Attribute>&, mlir::FailureOr<bool>&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x100582994)
#7 0x0000000102018b10 void llvm::function_ref<void (mlir::Attribute, llvm::function_ref<void (mlir::Attribute)>, llvm::function_ref<void (mlir::Type)>)>::callback_fn<mlir::detail::StorageUserBase<M::KGEN::StructAttr, mlir::Attribute, M::KGEN::detail::StructAttrStorage, mlir::detail::AttributeUniquer, mlir::TypedAttr::Trait, M::KGEN::ParameterAttr::Trait>::getWalkImmediateSubElementsFn()::'lambda'(auto, llvm::function_ref<void (mlir::Attribute)>, llvm::function_ref<void (mlir::Type)>)>(long, mlir::Attribute, llvm::function_ref<void (mlir::Attribute)>, llvm::function_ref<void (mlir::Type)>) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101b8cb10)
#8 0x0000000100a0e3a8 mlir::Attribute mlir::AttrTypeReplacer::replaceSubElements<mlir::Attribute>(mlir::Attribute) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1005823a8)
#9 0x0000000100a0d378 mlir::Attribute mlir::AttrTypeReplacer::replaceImpl<mlir::Attribute, std::__1::vector<std::__1::function<std::__1::optional<std::__1::pair<mlir::Attribute, mlir::WalkResult>> (mlir::Attribute)>, std::__1::allocator<std::__1::function<std::__1::optional<std::__1::pair<mlir::Attribute, mlir::WalkResult>> (mlir::Attribute)>>>>(mlir::Attribute, std::__1::vector<std::__1::function<std::__1::optional<std::__1::pair<mlir::Attribute, mlir::WalkResult>> (mlir::Attribute)>, std::__1::allocator<std::__1::function<std::__1::optional<std::__1::pair<mlir::Attribute, mlir::WalkResult>> (mlir::Attribute)>>>&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x100581378)
#10 0x000000010213eed8 M::InterpreterState::internalizeMemory(llvm::MutableArrayRef<mlir::Attribute>) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101cb2ed8)
#11 0x00000001020839d0 M::KGEN::ParamMaterializeOp::interpret(llvm::ArrayRef<mlir::Attribute>, M::InterpreterState&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101bf79d0)
#12 0x000000010206c588 M::detail::InterpreterOpInterfaceInterfaceTraits::Model<M::KGEN::ParamMaterializeOp>::interpret(M::detail::InterpreterOpInterfaceInterfaceTraits::Concept const*, mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, M::InterpreterState&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101be0588)
#13 0x00000001021404d4 M::InterpreterState::runInterpreter() (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101cb44d4)
#14 0x00000001021410a8 M::InterpreterState::executeRegionWithResultSlot(mlir::Type, mlir::Region&, llvm::ArrayRef<mlir::Attribute>) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101cb50a8)
#15 0x00000001018a28fc M::KGEN::IREvaluator::evaluateFunctionWithResultSlot(M::KGEN::FuncOp, llvm::ArrayRef<mlir::TypedAttr>) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1014168fc)
#16 0x00000001018a38f8 M::KGEN::IREvaluator::evaluateApplyLike(M::KGEN::ParamOperatorAttr, bool) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1014178f8)
#17 0x00000001018a2df0 M::KGEN::IREvaluator::evaluateExpression(M::KGEN::ParamOperatorAttr) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101416df0)
#18 0x00000001020e23c4 M::KGEN::ParameterEvaluator::doReplace(mlir::Attribute, unsigned long) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101c563c4)
#19 0x00000001008a17f0 std::__1::conditional<std::is_base_of_v<mlir::Type, mlir::Attribute>, mlir::Type, mlir::Attribute>::type M::KGEN::ParameterReplacer<M::KGEN::ParameterEvaluator>::replaceImpl<mlir::Attribute>(mlir::Attribute, unsigned long) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1004157f0)
#20 0x00000001018a4b28 M::KGEN::IREvaluator::concretizeParameterExpr(M::KGEN::ImplNode*, mlir::Location, mlir::Attribute) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101418b28)
#21 0x000000010188adfc (anonymous namespace)::ElaboratorImpl::processScope(M::KGEN::ImplNode*, M::KGEN::ImplNode::WorkItem&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1013fedfc)
#22 0x000000010188a468 void llvm::detail::UniqueFunctionBase<void>::CallImpl<(anonymous namespace)::ElaboratorImpl::scheduleImplNode(M::KGEN::ImplNode*)::$_37>(void*) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1013fe468)
#23 0x000000010217379c void (anonymous namespace)::WorkQueueThread::runItemsImpl<(anonymous namespace)::ThreadPoolWorkQueue::await(llvm::ArrayRef<M::LLCL::AnyAsyncValueRef>)::$_6, (anonymous namespace)::ThreadPoolWorkQueue::await(llvm::ArrayRef<M::LLCL::AnyAsyncValueRef>)::$_7>((anonymous namespace)::ThreadPoolWorkQueue::await(llvm::ArrayRef<M::LLCL::AnyAsyncValueRef>)::$_6, (anonymous namespace)::ThreadPoolWorkQueue::await(llvm::ArrayRef<M::LLCL::AnyAsyncValueRef>)::$_7, bool, llvm::StringLiteral, llvm::StringLiteral) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101ce779c)
#24 0x0000000102172060 (anonymous namespace)::ThreadPoolWorkQueue::await(llvm::ArrayRef<M::LLCL::AnyAsyncValueRef>) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101ce6060)
#25 0x00000001018850d4 elaborateGenerators(mlir::SymbolTableAnalysis&, M::KGEN::ParameterCollector::Analysis&, M::LLCL::Runtime&, M::TargetInfoAttr, llvm::ArrayRef<M::KGEN::GeneratorOp>, M::KGEN::ElaboratorCallbacks, M::KGEN::ElaborateGeneratorsOptions const&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1013f90d4)
#26 0x00000001018824f0 (anonymous namespace)::ElaborateGeneratorsPass::runOnOperation() (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1013f64f0)
#27 0x0000000102bcf080 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x102743080)
#28 0x0000000102bcf6e0 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1027436e0)
#29 0x0000000102bd1724 mlir::PassManager::run(mlir::Operation*) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x102745724)
#30 0x00000001018b5fdc void llvm::detail::UniqueFunctionBase<void, M::LLCL::AnyAsyncValueRef&&>::CallImpl<M::Cache::cachedTransform(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::RegionCacheKey>>, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, mlir::PassManager&, bool)::$_0::operator()(mlir::Operation*, M::RCRef<M::WriteableBuffer>, M::LLCL::AnyAsyncValueRef) const::'lambda'(M::LLCL::AnyAsyncValueRef&&)>(void*, M::LLCL::AnyAsyncValueRef&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101429fdc)
#31 0x000000010097f308 void M::LLCL::AnyAsyncValueRef::andThen<false>(llvm::unique_function<void (M::LLCL::AnyAsyncValueRef&&)>&&) && (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1004f3308)
#32 0x00000001018b5638 M::LLCL::AnyAsyncValueRef llvm::detail::UniqueFunctionBase<M::LLCL::AnyAsyncValueRef, M::RCRef<M::WriteableBuffer>, M::LLCL::AnyAsyncValueRef>::CallImpl<M::LLCL::AnyAsyncValueRef M::Cache::cachedTransform<M::Cache::cachedTransform(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::RegionCacheKey>>, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, mlir::PassManager&, bool)::$_0, M::Cache::cachedTransform(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::RegionCacheKey>>, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, mlir::PassManager&, bool)::$_1>(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, M::RCRef<M::WriteableBuffer>, M::Cache::cachedTransform(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::RegionCacheKey>>, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, mlir::PassManager&, bool)::$_0&&, M::Cache::cachedTransform(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::RegionCacheKey>>, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, mlir::PassManager&, bool)::$_1&&)::'lambda'(M::RCRef<M::WriteableBuffer>, M::LLCL::AnyAsyncValueRef)>(void*, M::RCRef<M::WriteableBuffer>&, M::LLCL::AnyAsyncValueRef&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101429638)
#33 0x00000001018b51a4 void llvm::detail::UniqueFunctionBase<void, M::LLCL::AsyncValueRef<std::__1::optional<M::RCRef<M::Buffer>>>&&>::CallImpl<M::LLCL::AnyAsyncValueRef M::Cache::cachedTransform<M::LLCL::AnyAsyncValueRef M::Cache::cachedTransform<M::Cache::cachedTransform(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::RegionCacheKey>>, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, mlir::PassManager&, bool)::$_0, M::Cache::cachedTransform(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::RegionCacheKey>>, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, mlir::PassManager&, bool)::$_1>(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, M::RCRef<M::WriteableBuffer>, M::Cache::cachedTransform(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::RegionCacheKey>>, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, mlir::PassManager&, bool)::$_0&&, M::Cache::cachedTransform(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::RegionCacheKey>>, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, mlir::PassManager&, bool)::$_1&&)::'lambda'(M::RCRef<M::WriteableBuffer>, M::LLCL::AnyAsyncValueRef)>(M::LLCL::EncodedLocation, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, M::RCRef<M::WriteableBuffer>, M::Cache::cachedTransform(mlir::Operation*, M::RCRef<M::Cache::BlobCache<M::Cache::RegionCacheKey>>, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, M::LLCL::AnyAsyncValueRef, mlir::PassManager&, bool)::$_0, llvm::unique_function<M::LLCL::AnyAsyncValueRef (M::RCRef<M::Buffer>)>)::'lambda'(M::LLCL::AsyncValueRef<std::__1::optional<M::RCRef<M::Buffer>>>&&)>(void*, M::LLCL::AsyncValueRef<std::__1::optional<M::RCRef<M::Buffer>>>&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1014291a4)
#34 0x00000001021736b0 void (anonymous namespace)::WorkQueueThread::runItemsImpl<(anonymous namespace)::ThreadPoolWorkQueue::await(llvm::ArrayRef<M::LLCL::AnyAsyncValueRef>)::$_6, (anonymous namespace)::ThreadPoolWorkQueue::await(llvm::ArrayRef<M::LLCL::AnyAsyncValueRef>)::$_7>((anonymous namespace)::ThreadPoolWorkQueue::await(llvm::ArrayRef<M::LLCL::AnyAsyncValueRef>)::$_6, (anonymous namespace)::ThreadPoolWorkQueue::await(llvm::ArrayRef<M::LLCL::AnyAsyncValueRef>)::$_7, bool, llvm::StringLiteral, llvm::StringLiteral) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101ce76b0)
#35 0x0000000102172060 (anonymous namespace)::ThreadPoolWorkQueue::await(llvm::ArrayRef<M::LLCL::AnyAsyncValueRef>) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x101ce6060)
#36 0x000000010097860c M::KGEN::KGENCompilerLayer::add(llvm::StringRef, mlir::ModuleOp, std::__1::function<M::ErrorOr<mlir::DenseResourceElementsAttr> (M::KGEN::PackageLinkOp)>, std::__1::function<M::ErrorOr<M::KGEN::PackageArchiveAttr> (M::KGEN::PackageLinkOp, M::TargetInfoAttr)>) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1004ec60c)
#37 0x00000001004ab6dc run(M::State const&) (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x10001f6dc)
#38 0x00000001004921a0 main (/Users/jack/src/modular/.derived/build-release/bin/mojo+0x1000061a0)
#39 0x0000000189ee10e0
[26519:1586296:20240215,202147.065443:WARNING in_range_cast.h:38] value -634136515 out of range
[26519:1586297:20240215,202147.069397:ERROR directory_reader_posix.cc:42] opendir /Users/jack/src/modular/.derived/crashdb/attachments/e0d6ac8d-c7cf-45d7-aaf2-7bde5b4c2b53: No such file or directory (2)
[26519:1586296:20240215,202147.069485:WARNING crash_report_exception_handler.cc:257] UniversalExceptionRaise: (os/kern) failure (5)
[1] 26517 segmentation fault mojo /tmp/wow.mojo
Related thread here: https://discord.com/channels/1087530497313357884/1207707299490308096
System information
M2 Macos
mojo 0.0.0 (7ebbcf84)
Also occurs on 0.7.0 but without the full error message