diff --git a/CMakeLists.txt b/CMakeLists.txt index c6067abb354a8502b96861751ba6ca0cea92a5fb..40af31918fa52f5d6096dc83728f65eb6872810e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -129,6 +129,7 @@ ExternalProject_Add(runtime_libraries -DSHARED_LIB_EXT=${SHARED_LIB_EXT} -DLINKSETSIZE=${LINKSETSIZE} -DRT_TARGETS:STRING=${_TARGETS} + -DBUILDGENERIC=${BUILDGENERIC} ) # We set dependencies on the configure step, this makes sure we propogate # certain values (such as from sac2crc). diff --git a/cmake/runtime/CMakeLists.txt b/cmake/runtime/CMakeLists.txt index a51925117f469a31142438c9036bc6a422ab5dba..e1678cd28ff480f1ef681f7b9c17a599e920dbd7 100644 --- a/cmake/runtime/CMakeLists.txt +++ b/cmake/runtime/CMakeLists.txt @@ -72,6 +72,13 @@ MACRO (ADD_LIBSAC_TARGET SRC_DIR VARIANT_TARGET) # NOTE (hans) we use the `rmod` target which uses compiler and linker specified # through the RCC and RLD sac2crc variables (additionally we use SET (SACCC "${SAC2C_BUILD_DIR}/saccc ${SAC2C_EXEC} rmod ${TARGET}") + + # In case we are using BUILDGENERIC, we need propogate the sac2c `-generic` + # flag to all the C files that we compile. + IF (BUILDGENERIC) + SET (SACCC "${SACCC} -Xs '-generic'") + ENDIF () + SET (CMAKE_C_COMPILE_OBJECT "${SACCC} -o -c ") # -o -c ") diff --git a/cmake/runtime/sac2c-variables.cmake b/cmake/runtime/sac2c-variables.cmake index de2918fd14f0ce62e4ec7485cbebd6f70f8694a7..9ffc25d05a5462b967a30b58decc9a7a671ac47d 100644 --- a/cmake/runtime/sac2c-variables.cmake +++ b/cmake/runtime/sac2c-variables.cmake @@ -1,5 +1,12 @@ # Create local variant of the SAC2C flags SET (SAC2C_T ${SAC2C_EXEC} -target ${TARGET}) + +# In case of BUILDGENERIC we need to propagate the -generic flag +# to all the sac libraries. +IF (BUILDGENERIC) + SET (SAC2C_T ${SAC2C_T} -generic) +ENDIF () + SET (SAC2C ${SAC2C_T} -Xc "\"${SAC2C_EXTRA_INC}\"" -Xtc "\"${SAC2C_EXTRA_INC}\"") SET (SAC2C_NT ${SAC2C_EXEC} -Xc "\"${SAC2C_EXTRA_INC}\"" -Xtc "\"${SAC2C_EXTRA_INC}\"") # defaults to SEQ diff --git a/cmake/sac2c/config.cmake b/cmake/sac2c/config.cmake index f9d592d6887c04ea71bfc0d798a88a86966ebca1..fedf0579f474d80ccfa460ea54c5f999177731f2 100644 --- a/cmake/sac2c/config.cmake +++ b/cmake/sac2c/config.cmake @@ -560,6 +560,7 @@ SET (INCS "${SAC2CRC_INCS_STR}:") # all variables need to be colon separa IF ((CMAKE_COMPILER_IS_GNUCC OR CLANG) AND (NOT MACC)) SET (GCC_FLAGS "") SET (GCC_NATIVE_FLAGS "") + SET (GCC_GENERIC_FLAGS "") CHECK_CC_FLAG ("-Wall" GCC_FLAGS) CHECK_CC_FLAG ("-Wextra" GCC_FLAGS) CHECK_CC_FLAG ("-Wstrict-prototypes" GCC_FLAGS) @@ -578,19 +579,15 @@ IF ((CMAKE_COMPILER_IS_GNUCC OR CLANG) AND (NOT MACC)) CHECK_CC_FLAG ("-Wno-strict-overflow" GCC_FLAGS) # allow fall through by virtue of comment CHECK_CC_FLAG ("-Wimplicit-fallthrough=3" GCC_FLAGS) - CHECK_CC_FLAG ("-march=native" GCC_NATIVE_FLAGS) - CHECK_CC_FLAG ("-mtune=native" GCC_NATIVE_FLAGS) # allow for vardic macros to have zero arguments CHECK_CC_FLAG ("-Wno-gnu-zero-variadic-macro-arguments" GCC_FLAGS) # give warnings if we are doing things that don't conform with C standard CHECK_CC_FLAG ("-pedantic" GCC_FLAGS) - # If the BUILDGENERIC flag is on, we build a compiler with -mtune=generic - # but we pass -march=native (if supported) to RCCCFLAGS. - SET (GCC_NATIVE_OR_GENERIC "${GCC_NATIVE_FLAGS}") - IF (BUILDGENERIC) - SET (GCC_NATIVE_OR_GENERIC "-mtune=generic") - ENDIF () + # check that this tune flags work + CHECK_CC_FLAG ("-march=native" GCC_NATIVE_FLAGS) + CHECK_CC_FLAG ("-mtune=native" GCC_NATIVE_FLAGS) + CHECK_CC_FLAG ("-mtune=generic" GCC_GENERIC_FLAGS) # FIXME(artem) Can we get these flags from the Pthread checking macro? EXECUTE_PROCESS ( @@ -606,13 +603,18 @@ IF ((CMAKE_COMPILER_IS_GNUCC OR CLANG) AND (NOT MACC)) SET (OPT_O2 "-O2") SET (OPT_O3 "-O3") SET (OPT_g "-g") - # FIXME (hans): we currently are using these flags for building the compiler as well as - # the SAC sources - which it not optimal for packaging - SET (RCCCFLAGS "${GCC_FLAGS} ${GCC_NATIVE_FLAGS} -std=gnu99 -pedantic -Wno-unused -fno-builtin") + SET (TUNE_native "${GCC_NATIVE_FLAGS}") + SET (TUNE_generic "${GCC_GENERIC_FLAGS}") + SET (RCCCFLAGS "${GCC_FLAGS} -std=gnu99 -pedantic -Wno-unused -fno-builtin") # FIXME (artem): This hack allows us to avoid propagating -Wconversion into default sac2c flags. STRING (REGEX REPLACE "-Wconversion" "" RCCCFLAGS ${RCCCFLAGS}) - SET (DEV_FLAGS "${GCC_FLAGS} ${GCC_NATIVE_OR_GENERIC} -std=gnu99 -g ${FLAGS_LTO}") - SET (PROD_FLAGS "${GCC_FLAGS} ${GCC_NATIVE_OR_GENERIC} -std=gnu99 -g -O3 ${FLAGS_LTO}") + IF (BUILDGENERIC) + SET (DEV_FLAGS "${GCC_FLAGS} ${GCC_GENERIC_FLAGS} -std=gnu99 -g ${FLAGS_LTO}") + SET (PROD_FLAGS "${GCC_FLAGS} ${GCC_GENERIC_FLAGS} -std=gnu99 -g -O3 ${FLAGS_LTO}") + ELSE () + SET (DEV_FLAGS "${GCC_FLAGS} ${GCC_NATIVE_FLAGS} -std=gnu99 -g ${FLAGS_LTO}") + SET (PROD_FLAGS "${GCC_FLAGS} ${GCC_NATIVE_FLAGS} -std=gnu99 -g -O3 ${FLAGS_LTO}") + ENDIF () SET (GENPIC "-fPIC") SET (DEPSFLAG "-M") SET (CPPFILE "${CPP_CMD} -C -x c") @@ -625,6 +627,8 @@ ELSEIF (SUNC) SET (OPT_O2 "-xO4") SET (OPT_O3 "-xO5") SET (OPT_g "-g") + SET (TUNE_native "") + SET (TUNE_generic "") SET (RCLDFLAGS "") SET (RCCCFLAGS "-dalign -fsimple -xsafe=mem -xc99=all") SET (DEV_FLAGS "-erroff=E_CAST_DOESNT_YIELD_LVALUE -g -xc99=all") @@ -641,6 +645,8 @@ ELSEIF (DECC) SET (OPT_O2 "-O2") SET (OPT_O3 "-O3") SET (OPT_g "-g") + SET (TUNE_native "") + SET (TUNE_generic "") SET (RCLDFLAGS "") SET (RCCCFLAGS "") SET (DEV_FLAGS "-g") @@ -654,6 +660,7 @@ ELSEIF (DECC) ELSEIF (MACC) SET (MACCC_FLAGS "") SET (MACCC_NATIVE_FLAGS "") + SET (MACCC_GENERIC_FLAGS "") # TODO(artem) Check whether this helps to handle the bracket error! IF ("${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang") CHECK_CC_FLAG ("-fbracket-depth=2048" MACCC_FLAGS) @@ -715,21 +722,23 @@ ELSEIF (MACC) #CHECK_CC_FLAG ("-Wconversion" MACCC_FLAGS) CHECK_CC_FLAG ("-march=native" MACCC_NATIVE_FLAGS) CHECK_CC_FLAG ("-mtune=native" MACCC_NATIVE_FLAGS) - - # but we pass -march=native (if supported) to RCCCFLAGS. - SET (MACCC_NATIVE_OR_GENERIC "${MACCC_NATIVE_FLAGS}") - IF (BUILDGENERIC) - SET (MACCC_NATIVE_OR_GENERIC "-mtune=generic") - ENDIF () + CHECK_CC_FLAG ("-mtune=generic" MACCC_GENERIC_FLAGS) SET (OPT_O0 "") SET (OPT_O1 "-O1") SET (OPT_O2 "-O2") SET (OPT_O3 "-O3") SET (OPT_g "-g") - SET (RCCCFLAGS "${MACCC_FLAGS} ${MACCC_NATIVE_FLAGS} -std=gnu99 -pedantic -Wno-unused -fno-builtin") - SET (DEV_FLAGS "${MACCC_FLAGS} ${MACCC_NATIVE_OR_GENERIC} -std=gnu99 -pedantic -g ${FLAGS_LTO}") - SET (PROD_FLAGS "${MACCC_FLAGS} ${MACCC_NATIVE_OR_GENERIC} -std=gnu99 -pedantic -g -O3 ${FLAGS_LTO}") + SET (TUNE_native "${MACCC_NATIVE_FLAGS}") + SET (TUNE_generic "${MACCC_GENERIC_FLAGS}") + SET (RCCCFLAGS "${MACCC_FLAGS} -std=gnu99 -pedantic -Wno-unused -fno-builtin") + IF (BUILDGENERIC) + SET (DEV_FLAGS "${MACCC_FLAGS} ${MACCC_GENERIC_FLAGS} -std=gnu99 -pedantic -g ${FLAGS_LTO}") + SET (PROD_FLAGS "${MACCC_FLAGS} ${MACCC_GENERIC_FLAGS} -std=gnu99 -pedantic -g -O3 ${FLAGS_LTO}") + ELSE () + SET (DEV_FLAGS "${MACCC_FLAGS} ${MACCC_NATIVE_FLAGS} -std=gnu99 -pedantic -g ${FLAGS_LTO}") + SET (PROD_FLAGS "${MACCC_FLAGS} ${MACCC_NATIVE_FLAGS} -std=gnu99 -pedantic -g -O3 ${FLAGS_LTO}") + ENDIF () SET (GENPIC "") SET (DEPSFLAG "-M") SET (CPPFILE "${CPP_CMD} -C -x c") diff --git a/setup/sac2crc.backend.cuda.in b/setup/sac2crc.backend.cuda.in index d861f50f75e7178f4a6cbd2a2146154bdf1c3d02..3451efeeb04ba7b35c9bec21e8b1c7b2536b658a 100644 --- a/setup/sac2crc.backend.cuda.in +++ b/setup/sac2crc.backend.cuda.in @@ -20,6 +20,8 @@ CUDA_ARCH := "@CUDA_ARCH@" USE_PHM_API := 0 CEXT := ".cu" CC := "@NVCC_PATH@/nvcc" +TUNE_native := "" +TUNE_generic := "" CFLAGS := "--maxrregcount 20 -Xcompiler -Wall -Xcompiler -fPIC " "-Xcompiler -Wno-unused -Xcompiler -fno-builtin " "-Xcudafe '--diag_suppress=set_but_not_used --diag_suppress=declared_but_not_referenced --diag_suppress=expr_has_no_effect' " diff --git a/setup/sac2crc.pre.in b/setup/sac2crc.pre.in index 98b6b7bf9e01d4fa20a7d4871be390c898effd74..f115bfa3623900c7a0d9250d792d3295746e21d3 100644 --- a/setup/sac2crc.pre.in +++ b/setup/sac2crc.pre.in @@ -78,7 +78,8 @@ * * %cc% Current value of CC * - * %opt% Which OPT_Ox value was selected with -O on the sac2c command line + * %opt% Which OPT_Ox value was selected with -O on the sac2c command line, + * additionally the TUNE_x flags are appended here. * * %dbg% OPT_g if -g was provided on the sac2c command line, empty otherwise * @@ -310,6 +311,16 @@ * into the generated machine code for later usage by a debugger. * This is triggered by the sac2c option -g. * + * TUNE_native The flag here instructs the C compiler to aggressively optimise + * the program, making use of extensions that are typically unique + * to the underlying architecture. Code compiled this way can typically + * not be run on other systems! + * + * TUNE_generic The flag here instructs the C compiler to apply optimisations that + * are applicable to most x86_64 architectures. Vectorisation and + * other architecture specific optimisations are not applied by the + * C compiler. + * * CEXT File name extension for generated Module/Program C source files. * * OBJEXT File name extension for Module/Program object files. @@ -448,6 +459,8 @@ OPT_O1 := "XXXXX" OPT_O2 := "XXXXX" OPT_O3 := "XXXXX" OPT_g := "XXXXX" +TUNE_native := "XXXXX" +TUNE_generic := "XXXXX" LDPATH := "XXXXX" LINK_MOD := "XXXXX" LINK_RMOD := "XXXXX" @@ -487,6 +500,8 @@ OPT_O1 := "@OPT_O1@" OPT_O2 := "@OPT_O2@" OPT_O3 := "@OPT_O3@" OPT_g := "@OPT_g@" +TUNE_native := "@TUNE_native@" +TUNE_generic := "@TUNE_generic@" LINK_MOD := "%ld% %objects% %linkflags% @LD_DYNAMIC@ @LD_PATH@ -o %target%" LINK_RMOD := "%ld% %objects% %linkflags% @LD_DYNAMIC@ @LD_PATH@ -o %target%" diff --git a/src/libsac2c/global/globals.mac b/src/libsac2c/global/globals.mac index 39afab9faa97d6bb11cf32a14552fe12644d06a4..ce6b00e163d6ad03c25a52e72342e76aa2b9c0c8 100644 --- a/src/libsac2c/global/globals.mac +++ b/src/libsac2c/global/globals.mac @@ -592,6 +592,11 @@ GLOBAL (int, cc_optimize, 0, xfree_dummy, ) * C compiler level of optimization */ +GLOBAL (bool, cc_tune_generic, FALSE, xfree_dummy, ) +/* On true, we wil build using the C compilers tune=generic + * flag, instead of its tune=native or similar flag. + */ + /* * Optimization counters */ diff --git a/src/libsac2c/global/options.c b/src/libsac2c/global/options.c index e77bcfd5e93e0af163c592132d78ab360eacf55e..9c51ed1c7f291b1c0931fa7ed23427050ebd1403 100644 --- a/src/libsac2c/global/options.c +++ b/src/libsac2c/global/options.c @@ -828,6 +828,7 @@ AnalyseCommandlineSac2c (int argc, char *argv[]) */ ARGS_FLAG ("g", global.cc_debug = TRUE); + ARGS_FLAG ("generic", global.cc_tune_generic = TRUE); ARGS_FLAG ("gg", global.cc_debug = TRUE; global.cc_debug_extra = TRUE); /* diff --git a/src/libsac2c/global/usage.c b/src/libsac2c/global/usage.c index 729ca6373614cfc3054c6c5d56e1b67c555e0b30..77febe5e8ea4f129a77516e37400860c022639f0 100644 --- a/src/libsac2c/global/usage.c +++ b/src/libsac2c/global/usage.c @@ -1115,16 +1115,21 @@ PrintCCompilerOptions (void) " 3: full C compiler optimizations.\n" " (default: %d)\n" "\n" + " -generic Specify that the generated C code should be compiled\n" + " without any architecture specific optimisations. This\n" + " is useful when compiling for other systems.\n" + " NOTE: if this flag is not given, the C code will be\n" + " aggresively optimised to take advantage of the\n" + " current architecture.\n" + "\n" " NOTE:\n" - " The actual effects of these options are specific to " - "the\n" - " C compiler used for code generation. Both the choice " - "of\n" - " a C compiler as well as the mapping of these generic\n" - " options to compiler-specific optimization options are\n" - " are determined via the sac2crc configuration file.\n" - " For details concerning sac2crc files see below under\n" - " \"customization\".\n", + " The actual effects of these options are specific to\n" + " the C compiler used for code generation. Both the\n" + " choice of a C compiler as well as the mapping of these\n" + " generic options to compiler-specific optimization\n" + " options are determined via the sac2crc configuration\n" + " file. For details concerning sac2crc files see below\n" + " under \"customization\".\n", global.cc_optimize); DBUG_RETURN (); diff --git a/src/libsac2c/modules/cctools.c b/src/libsac2c/modules/cctools.c index d8e7d3e3d202564300018cc205e0ef96627fea39..078bfdb491b654d732e33d2ec513ff54f6c5f9b9 100644 --- a/src/libsac2c/modules/cctools.c +++ b/src/libsac2c/modules/cctools.c @@ -172,22 +172,27 @@ CCTperformTask (ccm_task_t task) /******************* compilation flags ***********************/ // %opt% - const char *opt_subst = ""; + const char *p_opt_subst = ""; switch (global.cc_optimize) { case 0: - opt_subst = global.config.opt_o0; + p_opt_subst = global.config.opt_o0; break; case 1: - opt_subst = global.config.opt_o1; + p_opt_subst = global.config.opt_o1; break; case 2: - opt_subst = global.config.opt_o2; + p_opt_subst = global.config.opt_o2; break; case 3: - opt_subst = global.config.opt_o3; + p_opt_subst = global.config.opt_o3; break; } + // concat the tune flags + char *opt_subst = global.cc_tune_generic + ? STRcatn (3, p_opt_subst, " ", global.config.tune_generic) + : STRcatn (3, p_opt_subst, " ", global.config.tune_native); + // %dbg% const char *dbg_subst = global.cc_debug ? global.config.opt_g : ""; @@ -226,6 +231,7 @@ CCTperformTask (ccm_task_t task) = STRcatn (7, opt_subst, " ", dbg_subst, " ", cflags_subst, " ", sacincludes_subst); if (task == CCT_compileflags) { + MEMfree (opt_subst); MEMfree (cflags_subst); DBUG_RETURN (compileflags_subst); } @@ -290,6 +296,7 @@ CCTperformTask (ccm_task_t task) extlibdirs_subst, " ", saclibs_subst, " ", libs_subst); // Normally this should only be called by sac4c if (task == CCT_linkflags) { + MEMfree (opt_subst); MEMfree (cflags_subst); MEMfree (extlibdirs_subst); MEMfree (modlibdirs_subst); @@ -522,6 +529,7 @@ CCTperformTask (ccm_task_t task) } // Release all non-const strings allocated + MEMfree (opt_subst); MEMfree (cflags_subst); MEMfree (extlibdirs_subst); MEMfree (modlibdirs_subst); diff --git a/src/libsac2c/types/types.h b/src/libsac2c/types/types.h index 73a4a7b4cf3acd952fc475cf138b11215b7a7418..e402d543ffd3ff68674d2f1eb5b9034a7bb30b90 100644 --- a/src/libsac2c/types/types.h +++ b/src/libsac2c/types/types.h @@ -785,6 +785,8 @@ typedef struct TARGET_LIST_T { DEF_RESOURCE (OPT_O2, opt_o2, char *, str) \ DEF_RESOURCE (OPT_O3, opt_o3, char *, str) \ DEF_RESOURCE (OPT_g, opt_g, char *, str) \ + DEF_RESOURCE (TUNE_native, tune_native, char *, str) \ + DEF_RESOURCE (TUNE_generic, tune_generic, char *, str) \ DEF_RESOURCE (COMPILE_MOD, compile_mod, char *, str) \ DEF_RESOURCE (COMPILE_PROG, compile_prog, char *, str) \ DEF_RESOURCE (LINK_MOD, link_mod, char *, str) \ diff --git a/src/tools/saccc.in b/src/tools/saccc.in index 25feb68b80d5691b129dff20747f98f6669dc99a..54497daf3357996cef1f994902e6dfea967703de 100755 --- a/src/tools/saccc.in +++ b/src/tools/saccc.in @@ -37,7 +37,20 @@ for arg in "$@"; do act=$followaction followaction= case $act in - ccarg) cmdline+=(-Xc "$ccarg" -Xc "$arg" -Xl "$ccarg" -Xl "$arg"); continue;; + ccarg) + # when handling a C argument, we need check if the flag has an argument + # such as in the case of `-MF` which expects some filename or of `--param` + # which expects some name and value pair. If so, we append to the cmdline + # the flag and its argument as shown in the else-branch. If instead we have + # some C flag like `-install_name=name.so`, though this flag has an argument, + # it is not seperate from the flag given the `=`. As such we append it to + # cmdline as is, see the then-branch. + if test x"$ccarg" = x; then + cmdline+=(-Xc "$arg" -Xl "$arg") + else + cmdline+=(-Xc "$ccarg" -Xc "$arg" -Xl "$ccarg" -Xl "$arg") + fi + continue;; ldarg) cmdline+=(-Xl "$arg"); continue;; sacarg) cmdline+=("$arg"); continue;; esac