From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x22c.google.com (mail-lj1-x22c.google.com [IPv6:2a00:1450:4864:20::22c]) by sourceware.org (Postfix) with ESMTPS id 2434C3858C52 for ; Wed, 4 Oct 2023 14:22:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2434C3858C52 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-lj1-x22c.google.com with SMTP id 38308e7fff4ca-2c27d653856so12285601fa.0 for ; Wed, 04 Oct 2023 07:22:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696429365; x=1697034165; 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=Qlt1D5sWnnBDycvTEBFNxXTpqt19fN0W/WCe810GOW8=; b=lb6jCbw1bcLwYVJHweq5cBe0IA1BC0pRtDyy/p8O/94DXz6ZujalNx7AKxwK/Jw/u8 iM0vyxGgbCjtwRvNmgYJTlEiYOovtt5HnBobhMpfMtKWFMVUhtI/ttvLE9y/Lm1lhFyU RF32yPZKitT0BzsfAfrP9Dl0Rrv4b4ynOrBUkJtkGhnxfFlRxOd4jgbPXwublBfxjR5W eJdH71cmBA+9nUraPCKgVNfdH3Yw9bjLc9VyAggLntG8SQ14v9QsAk0lZDQ8QCNpHflI FKvquAmwZ/JSofb90zEptwJJKbOdfrDbTkIOJHy+ckD46dLs87pOSQmxfsOKH0Ldw5w4 DiWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696429365; x=1697034165; 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=Qlt1D5sWnnBDycvTEBFNxXTpqt19fN0W/WCe810GOW8=; b=u9w/1lLBH+xH+XFQBqLPPQ1jgvTDFfhn3KdogWI8GyuVY5/Mel1d2vI2Xw0nJ1XPCO QIWbZSPcm/9TFoZaDdsTXFwLxIN+XxDQIdh12MKwww9olPrIAxgIQ/LYpfJhB/aLJpaw xqKgbp/nrezoqapcidF/r1qna1Bo9GTqe/SJkfySTPfy8fmmK1IjDRJMx9GbJ8r4Czdy lCEpNRUJQk4QTnGP7br6vl0Vw9nr4VXHl1Oq/Bp5Uq8ZVBiyO43DHpValpxmGlC0Iqn2 02yYKnBx9CEppk4LO0toNT3V3hsNK3kNn/9Sj29iBqy5DdsNsVdXVdILY8pm+dnsH3e5 +7Mg== X-Gm-Message-State: AOJu0Yx+q67nzEsm2LaqR1aHaeTmpXamkbdqB6i7T/SRK43S2355ZQ5W bnhLgg2/jGTLeF9lh3lJwwbH7wf9ui4XrgZ3bnk= X-Google-Smtp-Source: AGHT+IFtpSVK1xQ6WsuMchgTA23hi/xLKliEp86Ahd60uadqu6SZGFN6khwa4M4BHvUCzdxOADm83OnW1Q/Phnse2XU= X-Received: by 2002:a19:a40f:0:b0:501:bf37:262a with SMTP id q15-20020a19a40f000000b00501bf37262amr2201616lfc.32.1696429365216; Wed, 04 Oct 2023 07:22:45 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Hanke Zhang Date: Wed, 4 Oct 2023 22:22:32 +0800 Message-ID: Subject: Re: Question about function splitting To: Richard Biener Cc: Martin Jambor , gcc@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=1.7 required=5.0 tests=BAYES_00,BODY_8BITS,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Level: * X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: But when I change the code 'opstatus =3D rand()' to 'opstatus =3D rand() %2', the probability of opstatus being 0 should be 50%, but the result remains the same, i.e. still split at that point. And the specific information can be found in Bugzilla, the link is https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D111672 Richard Biener =E4=BA=8E2023=E5=B9=B410=E6=9C= =884=E6=97=A5=E5=91=A8=E4=B8=89 16:20=E5=86=99=E9=81=93=EF=BC=9A > > 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 functi= on > > > > 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 = not > > > > bring any benefits, but may increase the time consumption, because = the > > > > 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? O= r > > > > 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 i= s > > > 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; > > }