From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) by sourceware.org (Postfix) with ESMTPS id 01ADB3857704 for ; Wed, 4 Oct 2023 08:20:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 01ADB3857704 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-lf1-x132.google.com with SMTP id 2adb3069b0e04-502e7d66c1eso2134985e87.1 for ; Wed, 04 Oct 2023 01:20:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696407628; x=1697012428; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=yUxEozlhcFRVPDXLvzRGaENVlpzuXrTICtHPEwfRdVs=; b=Q6H2GBG9AItxsc4O5TpUvtIpGNSc2jBIfGAkyRFCqjXvj9C84vCUAy9mXiPoetNj89 gO9sFzCXON5GOYGObjyFO+w1ZIlhagjgwxO4h2Rs+VsREpSHmJXs+isYHNQFx9VNlwF3 peRBa4NRdeweOgHl4V44N4scK7SSMbw1YZtXly9OaCxPITUlm2Kw0UBN2YR0ETU7yeBg eq+D8b006cG75jfugSY5X5cgg0bk1OrdxAmCKQ4GlgVdU/lCENQW/ZgHIT/5DGm8m3OH 3x6bGDp17fMm/IgjVmsBK9+VBFnHalnUV/xmwAH/ZP6Z5W5pYfk6b9tnU4EB+WnusfYY BLrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696407628; x=1697012428; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yUxEozlhcFRVPDXLvzRGaENVlpzuXrTICtHPEwfRdVs=; b=BAe6TQMzmJRx03kvaTOpEewj30gs3bOuUtyngFyKJ7Sce2is3WKD2OLMfIL/rYutau JLBwgjO12kq2AE5XfidyCWXVs/8Ci93e2BmbCfUaVW+H9sBIKN88YcOjvytB/UMqodYu kcxruBe4ZFjsvlnw4pIHn6lN3AfiFfNCf2iCXTpeijsDkLiglsuL3S7ePXBIb7bPGsYY DlGKQW5XlDU+Lv9ZctewuLJKRvnQIvvTzSYUk+luXDMHn6gwdav02p5/sGr/ka7dGaEU KQrgDjreHPaN/3kja8irYQ4hVPaxoRAhKk8K5pr94BwNj0+X+BcdfSB8ZURfXC9rgOiS edAg== X-Gm-Message-State: AOJu0YzTcuebcaXmL0ceCj8GHQPkfQl7I/nV10POHF7o6Y0+1x4qsabS yGo4j6AlgZppUZJhipHLxMtxeM1hGUPSQl0uQuR5Bggj X-Google-Smtp-Source: AGHT+IFLR+5rsWQ/RLwEvmJAlould9SV/2L0BdKwW8pkyZ2KvYdYTI/iIXlsjN5TnT0aIw+HoBzyQPTL59rGwA81Qu4= X-Received: by 2002:ac2:446b:0:b0:4fe:3724:fdae with SMTP id y11-20020ac2446b000000b004fe3724fdaemr1449860lfl.66.1696407628109; Wed, 04 Oct 2023 01:20:28 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Biener Date: Wed, 4 Oct 2023 10:17:52 +0200 Message-ID: Subject: Re: Question about function splitting To: Hanke Zhang Cc: Martin Jambor , gcc@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00,BODY_8BITS,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,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: On Mon, Oct 2, 2023 at 7:15=E2=80=AFPM Hanke Zhang via Gcc wrote: > > Martin Jambor =E4=BA=8E2023=E5=B9=B410=E6=9C=883=E6=97= =A5=E5=91=A8=E4=BA=8C 00:34=E5=86=99=E9=81=93=EF=BC=9A > > > > Hello, > > > > On Mon, Oct 02 2023, Hanke Zhang via Gcc wrote: > > > Hi, I have some questions about the strategy and behavior of function > > > splitting in gcc, like the following code: > > > > > > int glob; > > > void f() { > > > if (glob) { > > > printf("short path\n"); > > > return; > > > } > > > // do lots of expensive things > > > // ... > > > } > > > > > > I hope it can be broken down like below, so that the whole function > > > can perhaps be inlined, which is more efficient. > > > > > > int glob; > > > void f() { > > > if (glob) { > > > printf("short path\n"); > > > return; > > > } > > > f_part(); > > > } > > > > > > void f_part() { > > > // do lots of expensive things > > > // ... > > > } > > > > > > > > > But on the contrary, gcc splits it like these, which not only does no= t > > > bring any benefits, but may increase the time consumption, because th= e > > > function call itself is a more resource-intensive thing. > > > > > > int glob; > > > void f() { > > > if (glob) { > > > f_part(); > > > return; > > > } > > > // do lots of expensive things > > > // ... > > > } > > > > > > void f_part() { > > > printf("short path\n"); // just do this???? > > > } > > > > > > Are there any options I can offer to gcc to change this behavior? Or > > > do I need to make some changes in ipa-split.cc? > > > > I'd suggest you file a bug to Bugzilla with a specific example that is > > mis-handled, then we can have a look and discuss what and why happens > > and what can be done about it. > > > > Thanks, > > > > Martin > > Hi, thanks for your reply. > > I'm trying to create an account right now. And I put a copy of the > example code here in case someone is interested. > > And I'm using gcc 12.3.0. When you complie the code below via 'gcc > test.c -O3 -flto -fdump-tree-fnsplit', you will find a phenomenon that > is consistent with what I described above in the gimple which is > dumped from fnsplit. I think fnsplit currently splits out _cold_ code, I suppose !opstatus is predicted to be false most of the time. It looks like your intent is to inline this very early check as if (!opstatus) { test_split_write_1 (..); } else { test_split_write_2 (..= ); } to possibly elide that test? I would guess that IPA-CP is supposed to do this but eventually refuses to create a clone for this case since it would be large. Unfortunately function splitting doesn't run during IPA transforms, but maybe IPA-CP can be teached how to avoid the expensive clone by performing what IPA split does in the case a check in the entry block which splits control flow can be optimized? Richard. > #include > #include > > int opstatus; > unsigned char *objcode =3D 0; > unsigned long position =3D 0; > char *globalfile; > > int test_split_write(char *file) { > FILE *fhd; > > if (!opstatus) { > // short path here > printf("Object code generation not active! Forgot to call " > "quantum_objcode_start?\n"); > return 1; > } > > if (!file) > file =3D globalfile; > > fhd =3D fopen(file, "w"); > > if (fhd =3D=3D 0) > return -1; > > fwrite(objcode, position, 1, fhd); > > fclose(fhd); > > int *arr =3D malloc(1000); > for (int i =3D 0; i < 1000; i++) { > arr[i] =3D rand(); > } > > return 0; > } > > // to avoid `test_split_write` inlining into main > void __attribute__((noinline)) call() { test_split_write("./txt"); } > > int main() { > opstatus =3D rand(); > objcode =3D malloc(100); > position =3D 0; > call(); > return 0; > }