Skip to content

Commit 3e5ca79

Browse files
authored
pass cpu-architecture arguments only to host code (C;C++) (#4943)
1 parent 4483396 commit 3e5ca79

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

CMakeLists.txt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,13 @@ if (NOT MSVC)
594594
endif()
595595
endif()
596596

597+
function(add_compile_option_cpp ARG)
598+
# Adds a compile option to C/C++ only, but not for Cuda.
599+
# Use, e.g., for CPU-architecture flags.
600+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${ARG}>)
601+
add_compile_options($<$<COMPILE_LANGUAGE:C>:${ARG}>)
602+
endfunction()
603+
597604
if ((${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm") OR (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") OR ("${CMAKE_GENERATOR_PLATFORM_LWR}" MATCHES "arm64"))
598605
message(STATUS "ARM detected")
599606
if (MSVC)
@@ -628,8 +635,7 @@ elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$" OR "${CMAKE_GE
628635
include(cmake/FindSIMD.cmake)
629636
endif ()
630637
if (LLAMA_AVX512)
631-
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX512>)
632-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX512>)
638+
add_compile_option_cpp(/arch:AVX512)
633639
# MSVC has no compile-time flags enabling specific
634640
# AVX512 extensions, neither it defines the
635641
# macros corresponding to the extensions.
@@ -643,37 +649,35 @@ elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$" OR "${CMAKE_GE
643649
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VNNI__>)
644650
endif()
645651
elseif (LLAMA_AVX2)
646-
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX2>)
647-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX2>)
652+
add_compile_option_cpp(/arch:AVX2)
648653
elseif (LLAMA_AVX)
649-
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX>)
650-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX>)
654+
add_compile_option_cpp(/arch:AVX)
651655
endif()
652656
else()
653657
if (LLAMA_NATIVE)
654-
add_compile_options(-march=native)
658+
add_compile_option_cpp(-march=native)
655659
endif()
656660
if (LLAMA_F16C)
657-
add_compile_options(-mf16c)
661+
add_compile_option_cpp(-mf16c)
658662
endif()
659663
if (LLAMA_FMA)
660-
add_compile_options(-mfma)
664+
add_compile_option_cpp(-mfma)
661665
endif()
662666
if (LLAMA_AVX)
663-
add_compile_options(-mavx)
667+
add_compile_option_cpp(-mavx)
664668
endif()
665669
if (LLAMA_AVX2)
666-
add_compile_options(-mavx2)
670+
add_compile_option_cpp(-mavx2)
667671
endif()
668672
if (LLAMA_AVX512)
669-
add_compile_options(-mavx512f)
670-
add_compile_options(-mavx512bw)
673+
add_compile_option_cpp(-mavx512f)
674+
add_compile_option_cpp(-mavx512bw)
671675
endif()
672676
if (LLAMA_AVX512_VBMI)
673-
add_compile_options(-mavx512vbmi)
677+
add_compile_option_cpp(-mavx512vbmi)
674678
endif()
675679
if (LLAMA_AVX512_VNNI)
676-
add_compile_options(-mavx512vnni)
680+
add_compile_option_cpp(-mavx512vnni)
677681
endif()
678682
endif()
679683
elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "ppc64")

0 commit comments

Comments
 (0)