From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id C6D9D3858402 for ; Tue, 8 Nov 2022 21:15:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C6D9D3858402 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-x62d.google.com with SMTP id k2so41901598ejr.2 for ; Tue, 08 Nov 2022 13:15:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=zVpDhi4S3zgTSBeXyq9+5Y04sB87FRfV91j9VFiyvcU=; b=SzoIdOtnX/Gh/YLRcEXru9xtVkPiF1PXX5fjsVnCCTdE76rG9a6/0DB2UARm5Zk9y+ qSqN1zdIqasu+28Ik/DW+WBBZDDbBA/6j6byOf7VWU/3VSlbi0kuPXhifrZ6BelOTeyW 4LAkD78Op9yex5r2V92W3vWBzTE9Rs9PMEXO10NYGvQI6nAsF9UeuqeagqXHuKEPSntO BIyCugRV/fVbojR8ZuxQJpwqJBkazhGyR8xmHlz2nwTr7Nq620wx8NRDB7CRfLZyvMGY dfQwzhP/zC53AOhUODtXqXdakMYab5A/O70twakn4fBH8jPnNwo7YHRlbsfzBZqjKia4 +iUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zVpDhi4S3zgTSBeXyq9+5Y04sB87FRfV91j9VFiyvcU=; b=yVMfO1FGgqqTPDHW+EhqFFuOaoFbVCulI4cS6aWtM9jpL7sRtHniyLIIt1ni9vu8dV nn+y/Cv//jT6I2GmOHwSsTerZeMFhmGK53HCcnck16Rp6Zugj+YH4cHayUmV2ACL0sB8 RWiU1LX+Zq+1fSRp5uy/IaEH5LO8/TMNKyIr/qMTMEk3S2bc2KMplairZDxrhWny7D+E PJHHEciVbJn5q8tt+2lvBpTi550OjtXJqGTArW4egJ9tj3UhJB6VAwlJ6R9Ny9XXozAw S4Zg7usHdfCj3nTi1yoCNk5Nq3hwlXYPk2PhYXVo+4Uw1noyPdirfChYrCis1clq28Oc TKdQ== X-Gm-Message-State: ACrzQf0RKpIAHuphfFJfXDuPVOwkBHS9AtIJyhOxa/pgnorf3ThhWGhE NYniFJzqbVtKGPbJFScIWOq3qQwQ/gk= X-Google-Smtp-Source: AMsMyM5y85lIA42OHrMagYyc1bFzep6RLgSyWsVR7lEC4a5x37CD8EDyvwIe1KxIumP3qC0hnZfkNg== X-Received: by 2002:a17:907:b16:b0:7ad:855b:c08c with SMTP id h22-20020a1709070b1600b007ad855bc08cmr55446688ejl.667.1667942126227; Tue, 08 Nov 2022 13:15:26 -0800 (PST) Received: from [10.0.0.1] (138.sub226.ddfr.nl. [217.27.226.138]) by smtp.gmail.com with ESMTPSA id b26-20020aa7cd1a000000b004587c2b5048sm6018971edw.52.2022.11.08.13.15.25 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 08 Nov 2022 13:15:25 -0800 (PST) Message-ID: <757d040e-3356-5d02-0c86-df71dcd188e4@gmail.com> Date: Tue, 8 Nov 2022 22:15:25 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: [PING][PATCH] i386: Allow setting target attribute from conditional expression Content-Language: en-US To: gcc-patches@gcc.gnu.org References: <20221017144437.157424-1-jwjagersma@gmail.com> From: "J.W. Jagersma" In-Reply-To: <20221017144437.157424-1-jwjagersma@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-17.9 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 List-Id: I realize this is a feature with a somewhat niche use-case, but I'd really like to have it in gcc 13, if possible. Any feedback is appreciated. On 2022-10-17 16:44, J.W. Jagersma wrote: > Recently I tried to set a function's target attribute conditionally > based on template parameters, eg.: > > template > [[gnu::target (enable_sse ? "sse" : "")]] > void func () { /* ... */ } > > I then discovered that this is currently not possible. This small patch > resolves that. > > A possible alternative solution is to do this globally, eg. in > decl_attributes. But doing so would trigger empty-string warnings from > handle_target_attribute, and I don't know how safe it is to remove that. > There likely isn't much use for this with other attributes, anyway. > > 2022-10-17 Jan W. Jagersma > > gcc/ChangeLog: > * config/i386/i386-options.cc > (ix86_valid_target_attribute_inner_p): Dereference args string > from ADDR_EXPR. > > gcc/testsuite/ChangeLog: > * g++.target/i386/target-attr-conditional.C: New test. > --- > gcc/config/i386/i386-options.cc | 9 ++++ > .../g++.target/i386/target-attr-conditional.C | 53 +++++++++++++++++++ > 2 files changed, 62 insertions(+) > create mode 100644 gcc/testsuite/g++.target/i386/target-attr-conditional.C > > diff --git a/gcc/config/i386/i386-options.cc b/gcc/config/i386/i386-options.cc > index acb2291e70f..915f3b0c1f0 100644 > --- a/gcc/config/i386/i386-options.cc > +++ b/gcc/config/i386/i386-options.cc > @@ -1123,6 +1123,15 @@ ix86_valid_target_attribute_inner_p (tree fndecl, tree args, char *p_strings[], > = fndecl == NULL ? UNKNOWN_LOCATION : DECL_SOURCE_LOCATION (fndecl); > const char *attr_name = target_clone_attr ? "target_clone" : "target"; > > + args = tree_strip_nop_conversions (args); > + > + if (TREE_CODE (args) == ADDR_EXPR) > + { > + /* Attribute string is given by a constexpr function or conditional > + expression. Dereference ADDR_EXPR, operand should be a STRING_CST. */ > + args = TREE_OPERAND (args, 0); > + } > + > /* If this is a list, recurse to get the options. */ > if (TREE_CODE (args) == TREE_LIST) > { > diff --git a/gcc/testsuite/g++.target/i386/target-attr-conditional.C b/gcc/testsuite/g++.target/i386/target-attr-conditional.C > new file mode 100644 > index 00000000000..2d418ed90bf > --- /dev/null > +++ b/gcc/testsuite/g++.target/i386/target-attr-conditional.C > @@ -0,0 +1,53 @@ > +/* { dg-do compile } */ > +/* { dg-options "-Wno-psabi -m32 -march=i386 -std=c++20" } */ > + > +#pragma GCC push_options > +#pragma GCC target("sse") > + > +typedef int __m64 __attribute__ ((__vector_size__ (8), __may_alias__)); > +typedef short __v4hi __attribute__ ((__vector_size__ (8))); > + > +extern __inline int __attribute__((__gnu_inline__, __always_inline__, __artificial__)) > +_mm_extract_pi16 (__m64 const __A, int const __N) > +{ > + return (unsigned short) __builtin_ia32_vec_ext_v4hi ((__v4hi)__A, __N); > +} > + > +#pragma GCC pop_options > + > +consteval const char* > +target_string (bool enable_sse) > +{ > + return enable_sse ? "sse" : ""; > +} > + > +// Via consteval function > +template > +[[gnu::target (target_string (enable_sse))]] > +int > +extract1 (__m64 const src) > +{ > + if constexpr (enable_sse) > + return _mm_extract_pi16 (src, 0); > + else > + return reinterpret_cast<__v4hi>(src)[1]; > +} > + > +// Via ternary operator > +template > +[[gnu::target (enable_sse ? "sse" : "")]] > +int > +extract2 (__m64 const src) > +{ > + if constexpr (enable_sse) > + return _mm_extract_pi16 (src, 2); > + else > + return reinterpret_cast<__v4hi>(src)[3]; > +} > + > +int > +test (__m64 const src) > +{ > + return extract1(src) + extract1(src) > + + extract2(src) + extract2(src); > +}