From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id CA8BF3850414 for ; Fri, 6 Aug 2021 20:10:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CA8BF3850414 Received: by mail-ej1-x635.google.com with SMTP id u3so16960928ejz.1 for ; Fri, 06 Aug 2021 13:10:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:user-agent :mime-version:content-transfer-encoding; bh=fK02RntAWm9Qz196cqZsIrOX0V+uWizRLs1n10YiEGs=; b=rRDJt69Z/rZcUkTgBKj5jVeyBDfAA4EFyXv+VRzveYmMJSMQ83XtGnOKxjfYJm1nNf U8IcpCuJq9YQsrjygQqDCnHpkzWvWe/XQ5kbOOTajZWeEWd6/Qp/mM87NOJwBaUuCv24 HgowjttXtiomkPFEu0V1KO7WtHBrMk0UZ6Zo9++2rd+cpHj8VQaU/BtDOX4nt+PjOZnU s8+i0kcm6f/z1x8/Iq/swS3IrdnqYi+Dpm5BN89hDkXSOl6HmA+ZdSZLsK++ONSt8zwf lwGm/ALV+VU2qN+GgGAEbYZGnBgxhrvmjlK7KkyJHHPC87RPmn2+HMttTISzjxSvU9Ek Pf6Q== X-Gm-Message-State: AOAM532BlwH6XBdEtf6w//47nzqUxAVLyR6cKhPbY61ZMWR95enFJhuQ np0ppFO3c80NhueBYjGn/ojBvdgenj4= X-Google-Smtp-Source: ABdhPJzdZ6R6RlZUqzCoutEAXFtArmRmRvNrUp5mbBpCG6P4ZW7FXh9kJ82B51fibb+5HI5m2Im4mw== X-Received: by 2002:a17:906:e21a:: with SMTP id gf26mr11246050ejb.313.1628280654866; Fri, 06 Aug 2021 13:10:54 -0700 (PDT) Received: from [10.0.2.21] ([80.123.76.242]) by smtp.gmail.com with ESMTPSA id u10sm3165700ejf.121.2021.08.06.13.10.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Aug 2021 13:10:54 -0700 (PDT) Message-ID: Subject: Re: [RFC] Adding a new attribute to function param to mark it as constant From: Martin Uecker To: prathamesh.kulkarni@linaro.org Cc: "gcc@gcc.gnu.org" Date: Fri, 06 Aug 2021 22:10:53 +0200 Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.30.5-1.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=1.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.4 X-Spam-Level: * X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 Aug 2021 20:10:57 -0000 > On Wed, 4 Aug 2021 at 03:27, Segher Boessenkool > wrote: > > > > Hi! > > > > On Fri, Jul 23, 2021 at 04:23:42PM +0530, Prathamesh Kulkarni via Gcc wrote: > > > The constraint here is that, vshl_n intrinsics require that the > > > second arg (__b), > > > should be an immediate value. > > > > Something that matches the "n" constraint, not necessarily a literal, > > but stricter than just "immediate". It probably is a good idea to allow > > only "integer constant expression"s, so that the validity of the source > > code does not depend on what the optimisers do with the code. > > > > > As Richard suggested, sth like: > > > void foo(int x __attribute__((literal_constant (min_val, max_val))); > > > > The Linux kernel has a macro __is_constexpr to test if something is an > > integer constant expression, see . That is a much > > better idea imo. There could be a builtin for that of course, but an > > attribute is less powerful, less usable, less useful. > Hi Segher, > Thanks for the suggestions. I am not sure tho if we could use a macro > similar to __is_constexpr > to check if parameter is constant inside an inline function (which is > the case for intrinsics) ? > > For eg: > #define __is_constexpr(x) \ > (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) > > inline int foo(const int x) > { > _Static_assert (__is_constexpr (x)); > return x; > } > > int main() > { > return foo (1); > } > > results in: > foo.c: In function ‘foo’: > foo.c:8:3: error: static assertion failed > 8 | _Static_assert (__is_constexpr (x)); > > Initially we tried to use __Static_assert (__builtin_constant_p (arg)) > for the same purpose but that did not work > because while parsing the intrinsic function, the FE cannot determine > if the arg is indeed a constant. > I guess the static assertion or __is_constexpr would work only if the > intrinsic were defined as a macro instead of an inline function ? > Or am I misunderstanding ? You have to use it at the call site: #define __is_constexpr(x) \ (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) #define foo(x) foo(({ _Static_assert(__is_constexpr(x), "no ICE"); (x); })) inline int (foo)(const int x) { return x; } int main() { foo(1); int n = 1; foo(n); // error } --Martin