modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.33k stars 2.6k forks source link

[BUG] Error while building List of SIMD with more than 1 element #3687

Open franchesoni opened 1 month ago

franchesoni commented 1 month ago

Bug description

The following simple code doesn't work, is this because of memory? I find the documentation is lacking, it only says SIMD are restricted to powers of 2, which I compy with...

Steps to reproduce

alias S: Int = 256
alias S2: Int = S*S
alias SquareMatrix = SIMD[size=S2]

def main():
    var sqms = List[SquareMatrix[DType.float32]]()
    for ind in range(2):  # using 1 instead of 2 gives no issues
        sqm = SquareMatrix[DType.float32]()
        sqms.append(sqm)
    print('success!')  # we never get here :(

System information

- What OS did you do install Mojo on ?
pop os
- Provide version information for Mojo by pasting the output of `mojo -v`
mojo 24.5.0 (e8aacb95)
- Provide Magic CLI version by pasting the output of `magic -V` or `magic --version` 
magic 0.3.0
- Optionally, provide more information with `magic info`.
      Pixi version: 0.29.0
          Platform: linux-64
  Virtual packages: __unix=0=0
                  : __linux=5.15.0=0
                  : __glibc=2.35=0
                  : __cuda=12.4=0
                  : __archspec=1=cascadelake
         Cache dir: /home/.cache/rattler/cache
      Auth storage: /home/.rattler/credentials.json
  Config locations: No config files found

Project
------------
              Name: maskfire
           Version: 0.1.0
     Manifest file: /home/mojoproject.toml
      Last updated: 10-10-2024 03:53:03

Environments
------------
       Environment: default
          Features: default
          Channels: conda-forge, https://conda.modular.com/max
  Dependency count: 3
      Dependencies: max, numpy, matplotlib
  Target platforms: linux-64

Discussion was happening in:

https://discord.com/channels/1087530497313357884/1296558646192246886

Apparently using big SIMDs is not a good practice, but the documentation says nothing about it. I suggest clarifying this as a sharp edge, at least

JoeLoser commented 3 weeks ago

This seems to be something with splatting a high number of elements. If I reduce the program to something like this:

alias S: Int = 4
alias S2: Int = 16
alias SquareMatrix = SIMD[size=S2]

def main():
    var sqms = List[SquareMatrix[DType.float32]]()
    for _ in range(2):  # using 1 instead of 2 gives no issues
        sqm = SquareMatrix[DType.float32]()
        sqms.append(sqm)
    print('success!')  # we never get here :(

this prints fine. Otherwise, your original program is hitting an assert in LLVM:

Assertion failed: (SDNode::getMaxNumOperands() >= Vals.size() && "too many operands to fit into SDNode"), function createOperands, file SelectionDAG.cpp, line 13272.
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0.      Running pass 'Function Pass Manager' on module ''.
1.      Running pass 'AArch64 Instruction Selection' on function '@main'
 #0 0x0000000105a6a368 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x102e72368)
 #1 0x0000000105a6887c llvm::sys::RunSignalHandlers() (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x102e7087c)
 #2 0x0000000105a6a990 SignalHandler(int) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x102e72990)
 #3 0x0000000189c3e584 (/usr/lib/system/libsystem_platform.dylib+0x18047a584)
 #4 0x0000000189c0dc20 (/usr/lib/system/libsystem_pthread.dylib+0x180449c20)
 #5 0x0000000189b1aa30 (/usr/lib/system/libsystem_c.dylib+0x180356a30)
 #6 0x0000000189b19d20 (/usr/lib/system/libsystem_c.dylib+0x180355d20)
 #7 0x00000001060dee68 llvm::SelectionDAG::createOperands(llvm::SDNode*, llvm::ArrayRef<llvm::SDValue>) (.cold.2) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x1034e6e68)
 #8 0x0000000104962998 llvm::SelectionDAG::getCommutedVectorShuffle(llvm::ShuffleVectorSDNode const&) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101d6a998)
 #9 0x000000010497732c llvm::SelectionDAG::getNode(unsigned int, llvm::SDLoc const&, llvm::EVT, llvm::ArrayRef<llvm::SDValue>, llvm::SDNodeFlags) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101d7f32c)
#10 0x00000001039ce088 llvm::SelectionDAG::getSplatBuildVector(llvm::EVT, llvm::SDLoc const&, llvm::SDValue) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x100dd6088)
#11 0x0000000104061e5c llvm::SelectionDAG::getSplat(llvm::EVT, llvm::SDLoc const&, llvm::SDValue) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101469e5c)
#12 0x00000001049b3960 llvm::SelectionDAGBuilder::getValueImpl(llvm::Value const*) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101dbb960)
#13 0x00000001049b2960 llvm::SelectionDAGBuilder::getValue(llvm::Value const*) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101dba960)
#14 0x00000001049aa580 llvm::SelectionDAGBuilder::visitStore(llvm::StoreInst const&) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101db2580)
#15 0x00000001049a5300 llvm::SelectionDAGBuilder::visit(llvm::Instruction const&) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101dad300)
#16 0x00000001049ec52c llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator_w_bits<llvm::ilist_detail::node_options<llvm::Instruction, false, false, void, true, llvm::BasicBlock>, false, true>, llvm::ilist_iterator_w_bits<llvm::ilist_detail::node_options<llvm::Instruction, false, false, void, true, llvm::BasicBlock>, false, true>, bool&) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101df452c)
#17 0x00000001049ec060 llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101df4060)
#18 0x00000001049e9dbc llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101df1dbc)
#19 0x00000001049e85c0 llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101df05c0)
#20 0x00000001046600f0 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x101a680f0)
#21 0x0000000105543f80 llvm::FPPassManager::runOnFunction(llvm::Function&) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x10294bf80)
#22 0x0000000105549544 llvm::FPPassManager::runOnModule(llvm::Module&) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x102951544)
#23 0x000000010554458c llvm::legacy::PassManagerImpl::run(llvm::Module&) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x10294c58c)
#24 0x0000000102d5a8a4 runLlcPasses(llvm::Module&, M::KGEN::CompilationOptions&, llvm::TargetMachine&, llvm::raw_pwrite_stream&, std::__1::unique_ptr<llvm::MachineModuleInfo, std::__1::default_delete<llvm::MachineModuleInfo>>&, std::__1::unique_ptr<llvm::MCContext, std::__1::default_delete<llvm::MCContext>>&, llvm::CodeGenFileType, bool, unsigned int, M::Telemetry::TelemetryContext*) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x1001628a4)
#25 0x0000000102d6bd40 void llvm::detail::UniqueFunctionBase<void>::CallImpl<compileOptimizedLLVMModuleToObject(M::KGEN::LLVMModuleAndContext, mlir::Location, M::AsyncRT::Runtime&, bool, M::KGEN::CompilationOptions, M::RCRef<M::Cache::BlobCache<M::Cache::TransformCacheKey>>, std::__1::optional<unsigned long>, std::__1::optional<unsigned long>, unsigned int)::$_0>(void*) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x100173d40)
#26 0x00000001031b0580 void (anonymous namespace)::WorkQueueThread::runItemsImpl<(anonymous namespace)::WorkQueueThread::runOnThread()::$_0, (anonymous namespace)::WorkQueueThread::runOnThread()::$_1>((anonymous namespace)::WorkQueueThread::runOnThread()::$_0, (anonymous namespace)::WorkQueueThread::runOnThread()::$_1, bool, llvm::StringLiteral, llvm::StringLiteral) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x1005b8580)
#27 0x00000001031b0174 (anonymous namespace)::WorkQueueThread::runOnThread() (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x1005b8174)
#28 0x00000001031b0204 void* std::__1::__thread_proxy[abi:nn180100]<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, void ((anonymous namespace)::WorkQueueThread::*)(), (anonymous namespace)::WorkQueueThread*>>(void*) (/private/var/tmp/_bazel_joe/4afb8e723049f00882f81f0fb80fc5dd/execroot/_main/bazel-out/darwin_arm64-fastbuild-default/bin/KGEN/tools/mojo/mojo+0x1005b8204)
#29 0x0000000189c0df94 (/usr/lib/system/libsystem_pthread.dylib+0x180449f94)
#30 0x0000000189c08d34 (/usr/lib/system/libsystem_pthread.dylib+0x180444d34)