From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by sourceware.org (Postfix) with ESMTPS id 7A23A393D016 for ; Tue, 1 Dec 2020 09:29:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7A23A393D016 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kito.cheng@sifive.com Received: by mail-pl1-x643.google.com with SMTP id u2so801092pls.10 for ; Tue, 01 Dec 2020 01:29:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/tjFrriyfiafdZvLU5V4RpsQxfDCLrZC0LrhG6dW8FE=; b=YUtEzLm5Rft3x/C0Gt1i50pVU+fcE9Z183CIBkzRQY/u2tgsgmvJXq+OZMO7hhDQ9x EQ/f+j9PhzAp741Up9trNyV3jGQKaPyy4Sh9l591//17/fuoV0yy/dt+awSsN4M6XsuF I9Dcxvcx0B/QTUuf6+EA6tq0dTuf8gXw256s2IP5lmVubiattSesSmKB4jb5ZMkvBMfH oQ8Jl8k6hSSqF8tkNnpPjGvcI+DquPIXhRvIgCnMnSlrKbh5EpsxuYLPdZHcK25BlLhL EvcOsocjodbHsj8bGfeb+kA/6WsxQG9bSiCNCGSVJkz52ItUAqFEEyFrWUPmP4WERgN5 FbpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/tjFrriyfiafdZvLU5V4RpsQxfDCLrZC0LrhG6dW8FE=; b=o7nB5mtLvYEdjv8C+84SZo6wJ0KAXGCRAlkTtkwQGcS6TJralEN8pRtVGW1CAcyqqJ 8CHenXOZCq46vFkBE8K+bkSaSHiaJtiGu0mXIyHpcdDA2YBgxoa5rF6T+hVgXs2lbs+i AOOqXNbp8p3x6d2vs5XrLWMS4Ls5DP/RGbQOgopT+GwPEUvXriN0NAhf5gahddUeDRWG zjNP95BYPE2BwQKWKmCMJLGtXWwHXQ7r/c4t9M7j6zwJ1PqoPyG4v4dz7X9FT+9GmIDY Kwv50t/D6+iXO05iYJ+dfQGBgk9syizzpFplrpbce/Jr5aQb1ZSVqWKQnCiwGGECZ6aX G4sQ== X-Gm-Message-State: AOAM532Ro4l1B/alyoBRTDAwz8dBDWMzZIXqqQycHwVDT6HBxnhqJFsP lAcTl2ELQ+POEeqbhsfF7wZ6wcZatfL8jj0A X-Google-Smtp-Source: ABdhPJxv0sq4f7IRM45NYOhvHzSfK3SkRLZB22LxtwxJ9MkKzEXCB6ggBtUot3q7hRevPMJAshgZ+w== X-Received: by 2002:a17:90a:65c9:: with SMTP id i9mr1820119pjs.125.1606814979744; Tue, 01 Dec 2020 01:29:39 -0800 (PST) Received: from hsinchu02.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id q35sm2037503pjh.38.2020.12.01.01.29.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2020 01:29:39 -0800 (PST) From: Kito Cheng To: gcc-patches@gcc.gnu.org, kito.cheng@gmail.com, jimw@sifive.com Cc: Kito Cheng Subject: [PATCH 2/2] RISC-V: Implement TARGET_COMPUTE_MULTILIB Date: Tue, 1 Dec 2020 17:29:20 +0800 Message-Id: <20201201092920.8677-2-kito.cheng@sifive.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201201092920.8677-1-kito.cheng@sifive.com> References: <20201201092920.8677-1-kito.cheng@sifive.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Dec 2020 09:29:43 -0000 Use TARGET_COMPUTE_MULTILIB to search the multi-lib reuse for riscv*-*-elf*, according following rules: 1. Check ABI is same. 2. Check both has atomic extension or both don't have atomic extension. - Because mix soft and hard atomic operation doesn't make sense and won't work as expect. 3. Check current arch is superset of the target multi-lib arch. - It might result slower performance or larger code size, but it safe to run. 4. Pick most match multi-lib set if more than one multi-lib are pass the above checking. Example for how to select multi-lib: We build code with -march=rv32imaf and -mabi=ilp32, and we have following 5 multi-lib set: 1. rv32ia/ilp32 2. rv32ima/ilp32 3. rv32imf/ilp32 4. rv32imaf/ilp32f 5. rv32imafd/ilp32 The first and second multi-lib is safe to like, 3rd multi-lib can't re-use becasue it don't have atomic extension, which is mismatch according rule 2, and the 4th multi-lib can't re-use too due to the ABI mismatch, the last multi-lib can't use since current arch is not superset of the arch of multi-lib. And emit error if not found suitable multi-lib set, the error message only emit when link with standard libraries. Example for when error will be emitted: $ riscv64-unknown-elf-gcc -print-multi-lib .; rv32i/ilp32;@march=rv32i@mabi=ilp32 rv32im/ilp32;@march=rv32im@mabi=ilp32 rv32iac/ilp32;@march=rv32iac@mabi=ilp32 rv32imac/ilp32;@march=rv32imac@mabi=ilp32 rv32imafc/ilp32f;@march=rv32imafc@mabi=ilp32f rv64imac/lp64;@march=rv64imac@mabi=lp64 // No actual linking, so no error emitted. $ riscv64-unknown-elf-gcc -print-multi-directory -march=rv32ia -mabi=ilp32 . // Link to default libc and libgcc, so check the multi-lib, and emit // error because not found suitable multilib. $ riscv64-unknown-elf-gcc -march=rv32ia -mabi=ilp32 ~/hello.c riscv64-unknown-elf-gcc: fatal error: can't found suitable multilib set for '-march=rv32ia'/'-mabi=ilp32' compilation terminated. // No error emitted, because not link to stdlib. $ riscv64-unknown-elf-gcc -march=rv32ia -mabi=ilp32 ~/hello.c -nostdlib // No error emitted, because compile only. $ riscv64-unknown-elf-gcc -march=rv32ia -mabi=ilp32 ~/hello.c -c gcc/ChangeLog: * common/config/riscv/riscv-common.c (riscv_subset_list::match_score): New. (find_last_appear_switch): Ditto. (struct multi_lib_info_t): Ditto. (riscv_no_matched_multi_lib): Ditto. (riscv_current_arch_str): Ditto. (riscv_current_abi_str): Ditto. (riscv_compute_multilib): Ditto. (TARGET_COMPUTE_MULTILIB): Defined. * config/riscv/elf.h (LIB_SPEC): Call riscv_multi_lib_check if doing link. (RISCV_USE_CUSTOMISED_MULTI_LIB): New. * config/riscv/riscv.h (riscv_multi_lib_check): New. (EXTRA_SPEC_FUNCTIONS): Add riscv_multi_lib_check. --- gcc/common/config/riscv/riscv-common.c | 253 +++++++++++++++++++++++++ gcc/config/riscv/elf.h | 6 +- gcc/config/riscv/riscv.h | 4 +- 3 files changed, 261 insertions(+), 2 deletions(-) diff --git a/gcc/common/config/riscv/riscv-common.c b/gcc/common/config/riscv/riscv-common.c index 5e3ddcf3f81..481f0be9944 100644 --- a/gcc/common/config/riscv/riscv-common.c +++ b/gcc/common/config/riscv/riscv-common.c @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see #include "flags.h" #include "diagnostic-core.h" #include "config/riscv/riscv-protos.h" +#include #define RISCV_DONT_CARE_VERSION -1 @@ -171,12 +172,31 @@ public: static riscv_subset_list *parse (const char *, location_t); + int match_score (riscv_subset_list *) const; }; static const char *riscv_supported_std_ext (void); static riscv_subset_list *current_subset_list = NULL; +/* struct for recording multi-lib info. */ +struct riscv_multi_lib_info_t { + std::string path; + std::string arch_str; + std::string abi_str; + riscv_subset_list *subset_list; + + static bool parse (struct riscv_multi_lib_info_t *, + const std::string &); +}; + +/* Flag for checking if there is no suitable multi-lib found. */ +static bool riscv_no_matched_multi_lib; + +/* Used for record value of -march and -mabi. */ +static std::string riscv_current_arch_str; +static std::string riscv_current_abi_str; + riscv_subset_t::riscv_subset_t () : name (), major_version (0), minor_version (0), next (NULL), explicit_version_p (false), implied_p (false) @@ -202,6 +222,42 @@ riscv_subset_list::~riscv_subset_list () } } +/* Compute the match score of two arch string, return 0 if incompatible. */ +int +riscv_subset_list::match_score (riscv_subset_list *list) const +{ + riscv_subset_t *s; + int score = 0; + bool has_a_ext, list_has_a_ext; + + /* Impossible to match if XLEN is different. */ + if (list->m_xlen != this->m_xlen) + return 0; + + /* There is different code gen in libstdc++ and libatomic between w/ A-ext + and w/o A-ext, and it not work if using soft and hard atomic mechanism + at same time, so they are incompatible. */ + has_a_ext = this->lookup ("a") != NULL; + list_has_a_ext = list->lookup ("a") != NULL; + + if (has_a_ext != list_has_a_ext) + return 0; + + + /* list must be subset of current this list, otherwise it not safe to + link. + TODO: We might give different weigth for each extension, but the rule could + be complicated. + TODO: We might consider the version of each extension. */ + for (s = list->m_head; s != NULL; s = s->next) + if (this->lookup (s->name.c_str ()) != NULL) + score++; + else + return 0; + + return score; +} + /* Get the rank for single-letter subsets, lower value meaning higher priority. */ @@ -1109,6 +1165,203 @@ riscv_expand_arch_from_cpu (int argc ATTRIBUTE_UNUSED, return xasprintf ("-march=%s", arch.c_str()); } +static const char * +find_last_appear_switch ( + const struct switchstr *switches, + int n_switches, + const char *prefix) +{ + int i; + size_t len = strlen (prefix); + + for (i = 0; i < n_switches; ++i) { + const struct switchstr *this_switch = &switches[n_switches - i - 1]; + + if (this_switch->live_cond & SWITCH_FALSE) + continue; + + if (strncmp (this_switch->part1, prefix, len) == 0) + return this_switch->part1 + len; + } + + return NULL; +} + +bool +riscv_multi_lib_info_t::parse ( + struct riscv_multi_lib_info_t *multi_lib_info, + const std::string &path) +{ + if (path == ".") + { + multi_lib_info->arch_str = STRINGIZING (TARGET_RISCV_DEFAULT_ARCH); + multi_lib_info->abi_str = STRINGIZING (TARGET_RISCV_DEFAULT_ABI); + multi_lib_info->subset_list = + riscv_subset_list::parse( + STRINGIZING (TARGET_RISCV_DEFAULT_ARCH), + input_location); + } + else + { + size_t slash_pos = path.find('/'); + + if (slash_pos == std::string::npos) + return false; + + multi_lib_info->arch_str = path.substr(0, slash_pos); + multi_lib_info->abi_str = path.substr(slash_pos + 1, path.length() - 1); + } + + multi_lib_info->subset_list = + riscv_subset_list::parse(multi_lib_info->arch_str.c_str (), input_location); + + return true; +} + +const char * +riscv_multi_lib_check (int argc ATTRIBUTE_UNUSED, + const char **argv ATTRIBUTE_UNUSED) +{ + if (riscv_no_matched_multi_lib) + fatal_error ( + input_location, + "Can't found suitable multilib set for %<-march=%s%>/%<-mabi=%s%>", + riscv_current_arch_str.c_str (), + riscv_current_abi_str.c_str ()); + + return ""; +} + +/* We only override this in bare-matel toolchain. */ +#ifdef RISCV_USE_CUSTOMISED_MULTI_LIB +/* Implement TARGET_COMPUTE_MULTILIB. */ + +static const char * +riscv_compute_multilib ( + const struct switchstr *switches, + int n_switches, + const char *multilib_dir, + const char *multilib_defaults ATTRIBUTE_UNUSED, + const char *multilib_select, + const char *multilib_matches ATTRIBUTE_UNUSED, + const char *multilib_exclusions ATTRIBUTE_UNUSED, + const char *multilib_reuse ATTRIBUTE_UNUSED) +{ + const char *p; + const char *this_path; + size_t this_path_len; + bool result; + riscv_no_matched_multi_lib = false; + riscv_subset_list *subset_list = NULL; + + std::vector multilib_infos; + riscv_multi_lib_info_t multilib_info; + + /* Already found suitable, multi-lib, just use that. */ + if (multilib_dir != NULL) + return multilib_dir; + + /* Find last march. */ + riscv_current_arch_str = + find_last_appear_switch (switches, n_switches, "march="); + /* Find mabi. */ + riscv_current_abi_str = + find_last_appear_switch (switches, n_switches, "mabi="); + + /* Failed to find -march or -mabi, but it should not happened since we have + set both in OPTION_DEFAULT_SPECS. */ + if (riscv_current_arch_str.empty () || riscv_current_abi_str.empty ()) + return multilib_dir; + + subset_list = riscv_subset_list::parse (riscv_current_arch_str.c_str (), + input_location); + + /* Failed to parse -march, fallback to using what gcc use. */ + if (subset_list == NULL) + return multilib_dir; + + /* Parsing MULTILIB_SELECT, ignore MULTILIB_REUSE here, we have our own rules. + TODO: most codes are grab from gcc.c, maybe we should refine that? */ + p = multilib_select; + + while (*p != '\0') + { + /* Ignore newlines. */ + if (*p == '\n') + { + ++p; + continue; + } + + /* Get the initial path. */ + this_path = p; + while (*p != ' ') + { + if (*p == '\0') + { + fatal_error (input_location, "multilib select %qs %qs is invalid", + multilib_select, multilib_reuse); + } + ++p; + } + this_path_len = p - this_path; + multilib_info.path = std::string (this_path, this_path_len); + + while (*p != ';') + { + if (*p == '\0') + { + fatal_error (input_location, "multilib select %qs %qs is invalid", + multilib_select, multilib_reuse); + } + ++p; + } + + result = + riscv_multi_lib_info_t::parse ( + &multilib_info, + std::string (this_path, this_path_len)); + + if (result) + multilib_infos.push_back (multilib_info); + + p++; + } + + int match_score = 0; + int max_match_score = 0; + int best_match_multi_lib = -1; + /* Try to decition which set we should used. */ + /* We have 3 level dection tree here, ABI, A-ext, check input arch/ABI must be + superset of multi-lib arch. */ + for (size_t i = 0; i < multilib_infos.size (); ++i) + { + /* Check ABI is same first. */ + if (riscv_current_abi_str != multilib_infos[i].abi_str) + continue; + + /* Found a potential compatible multi-lib setting! + Calculate the match score. */ + match_score = subset_list->match_score (multilib_infos[i].subset_list); + + /* Record highest match score multi-lib setting. */ + if (match_score > max_match_score) + best_match_multi_lib = i; + } + + if (best_match_multi_lib == -1) + { + riscv_no_matched_multi_lib = true; + return multilib_dir; + } + else + return xstrdup (multilib_infos[best_match_multi_lib].path.c_str ()); +} + +#undef TARGET_COMPUTE_MULTILIB +#define TARGET_COMPUTE_MULTILIB riscv_compute_multilib +#endif + /* Implement TARGET_OPTION_OPTIMIZATION_TABLE. */ static const struct default_options riscv_option_optimization_table[] = diff --git a/gcc/config/riscv/elf.h b/gcc/config/riscv/elf.h index 4e2d6464d02..4aeb7d6141c 100644 --- a/gcc/config/riscv/elf.h +++ b/gcc/config/riscv/elf.h @@ -25,10 +25,14 @@ along with GCC; see the file COPYING3. If not see /* Link against Newlib libraries, because the ELF backend assumes Newlib. Handle the circular dependence between libc and libgloss. */ #undef LIB_SPEC -#define LIB_SPEC "--start-group -lc %{!specs=nosys.specs:-lgloss} --end-group" +#define LIB_SPEC \ + "--start-group -lc %{!specs=nosys.specs:-lgloss} --end-group " \ + "%{!nostartfiles:%{!nodefaultlibs:%{!nolibc:%{!nostdlib:%:riscv_multi_lib_check()}}}}" #undef STARTFILE_SPEC #define STARTFILE_SPEC "crt0%O%s crtbegin%O%s" #undef ENDFILE_SPEC #define ENDFILE_SPEC "crtend%O%s" + +#define RISCV_USE_CUSTOMISED_MULTI_LIB 1 diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h index df3003fbaa0..a92e0e220ad 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -43,11 +43,13 @@ along with GCC; see the file COPYING3. If not see extern const char *riscv_expand_arch (int argc, const char **argv); extern const char *riscv_expand_arch_from_cpu (int argc, const char **argv); extern const char *riscv_default_mtune (int argc, const char **argv); +extern const char *riscv_multi_lib_check (int argc, const char **argv); # define EXTRA_SPEC_FUNCTIONS \ { "riscv_expand_arch", riscv_expand_arch }, \ { "riscv_expand_arch_from_cpu", riscv_expand_arch_from_cpu }, \ - { "riscv_default_mtune", riscv_default_mtune }, + { "riscv_default_mtune", riscv_default_mtune }, \ + { "riscv_multi_lib_check", riscv_multi_lib_check }, /* Support for a compile-time default CPU, et cetera. The rules are: --with-arch is ignored if -march or -mcpu is specified. -- 2.29.2