diff --git a/gcc/ai-optimizer.cc b/gcc/ai-optimizer.cc index c3d99dd85c306e9d13b0e89bb8e736a9a0bf5890..cb96c139da889fe54e4400863f6436fef2344393 100644 --- a/gcc/ai-optimizer.cc +++ b/gcc/ai-optimizer.cc @@ -284,19 +284,52 @@ static int graph_infer (int argc1, const char **argv1, const char *mops, int argc2, int64_t *argv2) { - char *gcc_exec_prefix = getenv ("ONNX_FDATA_PATH"); - if (gcc_exec_prefix == NULL) + char gcc_exec_prefix[512]; + ssize_t len = readlink ("/proc/self/exe", gcc_exec_prefix, + sizeof (gcc_exec_prefix) - 1); + if (len == -1) return 0; + char native_file[512]; - if (gcc_exec_prefix) + strncpy (native_file, gcc_exec_prefix, sizeof (native_file) - 1); + const char *target = "bin/gcc"; + const char *target_cc1 = "cc1"; + const char *target_gpp = "bin/g++"; + const char *target_cc1plus = "cc1plus"; + const char *target_gfortran = "bin/gfortran"; + const char *target_f951 = "f951"; + const char *replacement = "../libexec/gcc/optimizer.fdata"; + const char *replacement_front_end = "../../optimizer.fdata"; + + + /* Replace the part of the current executable file path after the last slash + to locate the model file. */ + if (strstr (native_file, target) != NULL || + strstr (native_file, target_gpp) != NULL || + strstr (native_file, target_gfortran) != NULL) { - const char *onnx_fdata = "optimizer.fdata"; - strncpy (native_file, gcc_exec_prefix, sizeof (native_file) - 1); - native_file[sizeof (native_file) - 1] = '\0'; char *last_slash = strrchr (native_file, '/'); - if (last_slash) - strcpy (last_slash + 1, onnx_fdata); + if (last_slash != NULL) + { + size_t prefix_len = last_slash - native_file + 1; + native_file[prefix_len] = '\0'; + strncat (native_file, replacement, sizeof (native_file) - + strlen (native_file) - 1); + } + } + else if (strstr (native_file, target_cc1) != NULL || + strstr (native_file, target_cc1plus) != NULL || + strstr (native_file, target_f951) != NULL) + { + char *last_slash = strrchr (native_file, '/'); + if (last_slash != NULL) + { + size_t prefix_len = last_slash - native_file + 1; + native_file[prefix_len] = '\0'; + strncat (native_file, replacement_front_end, sizeof (native_file) - + strlen (native_file) - 1); + } } if (access (native_file, F_OK) == 0) diff --git a/gcc/ai4c-infer.cc b/gcc/ai4c-infer.cc index 42922e1ca14d1b55c4317c5f4ac97537707e156d..4cd4bfb00a055f633d01c985af3d8ef2ecc0cc50 100644 --- a/gcc/ai4c-infer.cc +++ b/gcc/ai4c-infer.cc @@ -333,29 +333,11 @@ preprocess (int argc, int64_t *argv, int64_t *in_modes) static int graph_infer (int argc, const char *argv, int argc2, int64_t *argv2) { - char *gcc_exec_prefix = getenv ("ONNX_FDATA_PATH"); - if (gcc_exec_prefix == NULL) - return 0; - char file_name[512]; - - if (gcc_exec_prefix) - { - const char *onnx_fdata = "onnx.fdata"; - strncpy (file_name, gcc_exec_prefix, sizeof (file_name) - 1); - file_name[sizeof (file_name) - 1] = '\0'; - char *last_slash = strrchr (file_name, '/'); - if (last_slash) - strcpy (last_slash + 1, onnx_fdata); - } - + const char *file_name = getenv ("GCC_AI4C_ONNX_FDATA"); if (access (file_name, F_OK) == 0) - { - fill_node (file_name); - } + fill_node (file_name); else - { - return 0; - } + return 0; int64_t in_modes[M_MODE_SIZE]; @@ -441,9 +423,7 @@ int get_optimize_decision_from_ai4c () { if (initialized== 1) - { - return optimize_result; - } + return optimize_result; if (native_tune && (strchr (native_tune, '+') != NULL)) { char hash[65]; diff --git a/gcc/gcc.cc b/gcc/gcc.cc index 179d507f2554f1e32a8ce25bbe2f44087cdfd8da..d2474a5800cce03214d113c013ea558f6b68e7c3 100644 --- a/gcc/gcc.cc +++ b/gcc/gcc.cc @@ -8133,6 +8133,7 @@ driver::main (int argc, char **argv) putenv_COLLECT_GCC (argv[0]); maybe_putenv_COLLECT_LTO_WRAPPER (); maybe_putenv_OFFLOAD_TARGETS (); + putenv_ONNX_FDATA (); handle_unrecognized_options (); if (completion) @@ -8189,9 +8190,6 @@ driver::expand_at_files (int *argc, char ***argv) const void driver::decode_argv (int argc, const char **argv) { - const char* libexec_path = standard_libexec_prefix; - if (libexec_path) - setenv ("ONNX_FDATA_PATH", libexec_path, 1); init_opts_obstack (); init_options_struct (&global_options, &global_options_set); @@ -8590,6 +8588,34 @@ driver::maybe_putenv_COLLECT_LTO_WRAPPER () const } +/* Set up to remember the pathname of the onnx.fdata. */ + +void +driver::putenv_ONNX_FDATA () const +{ + char *lto_wrapper_file; + lto_wrapper_file = find_a_program ("lto-wrapper"); + + if (lto_wrapper_file) + { + lto_wrapper_file = convert_white_space (lto_wrapper_file); + char native_file[512]; + const char *onnx_fdata = "../../onnx.fdata"; + strncpy (native_file, lto_wrapper_file, sizeof (native_file) - 1); + native_file[sizeof (native_file) - 1] = '\0'; + char *last_slash = strrchr (native_file, '/'); + if (last_slash) + strcpy (last_slash + 1, onnx_fdata); + obstack_init (&collect_obstack); + obstack_grow (&collect_obstack, "GCC_AI4C_ONNX_FDATA=", + sizeof ("GCC_AI4C_ONNX_FDATA=") - 1); + obstack_grow (&collect_obstack, native_file, + strlen ( native_file) + 1); + xputenv (XOBFINISH (&collect_obstack, char *)); + } + +} + /* Set up to remember the names of offload targets. */ void diff --git a/gcc/gcc.h b/gcc/gcc.h index 63231ddb3ee7d5e7fcf90559a5d2bb8686519654..ff3ae8bed0513cf734718a1832b82b25d3ca60d1 100644 --- a/gcc/gcc.h +++ b/gcc/gcc.h @@ -44,6 +44,7 @@ class driver void set_up_specs () const; void putenv_COLLECT_GCC (const char *argv0) const; void maybe_putenv_COLLECT_LTO_WRAPPER () const; + void putenv_ONNX_FDATA () const; void maybe_putenv_OFFLOAD_TARGETS () const; void handle_unrecognized_options (); int maybe_print_and_exit () const; diff --git a/gcc/opts-common.cc b/gcc/opts-common.cc index 35db76b84cae2858cdbbc591c71d1aab345637ad..ee94723fc56e65bd04f4fe9c6a56bf9a42fab042 100644 --- a/gcc/opts-common.cc +++ b/gcc/opts-common.cc @@ -1070,8 +1070,6 @@ handle_machine_option (unsigned int lang_mask, global_options.x_param_ipa_prefetch_distance_factor}; int64_t output_pred = get_optimize_decision_from_optimizer ( argc, argv, "hip09", argc_hw, argv_hw); - if (output_pred == 1) - return output_pred; if (output_pred != 1) return ret;