1006 lines
64 KiB
Diff
1006 lines
64 KiB
Diff
|
|
From 19a1074e87577f9b511f382569ac081871e84147 Mon Sep 17 00:00:00 2001
|
|||
|
|
From: zhenyu--zhao_admin <zhaozhenyu17@huawei.com>
|
|||
|
|
Date: Sat, 7 Dec 2024 16:05:59 +0800
|
|||
|
|
Subject: [PATCH] [BUGFIX] Fix build error on risv_64.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
gcc/Makefile.in | 10 +-
|
|||
|
|
gcc/ai-optimizer.cc | 395 ++++++++++++++++++++++++++++++++
|
|||
|
|
gcc/ai4c-infer.cc | 46 ++--
|
|||
|
|
gcc/ai4c-infer.h | 26 ++-
|
|||
|
|
gcc/c-family/c-common.h | 2 +
|
|||
|
|
gcc/c-family/c-opts.cc | 21 ++
|
|||
|
|
gcc/config/aarch64/aarch64-c.cc | 16 ++
|
|||
|
|
gcc/config/aarch64/aarch64.cc | 15 +-
|
|||
|
|
gcc/gcc.cc | 39 +---
|
|||
|
|
gcc/gcc.h | 1 -
|
|||
|
|
gcc/optimizer.fdata | 1 +
|
|||
|
|
gcc/opts-common.cc | 74 +-----
|
|||
|
|
gcc/opts-global.cc | 5 +-
|
|||
|
|
13 files changed, 521 insertions(+), 130 deletions(-)
|
|||
|
|
create mode 100644 gcc/ai-optimizer.cc
|
|||
|
|
create mode 100644 gcc/optimizer.fdata
|
|||
|
|
|
|||
|
|
diff --git a/gcc/Makefile.in b/gcc/Makefile.in
|
|||
|
|
index 5610854e6..65f683bbd 100644
|
|||
|
|
--- a/gcc/Makefile.in
|
|||
|
|
+++ b/gcc/Makefile.in
|
|||
|
|
@@ -1735,13 +1735,13 @@ OBJS-libcommon = diagnostic-spec.o diagnostic.o diagnostic-color.o \
|
|||
|
|
pretty-print.o intl.o \
|
|||
|
|
sbitmap.o \
|
|||
|
|
vec.o input.o hash-table.o ggc-none.o memory-block.o \
|
|||
|
|
- ai4c-infer.o selftest.o selftest-diagnostic.o sort.o
|
|||
|
|
+ ai4c-infer.o ai-optimizer.o selftest.o selftest-diagnostic.o sort.o
|
|||
|
|
|
|||
|
|
# Objects in libcommon-target.a, used by drivers and by the core
|
|||
|
|
# compiler and containing target-dependent code.
|
|||
|
|
OBJS-libcommon-target = $(common_out_object_file) prefix.o \
|
|||
|
|
opts.o opts-common.o options.o vec.o hooks.o common/common-targhooks.o \
|
|||
|
|
- hash-table.o file-find.o spellcheck.o ai4c-infer.o selftest.o opt-suggestions.o
|
|||
|
|
+ hash-table.o file-find.o spellcheck.o ai4c-infer.o ai-optimizer.o selftest.o opt-suggestions.o
|
|||
|
|
|
|||
|
|
# This lists all host objects for the front ends.
|
|||
|
|
ALL_HOST_FRONTEND_OBJS = $(foreach v,$(CONFIG_LANGUAGES),$($(v)_OBJS))
|
|||
|
|
@@ -2257,7 +2257,7 @@ gcc-nm.cc: gcc-ar.cc
|
|||
|
|
cp $^ $@
|
|||
|
|
|
|||
|
|
COLLECT2_OBJS = collect2.o collect2-aix.o vec.o ggc-none.o \
|
|||
|
|
- collect-utils.o file-find.o hash-table.o ai4c-infer.o selftest.o
|
|||
|
|
+ collect-utils.o file-find.o hash-table.o ai4c-infer.o ai-optimizer.o selftest.o
|
|||
|
|
COLLECT2_LIBS = @COLLECT2_LIBS@
|
|||
|
|
collect2$(exeext): $(COLLECT2_OBJS) $(LIBDEPS)
|
|||
|
|
# Don't try modifying collect2 (aka ld) in place--it might be linking this.
|
|||
|
|
@@ -3721,8 +3721,8 @@ install-plugin: installdirs lang.install-plugin s-header-vars install-gengtype
|
|||
|
|
|
|||
|
|
# Install the compiler executables built during cross compilation.
|
|||
|
|
install-common: native lang.install-common installdirs
|
|||
|
|
- rm -f $(DESTDIR)$(libexecdir)/onnx.fdata
|
|||
|
|
- cp $(srcdir)/onnx.fdata $(DESTDIR)$(libexecsubdir)/onnx.fdata
|
|||
|
|
+ rm -f $(DESTDIR)$(libexecdir)/gcc/*.fdata
|
|||
|
|
+ cp $(srcdir)/*.fdata $(DESTDIR)$(libexecdir)/gcc/
|
|||
|
|
for file in $(COMPILERS); do \
|
|||
|
|
if [ -f $$file ] ; then \
|
|||
|
|
rm -f $(DESTDIR)$(libexecsubdir)/$$file; \
|
|||
|
|
diff --git a/gcc/ai-optimizer.cc b/gcc/ai-optimizer.cc
|
|||
|
|
new file mode 100644
|
|||
|
|
index 000000000..c3d99dd85
|
|||
|
|
--- /dev/null
|
|||
|
|
+++ b/gcc/ai-optimizer.cc
|
|||
|
|
@@ -0,0 +1,395 @@
|
|||
|
|
+/* Lightweight AI Inference Framework.
|
|||
|
|
+ Copyright (C) 2024-2024 Free Software Foundation, Inc.
|
|||
|
|
+This file is part of GCC.
|
|||
|
|
+GCC is free software; you can redistribute it and/or modify it under
|
|||
|
|
+the terms of the GNU General Public License as published by the Free
|
|||
|
|
+Software Foundation; either version 3, or (at your option) any later
|
|||
|
|
+version.
|
|||
|
|
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
|||
|
|
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|||
|
|
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|||
|
|
+for more details.
|
|||
|
|
+
|
|||
|
|
+You should have received a copy of the GNU General Public License
|
|||
|
|
+along with GCC; see the file COPYING3. If not see
|
|||
|
|
+<http://www.gnu.org/licenses/>. */
|
|||
|
|
+
|
|||
|
|
+#include <unistd.h>
|
|||
|
|
+#include <math.h>
|
|||
|
|
+#include <cstring>
|
|||
|
|
+#include <cstdio>
|
|||
|
|
+#include <cstdlib>
|
|||
|
|
+#include <stdio.h>
|
|||
|
|
+#include <stdint.h>
|
|||
|
|
+#include <stdlib.h>
|
|||
|
|
+#include <cassert>
|
|||
|
|
+#include "config.h"
|
|||
|
|
+#include "system.h"
|
|||
|
|
+#include "ai4c-infer.h"
|
|||
|
|
+
|
|||
|
|
+#define M_OPTION_SIZE 11
|
|||
|
|
+#define M_MODE_SIZE 6
|
|||
|
|
+#define NATIVE_TUNE_SIZE 128
|
|||
|
|
+#define CATS_STRINGS_ROW 34
|
|||
|
|
+#define CATS_STRINGS_COL 65
|
|||
|
|
+#define CATS_STRINGS1_ROW 10
|
|||
|
|
+#define CATS_STRINGS1_COL 65
|
|||
|
|
+#define OFFSET_ROW 6
|
|||
|
|
+#define SCALE_ROW 6
|
|||
|
|
+#define UNITY_ROW 1
|
|||
|
|
+#define COEFFICIENT_ROW 356
|
|||
|
|
+#define COEFFICIENT_COL 10
|
|||
|
|
+#define COEFFICIENT1_ROW 10
|
|||
|
|
+#define COEFFICIENT1_COL 1
|
|||
|
|
+#define INTERCEPTS_ROW 10
|
|||
|
|
+#define INTERCEPTS1_ROW 1
|
|||
|
|
+
|
|||
|
|
+/* Intermediate computation results from the ONNX model. */
|
|||
|
|
+static char cats_strings[CATS_STRINGS_ROW][CATS_STRINGS_COL];
|
|||
|
|
+static char cats_strings1[CATS_STRINGS1_ROW][CATS_STRINGS1_COL];
|
|||
|
|
+static float offset[OFFSET_ROW];
|
|||
|
|
+static float scale[SCALE_ROW];
|
|||
|
|
+static float unity[UNITY_ROW];
|
|||
|
|
+static float coefficient[COEFFICIENT_ROW][COEFFICIENT_COL];
|
|||
|
|
+static float coefficient1[COEFFICIENT1_ROW][COEFFICIENT1_COL];
|
|||
|
|
+static float intercepts[INTERCEPTS_ROW];
|
|||
|
|
+static float intercepts1[INTERCEPTS1_ROW];
|
|||
|
|
+
|
|||
|
|
+/* Return an integer that represents the comparison result of the
|
|||
|
|
+ two strings. */
|
|||
|
|
+
|
|||
|
|
+static int
|
|||
|
|
+compare_strings (const void *a, const void *b)
|
|||
|
|
+{
|
|||
|
|
+ const char *str_a = *(const char **)a;
|
|||
|
|
+ const char *str_b = *(const char **)b;
|
|||
|
|
+
|
|||
|
|
+ int len = strlen (str_a) < strlen (str_b) ? strlen (str_a) : strlen (str_b);
|
|||
|
|
+ for (int i = 0; i < len; i++)
|
|||
|
|
+ {
|
|||
|
|
+ char c1 = str_a[i];
|
|||
|
|
+ char c2 = str_b[i];
|
|||
|
|
+ if (ISUPPER (c1) && !ISUPPER (c2))
|
|||
|
|
+ return 0;
|
|||
|
|
+ else if (!ISUPPER (c1) && ISUPPER (c2))
|
|||
|
|
+ return 1;
|
|||
|
|
+ else if (c1 != c2)
|
|||
|
|
+ return c1 < c2;
|
|||
|
|
+ }
|
|||
|
|
+ return strlen (str_a) > strlen (str_b);
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
+/* Return the substring before the first underscore ('_') in the input
|
|||
|
|
+ string. */
|
|||
|
|
+
|
|||
|
|
+static void
|
|||
|
|
+truncate_prefix (const char *str, char *result)
|
|||
|
|
+{
|
|||
|
|
+ const char *underscore_pos = strchr (str, '_');
|
|||
|
|
+ if (underscore_pos == NULL)
|
|||
|
|
+ {
|
|||
|
|
+ strcpy (result, str);
|
|||
|
|
+ return;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ size_t len = underscore_pos - str;
|
|||
|
|
+ strncpy (result, str, len + 1);
|
|||
|
|
+ result[len + 1] = '\0';
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
+
|
|||
|
|
+static void
|
|||
|
|
+preprocess (int argc1, const char **argv1, const char *mops,
|
|||
|
|
+ int argc2, int64_t *argv2, char (*in_options)[1024],
|
|||
|
|
+ int64_t *in_modes)
|
|||
|
|
+{
|
|||
|
|
+ strcpy (in_options[0], mops);
|
|||
|
|
+
|
|||
|
|
+ const char *output_option = "-o";
|
|||
|
|
+ const char *marco_prefix = "-D";
|
|||
|
|
+ const char *needle = "--param";
|
|||
|
|
+ const char *flag_prefix = "-";
|
|||
|
|
+ const char *default_option = "-default-option";
|
|||
|
|
+ const int default_int_val = 0;
|
|||
|
|
+ int m_size = 0;
|
|||
|
|
+ for (int i = 0; i < argc1; i++)
|
|||
|
|
+ {
|
|||
|
|
+ if (strncmp (argv1[i], marco_prefix, 2) == 0)
|
|||
|
|
+ m_size ++;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ char *m_options[m_size];
|
|||
|
|
+ char output_file[1024];
|
|||
|
|
+ int m_index = 0;
|
|||
|
|
+ for (int i = 0; i < argc1; i++)
|
|||
|
|
+ {
|
|||
|
|
+ if (strncmp (argv1[i], marco_prefix, 2) == 0)
|
|||
|
|
+ {
|
|||
|
|
+ m_options[m_index] = (char *)argv1[i];
|
|||
|
|
+ m_index ++;
|
|||
|
|
+ }
|
|||
|
|
+ if (strcmp (argv1[i], output_option) == 0)
|
|||
|
|
+ truncate_prefix (argv1[i + 1], output_file);
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ strcpy (in_options[1], output_file);
|
|||
|
|
+ int in_options_size = 2;
|
|||
|
|
+ qsort (m_options, m_size, sizeof (m_options[0]), compare_strings);
|
|||
|
|
+ for (int i = 0; i < m_size && in_options_size < M_OPTION_SIZE; i++)
|
|||
|
|
+ {
|
|||
|
|
+ strcpy (in_options[in_options_size], m_options[i]);
|
|||
|
|
+ in_options_size ++;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ for (int i = 0; i < argc1 && in_options_size < M_OPTION_SIZE; i++)
|
|||
|
|
+ {
|
|||
|
|
+ if (strncmp (argv1[i], marco_prefix, 2) != 0
|
|||
|
|
+ && strcmp (argv1[i], output_option) != 0
|
|||
|
|
+ && strncmp (argv1[i], needle, 7) != 0
|
|||
|
|
+ && strncmp (argv1[i], flag_prefix, 1) == 0)
|
|||
|
|
+ {
|
|||
|
|
+ strcpy (in_options[in_options_size], argv1[i]);
|
|||
|
|
+ in_options_size ++;
|
|||
|
|
+ }
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ while (in_options_size < M_OPTION_SIZE)
|
|||
|
|
+ {
|
|||
|
|
+ strcpy (in_options[in_options_size], default_option);
|
|||
|
|
+ in_options_size ++;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ /* Use sha256 to encrypt the input. */
|
|||
|
|
+ char hash[65];
|
|||
|
|
+ char input[64];
|
|||
|
|
+ for (int i = 0; i < M_OPTION_SIZE; i++)
|
|||
|
|
+ {
|
|||
|
|
+ execute_sha256 (in_options[i], hash, sizeof (hash));
|
|||
|
|
+ strcpy (in_options[i], hash);
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ for (int i = 0; i < argc2 && i < M_MODE_SIZE; i++)
|
|||
|
|
+ {
|
|||
|
|
+ if (i < argc2)
|
|||
|
|
+ in_modes[i] = argv2[i];
|
|||
|
|
+ else
|
|||
|
|
+ in_modes[i] = default_int_val;
|
|||
|
|
+ }
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
+/* To read model parameter information from optimizer.fdata and store it into
|
|||
|
|
+ the appropriate arrays. */
|
|||
|
|
+
|
|||
|
|
+static void
|
|||
|
|
+fill_node (const char *file_name)
|
|||
|
|
+{
|
|||
|
|
+ FILE *file = fopen (file_name, "rb");
|
|||
|
|
+
|
|||
|
|
+ if (!file)
|
|||
|
|
+ {
|
|||
|
|
+ perror ("Can not open file.");
|
|||
|
|
+ return;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ /* Read cats_strings from optimizer.fdata. */
|
|||
|
|
+ char hex_string[2];
|
|||
|
|
+ for (int i = 0; i < CATS_STRINGS_ROW; i++)
|
|||
|
|
+ {
|
|||
|
|
+ for (int j = 0; j < CATS_STRINGS_COL - 1; j++)
|
|||
|
|
+ {
|
|||
|
|
+ if (fscanf (file, "%2s", hex_string) != 1)
|
|||
|
|
+ {
|
|||
|
|
+ perror ("Can not read cats_strings from optimizer.fdata.");
|
|||
|
|
+ return;
|
|||
|
|
+ }
|
|||
|
|
+ cats_strings[i][j] = (unsigned char) strtol(hex_string, NULL, 16);
|
|||
|
|
+ }
|
|||
|
|
+ cats_strings[i][CATS_STRINGS_COL - 1] = '\0';
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ /* Read cats_strings1 from optimizer.fdata. */
|
|||
|
|
+ for (int i = 0; i < CATS_STRINGS1_ROW; i++)
|
|||
|
|
+ {
|
|||
|
|
+ for (int j = 0; j < CATS_STRINGS1_COL - 1; j++)
|
|||
|
|
+ {
|
|||
|
|
+ if (fscanf (file, "%2s", hex_string) != 1)
|
|||
|
|
+ {
|
|||
|
|
+ perror ("Can not read cats_strings1 from optimizer.fdata.");
|
|||
|
|
+ return;
|
|||
|
|
+ }
|
|||
|
|
+ cats_strings1[i][j] = (unsigned char) strtol(hex_string, NULL, 16);
|
|||
|
|
+ }
|
|||
|
|
+ cats_strings1[i][CATS_STRINGS1_COL - 1] = '\0';
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ /* Read offset from optimizer.fdata. */
|
|||
|
|
+ for (int i = 0; i < OFFSET_ROW; i++)
|
|||
|
|
+ {
|
|||
|
|
+ float result = read_float_from_file (file);
|
|||
|
|
+ offset[i] = result;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+
|
|||
|
|
+ /* Read scale from optimizer.fdata. */
|
|||
|
|
+ for (int i = 0; i < SCALE_ROW; i++)
|
|||
|
|
+ {
|
|||
|
|
+ float result = read_float_from_file (file);
|
|||
|
|
+ scale[i] = result;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ /* Read unity from optimizer.fdata. */
|
|||
|
|
+ for (int i = 0; i < UNITY_ROW; i++)
|
|||
|
|
+ {
|
|||
|
|
+ float result = read_float_from_file (file);
|
|||
|
|
+ unity[i] = result;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ /* Read coefficient from optimizer.fdata. */
|
|||
|
|
+ for (int i = 0; i < COEFFICIENT_ROW; i++)
|
|||
|
|
+ for (int j = 0; j < COEFFICIENT_COL; j++)
|
|||
|
|
+ {
|
|||
|
|
+ float result = read_float_from_file (file);
|
|||
|
|
+ coefficient[i][j] = result;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ /* Read coefficient1 from optimizer.fdata. */
|
|||
|
|
+ for (int i = 0; i < COEFFICIENT1_ROW; i++)
|
|||
|
|
+ for (int j = 0; j < COEFFICIENT1_COL; j++)
|
|||
|
|
+ {
|
|||
|
|
+ float result = read_float_from_file (file);
|
|||
|
|
+ coefficient1[i][j] = result;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ /* Read intercepts from optimizer.fdata. */
|
|||
|
|
+ for (int i = 0; i < INTERCEPTS_ROW; i++)
|
|||
|
|
+ {
|
|||
|
|
+ float result = read_float_from_file (file);
|
|||
|
|
+ intercepts[i] = result;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ /* Read intercepts1 from optimizer.fdata. */
|
|||
|
|
+ for (int i = 0; i < INTERCEPTS1_ROW; i++)
|
|||
|
|
+ {
|
|||
|
|
+ float result = read_float_from_file (file);
|
|||
|
|
+ intercepts1[i] = result;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ fclose (file);
|
|||
|
|
+ return;
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
+/* The process of model inference. */
|
|||
|
|
+
|
|||
|
|
+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)
|
|||
|
|
+ return 0;
|
|||
|
|
+ char native_file[512];
|
|||
|
|
+
|
|||
|
|
+ if (gcc_exec_prefix)
|
|||
|
|
+ {
|
|||
|
|
+ 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 (access (native_file, F_OK) == 0)
|
|||
|
|
+ fill_node (native_file);
|
|||
|
|
+ else
|
|||
|
|
+ return 0;
|
|||
|
|
+
|
|||
|
|
+ static int64_t in_modes[M_MODE_SIZE];
|
|||
|
|
+ static char in_options[M_OPTION_SIZE][1024];
|
|||
|
|
+
|
|||
|
|
+ preprocess (argc1, argv1, mops, argc2, argv2, in_options, in_modes);
|
|||
|
|
+
|
|||
|
|
+ /* concat_result and encoder_out are intermediate computation results from
|
|||
|
|
+ the ONNX model. concat_result is a 1 × 18 matrix, and encoder_out is a
|
|||
|
|
+ 1 × 12 matrix. */
|
|||
|
|
+
|
|||
|
|
+ const int concat_out_size = 350;
|
|||
|
|
+ float concat_result[concat_out_size];
|
|||
|
|
+ const int encoder_out_size = 34;
|
|||
|
|
+ const int encoder_last_size = 10;
|
|||
|
|
+ int concat_size = 0;
|
|||
|
|
+ const int size = encoder_out_size;
|
|||
|
|
+
|
|||
|
|
+ for (int i = 1; i < M_OPTION_SIZE; i++)
|
|||
|
|
+ {
|
|||
|
|
+ float encoder_out[size];
|
|||
|
|
+ one_hot_encoder (in_options[i], cats_strings, encoder_out, size);
|
|||
|
|
+ line_concat (encoder_out, size, concat_result, concat_size);
|
|||
|
|
+ concat_size += size;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ float encoder_out2[encoder_last_size];
|
|||
|
|
+ one_hot_encoder (in_options[0], cats_strings1, encoder_out2,
|
|||
|
|
+ encoder_last_size);
|
|||
|
|
+ line_concat (encoder_out2, encoder_last_size, concat_result, concat_size);
|
|||
|
|
+ concat_size += encoder_last_size;
|
|||
|
|
+
|
|||
|
|
+ float variable[M_MODE_SIZE];
|
|||
|
|
+ imputer (in_modes, M_MODE_SIZE, variable);
|
|||
|
|
+ float variable1[M_MODE_SIZE];
|
|||
|
|
+ scaler (variable, offset, scale, M_MODE_SIZE, variable1);
|
|||
|
|
+
|
|||
|
|
+ float transformed_column[concat_out_size + M_MODE_SIZE];
|
|||
|
|
+ /* line_concat is used to stro*/
|
|||
|
|
+ line_concat (variable1, M_MODE_SIZE, transformed_column, 0);
|
|||
|
|
+ line_concat (concat_result, concat_out_size, transformed_column, 6);
|
|||
|
|
+
|
|||
|
|
+ /* This requires performing matrix multiplication between a 1 × 356 matrix
|
|||
|
|
+ and an 356 × 10 matrix */
|
|||
|
|
+
|
|||
|
|
+ const int m = 1, k = 356, n = 10;
|
|||
|
|
+ float mul_result[n];
|
|||
|
|
+ matmul (transformed_column, coefficient[0], m, k, n, mul_result);
|
|||
|
|
+
|
|||
|
|
+ float add_result[n];
|
|||
|
|
+ add (mul_result, intercepts, n, add_result);
|
|||
|
|
+
|
|||
|
|
+ float next_activations[n];
|
|||
|
|
+ relu (add_result, n, next_activations);
|
|||
|
|
+
|
|||
|
|
+ /* This requires performing matrix multiplication between a 1 × 10 matrix
|
|||
|
|
+ and an 10 × 1 matrix */
|
|||
|
|
+
|
|||
|
|
+ const int m2 = 1, k2 = 10, n2 = 1;
|
|||
|
|
+ float mul_result1[n2];
|
|||
|
|
+ matmul (next_activations, coefficient1[0], m2, k2, n2, mul_result1);
|
|||
|
|
+
|
|||
|
|
+ float add_result1[n2];
|
|||
|
|
+ add (mul_result1, intercepts1, n2, add_result1);
|
|||
|
|
+
|
|||
|
|
+ float out_activations_result[n2];
|
|||
|
|
+ sigmoid (add_result1, n2, out_activations_result);
|
|||
|
|
+
|
|||
|
|
+ float negative_class_proba[n2];
|
|||
|
|
+ sub (unity, out_activations_result, n2, negative_class_proba);
|
|||
|
|
+ const int prob_size = n2 + n2;
|
|||
|
|
+ float probabilities[prob_size];
|
|||
|
|
+ line_concat (negative_class_proba, n2, probabilities, 0);
|
|||
|
|
+ line_concat (out_activations_result, n2, probabilities, n2);
|
|||
|
|
+
|
|||
|
|
+ int argmax_output = argmax (probabilities, prob_size);
|
|||
|
|
+ return argmax_output;
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
+int
|
|||
|
|
+get_optimize_decision_from_optimizer (int argc, const char **argv,
|
|||
|
|
+ const char *mops, int argc2,
|
|||
|
|
+ int64_t *argv2)
|
|||
|
|
+{
|
|||
|
|
+ int model_pred = graph_infer (argc, argv, mops, argc2, argv2);
|
|||
|
|
+ if (model_pred == 1)
|
|||
|
|
+ {
|
|||
|
|
+ putenv ("AI_INFER_LEVEL=1");
|
|||
|
|
+ }
|
|||
|
|
+ return model_pred;
|
|||
|
|
+}
|
|||
|
|
diff --git a/gcc/ai4c-infer.cc b/gcc/ai4c-infer.cc
|
|||
|
|
index 99f7a6b45..42922e1ca 100644
|
|||
|
|
--- a/gcc/ai4c-infer.cc
|
|||
|
|
+++ b/gcc/ai4c-infer.cc
|
|||
|
|
@@ -61,6 +61,12 @@ static int64_t optimize_result;
|
|||
|
|
void
|
|||
|
|
prepare_native_tune_str (const char *info)
|
|||
|
|
{
|
|||
|
|
+ if (info == NULL)
|
|||
|
|
+ {
|
|||
|
|
+ strcpy (native_tune, "=native+");
|
|||
|
|
+ return;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
gcc_assert (strlen (info) < NATIVE_TUNE_SIZE);
|
|||
|
|
if (info)
|
|||
|
|
strcpy (native_tune, info);
|
|||
|
|
@@ -83,7 +89,7 @@ set_cache_info (int prefetches, int l1_cache_size,
|
|||
|
|
|
|||
|
|
/* Read float from onnx.fdata. */
|
|||
|
|
|
|||
|
|
-float static
|
|||
|
|
+float
|
|||
|
|
read_float_from_file (FILE* file)
|
|||
|
|
{
|
|||
|
|
char hex_float[8];
|
|||
|
|
@@ -196,7 +202,7 @@ fill_node (const char *file_name)
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-static void
|
|||
|
|
+void
|
|||
|
|
matmul (const float *lhs, const float *rhs, int m, int k, int n, float *out)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < m; i++)
|
|||
|
|
@@ -212,7 +218,7 @@ matmul (const float *lhs, const float *rhs, int m, int k, int n, float *out)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-static void
|
|||
|
|
+void
|
|||
|
|
add (const float *lhs, const float *rhs, int length, float *out)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < length; i++)
|
|||
|
|
@@ -221,7 +227,7 @@ add (const float *lhs, const float *rhs, int length, float *out)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-static void
|
|||
|
|
+void
|
|||
|
|
sub (const float *lhs, const float *rhs, int length, float *out)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < length; i++)
|
|||
|
|
@@ -230,7 +236,7 @@ sub (const float *lhs, const float *rhs, int length, float *out)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-static void
|
|||
|
|
+void
|
|||
|
|
sigmoid (const float *in, int length, float *out)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < length; i++)
|
|||
|
|
@@ -239,7 +245,7 @@ sigmoid (const float *in, int length, float *out)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-static void
|
|||
|
|
+void
|
|||
|
|
relu (const float *data, int length, float *out)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < length; i++)
|
|||
|
|
@@ -255,14 +261,14 @@ relu (const float *data, int length, float *out)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-static void
|
|||
|
|
+void
|
|||
|
|
line_concat (const float *in, int in_size, float *out, int out_size)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < in_size; i++)
|
|||
|
|
out[out_size + i] = in[i];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-static void
|
|||
|
|
+void
|
|||
|
|
one_hot_encoder (const char *in, const char (*cats)[65], float *out,
|
|||
|
|
int out_size)
|
|||
|
|
{
|
|||
|
|
@@ -279,14 +285,14 @@ one_hot_encoder (const char *in, const char (*cats)[65], float *out,
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-static void
|
|||
|
|
+void
|
|||
|
|
imputer (const int64_t *in, int size, float *out)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < size; i++)
|
|||
|
|
out[i] = in[i] * 1.0f;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-static void
|
|||
|
|
+void
|
|||
|
|
scaler (const float *in, const float *offset, const float *scale, int size,
|
|||
|
|
float *out)
|
|||
|
|
{
|
|||
|
|
@@ -294,7 +300,7 @@ scaler (const float *in, const float *offset, const float *scale, int size,
|
|||
|
|
out[i] = (in[i] - offset[i]) * scale[i];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-static int
|
|||
|
|
+int
|
|||
|
|
argmax (const float *in, int in_size)
|
|||
|
|
{
|
|||
|
|
int out_idx = 0;
|
|||
|
|
@@ -327,7 +333,20 @@ preprocess (int argc, int64_t *argv, int64_t *in_modes)
|
|||
|
|
static int
|
|||
|
|
graph_infer (int argc, const char *argv, int argc2, int64_t *argv2)
|
|||
|
|
{
|
|||
|
|
- const char *file_name = getenv ("GCC_AI4C_ONNX_FDATA");
|
|||
|
|
+ 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);
|
|||
|
|
+ }
|
|||
|
|
|
|||
|
|
if (access (file_name, F_OK) == 0)
|
|||
|
|
{
|
|||
|
|
@@ -401,7 +420,8 @@ graph_infer (int argc, const char *argv, int argc2, int64_t *argv2)
|
|||
|
|
return argmax_output;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-void execute_sha256 (const char *input, char *output, size_t output_size)
|
|||
|
|
+void
|
|||
|
|
+execute_sha256 (const char *input, char *output, size_t output_size)
|
|||
|
|
{
|
|||
|
|
char command[256];
|
|||
|
|
snprintf (command, sizeof (command), "echo -n \"%s\" | sha256sum", input);
|
|||
|
|
diff --git a/gcc/ai4c-infer.h b/gcc/ai4c-infer.h
|
|||
|
|
index 7fb75900b..fa5156ab1 100644
|
|||
|
|
--- a/gcc/ai4c-infer.h
|
|||
|
|
+++ b/gcc/ai4c-infer.h
|
|||
|
|
@@ -21,9 +21,25 @@
|
|||
|
|
#ifndef AI4C_INFER_H
|
|||
|
|
#define AI4C_INFER_H
|
|||
|
|
|
|||
|
|
+extern void matmul (const float *, const float *, int, int, int, float *);
|
|||
|
|
+extern void add (const float *, const float *, int, float *);
|
|||
|
|
+extern void sub (const float *, const float *, int, float *);
|
|||
|
|
+extern void sigmoid (const float *, int, float *);
|
|||
|
|
+extern void relu (const float *, int, float *);
|
|||
|
|
+extern void line_concat (const float *, int, float *, int);
|
|||
|
|
+extern void one_hot_encoder (const char *, const char (*)[65], float *, int);
|
|||
|
|
+extern void imputer (const int64_t *, int, float *);
|
|||
|
|
+extern void scaler (const float *, const float *, const float *, int, float *);
|
|||
|
|
+extern int argmax (const float *, int);
|
|||
|
|
+
|
|||
|
|
+extern void
|
|||
|
|
+execute_sha256 (const char *, char *, size_t);
|
|||
|
|
+extern float read_float_from_file (FILE*);
|
|||
|
|
+
|
|||
|
|
extern int get_optimize_decision_from_ai4c ();
|
|||
|
|
-extern void set_cache_info (int prefetches, int l1_cache_size,
|
|||
|
|
- int l1_cache_line_size, int l2_cache_size,
|
|||
|
|
- int prefetch_latency, int prefetch_distance_factor);
|
|||
|
|
-extern void prepare_native_tune_str (const char *info);
|
|||
|
|
-#endif /* AI4C_INFER_H */
|
|||
|
|
\ No newline at end of file
|
|||
|
|
+extern int get_optimize_decision_from_optimizer (int, const char **,
|
|||
|
|
+ const char *, int ,
|
|||
|
|
+ int64_t *);
|
|||
|
|
+extern void set_cache_info (int, int, int, int, int, int);
|
|||
|
|
+extern void prepare_native_tune_str (const char *);
|
|||
|
|
+#endif /* AI4C_INFER_H */
|
|||
|
|
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
|
|||
|
|
index d1503c5a7..47c502e13 100644
|
|||
|
|
--- a/gcc/c-family/c-common.h
|
|||
|
|
+++ b/gcc/c-family/c-common.h
|
|||
|
|
@@ -940,6 +940,8 @@ extern void set_compound_literal_name (tree decl);
|
|||
|
|
|
|||
|
|
extern tree build_va_arg (location_t, tree, tree);
|
|||
|
|
|
|||
|
|
+extern void deferred_opts_add_macro_front (const char *);
|
|||
|
|
+
|
|||
|
|
extern const unsigned int c_family_lang_mask;
|
|||
|
|
extern unsigned int c_common_option_lang_mask (void);
|
|||
|
|
extern void c_common_diagnostics_set_defaults (diagnostic_context *);
|
|||
|
|
diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
|
|||
|
|
index 744b54dc3..4cde773bf 100644
|
|||
|
|
--- a/gcc/c-family/c-opts.cc
|
|||
|
|
+++ b/gcc/c-family/c-opts.cc
|
|||
|
|
@@ -94,6 +94,9 @@ static bool std_cxx_inc = true;
|
|||
|
|
/* If the quote chain has been split by -I-. */
|
|||
|
|
static bool quote_chain_split;
|
|||
|
|
|
|||
|
|
+/* Size of deferred_opts. */
|
|||
|
|
+static size_t deferred_opts_size;
|
|||
|
|
+
|
|||
|
|
/* Number of deferred options. */
|
|||
|
|
static size_t deferred_count;
|
|||
|
|
|
|||
|
|
@@ -145,6 +148,23 @@ static struct deferred_opt
|
|||
|
|
extern const unsigned int
|
|||
|
|
c_family_lang_mask = (CL_C | CL_CXX | CL_ObjC | CL_ObjCXX);
|
|||
|
|
|
|||
|
|
+/* Add macro to the front of deferred_opts. */
|
|||
|
|
+void
|
|||
|
|
+deferred_opts_add_macro_front (const char *arg)
|
|||
|
|
+{
|
|||
|
|
+ /* Allocate a new vec and move elements back. */
|
|||
|
|
+ auto *new_opts = XNEWVEC (struct deferred_opt, deferred_opts_size + 1);
|
|||
|
|
+ memcpy (new_opts + 1, deferred_opts,
|
|||
|
|
+ sizeof (struct deferred_opt) * deferred_opts_size);
|
|||
|
|
+ XDELETEVEC (deferred_opts);
|
|||
|
|
+ deferred_opts = new_opts;
|
|||
|
|
+ deferred_opts_size++;
|
|||
|
|
+ deferred_count++;
|
|||
|
|
+
|
|||
|
|
+ deferred_opts[0].code = OPT_D;
|
|||
|
|
+ deferred_opts[0].arg = arg;
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
/* Defer option CODE with argument ARG. */
|
|||
|
|
static void
|
|||
|
|
defer_opt (enum opt_code code, const char *arg)
|
|||
|
|
@@ -251,6 +271,7 @@ c_common_init_options (unsigned int decoded_options_count,
|
|||
|
|
cpp_opts->warn_dollars = 0;
|
|||
|
|
|
|||
|
|
deferred_opts = XNEWVEC (struct deferred_opt, decoded_options_count);
|
|||
|
|
+ deferred_opts_size = decoded_options_count;
|
|||
|
|
|
|||
|
|
if (c_language == clk_c)
|
|||
|
|
{
|
|||
|
|
diff --git a/gcc/config/aarch64/aarch64-c.cc b/gcc/config/aarch64/aarch64-c.cc
|
|||
|
|
index 2d2ac42c4..dd739288c 100644
|
|||
|
|
--- a/gcc/config/aarch64/aarch64-c.cc
|
|||
|
|
+++ b/gcc/config/aarch64/aarch64-c.cc
|
|||
|
|
@@ -47,6 +47,21 @@ aarch64_def_or_undef (bool def_p, const char *macro, cpp_reader *pfile)
|
|||
|
|
cpp_undef (pfile, macro);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
+/* Reset the optimize option.
|
|||
|
|
+ After checking the model result, this function can
|
|||
|
|
+ reset the more appropriate options. */
|
|||
|
|
+static void
|
|||
|
|
+reset_machine_option (struct gcc_options *opts)
|
|||
|
|
+{
|
|||
|
|
+ const char *ai_infer_level = getenv ("AI_INFER_LEVEL");
|
|||
|
|
+ if (ai_infer_level)
|
|||
|
|
+ {
|
|||
|
|
+ auto *cpp_opts = cpp_get_options (parse_in);
|
|||
|
|
+ cpp_opts->macro_use_commandline = 1;
|
|||
|
|
+ deferred_opts_add_macro_front ("OBSTACK_CHUNK_SIZE=65536");
|
|||
|
|
+ }
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
/* Define the macros that we always expect to have on AArch64. */
|
|||
|
|
|
|||
|
|
static void
|
|||
|
|
@@ -119,6 +134,7 @@ aarch64_define_unconditional_macros (cpp_reader *pfile)
|
|||
|
|
cpp_opts->warn_variadic_macros = old_warn_variadic_macros;
|
|||
|
|
cpp_opts->cpp_warn_c90_c99_compat = old_cpp_warn_c90_c99_compat;
|
|||
|
|
}
|
|||
|
|
+ reset_machine_option(&global_options);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* Undefine/redefine macros that depend on the current backend state and may
|
|||
|
|
diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
|
|||
|
|
index 829e0da8f..debb15522 100644
|
|||
|
|
--- a/gcc/config/aarch64/aarch64.cc
|
|||
|
|
+++ b/gcc/config/aarch64/aarch64.cc
|
|||
|
|
@@ -18771,6 +18771,7 @@ override_C_optimize_options (struct gcc_options *opts)
|
|||
|
|
opts->x_semi_relayout_level = 14;
|
|||
|
|
opts->x_flag_ipa_prefetch = 1;
|
|||
|
|
opts->x_flag_ipa_ic = 1;
|
|||
|
|
+ opts->x_flag_cmlt_arith = 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* Check whether in CPP language or LTO with only CPP language. */
|
|||
|
|
@@ -18872,23 +18873,28 @@ reset_machine_option (struct gcc_options *opts)
|
|||
|
|
const char *ai_infer_level = getenv ("AI_INFER_LEVEL");
|
|||
|
|
if (ai_infer_level)
|
|||
|
|
{
|
|||
|
|
+ char *collect_gcc = getenv("COLLECT_GCC");
|
|||
|
|
+ const char* gcc_exec = basename(ASTRDUP(collect_gcc));
|
|||
|
|
+ if (gcc_exec == NULL)
|
|||
|
|
+ {
|
|||
|
|
+ return;
|
|||
|
|
+ }
|
|||
|
|
override_optimize_options_1 (opts);
|
|||
|
|
- if (lang_c_p ())
|
|||
|
|
+ if (strstr(gcc_exec, "gcc") != NULL)
|
|||
|
|
{
|
|||
|
|
override_C_optimize_options (opts);
|
|||
|
|
}
|
|||
|
|
- else if (lang_cpp_p ())
|
|||
|
|
+ else if (strstr(gcc_exec, "g++") != NULL)
|
|||
|
|
{
|
|||
|
|
override_CPP_optimize_options (opts);
|
|||
|
|
}
|
|||
|
|
- else if (lang_GNU_Fortran ())
|
|||
|
|
+ else if (strstr(gcc_exec, "gfortran") != NULL)
|
|||
|
|
{
|
|||
|
|
override_Fortran_optimize_options (opts);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-
|
|||
|
|
/* STMT_COST is the cost calculated for STMT_INFO, which has cost kind KIND
|
|||
|
|
and which when vectorized would operate on vector type VECTYPE. Add the
|
|||
|
|
cost of any embedded operations. */
|
|||
|
|
@@ -20348,7 +20354,6 @@ aarch64_override_options_internal (struct gcc_options *opts)
|
|||
|
|
&& aarch64_tune_params.prefetch->default_opt_level >= 0
|
|||
|
|
&& opts->x_optimize >= aarch64_tune_params.prefetch->default_opt_level)
|
|||
|
|
opts->x_flag_prefetch_loop_arrays = 1;
|
|||
|
|
-
|
|||
|
|
reset_machine_option (opts);
|
|||
|
|
aarch64_override_options_after_change_1 (opts);
|
|||
|
|
}
|
|||
|
|
diff --git a/gcc/gcc.cc b/gcc/gcc.cc
|
|||
|
|
index 90f6dfe85..179d507f2 100644
|
|||
|
|
--- a/gcc/gcc.cc
|
|||
|
|
+++ b/gcc/gcc.cc
|
|||
|
|
@@ -5799,10 +5799,13 @@ do_self_spec (const char *spec)
|
|||
|
|
do_spec_2 (spec, NULL);
|
|||
|
|
do_spec_1 (" ", 0, NULL);
|
|||
|
|
|
|||
|
|
- const char* tune_native = eval_spec_function ("local_cpu_detect", "cpu", "");
|
|||
|
|
+ const char* tune_native = NULL;
|
|||
|
|
+#if defined (__x86_64__) || defined (__aarch64__)
|
|||
|
|
+ tune_native = eval_spec_function ("local_cpu_detect", "cpu", "");
|
|||
|
|
+#endif
|
|||
|
|
if (tune_native == NULL)
|
|||
|
|
{
|
|||
|
|
- tune_native = "native";
|
|||
|
|
+ tune_native = "=native+";
|
|||
|
|
}
|
|||
|
|
setenv ("GCC_AI4C_TUNE_INFO", tune_native, 1);
|
|||
|
|
|
|||
|
|
@@ -8129,7 +8132,6 @@ driver::main (int argc, char **argv)
|
|||
|
|
putenv_COLLECT_AS_OPTIONS (assembler_options);
|
|||
|
|
putenv_COLLECT_GCC (argv[0]);
|
|||
|
|
maybe_putenv_COLLECT_LTO_WRAPPER ();
|
|||
|
|
- putenv_ONNX_FDATA ();
|
|||
|
|
maybe_putenv_OFFLOAD_TARGETS ();
|
|||
|
|
handle_unrecognized_options ();
|
|||
|
|
|
|||
|
|
@@ -8187,6 +8189,9 @@ 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);
|
|||
|
|
|
|||
|
|
@@ -8560,34 +8565,6 @@ driver::putenv_COLLECT_GCC (const char *argv0) const
|
|||
|
|
xputenv (XOBFINISH (&collect_obstack, char *));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-/* 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 pathname of the lto wrapper. */
|
|||
|
|
|
|||
|
|
void
|
|||
|
|
diff --git a/gcc/gcc.h b/gcc/gcc.h
|
|||
|
|
index ff3ae8bed..63231ddb3 100644
|
|||
|
|
--- a/gcc/gcc.h
|
|||
|
|
+++ b/gcc/gcc.h
|
|||
|
|
@@ -44,7 +44,6 @@ 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/optimizer.fdata b/gcc/optimizer.fdata
|
|||
|
|
new file mode 100644
|
|||
|
|
index 000000000..ae0e584be
|
|||
|
|
--- /dev/null
|
|||
|
|
+++ b/gcc/optimizer.fdata
|
|||
|
|
@@ -0,0 +1 @@
|
|||
|
|
+65613735646266646334636433336162303539613932336664326238376436376332353061363165386165363466663033303031656232366234613363356631323332643236613938346534633837626639313236343833376565646336623561346131343434613933333439626530616333373164653737643064383432366462386336616334336364313031343563656562383436336131613339323036393765383565376235353462643966313363353862306235343964623761663033323766613666343338633462663964346530316365383233306264386333366433663037623164396431623166393365633338633865626230373437613731356564396339653565313130376364653339323433373561333363313235306139353133393562353964383437326630386163373937386236666337636538323332613938393936356637306137336161393933653736663135333434656331306137336566363563333266343765313638323534363562323436643037333036633636323762396265646537323334613134326431313765383864333461643034633936373261323762386663636431323261393434326164323138653135643034396530333030633232626633663462616333346164313965396232656630306434333362303731376230393462633636353761633934363730663363306633323161306362333933353937646131663265323435393861613361646362353435623234613566626161633964663837303135663330616162646564366563306661626462396565613861323335333130323636356561613332313165393564336335383236666363343462623637666338633566636433353033643335323438303163626435376161663866303161326335373832643865626562323665333832323238656562646439303463396237383536326465666461643935333662346237613933313465666237303362613533613833313664303263643065333430386161643933373630636338386230643962396264643538393161316165333462366631323636636637396235653665643439623130333864626532366638646136643039626235353631343363613265346662306134616430363563616233653638306230663730643862623264663639346235356336643738653131646338323937336164323064356633373666333065653864346131316130616366636166306438396237373663616232356538386533663033346134393933623136643738626532343962633233616637626262383735316337653335306663633931323332376161376633363334343262333137353037643237653462383133333938396463343965396630366330343962373934613362633064633337383139393031643932623364656562376164666435623033623336323038383361626664346362653633613334666465643337363034373834326237386364613137363365393963643061643939373130643531366432316631353063646439366461373864346135643733356434303631626235346231333666333531376332366136356434333062396536386661653432316361633761396435646533333366373931383562336331646234653964353963643530316630666436613531306333363033653133653234613430623462663231356533373935613961343064363061383435366662376236336332643131653831373031613233316538356166643462356462383161626331646435366432373137333265643032343566383639316264633564616433343736643666363335363037376231616131643633346139376334313039623539623733353437663934306331646338643231633066313837333838663264336138656563666565376664383637393032666434616333316261316339666466353162616132613433376266633039326131663733326532613866303636373633373432623037373061366532366533326632626535653437613337333039346633663962343138386338343364356334626561356132613461633462626264663766616337636533396232303632353733373731353431306632336462393730353462366562323730656663636662376362343137366638323638313432636561313665323862323164633935393837303065663164623865396164343465376531383431623134623132356363663335613137653863666661386630363832336264343663663835366534616632363362386161663936386261383530623464396166666265633439336661306665306138333365396534363365383761326363653063386330386131336363636134613261343136396233653961346331336663393463376136376162303939636539326363363865613437616261663136383861623861313732303266386461303531336335333539643237353036323366323434313634633966343533383465663036653735333630613736643464626430346630363366363038636363633738306631663132383635313539313464613937303532363734616430396532353666633539386463636434643338356461643538386236646432613033653631393463613938643837346233316236623062316532353333626331343038643064366138626163306131363161363931323231373438313633646463623132313033623161313137333663623865663535363562666630353566333136333235333831336339633431363133303465613366636535356164383035656563313734
|
|||
|
|
\ No newline at end of file
|
|||
|
|
diff --git a/gcc/opts-common.cc b/gcc/opts-common.cc
|
|||
|
|
index 176041bfe..35db76b84 100644
|
|||
|
|
--- a/gcc/opts-common.cc
|
|||
|
|
+++ b/gcc/opts-common.cc
|
|||
|
|
@@ -27,6 +27,7 @@ along with GCC; see the file COPYING3. If not see
|
|||
|
|
#include "diagnostic.h"
|
|||
|
|
#include "spellcheck.h"
|
|||
|
|
#include "opts-jobserver.h"
|
|||
|
|
+#include "ai4c-infer.h"
|
|||
|
|
|
|||
|
|
static void prune_options (struct cl_decoded_option **, unsigned int *,
|
|||
|
|
unsigned int);
|
|||
|
|
@@ -992,71 +993,6 @@ opts_concat (const char *first, ...)
|
|||
|
|
return newstr;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-typedef int64_t (*run_ai_model_func)(int, const char **,
|
|||
|
|
- const char *, int, int64_t *);
|
|||
|
|
-#define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
|
|||
|
|
-#define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
|
|||
|
|
-#define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
|
|||
|
|
-
|
|||
|
|
-static int64_t
|
|||
|
|
-ai_infer_optimization (int argc, const char **argv,
|
|||
|
|
- const char *mcpu_option,
|
|||
|
|
- int argc_hw, int64_t *argv_hw)
|
|||
|
|
-{
|
|||
|
|
- /* Load dependent AI-framework libraries. */
|
|||
|
|
- void *onnxruntime_lib_handle = NULL;
|
|||
|
|
- const char *onnxruntime_lib_path = "libonnxruntime.so";
|
|||
|
|
-
|
|||
|
|
- onnxruntime_lib_handle = dlopen (onnxruntime_lib_path,
|
|||
|
|
- RTLD_LAZY | RTLD_GLOBAL);
|
|||
|
|
- if (!onnxruntime_lib_handle)
|
|||
|
|
- {
|
|||
|
|
- return -1;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
- void *ai4c_lib_handle = NULL;
|
|||
|
|
- const char *ai4c_lib_path = "libONNXRunner.so";
|
|||
|
|
-
|
|||
|
|
- ai4c_lib_handle = dlopen (ai4c_lib_path, RTLD_LAZY | RTLD_GLOBAL);
|
|||
|
|
- if (!ai4c_lib_handle)
|
|||
|
|
- {
|
|||
|
|
- return -1;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
- /* Clear any existing error. */
|
|||
|
|
- dlerror ();
|
|||
|
|
-
|
|||
|
|
- /* Run AI4Compiler model. */
|
|||
|
|
- if (ai4c_lib_handle == NULL || onnxruntime_lib_handle == NULL)
|
|||
|
|
- {
|
|||
|
|
- return -1;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
- run_ai_model_func run_ai_model;
|
|||
|
|
- PTR_UNION_TYPE (run_ai_model_func) run_ai_model_func_union;
|
|||
|
|
- PTR_UNION_AS_VOID_PTR (run_ai_model_func_union)
|
|||
|
|
- = dlsym (ai4c_lib_handle, "runONNXModelOptimizer");
|
|||
|
|
- run_ai_model = PTR_UNION_AS_CAST_PTR (run_ai_model_func_union);
|
|||
|
|
- if (!run_ai_model)
|
|||
|
|
- {
|
|||
|
|
- dlclose (ai4c_lib_handle);
|
|||
|
|
- dlclose (onnxruntime_lib_handle);
|
|||
|
|
- return -1;
|
|||
|
|
- }
|
|||
|
|
- int64_t model_pred = (*run_ai_model) (argc, argv,
|
|||
|
|
- mcpu_option, argc_hw, argv_hw);
|
|||
|
|
-
|
|||
|
|
- if (ai4c_lib_handle)
|
|||
|
|
- dlclose (ai4c_lib_handle);
|
|||
|
|
-
|
|||
|
|
- if (onnxruntime_lib_handle)
|
|||
|
|
- dlclose (onnxruntime_lib_handle);
|
|||
|
|
-
|
|||
|
|
- if (model_pred == 1)
|
|||
|
|
- setenv ("AI_INFER_LEVEL", "1", 1);
|
|||
|
|
- return model_pred;
|
|||
|
|
-}
|
|||
|
|
-
|
|||
|
|
static int
|
|||
|
|
handle_lto_option (unsigned int lang_mask,
|
|||
|
|
unsigned int num_decoded_options,
|
|||
|
|
@@ -1132,12 +1068,12 @@ handle_machine_option (unsigned int lang_mask,
|
|||
|
|
global_options.x_param_l2_cache_size,
|
|||
|
|
global_options.x_param_prefetch_latency,
|
|||
|
|
global_options.x_param_ipa_prefetch_distance_factor};
|
|||
|
|
- int64_t output_pred = ai_infer_optimization (
|
|||
|
|
+ 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;
|
|||
|
|
- }
|
|||
|
|
+ return ret;
|
|||
|
|
|
|||
|
|
return handle_lto_option (lang_mask, num_decoded_options,
|
|||
|
|
argc, argv, opt_array);
|
|||
|
|
diff --git a/gcc/opts-global.cc b/gcc/opts-global.cc
|
|||
|
|
index e684bc5e3..843ace666 100644
|
|||
|
|
--- a/gcc/opts-global.cc
|
|||
|
|
+++ b/gcc/opts-global.cc
|
|||
|
|
@@ -312,7 +312,10 @@ decode_options (struct gcc_options *opts, struct gcc_options *opts_set,
|
|||
|
|
global_options.x_param_prefetch_latency,
|
|||
|
|
global_options.x_param_ipa_prefetch_distance_factor);
|
|||
|
|
const char *tune_native = getenv ("GCC_AI4C_TUNE_INFO");
|
|||
|
|
- prepare_native_tune_str (tune_native);
|
|||
|
|
+ if (tune_native != nullptr)
|
|||
|
|
+ {
|
|||
|
|
+ prepare_native_tune_str (tune_native);
|
|||
|
|
+ }
|
|||
|
|
|
|||
|
|
struct cl_option_handlers handlers;
|
|||
|
|
|
|||
|
|
--
|
|||
|
|
2.33.0
|
|||
|
|
|