Skip to content

add loongarch64 build support #1936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmrtlib/build_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ case $BUILD_SIZE in
if [[ $BUILD_64 -eq 1 ]]; then
CROSS_BUILD=1
BUILD_SIZE=64
EXTRA_OPTIONS="export CFLAGS=-m64 CXXFLAGS=-m64"
if [ ${MACHINE_TYPE} == 'loongarch64' ]; then
EXTRA_OPTIONS=""
else
EXTRA_OPTIONS="export CFLAGS=-m64 CXXFLAGS=-m64"
fi
fi
;;
64)
Expand Down
13 changes: 13 additions & 0 deletions media_driver/agnostic/common/cm/cm_mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,40 @@

#include "cm_mem.h"
#include "cm_mem_c_impl.h"

#if !defined(__loongarch64)
#include "cm_mem_sse2_impl.h"
#endif

typedef void(*t_CmFastMemCopy)( void* dst, const void* src, const size_t bytes );
typedef void(*t_CmFastMemCopyWC)( void* dst, const void* src, const size_t bytes );

#if !defined(__loongarch64)
#define CM_FAST_MEM_COPY_CPU_INIT_C(func) (func ## _C)
#define CM_FAST_MEM_COPY_CPU_INIT_SSE2(func) (func ## _SSE2)
#define CM_FAST_MEM_COPY_CPU_INIT(func) (is_SSE2_available ? CM_FAST_MEM_COPY_CPU_INIT_SSE2(func) : CM_FAST_MEM_COPY_CPU_INIT_C(func))
#endif

void CmFastMemCopy( void* dst, const void* src, const size_t bytes )
{
#if defined(__loongarch64)
CmFastMemCopy_C(dst, src, bytes);
#else
static const bool is_SSE2_available = (GetCpuInstructionLevel() >= CPU_INSTRUCTION_LEVEL_SSE2);
static const t_CmFastMemCopy CmFastMemCopy_impl = CM_FAST_MEM_COPY_CPU_INIT(CmFastMemCopy);

CmFastMemCopy_impl(dst, src, bytes);
#endif
}

void CmFastMemCopyWC( void* dst, const void* src, const size_t bytes )
{
#if defined(__loongarch64)
CmFastMemCopyWC_C(dst, src, bytes);
#else
static const bool is_SSE2_available = (GetCpuInstructionLevel() >= CPU_INSTRUCTION_LEVEL_SSE2);
static const t_CmFastMemCopyWC CmFastMemCopyWC_impl = CM_FAST_MEM_COPY_CPU_INIT(CmFastMemCopyWC);

CmFastMemCopyWC_impl(dst, src, bytes);
#endif
}
14 changes: 14 additions & 0 deletions media_driver/agnostic/common/cm/cm_mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@
//!
#pragma once

#if defined(__loongarch64)
#define SIMDE_X86_SSE2_ENABLE_NATIVE_ALIASES
#include <simde/x86/sse2.h>
#else
#include <mmintrin.h>
#include <xmmintrin.h>
#include <emmintrin.h>
#endif

#include "cm_debug.h"
#include "mos_utilities.h"

Expand All @@ -43,7 +49,11 @@ enum CPU_INSTRUCTION_LEVEL
NUM_CPU_INSTRUCTION_LEVELS
};

#if defined(__loongarch64)
typedef __m128i DQWORD; // 128-bits, 16-bytes
#else
typedef __m128 DQWORD; // 128-bits, 16-bytes
#endif
typedef uint32_t PREFETCH[8]; // 32-bytes
typedef uint32_t CACHELINE[8]; // 32-bytes
typedef uint16_t DHWORD[32]; // 512-bits, 64-bytes
Expand Down Expand Up @@ -230,9 +240,12 @@ inline CPU_INSTRUCTION_LEVEL GetCpuInstructionLevel( void )
int cpuInfo[4];
memset( cpuInfo, 0, 4*sizeof(int) );

#if !defined(__loongarch64)
GetCPUID(cpuInfo, 1);
#endif

CPU_INSTRUCTION_LEVEL cpuInstructionLevel = CPU_INSTRUCTION_LEVEL_UNKNOWN;
#if !defined(__loongarch64)
if( (cpuInfo[2] & BIT(19)) && TestSSE4_1() )
{
cpuInstructionLevel = CPU_INSTRUCTION_LEVEL_SSE4_1;
Expand All @@ -253,6 +266,7 @@ inline CPU_INSTRUCTION_LEVEL GetCpuInstructionLevel( void )
{
cpuInstructionLevel = CPU_INSTRUCTION_LEVEL_MMX;
}
#endif

return cpuInstructionLevel;
}
Expand Down
9 changes: 7 additions & 2 deletions media_driver/cmake/linux/media_compile_flags_linux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ set(MEDIA_COMPILER_FLAGS_COMMON

# Enable c++14 features
-std=c++14
# -m32 or -m64
-m${ARCH}

# Global defines
-DLINUX=1
Expand All @@ -66,6 +64,13 @@ set(MEDIA_COMPILER_FLAGS_COMMON
-g
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Disable x86-only legacy iGPUs
# DG1, DG2 and BMG (possibly) dGPUs rely on GEN12
-DGEN8=OFF
-DGEN9=OFF
-DGEN11=OFF

This will trim away code that will never be reached on non-x86 platforms to save space. Theoretically it is also possible to disable the GEN12+ iGPUs. However, this would increase the maintenance burden.

The same if true for media_softlet/cmake/linux/media_compile_flags_linux.cmake.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking of reusing the common flags for all architectures and then add architecture specific flags in conditions, so we don't have the long same code.

If GEN8, GEN9 and GEN11 are x86 only, it should be fine to disable them on other platforms.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Apply common build options first, then conditionally add platform-specific options.

Intel has never released iGPU for non-x86 platforms. Now iHD*.so has swelled to 40 MB+ on x86, disabling irrelevant platforms (especially media kernels) on non-x86 should shrink it a lot.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is loongarch CPU and Intel Arc , so suppose you could disable all integrate card support.
about the patch itself:
no need to check whether it is loongarch, because if host is arm or other arch it also need some changes. , just check whether SSE instincts exists? there are another similar PR
#1446 to enable ARC on non-x86 platforms

from another perspective , I am wondering whether ARC still use cmfastcopy function .

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XinfengZhang anothe PR of gmmlib is trying to import sse support to riscv: intel/gmmlib#131. And the supported arm64 gmmlib is using sse2neon for sse2.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nyanmisaka I find that add flags like -DGEN8=OFF won't work, because MEDIA_COMPILER_FLAGS_COMMON defines flags used by compiler, not cmake. We should add it when running cmake command.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nyanmisaka I find that add flags like -DGEN8=OFF won't work, because MEDIA_COMPILER_FLAGS_COMMON defines flags used by compiler, not cmake. We should add it when running cmake command.

You seem to be right. But it is unlikely that downstream package maintainers will be aware of these undocumented flags and make small builds.

If possible, it might be better to let CMAKE_SYSTEM_PROCESSOR take effect in media_gen_flags_linux.cmake.


if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^loongarch")
list(APPEND MEDIA_COMPILER_FLAGS_COMMON "-Wno-overloaded-virtual")
else()
# -m32 or -m64
list(APPEND MEDIA_COMPILER_FLAGS_COMMON "-m${ARCH}")
endif()

if(MEDIA_BUILD_HARDENING)
set(MEDIA_COMPILER_FLAGS_COMMON
${MEDIA_COMPILER_FLAGS_COMMON}
Expand Down
9 changes: 9 additions & 0 deletions media_driver/linux/common/cm/hal/osservice/cm_mem_os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,27 @@
#include "cm_mem.h"
#include "cm_mem_os.h"
#include "cm_mem_os_c_impl.h"

#if !defined(__loongarch64)
#include "cm_mem_os_sse4_impl.h"
#endif

typedef void(*t_CmFastMemCopyFromWC)( void* dst, const void* src, const size_t bytes );

#if !defined(__loongarch64)
#define CM_FAST_MEM_COPY_CPU_INIT_C(func) (func ## _C)
#define CM_FAST_MEM_COPY_CPU_INIT_SSE4(func) (func ## _SSE4)
#define CM_FAST_MEM_COPY_CPU_INIT(func) (is_SSE4_available ? CM_FAST_MEM_COPY_CPU_INIT_SSE4(func) : CM_FAST_MEM_COPY_CPU_INIT_C(func))
#endif

void CmFastMemCopyFromWC( void* dst, const void* src, const size_t bytes, CPU_INSTRUCTION_LEVEL cpuInstructionLevel )
{
#if defined(__loongarch64)
CmFastMemCopyFromWC_C(dst, src, bytes);
#else
static const bool is_SSE4_available = (cpuInstructionLevel >= CPU_INSTRUCTION_LEVEL_SSE4_1);
static const t_CmFastMemCopyFromWC CmFastMemCopyFromWC_impl = CM_FAST_MEM_COPY_CPU_INIT(CmFastMemCopyFromWC);

CmFastMemCopyFromWC_impl(dst, src, bytes);
#endif
}
9 changes: 9 additions & 0 deletions media_driver/linux/common/cm/hal/osservice/cm_mem_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
#pragma once

#include <iostream>
#if defined(__loongarch64)
#define SIMDE_X86_SSE2_ENABLE_NATIVE_ALIASES
#include <simde/x86/sse2.h>
#else
#include "cpuid.h"
#include <smmintrin.h>
#endif

typedef uintptr_t UINT_PTR;
#define __fastcall
Expand Down Expand Up @@ -120,6 +125,9 @@ Inline Function:
\*****************************************************************************/
inline void GetCPUID(int cpuInfo[4], int infoType)
{
#if defined(__loongarch64)
return;
#else
#ifndef NO_EXCEPTION_HANDLING
__try
{
Expand All @@ -135,6 +143,7 @@ inline void GetCPUID(int cpuInfo[4], int infoType)
return;
}
#endif //NO_EXCEPTION_HANDLING
#endif //defined(__loongarch64)
}

void CmFastMemCopyFromWC( void* dst, const void* src, const size_t bytes, CPU_INSTRUCTION_LEVEL cpuInstructionLevel );
43 changes: 43 additions & 0 deletions media_driver/media_top_cmake.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ if(NOT DEFINED SKIP_GMM_CHECK)
endif()
endif(NOT DEFINED SKIP_GMM_CHECK)

if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^loongarch")
find_path(SIMDE_INCLUDE_DIR
NAMES simde/simde-common.h # A key SIMDE header
PATHS /usr/include /usr/local/include # Default paths
DOC "Path to SIMDE headers"
)
if(SIMDE_INCLUDE_DIR)
include_directories(${SIMDE_INCLUDE_DIR})
message(STATUS "Found SIMDE: ${SIMDE_INCLUDE_DIR}")
else()
message(FATAL_ERROR "SIMDE not found. Install it or set SIMDE_INCLUDE_DIR manually.")
endif()
endif()

message("-- media -- PLATFORM = ${PLATFORM}")
message("-- media -- ARCH = ${ARCH}")
message("-- media -- CMAKE_CURRENT_LIST_DIR = ${CMAKE_CURRENT_LIST_DIR}")
Expand Down Expand Up @@ -248,8 +262,11 @@ set_source_files_properties(${CP_COMMON_SHARED_SOURCES_} PROPERTIES LANGUAGE "CX
set_source_files_properties(${CP_COMMON_NEXT_SOURCES_} PROPERTIES LANGUAGE "CXX")
set_source_files_properties(${CP_SOURCES_} PROPERTIES LANGUAGE "CXX")
set_source_files_properties(${SOFTLET_DDI_SOURCES_} PROPERTIES LANGUAGE "CXX")

if(NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES "^loongarch")
set_source_files_properties(${SOURCES_SSE2} PROPERTIES LANGUAGE "CXX")
set_source_files_properties(${SOURCES_SSE4} PROPERTIES LANGUAGE "CXX")
endif()

# MHW settings
set(SOFTLET_MHW_PRIVATE_INCLUDE_DIRS_
Expand Down Expand Up @@ -421,13 +438,15 @@ set (VP_PRIVATE_INCLUDE_DIRS_
${VP_PRIVATE_INCLUDE_DIRS_}
${SOFTLET_VP_PRIVATE_INCLUDE_DIRS_})

if(NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES "^loongarch")
add_library(${LIB_NAME}_SSE2 OBJECT ${SOURCES_SSE2})
target_compile_options(${LIB_NAME}_SSE2 PRIVATE -msse2)
target_include_directories(${LIB_NAME}_SSE2 BEFORE PRIVATE ${SOFTLET_MOS_PREPEND_INCLUDE_DIRS_} ${MOS_PUBLIC_INCLUDE_DIRS_} ${SOFTLET_MOS_PUBLIC_INCLUDE_DIRS_} ${COMMON_PRIVATE_INCLUDE_DIRS_} ${SOFTLET_MHW_PRIVATE_INCLUDE_DIRS_} ${SOFTLET_DDI_PUBLIC_INCLUDE_DIRS_})

add_library(${LIB_NAME}_SSE4 OBJECT ${SOURCES_SSE4})
target_compile_options(${LIB_NAME}_SSE4 PRIVATE -msse4.1)
target_include_directories(${LIB_NAME}_SSE4 BEFORE PRIVATE ${SOFTLET_MOS_PREPEND_INCLUDE_DIRS_} ${MOS_PUBLIC_INCLUDE_DIRS_} ${SOFTLET_MOS_PUBLIC_INCLUDE_DIRS_} ${COMMON_PRIVATE_INCLUDE_DIRS_} ${SOFTLET_MHW_PRIVATE_INCLUDE_DIRS_} ${SOFTLET_DDI_PUBLIC_INCLUDE_DIRS_})
endif()

add_library(${LIB_NAME}_COMMON OBJECT ${COMMON_SOURCES_} ${SOFTLET_DDI_SOURCES_})
set_property(TARGET ${LIB_NAME}_COMMON PROPERTY POSITION_INDEPENDENT_CODE 1)
Expand Down Expand Up @@ -603,6 +622,17 @@ target_include_directories(${LIB_NAME}_mos_softlet BEFORE PRIVATE
############## MOS LIB END ########################################

############## Media Driver Static and Shared Lib #################
if( ${CMAKE_SYSTEM_PROCESSOR} MATCHES "^loongarch")
add_library(${LIB_NAME} SHARED
$<TARGET_OBJECTS:${LIB_NAME}_mos>
$<TARGET_OBJECTS:${LIB_NAME}_COMMON>
$<TARGET_OBJECTS:${LIB_NAME}_CODEC>
$<TARGET_OBJECTS:${LIB_NAME}_VP>
$<TARGET_OBJECTS:${LIB_NAME}_CP>
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_VP>
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_CODEC>
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_COMMON>)
else()
add_library(${LIB_NAME} SHARED
$<TARGET_OBJECTS:${LIB_NAME}_mos>
$<TARGET_OBJECTS:${LIB_NAME}_COMMON>
Expand All @@ -614,8 +644,20 @@ add_library(${LIB_NAME} SHARED
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_VP>
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_CODEC>
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_COMMON>)
endif()


if( ${CMAKE_SYSTEM_PROCESSOR} MATCHES "^loongarch")
add_library(${LIB_NAME_STATIC} STATIC
$<TARGET_OBJECTS:${LIB_NAME}_mos>
$<TARGET_OBJECTS:${LIB_NAME}_COMMON>
$<TARGET_OBJECTS:${LIB_NAME}_CODEC>
$<TARGET_OBJECTS:${LIB_NAME}_VP>
$<TARGET_OBJECTS:${LIB_NAME}_CP>
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_VP>
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_CODEC>
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_COMMON>)
else()
add_library(${LIB_NAME_STATIC} STATIC
$<TARGET_OBJECTS:${LIB_NAME}_mos>
$<TARGET_OBJECTS:${LIB_NAME}_COMMON>
Expand All @@ -627,6 +669,7 @@ add_library(${LIB_NAME_STATIC} STATIC
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_VP>
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_CODEC>
$<TARGET_OBJECTS:${LIB_NAME}_SOFTLET_COMMON>)
endif()

set_target_properties(${LIB_NAME_STATIC} PROPERTIES OUTPUT_NAME ${LIB_NAME})

Expand Down
9 changes: 6 additions & 3 deletions media_softlet/cmake/linux/media_compile_flags_linux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ set(MEDIA_COMPILER_FLAGS_COMMON
-ffunction-sections
-Wl,--gc-sections

# -m32 or -m64
-m${ARCH}

# Global defines
-DLINUX=1
-DLINUX
Expand All @@ -65,6 +62,12 @@ set(MEDIA_COMPILER_FLAGS_COMMON
-g
)

if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^loongarch")
list(APPEND MEDIA_COMPILER_FLAGS_COMMON "-Wno-overloaded-virtual")
else()
# -m32 or -m64
list(APPEND MEDIA_COMPILER_FLAGS_COMMON "-m${ARCH}")
endif()

if(${UFO_MARCH} STREQUAL "slm")
set(MEDIA_COMPILER_FLAGS_COMMON
Expand Down