From patchwork Thu Sep 11 12:09:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongyao Chen X-Patchwork-Id: 120071 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 4E7F73858431 for ; Thu, 11 Sep 2025 12:10:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E7F73858431 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=OE8/smx7 X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by sourceware.org (Postfix) with ESMTPS id 9672A3858412 for ; Thu, 11 Sep 2025 12:09:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9672A3858412 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9672A3858412 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1029 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757592581; cv=none; b=LOcPolfjeH9DrAk9bgfcyXGGTrnrmMq8DKuDz1YAhrvtfXejcgLAz58ZbiRhtpUtwRuciSqOqd1z2Ae9vxMIk8pGOFnYk180gTVE0LHY/GxBXBC6TEFrQUoYX6TJnYpKJDhcQhgzy82K24hsrBsJr+Hv3FdiJwbuHnFMSfMjhz0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757592581; c=relaxed/simple; bh=o8ck4Eoq4G5vW1BNYphj4UA52RC2zQYRcNNJk31deQQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=oZOWb+HnZgaxCQcchcrR/uJ7V9dJCrg7HmO7WRHxDwnwe194IGbnb4doEoPezXEPNwtNB8Y4i592rtKJEP260iVtsbly8VogEWzQVaI1sWlQobHG8lsQMGDnZB0uwRV/cbFu2ozoHdDCWXRL8zrw1UqBKWf46x1nafTlnLJaDy4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9672A3858412 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-32b92d75eaeso567130a91.0 for ; Thu, 11 Sep 2025 05:09:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1757592580; x=1758197380; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=osCNyh2LyPXssfM2SEKiqj9AXATB8QtL0yG8x/DIouI=; b=OE8/smx7T+71vQVQLdWi4ULz/0UH69Q3WHCrUv0ZzTFgrmfMYpdHpfO/KLSfLNe0cv 1vSpuynkLPG8tRnAA97Hf4ouW/tL66qootUyJl578crOhkGSq9Gxv/0Dm1lP+lJH1Lx1 UJchsv2Ux0LU9ctCy/Hee4gTZvg39bAb61829yUEPJptP7a2KETzIXEF3u/GdRvWhmC+ TCb3LPUogSPcKF2p2tIgU0iGxwS6HEA9oZdoO2XGaWKtZ1taHoDdsJ/RElWXfgSTu0Ch 7GCmMALCmHA04qzFCG7rxZewDzKdawY89CwrO3dNwfF9GeoaQO6n3Wgm37oQXY7puTCG PpKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757592580; x=1758197380; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=osCNyh2LyPXssfM2SEKi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O5sCxSwYar68jeitsG745+Obdg== X-Received: by 2002:a17:90b:3911:b0:327:faa6:b5b7 with SMTP id 98e67ed59e1d1-32dd1daf30dmr3248724a91.17.1757592579915; Thu, 11 Sep 2025 05:09:39 -0700 (PDT) Received: from codespaces-409d84.fssiothb10mu5gkmp1tljntcza.phxx.internal.cloudapp.net ([172.182.200.128]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-77607b347f8sm1906465b3a.82.2025.09.11.05.09.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Sep 2025 05:09:39 -0700 (PDT) From: Zhongyao Chen To: gcc-patches@gcc.gnu.org Cc: Zhongyao Chen Subject: [PATCH] RISC-V: Implement RISC-V profile macro support Date: Thu, 11 Sep 2025 12:09:17 +0000 Message-ID: <20250911120917.8678-1-chenzhongyao.hit@gmail.com> X-Mailer: git-send-email 2.50.1 MIME-Version: 1.0 X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org From: Zhongyao Chen users can now write code like the following to adapt to the current RISC-V profile selected at compile time: ```c #ifdef __riscv_rva23u64 // Code specific to the rva23u64 profile #endif ``` gcc/ * common/config/riscv/riscv-common.cc (riscv_subset_list::get_profile_name): New function. * config/riscv/riscv-c.cc (riscv_cpu_cpp_builtins): Define profile macro if a profile is detected. * config/riscv/riscv-subset.h (riscv_subset_list::get_profile_name): Declare. Signed-off-by: Zhongyao Chen --- gcc/common/config/riscv/riscv-common.cc | 40 +++++++++++++++++++++++++ gcc/config/riscv/riscv-c.cc | 9 ++++++ gcc/config/riscv/riscv-subset.h | 2 ++ 3 files changed, 51 insertions(+) diff --git a/gcc/common/config/riscv/riscv-common.cc b/gcc/common/config/riscv/riscv-common.cc index a165506f4..e7536edab 100644 --- a/gcc/common/config/riscv/riscv-common.cc +++ b/gcc/common/config/riscv/riscv-common.cc @@ -1450,6 +1450,46 @@ fail: return NULL; } +/* Get profile name from the subset_list. */ + +const char * +riscv_subset_list::get_profile_name () const +{ + const char *best_profile = NULL; + int max_ext_count = -1; + + for (int i = 0; riscv_profiles_table[i].profile_name != nullptr; ++i) + { + riscv_subset_list *subset_list = riscv_subset_list::parse ( + riscv_profiles_table[i].profile_string, NULL); + if (!subset_list) + continue; + if (subset_list->xlen () == this->xlen ()) + { + int ext_count = 0; + bool all_found = true; + for (riscv_subset_t *p = subset_list->m_head; p != NULL; + p = p->next, ++ext_count) + { + if (!this->lookup (p->name.c_str (), + p->major_version, + p->minor_version)) + { + all_found = false; + break; + } + } + if (all_found && ext_count > max_ext_count) + { + max_ext_count = ext_count; + best_profile = riscv_profiles_table[i].profile_name; + } + } + delete subset_list; + } + return best_profile; +} + /* Clone whole subset list. */ riscv_subset_list * diff --git a/gcc/config/riscv/riscv-c.cc b/gcc/config/riscv/riscv-c.cc index 4fc052817..d497326e0 100644 --- a/gcc/config/riscv/riscv-c.cc +++ b/gcc/config/riscv/riscv-c.cc @@ -165,6 +165,15 @@ riscv_cpu_cpp_builtins (cpp_reader *pfile) if (!subset_list) return; + /* Define profile macro if a profile was used. */ + const char *profile_name = subset_list->get_profile_name (); + if (profile_name) + { + char *profile_macro = (char *)alloca (strlen (profile_name) + 10); + sprintf (profile_macro, "__riscv_%s", profile_name); + builtin_define (profile_macro); + } + size_t max_ext_len = 0; /* Figure out the max length of extension name for reserving buffer. */ diff --git a/gcc/config/riscv/riscv-subset.h b/gcc/config/riscv/riscv-subset.h index 4cd860fee..1887ed7cc 100644 --- a/gcc/config/riscv/riscv-subset.h +++ b/gcc/config/riscv/riscv-subset.h @@ -105,6 +105,8 @@ public: unsigned xlen () const {return m_xlen;}; + const char *get_profile_name () const; + riscv_subset_list *clone () const; static riscv_subset_list *parse (const char *, location_t *);