Skip to content

Commit

Permalink
Fix compilation errors if host does not support AVX2 (#50316)
Browse files Browse the repository at this point in the history
* Fix compilation errors if host does not support AVX2

* Add comments to empty preprocessor branches

* Explicitly ask for architecture support

* Apply -march-native only on GCC

* Make sure if clang on x64 would be used
it actually pass -march=native, otherwise AVX2 would not be enabled.

* Ask test use AVX2 when run on Windows.
  • Loading branch information
kant2002 committed Jun 8, 2021
1 parent aa558fa commit bdc5d79
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 22 deletions.
16 changes: 13 additions & 3 deletions src/tests/Interop/PInvoke/Generics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project (GenericsNative)
include ("${CLR_INTEROP_TEST_ROOT}/Interop.cmake")
project (GenericsNative)
include ("${CLR_INTEROP_TEST_ROOT}/Interop.cmake")
if(CLR_CMAKE_TARGET_ARCH_I386)
add_definitions(-DTARGET_X86)
add_definitions(-DTARGET_XARCH)
Expand All @@ -17,6 +17,16 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# The ABI for passing parameters with 32-byte alignment has changed in GCC 4.6
add_compile_options(-Wno-psabi)
endif()
if (MSVC)
# The MSVC require explicitly ask for AVX2 so define would be present.
add_compile_options(/arch:AVX2)
else()
if (NOT CLR_CMAKE_TARGET_ARCH_ARM64 AND NOT CLR_CMAKE_TARGET_ARCH_ARM)
# We need -march=native so we can detect if AVX2 is present.
# ARM does not like that option too and it make no sense to have this detection there.
add_compile_options(-march=native)
endif()
endif()
set(SOURCES
GenericsNative.IUnknown.cpp
GenericsNative.NullableB.cpp
Expand Down Expand Up @@ -87,4 +97,4 @@ set(SOURCES
GenericsNative.VectorU.cpp
)
add_library (GenericsNative SHARED ${SOURCES})
install (TARGETS GenericsNative DESTINATION bin)
install (TARGETS GenericsNative DESTINATION bin)
10 changes: 7 additions & 3 deletions src/tests/Interop/PInvoke/Generics/GenericsNative.Vector256B.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@

#if defined(TARGET_XARCH)
#include <immintrin.h>
#elif defined(TARGET_ARMARCH)
// Intentionally empty
#else
#error Unsupported target architecture
#endif

#if defined(__AVX2__)
typedef __m256i Vector256B;
#elif defined(TARGET_ARMARCH)
#else
typedef struct {
bool e00;
bool e01;
Expand Down Expand Up @@ -45,8 +51,6 @@
bool e30;
bool e31;
} Vector256B;
#else
#error Unsupported target architecture
#endif

static Vector256B Vector256BValue = { };
Expand Down
10 changes: 7 additions & 3 deletions src/tests/Interop/PInvoke/Generics/GenericsNative.Vector256C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@

#if defined(TARGET_XARCH)
#include <immintrin.h>
#elif defined(TARGET_ARMARCH)
// Intentionally empty
#else
#error Unsupported target architecture
#endif

#if defined(__AVX2__)
typedef __m256i Vector256C;
#elif defined(TARGET_ARMARCH)
#else
typedef struct {
char16_t e00;
char16_t e01;
Expand All @@ -29,8 +35,6 @@
char16_t e14;
char16_t e15;
} Vector256C;
#else
#error Unsupported target architecture
#endif

static Vector256C Vector256CValue = { };
Expand Down
12 changes: 8 additions & 4 deletions src/tests/Interop/PInvoke/Generics/GenericsNative.Vector256D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@

#if defined(TARGET_XARCH)
#include <immintrin.h>

typedef __m256d Vector256D;
#elif defined(TARGET_ARMARCH)
// Intentionally empty
#else
#error Unsupported target architecture
#endif

#if defined(__AVX2__)
typedef __m256 Vector256D;
#else
typedef struct {
double e00;
double e01;
double e02;
double e03;
} Vector256D;
#else
#error Unsupported target architecture
#endif

static Vector256D Vector256DValue = { };
Expand Down
10 changes: 7 additions & 3 deletions src/tests/Interop/PInvoke/Generics/GenericsNative.Vector256F.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@

#if defined(TARGET_XARCH)
#include <immintrin.h>
#elif defined(TARGET_ARMARCH)
// Intentionally empty
#else
#error Unsupported target architecture
#endif

#if defined(__AVX2__)
typedef __m256 Vector256F;
#elif defined(TARGET_ARMARCH)
#else
typedef struct {
float e00;
float e01;
Expand All @@ -21,8 +27,6 @@
float e06;
float e07;
} Vector256F;
#else
#error Unsupported target architecture
#endif

static Vector256F Vector256FValue = { };
Expand Down
10 changes: 7 additions & 3 deletions src/tests/Interop/PInvoke/Generics/GenericsNative.Vector256L.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@

#if defined(TARGET_XARCH)
#include <immintrin.h>
#elif defined(TARGET_ARMARCH)
// Intentionally empty
#else
#error Unsupported target architecture
#endif

#if defined(__AVX2__)
typedef __m256i Vector256L;
#elif defined(TARGET_ARMARCH)
#else
typedef struct {
int64_t e00;
int64_t e01;
int64_t e02;
int64_t e03;
} Vector256L;
#else
#error Unsupported target architecture
#endif

static Vector256L Vector256LValue = { };
Expand Down
10 changes: 7 additions & 3 deletions src/tests/Interop/PInvoke/Generics/GenericsNative.Vector256U.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@

#if defined(TARGET_XARCH)
#include <immintrin.h>
#elif defined(TARGET_ARMARCH)
// Intentionally empty
#else
#error Unsupported target architecture
#endif

#if defined(__AVX2__)
typedef __m256i Vector256U;
#elif defined(TARGET_ARMARCH)
#else
typedef struct {
uint32_t e00;
uint32_t e01;
Expand All @@ -21,8 +27,6 @@
uint32_t e06;
uint32_t e07;
} Vector256U;
#else
#error Unsupported target architecture
#endif

static Vector256U Vector256UValue = { };
Expand Down

0 comments on commit bdc5d79

Please sign in to comment.