[WebAssembly] Backend crash (SIGSEGV in SelectionDAG::isKnownNeverNaN) compiling OpenCV's color_lab.cpp with -O2/-O3 -msimd128
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
### Summary
clang++ segfaults in `llvm::SelectionDAG::isKnownNeverNaN` (called from `WebAssemblyDAGToDAGISel::CheckNodePredicate`/`Select`) compiling OpenCV's `color_lab.cpp` for `wasm32-unknown-emscripten` with `-O2`/`-O3 -msimd128`.
Reproduces with plain `clang++` too (no em++/emcc driver involved), so it's a pure LLVM backend bug.
### Environment
- clang/LLVM `23.0.0git` (llvm-project@592953beff733a1e28f6c6e5e39f948fb035a329), via emsdk `latest` (currently 6.0.3)
- Target: `wasm32-unknown-emscripten`, host: Linux x86_64
- Source: https://github.com/opencv/opencv/blob/4.9.0/modules/imgproc/src/color_lab.cpp
- Does NOT reproduce on LLVM 20.0.0git (emsdk 3.1.69)
- `-msimd128` at `-O0`/`-O1` compiles fine; crash starts at `-O2`
### Steps to reproduce
```sh
git clone https://github.com/emscripten-core/emsdk.git && cd emsdk
./emsdk install latest && ./emsdk activate latest && source ./emsdk_env.sh
git clone --depth 1 --branch 4.9.0 https://github.com/opencv/opencv.git
emcmake cmake -S opencv -B build \
-DBUILD_LIST=core,imgproc,features2d,calib3d,flann \
-DBUILD_opencv_apps=OFF -DBUILD_opencv_js=OFF -DBUILD_TESTS=OFF \
-DBUILD_PERF_TESTS=OFF -DBUILD_EXAMPLES=OFF \
-DCMAKE_C_FLAGS="-msimd128" -DCMAKE_CXX_FLAGS="-msimd128"
em++ -c -O3 -msimd128 -std=c++11 -fPIC -D__OPENCV_BUILD=1 -DNDEBUG \
-I opencv/modules/imgproc/include -I build/modules/imgproc \
-I opencv/modules/core/include -isystem build \
-o color_lab.o opencv/modules/imgproc/src/color_lab.cpp
```
### Backtrace
```
llvm::SelectionDAG::isKnownNeverNaN(llvm::SDValue, bool, unsigned int) const
(anonymous namespace)::WebAssemblyDAGToDAGISel::CheckNodePredicate(llvm::SDValue, unsigned int) const
llvm::SelectionDAGISel::SelectCodeCommon(...)
(anonymous namespace)::WebAssemblyDAGToDAGISel::Select(llvm::SDNode*)
llvm::SelectionDAGISel::DoInstructionSelection()
clang::ParseAST(clang::Sema&, bool, bool)
clang::FrontendAction::Execute()
cc1_main(...)
```
`clang++: error: clang frontend command failed due to signal`
### Additional context
No existing issue matches exactly, though [#175125](https://github.com/llvm/llvm-project/issues/175125) looks related (same WebAssemblyDAGToDAGISel::Select/SelectCodeCommon path, different node type).
Not OpenCV-specific -- checked opencv/opencv's tracker, nothing matches; [emscripten-core/emscripten#22170](https://github.com/emscripten-core/emscripten/issues/22170) is a prior, different crash in similar territory (OpenCV + wasm SIMD/exceptions) that was fixed once triaged.
Contributor guide
Research direction
Reproduce the crash by compiling OpenCV's modules/imgproc/src/color_lab.cpp with the provided wasm32 and -O2/-O3 -msimd128 command. Then inspect llvm::SelectionDAG::isKnownNeverNaN and the WebAssemblyDAGToDAGISel::CheckNodePredicate/Select entry points named in the backtrace. Done means the compiler no longer crashes on this reproduction and the behavior is covered by an appropriate LLVM regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, wasm
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100