Hi Sai Krishna,
You can use the cpuid
instruction to determine the supported architecture during runtime, and then use that information to conditionally set compiler flags in your Makefile. Here's an example Makefile:
# Determine if AVX512 is supported
AVX512_SUPPORTED := $(shell (echo -n "int main() { __asm__ __volatile__ (\"cpuid\" : : : \"%rax\", \"%rbx\", \"%rcx\", \"%rdx\"); return 0; }" | $(CC) -x c - -o /dev/null -march=native) && echo "1" || echo "0")
# Set compiler flags based on AVX512 support
ifeq ($(AVX512_SUPPORTED), 1)
CFLAGS += -march=native -mavx512f -mavx512cd -mavx512bw -mavx512dq
else
CFLAGS += -march=native
endif
# Add other compiler flags as needed
CFLAGS += -Wall -Wextra -Werror -Weverything -Wfatal-errors
# Rest of your Makefile...
In this example, the AVX512_SUPPORTED
variable is determined using a small C program that includes inline assembly with the cpuid
instruction. The result is then used to conditionally add the necessary compiler flags for AVX512 support.
Please note that the specific compiler flags may vary depending on your compiler and platform. Adjust the flags accordingly based on your compiler documentation and requirements.
Regards,