From patchwork Sun Nov 13 20:41:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 19445 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1825638wru; Sun, 13 Nov 2022 12:42:24 -0800 (PST) X-Google-Smtp-Source: AA0mqf52FYuvMoGM3Drx1mNfmlUBBRiM2UHUpX9JeI6NGUvnVSMArdBoC9hAuhJHRPMrO4SBSMTY X-Received: by 2002:a17:906:3790:b0:7ae:b9fb:ce07 with SMTP id n16-20020a170906379000b007aeb9fbce07mr7854003ejc.575.1668372143956; Sun, 13 Nov 2022 12:42:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668372143; cv=none; d=google.com; s=arc-20160816; b=Bmqh8TBU0WLfo0cTrFP1W4UWurse4EJfveyB1mgHXVNudfgB4pI5vrMAl+zGWpXpvC sz7xBaA3MkOdFiIV1A5AB1Fw00XDUZ23cZ4acr0BNqvvADEAo8cNO0olFTlXIIxTMiwG 6qzy7dsCwBtXEWLs9gVdNeoQXLY0HpEAPtVIkKMtpdICSGiujKI0pNhcbaEPViUN6URM lt0+hvb6mS6/bROt1sso6QqmW4XrReqk3KmKb3leiS32/W+5oT7/h6hJgvbp2gcoJFiC nkAn4OUCjKbOP9zefYu6U5aHht53po56ZK/CpOlJ80kjF96KegAPEJjHcj6Cajksl3wi ZktQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=+sERjltpQeJfY+/HM3V3Al8NQa675XJsilgAJnL6kdE=; b=lU7cU/uqKV+pCkJUSIrGH6TZELdNjUcFNbG/yNRqvkT7MdQQooEXxBe/kovD42YA2D HOwjYOrximjwJQs54GswyAqxkrTroJUbk/NvAOkFq+C8IpDB8/JGA55NCAVJfJB1yPsj NBtaWCU/CZn2tuKrIAJJFPDh+9lqRaMZdB+eQeTKzgtXkTyFo3s+kAWhQa/Zas2cQXGk CjQumsM2LUkblXDRSYqeJlGiLHVZ02RlkD1oH/WamBj6P08pnd4R1l6Su3guNpeq9gUM DZspVyu35k0pf2cNEtHXcRRzLW791xsVVjjZdtIlFTsNpebFs1Beo7vt3frdDCe/yqe7 5ZGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@vrull.eu header.s=google header.b=kDMqODuk; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id cw4-20020a170906478400b007801a579899si8962797ejc.448.2022.11.13.12.42.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 12:42:23 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@vrull.eu header.s=google header.b=kDMqODuk; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C28CD3886C46 for ; Sun, 13 Nov 2022 20:42:11 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by sourceware.org (Postfix) with ESMTPS id D67403882FC6 for ; Sun, 13 Nov 2022 20:41:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D67403882FC6 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=vrull.eu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vrull.eu Received: by mail-lj1-x229.google.com with SMTP id d20so10635752ljc.12 for ; Sun, 13 Nov 2022 12:41:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=+sERjltpQeJfY+/HM3V3Al8NQa675XJsilgAJnL6kdE=; b=kDMqODukmW3QOAFimcixaAljQ9tLBT60W5l1ewsAQmYEv/aE+B+30hHNAWOrK07uw4 ypjyZhouGsD60khrMhYhHcsP37LpKZPBQOWgHpkQSYdjY6yWAZ01IjKfjZgekbuLYZ21 auNcgj4/G+9Ad+bSSlVGoFVo7HgcX/zfIa6yW32yq448QTJZqbxtUlkXd49cM566plN3 3ROVsp7ZnH8I8OZu4abzKKDk+Tfe5+y6A7VQwVYOWRF6LqxeKmlbM0jF+LAFcTO+koRW z2iyPuCfijzGk4gUhj8F+rlBL9G347Xxt3YS7+XHzqhlWcj0f1NVGtUg7ZGEmk9KXEmN EVQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=+sERjltpQeJfY+/HM3V3Al8NQa675XJsilgAJnL6kdE=; b=IQpjxy1H9lBmaugKVW02e8uGkBK199BU3B+G+uad+eP5MrliVxv3xzLvmm1vXuZ6lX rm63dmoEkS1nbO940F+Ru7nXDaRBTwQ4aeV/Ii5k3o63DzQgPZZr2rHpNuWD4NdzlPBf 3bbZhljD7/+uqopIRvrH92/Og6k1/sbFUR0Waped6KEhRhKdy1PbPplHaLNgpp4inJH3 vnn60/BaDrp1HlnoH0MMi3V3RNirMpd8duROlMpZ6HvgVezeMzrKUnPXGmcG7P3Ng8C9 k+hEQSenI5HV5QuAHiWGp1l9qV5Ycwz1NV/KK3rL+YCT0jzt/HVuG4pKsCL81jPYqzns 0eGw== X-Gm-Message-State: ANoB5pnw9cj/L7bmFz7f8xdwowoD4KrwPubI52JJpc+FRCiWas3UylBP 664qyJz/M8Xj4VgIfWEbUyKqUTjdG1IMiPsF X-Received: by 2002:a05:651c:88a:b0:277:2:1efc with SMTP id d10-20020a05651c088a00b0027700021efcmr3583485ljq.77.1668372101853; Sun, 13 Nov 2022 12:41:41 -0800 (PST) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id p27-20020a056512139b00b0049771081b10sm1521060lfa.31.2022.11.13.12.41.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 12:41:41 -0800 (PST) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Cc: Vineet Gupta , Palmer Dabbelt , Jeff Law , Kito Cheng , Christoph Muellner , Philipp Tomsich Subject: [PATCH] RISC-V: Zihintpause: add __builtin_riscv_pause Date: Sun, 13 Nov 2022 21:41:39 +0100 Message-Id: <20221113204139.4061479-1-philipp.tomsich@vrull.eu> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_SHORT, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749414989131343848?= X-GMAIL-MSGID: =?utf-8?q?1749414989131343848?= The Zihintpause extension uses an opcode from the 'fence' opcode range to add a true hint instruction (i.e. if it is not supported on any given platform, the 'fence' that is encoded will not enforce any specific ordering on memory accesses) for entering a low-power state (e.g. in an idle thread). We expose this new instruction through a machine-dependent builtin to allow generating it without a requirement for any inline assembly. Given that the encoding of 'pause' is valid (as a 'fence' encoding) even for processors that do not (yet) support Zihintpause, we make this builtin available without any further TARGET_* constraints. gcc/ChangeLog: * config/riscv/riscv-builtins.cc (struct riscv_builtin_description): add the pause machine-dependent builtin with no result and no arguments; mark it as always present (pause is a true hint that encodes into a fence-insn, if not supported with the new pause semantics). * config/riscv/riscv-ftypes.def: Add type for void -> void. * config/riscv/riscv.md (riscv_pause): Add risc_pause and UNSPECV_PAUSE * doc/gcc/extensions-to-the-c-language-family/target-builtins/risc-v-built-in-functions.rst: Document. * optabs.cc (maybe_gen_insn): Allow nops == 0 (void -> void). gcc/testsuite/ChangeLog: * gcc.target/riscv/builtin_pause.c: New test. Signed-off-by: Philipp Tomsich --- gcc/config/riscv/riscv-builtins.cc | 6 +++--- gcc/config/riscv/riscv-ftypes.def | 1 + gcc/config/riscv/riscv.md | 8 ++++++++ .../target-builtins/risc-v-built-in-functions.rst | 4 ++++ gcc/optabs.cc | 2 ++ gcc/testsuite/gcc.target/riscv/builtin_pause.c | 10 ++++++++++ 6 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.target/riscv/builtin_pause.c diff --git a/gcc/config/riscv/riscv-builtins.cc b/gcc/config/riscv/riscv-builtins.cc index 021f6c6b69a..24ae22c99cd 100644 --- a/gcc/config/riscv/riscv-builtins.cc +++ b/gcc/config/riscv/riscv-builtins.cc @@ -88,8 +88,6 @@ struct riscv_builtin_description { }; AVAIL (hard_float, TARGET_HARD_FLOAT || TARGET_ZFINX) - - AVAIL (clean32, TARGET_ZICBOM && !TARGET_64BIT) AVAIL (clean64, TARGET_ZICBOM && TARGET_64BIT) AVAIL (flush32, TARGET_ZICBOM && !TARGET_64BIT) @@ -100,6 +98,7 @@ AVAIL (zero32, TARGET_ZICBOZ && !TARGET_64BIT) AVAIL (zero64, TARGET_ZICBOZ && TARGET_64BIT) AVAIL (prefetchi32, TARGET_ZICBOP && !TARGET_64BIT) AVAIL (prefetchi64, TARGET_ZICBOP && TARGET_64BIT) +AVAIL (always, (!0)) /* Construct a riscv_builtin_description from the given arguments. @@ -148,7 +147,8 @@ static const struct riscv_builtin_description riscv_builtins[] = { #include "riscv-cmo.def" DIRECT_BUILTIN (frflags, RISCV_USI_FTYPE, hard_float), - DIRECT_NO_TARGET_BUILTIN (fsflags, RISCV_VOID_FTYPE_USI, hard_float) + DIRECT_NO_TARGET_BUILTIN (fsflags, RISCV_VOID_FTYPE_USI, hard_float), + DIRECT_NO_TARGET_BUILTIN (pause, RISCV_VOID_FTYPE, always), }; /* Index I is the function declaration for riscv_builtins[I], or null if the diff --git a/gcc/config/riscv/riscv-ftypes.def b/gcc/config/riscv/riscv-ftypes.def index c2b45c63ea1..bf2d30782d9 100644 --- a/gcc/config/riscv/riscv-ftypes.def +++ b/gcc/config/riscv/riscv-ftypes.def @@ -27,6 +27,7 @@ along with GCC; see the file COPYING3. If not see argument type. */ DEF_RISCV_FTYPE (0, (USI)) +DEF_RISCV_FTYPE (0, (VOID)) DEF_RISCV_FTYPE (1, (VOID, USI)) DEF_RISCV_FTYPE (1, (VOID, VOID_PTR)) DEF_RISCV_FTYPE (1, (SI, SI)) diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index d1f3270a3c8..a933764e897 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -94,6 +94,9 @@ UNSPECV_INVAL UNSPECV_ZERO UNSPECV_PREI + + ;; Zihintpause unspec + UNSPECV_PAUSE ]) (define_constants @@ -1982,6 +1985,11 @@ "TARGET_ZIFENCEI" "fence.i") +(define_insn "riscv_pause" + [(unspec_volatile [(const_int 0)] UNSPECV_PAUSE)] + "" + "pause") + ;; ;; .................... ;; diff --git a/gcc/doc/gcc/extensions-to-the-c-language-family/target-builtins/risc-v-built-in-functions.rst b/gcc/doc/gcc/extensions-to-the-c-language-family/target-builtins/risc-v-built-in-functions.rst index fca4852ad74..b2f59b310fb 100644 --- a/gcc/doc/gcc/extensions-to-the-c-language-family/target-builtins/risc-v-built-in-functions.rst +++ b/gcc/doc/gcc/extensions-to-the-c-language-family/target-builtins/risc-v-built-in-functions.rst @@ -14,3 +14,7 @@ processors. .. function:: void * __builtin_thread_pointer (void) Returns the value that is currently set in the :samp:`tp` register. + +.. function:: void __builtin_riscv_pause (void) + + Generates the :samp:`pause` (hint) machine instruction diff --git a/gcc/optabs.cc b/gcc/optabs.cc index 9fc9b1fc6e9..09d3b08cb00 100644 --- a/gcc/optabs.cc +++ b/gcc/optabs.cc @@ -7961,6 +7961,8 @@ maybe_gen_insn (enum insn_code icode, unsigned int nops, switch (nops) { + case 0: + return GEN_FCN (icode) (); case 1: return GEN_FCN (icode) (ops[0].value); case 2: diff --git a/gcc/testsuite/gcc.target/riscv/builtin_pause.c b/gcc/testsuite/gcc.target/riscv/builtin_pause.c new file mode 100644 index 00000000000..9250937cabb --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/builtin_pause.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +void test_pause() +{ + __builtin_riscv_pause (); +} + +/* { dg-final { scan-assembler "pause" } } */ +