From patchwork Thu May 23 10:56:41 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 245917 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 061232C0089 for ; Thu, 23 May 2013 20:56:40 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:subject:from:to:cc:date:in-reply-to:references :content-type:mime-version; q=dns; s=default; b=Ko+LpDUmuBS0v45m hVBJha0Ah4ohvPFwh0Ce93295ZGm+Zc7s9Ofcvc3EcFWW3uktq0Bu2cZ2umCi1YB R5lDNj1x4MMjMpn5mHGmssVnr5ujG7Z8Y4/pHnDxG6h4kXwOuF3A5f4aYWHZ8u23 obef8CYcRgYdMhn+0IEdCtVgy+4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:subject:from:to:cc:date:in-reply-to:references :content-type:mime-version; s=default; bh=pWdlMmXeE6W6xic++M31e6 TNHJk=; b=oxNMlkkniuCr0C8i63UOIxpYFWtsG9zkStUdnx4W4dUPgiyeqgP/en b0i7F3gNi3fVlLBo3dlxDha3WbWyU/BUQ9u1tbaEI8izSOIgW59KuEefhCuyj/eo rA1EmiQCZ2EGXTaV7z0TqUk1NaOdBdQ7gxzHqaMTSx+TBKemT2d/U= Received: (qmail 8698 invoked by alias); 23 May 2013 10:56:34 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 8689 invoked by uid 89); 23 May 2013 10:56:34 -0000 X-Spam-SWARE-Status: No, score=-7.6 required=5.0 tests=AWL, BAYES_00, KHOP_THREADED, RCVD_IN_HOSTKARMA_W, RCVD_IN_HOSTKARMA_WL, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.1 Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Thu, 23 May 2013 10:56:33 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r4NAuWKT019570 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 23 May 2013 06:56:32 -0400 Received: from [10.3.227.92] (vpn-227-92.phx2.redhat.com [10.3.227.92]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r4NAuVGo031683; Thu, 23 May 2013 06:56:31 -0400 Message-ID: <1369306601.26167.70.camel@surprise> Subject: Re: Remove global state from gcc/tracer.c From: David Malcolm To: Jakub Jelinek Cc: GCC Patches Date: Thu, 23 May 2013 06:56:41 -0400 In-Reply-To: <20130523051453.GN1377@tucnak.redhat.com> References: <1369269945.26167.50.camel@surprise> <20130523051453.GN1377@tucnak.redhat.com> Mime-Version: 1.0 X-Virus-Found: No On Thu, 2013-05-23 at 07:14 +0200, Jakub Jelinek wrote: > On Wed, May 22, 2013 at 08:45:45PM -0400, David Malcolm wrote: > > I'm attempting to eliminate global state from the insides of gcc. > > > > gcc/tracer.c has various global variables, which are only used during > > the lifetime of the execute callback of that pass, and cleaned up at the > > end of each invocation of the pass. > > > > The attached patch introduces a class to hold the state of the pass > > ("tracer_state"), eliminating these globals. An instance of the state > > is created on the stack, and all of the various "static" functions in > > tracer.c that used the globals become member functions of the state. > > Hence the state is passed around by the implicit "this" of the > > tracer_state, avoiding the need to patch each individual use of a field > > within the state, minimizing the diff. > > But do we want to handle the global state this way? This adds overhead > to (almost?) every single function (now method) in the file (because it gets > an extra argument). While that might be fine for rarely executed functions, > if it involves also hot functions called many times, where especially on > register starved hosts it could increase register pressure, plus the > overhead of passing the this argument everywhere, this could start to be > noticeable. Sure, if you plan to do that just in one pass (but, why then?), > it might be tiny slowdown, but after you convert the hundreds of passes in > gcc that contain global state it might become significant. > > There are alternative approaches that should be considered. I thought of a possible way of doing this, attached is a proof-of-concept attempt. The idea is to use (and then not use) C++'s "static" syntax for class methods and fields. By making that optional with a big configure-time switch, it gives us a way of making state be either global vs on-stack, with minimal syntax changes. In one configuration (for building gcc as a library) there would be implicit this-> throughout, but in the other (for speedy binaries) it would all compile away to global state, as per the status quo. This assumes that doing: tracer_state state; changed = state.tail_duplicate (); is legitimate; when using global state, "state" is empty, and the call to state.tail_duplicate () becomes effectively: state::tail_duplicate () since it's static in that configuration. > E.g. global state of a pass can be moved into a per-pass structure, > and have some way how to aggregate those per pass structures together from > all the passes in the whole compiler (that can be either manual process, > say each pass providing its own *-passstate.h and one big header including > all that together), or automatic ones (say gengstate or a new tool could > create those for us from special markings in the source, say new option on > GTY or something) and have some magic macro how to access the global state > within the pass (thispass->fieldname ?). Then e.g. depending on how the > compiler would be configured and built, thispass could be just address of a > pass struct var (i.e. essentially keep the global state as is, for > performance reasons), or when trying to build compiler as a library (with > -fpic overhead we probably don't want for cc1/cc1plus - we can build all the > *.o files twice, like libtool does) thispass could expand to __thread > pointer var dereference plus a field inside of the global compiler state > structure it points to for the current pass. Thus, the library version > of the compiler would be somewhat slower (both -fpic overhead and TLS > overhead), and would need either a few of the entrypoints tweaked to adjust > the TLS pointer to the global state, or we could require users to just call > a special function to make the global state current in the current thread > before calling compiler internals. Thanks. Though I thought we were trying to move away from relying on GTY parsing? (Sorry not to be able to answer more fully yet, need to get family ready for school...) Dave diff --git a/gcc/tracer.c b/gcc/tracer.c index 975cadb..f83ac0b 100644 --- a/gcc/tracer.c +++ b/gcc/tracer.c @@ -53,20 +53,74 @@ static int count_insns (basic_block); static bool ignore_bb_p (const_basic_block); static bool better_p (const_edge, const_edge); -static edge find_best_successor (basic_block); -static edge find_best_predecessor (basic_block); -static int find_trace (basic_block, basic_block *); -/* Minimal outgoing edge probability considered for superblock formation. */ -static int probability_cutoff; -static int branch_ratio_cutoff; +/* Crude testing hack for switching between: + global state + vs + (on-stack state plus implicit this->) + This would be a config option controlling the whole build, so that + you'd use the former for a standalone build of gcc, and the latter + when building the code for use as a dynamic library. */ +#define GLOBAL_STATE 1 + +#if GLOBAL_STATE +/* When using global state, all methods and fields of state classes + become "static", so that there is effectively a single global instance + of the state, and there is no implicit "this->" being passed around. */ +# define STATEFUL static +#else +/* When using on-stack state, all methods and fields of state classes + lose the "static", so that there can be multiple instances of the state + with an implicit "this->" everywhere the state is used. */ +# define STATEFUL +#endif + +class tracer_state +{ +public: + tracer_state(); + + STATEFUL bool tail_duplicate (); + +private: + + STATEFUL edge find_best_successor (basic_block); + STATEFUL edge find_best_predecessor (basic_block); + STATEFUL int find_trace (basic_block, basic_block *); + STATEFUL void mark_bb_seen (basic_block bb); + STATEFUL bool bb_seen_p (basic_block bb); + +private: + + /* Minimal outgoing edge probability considered for superblock + formation. */ + STATEFUL int probability_cutoff; + STATEFUL int branch_ratio_cutoff; + + /* A bit BB->index is set if BB has already been seen, i.e. it is + connected to some trace already. */ + STATEFUL sbitmap bb_seen; -/* A bit BB->index is set if BB has already been seen, i.e. it is - connected to some trace already. */ -sbitmap bb_seen; +}; // tracer_state -static inline void -mark_bb_seen (basic_block bb) +#if GLOBAL_STATE +/* Global definitions of static data. */ +int tracer_state::probability_cutoff; +int tracer_state::branch_ratio_cutoff; +sbitmap tracer_state::bb_seen; +#endif + +tracer_state::tracer_state() +#if !GLOBAL_STATE + : probability_cutoff(0), + branch_ratio_cutoff(0), + bb_seen() +#endif +{ +} + +inline void +tracer_state::mark_bb_seen (basic_block bb) { unsigned int size = SBITMAP_SIZE (bb_seen); @@ -76,8 +130,8 @@ mark_bb_seen (basic_block bb) bitmap_set_bit (bb_seen, bb->index); } -static inline bool -bb_seen_p (basic_block bb) +inline bool +tracer_state::bb_seen_p (basic_block bb) { return bitmap_bit_p (bb_seen, bb->index); } @@ -138,8 +192,8 @@ better_p (const_edge e1, const_edge e2) /* Return most frequent successor of basic block BB. */ -static edge -find_best_successor (basic_block bb) +edge +tracer_state::find_best_successor (basic_block bb) { edge e; edge best = NULL; @@ -157,8 +211,8 @@ find_best_successor (basic_block bb) /* Return most frequent predecessor of basic block BB. */ -static edge -find_best_predecessor (basic_block bb) +edge +tracer_state::find_best_predecessor (basic_block bb) { edge e; edge best = NULL; @@ -178,8 +232,8 @@ find_best_predecessor (basic_block bb) /* Find the trace using bb and record it in the TRACE array. Return number of basic blocks recorded. */ -static int -find_trace (basic_block bb, basic_block *trace) +int +tracer_state::find_trace (basic_block bb, basic_block *trace) { int i = 0; edge e; @@ -220,8 +274,8 @@ find_trace (basic_block bb, basic_block *trace) /* Look for basic blocks in frequency order, construct traces and tail duplicate if profitable. */ -static bool -tail_duplicate (void) +bool +tracer_state::tail_duplicate () { fibnode_t *blocks = XCNEWVEC (fibnode_t, last_basic_block); basic_block *trace = XNEWVEC (basic_block, n_basic_blocks); @@ -376,7 +430,8 @@ tracer (void) brief_dump_cfg (dump_file, dump_flags); /* Trace formation is done on the fly inside tail_duplicate */ - changed = tail_duplicate (); + tracer_state state; + changed = state.tail_duplicate (); if (changed) { free_dominance_info (CDI_DOMINATORS);