From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B90E6C433ED for ; Fri, 9 Apr 2021 13:48:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 97C5261108 for ; Fri, 9 Apr 2021 13:48:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232615AbhDINsy (ORCPT ); Fri, 9 Apr 2021 09:48:54 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:56435 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232395AbhDINsx (ORCPT ); Fri, 9 Apr 2021 09:48:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617976120; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZneTleoditFJnDvsvcmD0oLbFf5R+kuV2tQYXKjo4DA=; b=NlysiSbuzYVpzp2uZy+dYTZAELPngu8ursUIpHT0zE8Pg4J6XTKVL9cDG7dITpIH7hLuCC ndoFjridr4RDCKjJTnhk505Kguf2TBN46igh9gO0VtHAslBnpgmjid4ockHPH5ns4tTTTU V2P6ZglZ/37DH0waJWbDyUKQYhsTjew= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-141-roXGcz9xOZO0WnT8elFszQ-1; Fri, 09 Apr 2021 09:48:38 -0400 X-MC-Unique: roXGcz9xOZO0WnT8elFszQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BCEB18030BB; Fri, 9 Apr 2021 13:48:36 +0000 (UTC) Received: from t14s.localdomain (ovpn-112-65.phx2.redhat.com [10.3.112.65]) by smtp.corp.redhat.com (Postfix) with ESMTP id EF19F1042A46; Fri, 9 Apr 2021 13:48:33 +0000 (UTC) Message-ID: <3c062f70ffef2dcd48a661f7c8162fb8fbaf6869.camel@redhat.com> Subject: Re: static_branch/jump_label vs branch merging From: David Malcolm To: Peter Zijlstra Cc: Ard Biesheuvel , linux-toolchains@vger.kernel.org, Linux Kernel Mailing List , Josh Poimboeuf , Jason Baron , "Steven Rostedt (VMware)" Date: Fri, 09 Apr 2021 09:48:33 -0400 In-Reply-To: References: <5f78b7e2f9ae937271ef52ee9e999a91c2719da9.camel@redhat.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.38.3 (3.38.3-1.fc33) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-toolchains@vger.kernel.org On Fri, 2021-04-09 at 15:13 +0200, Peter Zijlstra wrote: > On Fri, Apr 09, 2021 at 03:01:50PM +0200, Peter Zijlstra wrote: > > On Fri, Apr 09, 2021 at 02:03:46PM +0200, Peter Zijlstra wrote: > > > On Fri, Apr 09, 2021 at 07:55:42AM -0400, David Malcolm wrote: > > > > > > Sorry if this is a dumb question, but does the function > > > > attribute: > > > >   __attribute__ ((pure)) > > > > help here?  It's meant to allow multiple calls to a predicate > > > > to be > > > > merged - though I'd be nervous of using it here, the predicate > > > > isn't > > > > 100% pure, since AIUI the whole point of what you've built is > > > > for > > > > predicates that very rarely change - but can change > > > > occasionally. > > > > > > I actually tried that, but it doesn't seem to work. Given the > > > function > > > arguments are all compile time constants it should DTRT AFAICT, > > > but > > > alas. > > > > FWIW, I tried the below patch and GCC-10.2.1 on current tip/master. > > I also just tried __attribute__((__const__)), which is stronger still > than __pure__ and that's also not working :/ > > I then also tried to replace the __buildin_types_compatible_p() magic > in > static_branch_unlikely() with _Generic(), but still no joy. > [..snip...] You tried __pure on arch_static_branch; did you try it on static_branch_unlikely? With the caveat that my knowledge of GCC's middle-end is mostly about implementing warnings, rather than optimization, I did some experimentation, with gcc trunk on x86_64 FWIW. Given: int __attribute__((pure)) foo(void); int t(void) { int a; if (foo()) a++; if (foo()) a++; if (foo()) a++; return a; } At -O1 and above this is optimized to a single call to foo, returning 0 or 3 accordingly. -fdump-tree-all shows that it's the "fre1" pass that eliminates the subsequent calls to foo, replacing them with reuses of the result of the first call. This is in gcc/tree-ssa-sccvn.c, a value-numbering pass. I think you want to somehow "teach" the compiler that: static_branch_unlikely(&sched_schedstats) is "pure-ish", that for some portion of the surrounding code that you want the result to be treated as pure - though I suspect compiler maintainers with more experience than me are thinking "but which portion? what is it safe to assume, and what will users be annoyed about if we optimize away? what if t itself is inlined somewhere?" and similar concerns. Or maybe the asm stmt itself could somehow be marked as pure??? (with similar concerns about semantics as above) Hope this is constructive Dave