gcc/0332-Bugfix-Can-not-find-fdata-file.patch
2024-12-30 22:18:32 +08:00

196 lines
6.1 KiB
Diff

From e0b6e2e9d8226881886c28826a2fe2e55fd29511 Mon Sep 17 00:00:00 2001
From: zhenyu--zhao_admin <zhaozhenyu17@huawei.com>
Date: Sun, 29 Dec 2024 14:11:14 +0800
Subject: [PATCH] [Bugfix] Can not find fdata file.
---
gcc/ai-optimizer.cc | 49 ++++++++++++++++++++++++++++++++++++---------
gcc/ai4c-infer.cc | 28 ++++----------------------
gcc/gcc.cc | 31 +++++++++++++++++++++++++---
gcc/gcc.h | 1 +
4 files changed, 73 insertions(+), 36 deletions(-)
diff --git a/gcc/ai-optimizer.cc b/gcc/ai-optimizer.cc
index c3d99dd85..70ff24077 100644
--- a/gcc/ai-optimizer.cc
+++ b/gcc/ai-optimizer.cc
@@ -284,19 +284,50 @@ 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)
+ char native_file[512];
+ 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 42922e1ca..4cd4bfb00 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 179d507f2..b4beb1957 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,33 @@ 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 63231ddb3..ff3ae8bed 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;
--
2.43.0