Fix MLAS 32-bit x86 build by integrating missing upstream assembly files #29226 ### Pull Request Readiness Checklist This resolves the 32-bit x86 build failure for MLAS. Related to: https://github.com/opencv/opencv/pull/29218 * Added the required `x86` assembly files and headers from upstream. * Added `__x86_64__` guards around the AMX `syscall` and the FMA3/AVX512F kernel assignments to prevent 32-bit compilation crashes. See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
MLAS (Microsoft Linear Algebra Subprograms)
MLAS is a compute library containing processor-optimized GEMM kernels and platform-specific threading code. It is the default math kernel library used internally by ONNX Runtime.
Provenance
- Upstream: https://github.com/microsoft/onnxruntime
- Source path:
onnxruntime/core/mlas/ - Imported: 2026-05-04
- Upstream commit:
62f742f1aa0c3102745ed35e3d869eaee845b9ac(2026-04-30, last MLAS-touching commit onmainat import time; released as part of ORT v1.26.0) - License: MIT (see LICENSE)
What is vendored
The SGEMM (single-precision GEMM) subset of MLAS plus MlasFlashAttention
(fused multi-head attention). The rest of MLAS (quantized GEMM, conv,
FP16-dispatch SoftMax, etc.) is excluded so the OpenCV DNN module gets the
fast SGEMM and FlashAttention paths without dragging in the full library.
Source files imported verbatim from upstream:
lib/sgemm.cpp— SGEMM dispatch and host-side glue.lib/compute.cpp— softmax / exp / row-max / sum-exp kernels. Only the portable C++ fallbacks forMlasReduceMaximumF32KernelandMlasComputeSumExpF32Kernelare exercised; no per-arch.Ssoftmax kernels are vendored. The file is imported whole (FP16 / GQA template specializations compile but never run —SoftmaxDispatchstays nullptr).lib/flashattn.cpp— theMlasFlashAttention/MlasFlashAttentionThreadedentry points. Depends onMlasSgemmOperation(insgemm.cpp) and the two portable kernels above.lib/softmax.h— header included bycompute.cpp; pure FP16-dispatch typedefs, harmless under FP32-only builds.- Per-arch SGEMM kernels under
lib/<arch>/.
Top-level layout:
inc/— public MLAS headers (kept verbatim from upstream).lib/— implementation, kept verbatim from upstream except for the local patches listed below. Per-architecture kernels live in subdirectories (x86_64/,aarch64/,arm/,power/,riscv64/,loongarch64/,s390x/,sve/,kleidiai/).CMakeLists.txt— OpenCV-side build glue. Builds an OBJECT library (opencv_dnn_mlas) whose objects are linked directly intoopencv_dnn.threading_opencv.cpp— OpenCV-side replacement forlib/threading.cpp(see "Local patches" below). Carries the OpenCV license header.
Copyright
Most files are © Microsoft Corporation and licensed MIT. Some upstream
contributions in lib/ carry additional MIT-licensed copyrights — they are
preserved verbatim in the file headers:
lib/kleidiai/mlasi_kleidiai.h— © Arm Limited 2025.lib/erf_neon_fp16.{h,cpp},lib/gelu_neon_fp16.{h,cpp}— © FUJITSU LIMITED 2025 (jointly with Microsoft).
The OpenCV-authored files in this directory (CMakeLists.txt,
threading_opencv.cpp, this README.md) are licensed under OpenCV's
top-level license (Apache 2.0).
Local patches against upstream
These deviate from a clean upstream import and must be re-applied on every
re-vendor. The unified-diff form of each in-tree edit lives under
patches/ (same convention as
3rdparty/zlib/patches/); re-apply with
git apply --directory=3rdparty/mlas patches/*.diff after re-importing.
lib/threading.cppis dropped (not vendored). Its three threading entry points (MlasExecuteThreaded,MlasTrySimpleParallel,MlasTryBatchParallel) are reimplemented inmodules/dnn/src/layers/cpu_kernels/mlas_threading.cppon top ofcv::parallel_for_. No.diff— the file is simply absent.lib/mlasi.h—MlasGetMaximumThreadCount()returnscv::getNumThreads()whenMLAS_OPENCV_THREADINGis defined;#include "core/mlas/inc/mlas.h"is rewritten to#include "../inc/mlas.h"because the ORT in-tree path does not exist here. Seepatches/0001-mlasi-opencv-threading.diff.lib/platform.cpp— non-SGEMM dispatch is wrapped underMLAS_GEMM_ONLYso the SGEMM-only subset builds without the rest of the MLAS sources. The top-of-fileerf_neon_fp16.h/gelu_neon_fp16.hincludes are also gated on!defined(MLAS_GEMM_ONLY)because those headers transitively pull in non-vendored FP16 sources (fp16_common.h,softmax_kernel_neon.h). TheMLAS_GEMM_ONLYctor also assignsReduceMaximumF32KernelandComputeSumExpF32Kernelto the portablecompute.cppfallbacks soMlasFlashAttentionworks without per-arch softmax kernels. Seepatches/0002-platform-gemm-only.diff.inc/mlas.h— guard_MSC_VERwithdefined()so-Wundefbuilds under GCC/Clang don't warn. Seepatches/0003-mlas-h-msc-ver-guard.diff.lib/core/common/{narrow,common}.h— minimal shims for ORT internals that MLAS calls (not present upstream as MLAS sources, only as ORT includes). These are new files, not edits — no.diffneeded.
Build flags
HAVE_MLASis set by this directory'sCMakeLists.txtwhen the host arch/OS is wired up.BUILD_MLAS_NO_ONNXRUNTIME=1,MLAS_OPENCV_THREADING=1,MLAS_GEMM_ONLY=1are set as private compile definitions on the OBJECT library.
Caller in OpenCV
The thin wrapper that dispatches OpenCV GEMMs to MLAS lives at
modules/dnn/src/layers/cpu_kernels/mlas_gemm.{hpp,cpp}.
It only includes mlas.h (the public header) and falls back to the existing
fast_gemm path when MLAS is unavailable or the requested shape is unsupported.
Upstream unit tests
Unit tests for the SGEMM kernels live in upstream ONNX Runtime under
onnxruntime/test/mlas. They are not vendored here; OpenCV's own DNN tests
exercise the integration.