Skip to content

Commit

Permalink
Patch onnxruntime to compile with clang-cl (#165)
Browse files Browse the repository at this point in the history
* Fix onnxruntime port for clang-cl

* Bump port versions

* [onnxruntime] clang arch options for SSE

* several features fail with clang-cl, but ok
* may requires customized triplet file for clang-cl

---------

Co-authored-by: luncliff <[email protected]>
  • Loading branch information
kokulshan and luncliff authored Feb 5, 2024
1 parent 5a6b3bf commit 4591b57
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 2 deletions.
80 changes: 80 additions & 0 deletions ports/onnxruntime/fix-clang-cl-simd-compile.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
diff --git a/cmake/onnxruntime_mlas.cmake b/cmake/onnxruntime_mlas.cmake
index bee83ff07c..04b4cf42b7 100644
--- a/cmake/onnxruntime_mlas.cmake
+++ b/cmake/onnxruntime_mlas.cmake
@@ -159,15 +159,27 @@ function(setup_mlas_source_for_windows)
)
set_source_files_properties(${mlas_platform_srcs_avx2} PROPERTIES COMPILE_FLAGS "/arch:AVX2")

+ file(GLOB_RECURSE mlas_platform_srcs_avx512 CONFIGURE_DEPENDS
+ "${MLAS_SRC_DIR}/intrinsics/avx512/*.cpp"
+ )
+ set(mlas_platform_srcs_amx "${MLAS_SRC_DIR}/qgemm_kernel_amx.cpp")
+
+ # clang-cl requires us to enable the platform feature flags explicitly to compile the intrinsics code
+ # unlike MSVC. See: https://github.com/llvm/llvm-project/issues/53520
+ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+ set_source_files_properties(${mlas_platform_srcs_avx512} PROPERTIES COMPILE_FLAGS "/arch:AVX512")
+ set_source_files_properties(${mlas_platform_srcs_amx} PROPERTIES COMPILE_FLAGS "/arch:AVX512 -mamx-tile -mamx-int8")
+ endif()
+
target_sources(onnxruntime_mlas PRIVATE
${MLAS_SRC_DIR}/dgemm.cpp
${mlas_platform_srcs_avx}
${mlas_platform_srcs_avx2}
- ${MLAS_SRC_DIR}/qgemm_kernel_amx.cpp
+ ${mlas_platform_srcs_avx512}
+ ${mlas_platform_srcs_amx}
${MLAS_SRC_DIR}/qgemm_kernel_avx2.cpp
${MLAS_SRC_DIR}/qgemm_kernel_sse.cpp
${MLAS_SRC_DIR}/qgemm_kernel_sse41.cpp
- ${MLAS_SRC_DIR}/intrinsics/avx512/quantize_avx512f.cpp
${MLAS_SRC_DIR}/amd64/QgemmU8S8KernelAmx.asm
${MLAS_SRC_DIR}/amd64/QgemmU8S8KernelAvx2.asm
${MLAS_SRC_DIR}/amd64/QgemmU8U8KernelAvx2.asm
@@ -205,9 +217,15 @@ function(setup_mlas_source_for_windows)
${MLAS_SRC_DIR}/amd64/ErfKernelFma3.asm
)
if (NOT onnxruntime_ORT_MINIMAL_BUILD)
- target_sources(onnxruntime_mlas PRIVATE
+ set(onnxruntime_mlas_q4gemm_avx512
${MLAS_SRC_DIR}/q4gemm_avx512.cpp
)
+ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+ # clang-cl requires us to enable the platform feature flags explicitly to compile the intrinsics code
+ # unlike MSVC. See: https://github.com/llvm/llvm-project/issues/53520
+ set_source_files_properties(${onnxruntime_mlas_q4gemm_avx512} PROPERTIES COMPILE_FLAGS "/arch:AVX512 -mavx512vnni")
+ endif()
+ target_sources(onnxruntime_mlas PRIVATE ${onnxruntime_mlas_q4gemm_avx512})
endif()
else()
target_sources(onnxruntime_mlas PRIVATE
diff --git a/cmake/onnxruntime_mlas.cmake b/cmake/onnxruntime_mlas.cmake
index 5c294b5..a466c77 100644
--- a/cmake/onnxruntime_mlas.cmake
+++ b/cmake/onnxruntime_mlas.cmake
@@ -169,6 +169,9 @@ function(setup_mlas_source_for_windows)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set_source_files_properties(${mlas_platform_srcs_avx512} PROPERTIES COMPILE_FLAGS "/arch:AVX512")
set_source_files_properties(${mlas_platform_srcs_amx} PROPERTIES COMPILE_FLAGS "/arch:AVX512 -mamx-tile -mamx-int8")
+ # https://clang.llvm.org/docs/UsersManual.html#cpu-architectures-features-and-limitations
+ set_source_files_properties(${MLAS_SRC_DIR}/qgemm_kernel_sse.cpp PROPERTIES COMPILE_FLAGS "-march=x86-64")
+ set_source_files_properties(${MLAS_SRC_DIR}/qgemm_kernel_sse41.cpp PROPERTIES COMPILE_FLAGS "-march=x86-64-v2")
endif()

target_sources(onnxruntime_mlas PRIVATE
diff --git a/onnxruntime/core/mlas/lib/qgemm_kernel_sse41.cpp b/onnxruntime/core/mlas/lib/qgemm_kernel_sse41.cpp
index 68931c5..6c095bd 100644
--- a/onnxruntime/core/mlas/lib/qgemm_kernel_sse41.cpp
+++ b/onnxruntime/core/mlas/lib/qgemm_kernel_sse41.cpp
@@ -16,6 +16,10 @@ Abstract:

#include "mlasi.h"
#include "qgemm.h"
+#if defined(__clang__)
+#include <smmintrin.h>
+#include <tmmintrin.h>
+#endif

// N.B. MSVC does not require turning on SSE 4.1 intrinsics and the current use
// for this code is Windows only, so restrict this kernel to that environment.
16 changes: 16 additions & 0 deletions ports/onnxruntime/fix-llvm-rc-unicode.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
diff --git a/onnxruntime/core/dll/onnxruntime.rc b/onnxruntime/core/dll/onnxruntime.rc
index 4b08dfdb7e..c566a4f713 100644
--- a/onnxruntime/core/dll/onnxruntime.rc
+++ b/onnxruntime/core/dll/onnxruntime.rc
@@ -32,9 +32,9 @@ BEGIN
VALUE "FileDescription", "ONNX Runtime"
VALUE "FileVersion", VER_STRING
VALUE "InternalName", "ONNX Runtime"
- VALUE "LegalCopyright", "\251 Microsoft Corporation. All rights reserved."
+ VALUE "LegalCopyright", L"\251 Microsoft Corporation. All rights reserved."
VALUE "OriginalFilename", FILE_NAME
- VALUE "ProductName", "Microsoft\256 Windows\256 Operating System"
+ VALUE "ProductName", L"Microsoft\256 Windows\256 Operating System"
VALUE "ProductVersion", VER_STRING
END
END
2 changes: 2 additions & 0 deletions ports/onnxruntime/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ vcpkg_from_github(
fix-cmake.patch
fix-source-flatbuffers.patch
fix-sources.patch
fix-clang-cl-simd-compile.patch
fix-llvm-rc-unicode.patch
)
file(COPY "${CMAKE_CURRENT_LIST_DIR}/onnxruntime_vcpkg_deps.cmake" DESTINATION "${SOURCE_PATH}/cmake/external")

Expand Down
2 changes: 1 addition & 1 deletion ports/onnxruntime/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "onnxruntime",
"version-date": "2024-01-04",
"port-version": 1,
"port-version": 2,
"description": "ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator",
"homepage": "https://www.onnxruntime.ai",
"dependencies": [
Expand Down
2 changes: 1 addition & 1 deletion versions/baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
},
"onnxruntime": {
"baseline": "2024-01-04",
"port-version": 1
"port-version": 2
},
"opencl-headers": {
"baseline": "2023-12-13",
Expand Down
5 changes: 5 additions & 0 deletions versions/o-/onnxruntime.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"versions": [
{
"git-tree": "233f074d172ba82b09562457e252601b04fc99cb",
"version-date": "2024-01-04",
"port-version": 2
},
{
"git-tree": "6af6c57d2c15411b9baba5f49e5ecd5608fa9477",
"version-date": "2024-01-04",
Expand Down

0 comments on commit 4591b57

Please sign in to comment.