From patchwork Thu Sep 16 09:38:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kito Cheng X-Patchwork-Id: 45075 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 49F9A385782A for ; Thu, 16 Sep 2021 09:39:59 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by sourceware.org (Postfix) with ESMTPS id E696C3857830 for ; Thu, 16 Sep 2021 09:38:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E696C3857830 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-pj1-x1036.google.com with SMTP id f3-20020a17090a638300b00199097ddf1aso7129354pjj.0 for ; Thu, 16 Sep 2021 02:38:48 -0700 (PDT) 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=HXGmzvra77ZhrjwNEyehx8ktBXOZzSIwmFFThgA3hTY=; b=GcLYrWfCcT6OjOP0qK0otQecPsjI/jF58jU7PHEX/XnYz4tu0fmYXq+wMt/KMUZ6A4 w7JIwwr77ff8qN1KgaP7cfvcBPBMGAp9gNny2dkzXonTYsd826GpwesPQlSP/A1HWpS4 Y/+qe4c7qkPJbXQpsI43hpd1ImtEs8hNokSmhlsc8Ke1dvO9lEP6Qa4qo1Rc0h3visP4 Y6kXcnx714EBbyRRqT/n4KNgTxaT5JMIg2rlP0vRCLkHe+SlMQ0I+iiOlhmVrSAp9jC0 F2EaEH5ze8fX8h2DVi+gBuxEAjGYhzNzYFElQ4HW+j5ejmeXWowA9+9N9pkLKq9sgW/N 7hWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HXGmzvra77ZhrjwNEyehx8ktBXOZzSIwmFFThgA3hTY=; b=5GwT2MxgAgWeQwfzkVSu+CUjkPzI07N7jLaTFIPYsMoZaWyp4bGdkubK/HnU0zCo8N DMAy6rhA9EGY9u0JXKe7Wj2rTX2IQ6j5mJsrTnIHfk315lfYoDgvia72pnl/gxhGfh2I /p50C3y6iZVRnMiaFMwmFrL4yFALnRCL1Q4TwRenLFFHFi8kv/bWEwd2K5zrradojGD8 71cJKk1Z2r3YLWbsOy4V8OlDnu7Xkoxrh34ylfkS7hSj+pzYV5d0TTyxm7tANeJqAY5r /ejlq6Zss2L8xTosGEqhucatvx4OWmvVmGHBc73/zTmZtOC8R6s9GHcj3Drdm7SRrEYk wgzw== X-Gm-Message-State: AOAM533smrzmAvxDvqzMY80AL+ojTgg+q4i4SRjP7fADzVHu8c5PhXlu sTGbQ8elpAIEqPQJ+L3+8aYMJnriqainvw== X-Google-Smtp-Source: ABdhPJz2qoSCdz9lKGwd0EoXTRJ+A/oLNSueVZEwn7o36co6PUO7Vo5Nsqtm6bQVq83d5RjEHVA1Gg== X-Received: by 2002:a17:90b:124c:: with SMTP id gx12mr5146740pjb.106.1631785127542; Thu, 16 Sep 2021 02:38:47 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id 77sm2407806pfu.219.2021.09.16.02.38.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Sep 2021 02:38:47 -0700 (PDT) From: Kito Cheng To: gcc-patches@gcc.gnu.org, kito.cheng@gmail.com, jimw@sifive.com, palmer@dabbelt.com, andrew@sifive.com Subject: [PATCH v3 1/2] Add TARGET_COMPUTE_MULTILIB hook to override multi-lib result. Date: Thu, 16 Sep 2021 17:38:37 +0800 Message-Id: <20210916093838.95184-2-kito.cheng@sifive.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916093838.95184-1-kito.cheng@sifive.com> References: <20210916093838.95184-1-kito.cheng@sifive.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.6 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , Cc: Kito Cheng Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Create a new hook to let target could override the multi-lib result, the motivation is RISC-V might have very complicated multi-lib re-use rule*, which is hard to maintain and use current multi-lib scripts, we even hit the "argument list too long" error when we tried to add more multi-lib reuse rule. So I think it would be great to have a target specific way to determine the multi-lib re-use rule, then we could write those rule in C, instead of expand every possible case in MULTILIB_REUSE. * Here is an example for RISC-V multi-lib rules: https://gist.github.com/kito-cheng/0289cd42d9a756382e5afeb77b42b73b gcc/ChangeLog: * common/common-target.def (compute_multilib): New. * common/common-targhooks.c (default_compute_multilib): New. * doc/tm.texi.in (TARGET_COMPUTE_MULTILIB): New. * doc/tm.texi: Regen. * gcc.c: Include common/common-target.h. (set_multilib_dir) Call targetm_common.compute_multilib. (SWITCH_LIVE): Move to opts.h. (SWITCH_FALSE): Ditto. (SWITCH_IGNORE): Ditto. (SWITCH_IGNORE_PERMANENTLY): Ditto. (SWITCH_KEEP_FOR_GCC): Ditto. (struct switchstr): Ditto. * opts.h (SWITCH_LIVE): Move from gcc.c. (SWITCH_FALSE): Ditto. (SWITCH_IGNORE): Ditto. (SWITCH_IGNORE_PERMANENTLY): Ditto. (SWITCH_KEEP_FOR_GCC): Ditto. (struct switchstr): Ditto. --- gcc/common/common-target.def | 25 ++++++++++++++++++ gcc/common/common-targhooks.c | 15 +++++++++++ gcc/doc/tm.texi | 17 +++++++++++++ gcc/doc/tm.texi.in | 3 +++ gcc/gcc.c | 48 +++++++++-------------------------- gcc/opts.h | 36 ++++++++++++++++++++++++++ 6 files changed, 108 insertions(+), 36 deletions(-) diff --git a/gcc/common/common-target.def b/gcc/common/common-target.def index f54590a2a54..b7cf713770c 100644 --- a/gcc/common/common-target.def +++ b/gcc/common/common-target.def @@ -84,6 +84,31 @@ The result will be pruned to cases with PREFIX if not NULL.", vec, (int option_code, const char *prefix), default_get_valid_option_values) +DEFHOOK +(compute_multilib, + "Some targets like RISC-V might have complicated multilib reuse rules which\n\ +are hard to implement with the current multilib scheme. This hook allows\n\ +targets to override the result from the built-in multilib mechanism.\n\ +@var{switches} is the raw option list with @var{n_switches} items;\n\ +@var{multilib_dir} is the multi-lib result which is computed by the built-in\n\ +multi-lib mechanism;\n\ +@var{multilib_defaults} is the default options list for multi-lib;\n\ +@var{multilib_select} is the string containing the list of supported\n\ +multi-libs, and the option checking list.\n\ +@var{multilib_matches}, @var{multilib_exclusions}, and @var{multilib_reuse}\n\ +are corresponding to @var{MULTILIB_MATCHES}, @var{MULTILIB_EXCLUSIONS},\n\ +and @var{MULTILIB_REUSE}.\n\ +The default definition does nothing but return @var{multilib_dir} directly.", + const char *, (const struct switchstr *switches, + int n_switches, + const char *multilib_dir, + const char *multilib_defaults, + const char *multilib_select, + const char *multilib_matches, + const char *multilib_exclusions, + const char *multilib_reuse), + default_compute_multilib) + /* Leave the boolean fields at the end. */ /* True if unwinding tables should be generated by default. */ diff --git a/gcc/common/common-targhooks.c b/gcc/common/common-targhooks.c index 325f199bff3..1477aeeb536 100644 --- a/gcc/common/common-targhooks.c +++ b/gcc/common/common-targhooks.c @@ -90,3 +90,18 @@ const struct default_options empty_optimization_table[] = { { OPT_LEVELS_NONE, 0, NULL, 0 } }; + +/* Default version of TARGET_COMPUTE_MULTILIB. */ +const char * +default_compute_multilib( + const struct switchstr *, + int, + const char *multilib, + const char *, + const char *, + const char *, + const char *, + const char *) +{ + return multilib; +} diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index be8148583d8..6f1e0293b6c 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -778,6 +778,23 @@ options are changed via @code{#pragma GCC optimize} or by using the Set target-dependent initial values of fields in @var{opts}. @end deftypefn +@deftypefn {Common Target Hook} {const char *} TARGET_COMPUTE_MULTILIB (const struct switchstr *@var{switches}, int @var{n_switches}, const char *@var{multilib_dir}, const char *@var{multilib_defaults}, const char *@var{multilib_select}, const char *@var{multilib_matches}, const char *@var{multilib_exclusions}, const char *@var{multilib_reuse}) +Some targets like RISC-V might have complicated multilib reuse rules which +are hard to implement with the current multilib scheme. This hook allows +targets to override the result from the built-in multilib mechanism. +@var{switches} is the raw option list with @var{n_switches} items; +@var{multilib_dir} is the multi-lib result which is computed by the built-in +multi-lib mechanism; +@var{multilib_defaults} is the default options list for multi-lib; +@var{multilib_select} is the string containing the list of supported +multi-libs, and the option checking list. +@var{multilib_matches}, @var{multilib_exclusions}, and @var{multilib_reuse} +are corresponding to @var{MULTILIB_MATCHES}, @var{MULTILIB_EXCLUSIONS}, +and @var{MULTILIB_REUSE}. +The default definition does nothing but return @var{multilib_dir} directly. +@end deftypefn + + @defmac SWITCHABLE_TARGET Some targets need to switch between substantially different subtargets during compilation. For example, the MIPS target has one subtarget for diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in index d088eee4afe..ad78439ea61 100644 --- a/gcc/doc/tm.texi.in +++ b/gcc/doc/tm.texi.in @@ -736,6 +736,9 @@ options are changed via @code{#pragma GCC optimize} or by using the @hook TARGET_OPTION_INIT_STRUCT +@hook TARGET_COMPUTE_MULTILIB + + @defmac SWITCHABLE_TARGET Some targets need to switch between substantially different subtargets during compilation. For example, the MIPS target has one subtarget for diff --git a/gcc/gcc.c b/gcc/gcc.c index 3e98bc7973e..dafe754267a 100644 --- a/gcc/gcc.c +++ b/gcc/gcc.c @@ -43,6 +43,7 @@ compilation is specified by a string called a "spec". */ #include "opts.h" #include "filenames.h" #include "spellcheck.h" +#include "common/common-target.h" @@ -3573,42 +3574,6 @@ execute (void) } } -/* Find all the switches given to us - and make a vector describing them. - The elements of the vector are strings, one per switch given. - If a switch uses following arguments, then the `part1' field - is the switch itself and the `args' field - is a null-terminated vector containing the following arguments. - Bits in the `live_cond' field are: - SWITCH_LIVE to indicate this switch is true in a conditional spec. - SWITCH_FALSE to indicate this switch is overridden by a later switch. - SWITCH_IGNORE to indicate this switch should be ignored (used in % X-Patchwork-Id: 45076 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 04B6A3857419 for ; Thu, 16 Sep 2021 09:40:29 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by sourceware.org (Postfix) with ESMTPS id C9D043857800 for ; Thu, 16 Sep 2021 09:38:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C9D043857800 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-pg1-x52e.google.com with SMTP id n18so5566971pgm.12 for ; Thu, 16 Sep 2021 02:38:50 -0700 (PDT) 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=WkBEOKBxNHqZaRYd+L9hH4uh9T+dvlZaNvV0kadtoWA=; b=h3qz1rqkJPyiS8OhQVug8Hnt6TAR2R+vYiwCtgg/OeIbvQxBJOML1XtnC809Bb9NDH XoaV16jpGEz6S/6OKsT4vueUtMTi4hMNh6xQy8PsxNpI2emCDB+Gm8d41A251PF+vxnZ 6gthbInCt6G0gWyGzcmklo/Ie0nNt+IJI+Ex4eT79FxPgx5XLXe20rtm129MWz/oXORo 6delhMrsoXi1/TT3LUmXUL1wbhqKT4B9N+OJwdzwePnGR3EarmjKXvqZYqF5whvjiyCr Ns3X55fJ3kmxWG+FLeAXcZt6AQDBt7IPQKU/2DQnsSjOYdeQs9dFJQ4BjS+aXeajYkjO drcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WkBEOKBxNHqZaRYd+L9hH4uh9T+dvlZaNvV0kadtoWA=; b=MvfAW9GgxyMFqE8HOyF2i5fXQ3z+5gaTog22+zLPol4o2xPgXy3+LaoMpb8ZaOHd3s r7XrbqZUCs3Lnxwj3bnxtufYTE2sucKYhWeTsiwlAYeXun7YVmjLaA3odaSE0G4buYnV XMMUUTpLhrxgi5dwOvW4HyKWDJKuiMrRMHX33UqYcT/vRDoDN8A8oNvScmaTiwAaVhB6 m9+6Efym+YahmN0S027siymue3QGiyi/LyFXpAR7yS/ta0AheXWvlcGRYRZJphOPP+1v EcBoN3Z9M25Uy9KLh4U3FBXu2diUJdZQfjKI4WO/snqXs+dmP7GpjDvGXx+b3MZAV85y 7meg== X-Gm-Message-State: AOAM530XND/IyYXWXLBdsG8y7f74K1N9YxDV81Fw8Di98qv0RuhFnPJG tWyTib+H411Bbz58JGr5meMk1UQDIdJI3w== X-Google-Smtp-Source: ABdhPJyNFrMqSGGItWLFFW3H9D5c/aeU+ewmyvH53ourEflsFDKMzBOvqt/dOEeuB8lhUVu22xvRBQ== X-Received: by 2002:a63:4f43:: with SMTP id p3mr4071848pgl.435.1631785129389; Thu, 16 Sep 2021 02:38:49 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id 77sm2407806pfu.219.2021.09.16.02.38.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Sep 2021 02:38:49 -0700 (PDT) From: Kito Cheng To: gcc-patches@gcc.gnu.org, kito.cheng@gmail.com, jimw@sifive.com, palmer@dabbelt.com, andrew@sifive.com Subject: [PATCH v3 2/2] RISC-V: Implement TARGET_COMPUTE_MULTILIB Date: Thu, 16 Sep 2021 17:38:38 +0800 Message-Id: <20210916093838.95184-3-kito.cheng@sifive.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916093838.95184-1-kito.cheng@sifive.com> References: <20210916093838.95184-1-kito.cheng@sifive.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , Cc: Kito Cheng Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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: Include . (struct riscv_multi_lib_info_t): New. (riscv_subset_list::match_score): Ditto. (find_last_appear_switch): Ditto. (prefixed_with): Ditto. (struct multi_lib_info_t): Ditto. (riscv_current_arch_str): Ditto. (riscv_current_abi_str): Ditto. (riscv_multi_lib_info_t::parse): Ditto. (riscv_check_cond): Ditto. (riscv_check_conds): 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 | 377 +++++++++++++++++++++++++ gcc/config/riscv/elf.h | 6 +- gcc/config/riscv/riscv-subset.h | 2 + gcc/config/riscv/riscv.h | 4 +- 4 files changed, 387 insertions(+), 2 deletions(-) diff --git a/gcc/common/config/riscv/riscv-common.c b/gcc/common/config/riscv/riscv-common.c index 10868fd417d..d87418c02a6 100644 --- a/gcc/common/config/riscv/riscv-common.c +++ b/gcc/common/config/riscv/riscv-common.c @@ -18,6 +18,7 @@ along with GCC; see the file COPYING3. If not see . */ #include +#include #define INCLUDE_STRING #include "config.h" @@ -122,6 +123,26 @@ const riscv_subset_list *riscv_current_subset_list () return current_subset_list; } +/* struct for recording multi-lib info. */ +struct riscv_multi_lib_info_t { + std::string path; + std::string arch_str; + std::string abi_str; + std::vector conds; + riscv_subset_list *subset_list; + + static bool parse (struct riscv_multi_lib_info_t *, + const std::string &, + const std::vector &); +}; + +/* 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) @@ -147,6 +168,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 weight 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. */ @@ -1054,6 +1111,326 @@ riscv_expand_arch_from_cpu (int argc ATTRIBUTE_UNUSED, return xasprintf ("-march=%s", arch.c_str()); } +/* Find last switch with the prefix, options are take last one in general, + return NULL if not found, and return the option value if found, it could + return empty string if the option has no value. */ + +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; +} + +/* Utils functions to check STR is start with PREFIX or not. */ + +static bool +prefixed_with (const std::string &str, const char *prefix) +{ + return strncmp (prefix, str.c_str (), strlen (prefix)) == 0; +} + +/* Parse the path and cond string into riscv_multi_lib_info_t, return false + if parsing failed. */ + +bool +riscv_multi_lib_info_t::parse ( + struct riscv_multi_lib_info_t *multi_lib_info, + const std::string &path, + const std::vector &conds) +{ + const char *default_arch_str = STRINGIZING (TARGET_RISCV_DEFAULT_ARCH); + const char *default_abi_str = STRINGIZING (TARGET_RISCV_DEFAULT_ABI); + multi_lib_info->conds = conds; + if (path == ".") + { + multi_lib_info->arch_str = default_arch_str; + multi_lib_info->abi_str = default_abi_str; + } + else + { + std::vector::const_iterator itr; + for (itr = conds.begin (); itr != conds.end (); ++itr) + if (prefixed_with (*itr, "march=")) + multi_lib_info->arch_str = itr->c_str () + strlen ("march="); + else if (prefixed_with (*itr, "mabi=")) + multi_lib_info->abi_str = itr->c_str () + strlen ("mabi="); + + /* Skip this multi-lib if this configuration is exactly same as + default multi-lib settings. */ + if (multi_lib_info->arch_str == default_arch_str + && multi_lib_info->abi_str == default_abi_str) + return false; + } + + multi_lib_info->subset_list = + riscv_subset_list::parse (multi_lib_info->arch_str.c_str (), input_location); + + return true; +} + +/* Report error if not found suitable multilib. */ +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 find suitable multilib set for %<-march=%s%>/%<-mabi=%s%>", + riscv_current_arch_str.c_str (), + riscv_current_abi_str.c_str ()); + + return ""; +} + +/* Checking ARG is not appeared in SWITCHES if NOT_ARG is set or + ARG is appeared if NOT_ARG is not set. */ + +static bool +riscv_check_cond ( + const struct switchstr *switches, + int n_switches, + const std::string &arg, + bool not_arg) +{ + int i; + for (i = 0; i < n_switches; ++i) + { + const struct switchstr *this_switch = &switches[n_switches - i - 1]; + + if ((this_switch->live_cond & SWITCH_IGNORE) != 0) + continue; + + if (this_switch->live_cond & SWITCH_FALSE) + continue; + + /* ARG should not appear if NOT_ARG is set. */ + if (arg == this_switch->part1) + return not_arg ? false : true; + } + + /* Not found ARG? that's ok if NOT_ARG is not set. */ + return not_arg ? true : false; +} + +/* Check the other cond is found or not, return -1 if we should reject this + multi-lib option set, otherwise return updated MATCH_SCORE. */ + +static int +riscv_check_conds ( + const struct switchstr *switches, + int n_switches, + int match_score, + const std::vector &conds) +{ + bool not_arg; + bool ok; + int ok_count = 0; + std::vector::const_iterator itr; + const char *checking_arg; + + if (match_score == 0) + return 0; + + for (itr = conds.begin (); itr != conds.end (); ++itr) + { + /* We'll check march= and mabi= in ohter place. */ + if (prefixed_with (*itr, "march=") || prefixed_with (*itr, "mabi=")) + continue; + + checking_arg = itr->c_str (); + if (checking_arg[0] == '!') + { + not_arg = true; + /* Skip '!'. */ + checking_arg = checking_arg + 1; + } + else + not_arg = false; + + ok = riscv_check_cond (switches, n_switches, checking_arg, not_arg); + + if (!ok) + return -1; + + ok_count++; + } + + /* 100 is magic number, it's just used for make sure this multi-lib has + higher priority if we found any some option is listed in the option check + list. */ + return match_score + ok_count * 100; +} + +/* We only override this in bare-metal 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; + std::vector option_conds; + std::string option_cond; + riscv_multi_lib_info_t multilib_info; + + /* Already found suitable, multi-lib, just use that. */ + if (multilib_dir != NULL) + return multilib_dir; + + /* Find 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; + } + + /* Format of each multilib: + ... ; */ + /* Get the 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); + + option_conds.clear (); + /* Pasrse option check list into vector. + e.g. "march=rv64imafd mabi=lp64 !mcmodel=medany" to + ["march=rv64imafd", "mabi=lp64", "!mcmodel=medany"]. */ + while (*p != ';') + { + option_cond = ""; + /* Skip space. */ + while (*p == ' ') p++; + + while (*p && *p != ' ' && *p != ';') + option_cond.push_back (*p++); + + /* Ignore `!march=` and `!mabi=`, we will handle march and mabi + later. */ + if (option_cond.size () + && !prefixed_with (option_cond, "!march=") + && !prefixed_with (option_cond, "!mabi=")) + option_conds.push_back (option_cond); + } + + result = + riscv_multi_lib_info_t::parse ( + &multilib_info, + std::string (this_path, this_path_len), + option_conds); + + 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 decision which set we should used. */ + /* We have 3 level decision tree here, ABI, check input arch/ABI must + be superset of multi-lib arch, and other rest option checking. */ + 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); + + /* Checking other cond in the multi-lib setting. */ + match_score = riscv_check_conds (switches, + n_switches, + match_score, + multilib_infos[i].conds); + + /* 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 7e65e499031..cc0fd4d9de9 100644 --- a/gcc/config/riscv/elf.h +++ b/gcc/config/riscv/elf.h @@ -27,10 +27,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-subset.h b/gcc/config/riscv/riscv-subset.h index 793655a01f2..56bb4d0a7a1 100644 --- a/gcc/config/riscv/riscv-subset.h +++ b/gcc/config/riscv/riscv-subset.h @@ -88,6 +88,8 @@ public: const riscv_subset_t *begin () const {return m_head;}; const riscv_subset_t *end () const {return NULL;}; + + int match_score (riscv_subset_list *) const; }; extern const riscv_subset_list *riscv_current_subset_list (void); diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h index f47d5b40a66..4d8a4c76c1c 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -50,11 +50,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.