From patchwork Thu Jul 18 04:30:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1961889 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=UOYFdcXq; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WPg803t3jz1yY1 for ; Thu, 18 Jul 2024 14:39:44 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CC7B4385E839 for ; Thu, 18 Jul 2024 04:39:42 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) by sourceware.org (Postfix) with ESMTPS id 71C6A385DC1B for ; Thu, 18 Jul 2024 04:38:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 71C6A385DC1B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.intel.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=linux.intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 71C6A385DC1B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.19 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721277485; cv=none; b=WKSXQVc29IYdT3rNHaUw+yn969B73BstWoj3q+AzMArHYSeLudgTPgzYxcLEeHtCRAoyiYu+uSzU2Uv/EXNeFmcp20sdilVyzyjm2on28ZO5BtmRCEwgAxeYudgwK6Pf3Bz3DcV0EqtYDIyKaTL3dxiiGjM1QzzYR2Ny3M8eoZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721277485; c=relaxed/simple; bh=HKM/FPCDbDnJxLqgmhj5KP6EMsptPUlTk/Zj8GuFDwc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=U8Ru20GGmAxrbELFIMMDPnaQWCIje14GM5V8K+bXx/ddwn/WgjLgyNpW7fL5JEtyA/lUijulgyvnuS9GJjGI2BjrLP9vOKHFRGDR8ujvWnXX8L1uXgqfzUEZ79oO7hRt/CPQiPZaDyQ+JKJU4CMrTQCenVgICeH1e2KMYyzLMSw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1721277480; x=1752813480; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HKM/FPCDbDnJxLqgmhj5KP6EMsptPUlTk/Zj8GuFDwc=; b=UOYFdcXqjf4OSe3bT30XfExommPFoyqN0G2iwXK8+GpXfAgF2g9hPIj5 Y6BruMjLlv1lamXYMBqWnKe1yQx8IcbxRr/pI+4R4ArnlwiKQ5jW6ZGfg 47d/jL1oYWSI6l99LAzXeR4WRSZuN8ZcHES8HVj2yEn1YTNDFV7+FArt2 1r0nwjWH/bfNLgYPhkDMv3tjejHSUD+i0xHIf0gdaVc+3tSi36hKleGAC 8yrmNXLL0kevvkDA4Pj3nmOiT4Q+tqyp0VBygwdu9/Z9dvrFV9/9vaLid wKG3Q3Se4UqYzxPTvjGGVla/bUYVCORzyPPkeSB45r9Leh8gm54OV/jXQ Q==; X-CSE-ConnectionGUID: Img9kq1YQ6etFxMDq0RE1w== X-CSE-MsgGUID: KLb2g/hTRxGNj0d07ufUfQ== X-IronPort-AV: E=McAfee;i="6700,10204,11136"; a="18515143" X-IronPort-AV: E=Sophos;i="6.09,216,1716274800"; d="scan'208";a="18515143" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jul 2024 21:37:57 -0700 X-CSE-ConnectionGUID: NZ4ipA1BRLWufYZE0ilhwA== X-CSE-MsgGUID: ddX0OJR/QxK6l4Fe+DuDyA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,216,1716274800"; d="scan'208";a="54810600" Received: from tassilo.jf.intel.com ([10.54.38.190]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jul 2024 21:37:57 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: polacek@redhat.com, josmyers@redhat.com, jason@redhat.com, Andi Kleen Subject: [PATCH v10 3/3] Add tests for C/C++ musttail attributes Date: Wed, 17 Jul 2024 21:30:01 -0700 Message-ID: <20240718043750.2480283-4-ak@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240718043750.2480283-1-ak@linux.intel.com> References: <20240718043750.2480283-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP, UNWANTED_LANGUAGE_BODY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Some adopted from the existing C musttail plugin tests. Also extends the ability to query the sibcall capabilities of the target. gcc/testsuite/ChangeLog: * testsuite/lib/target-supports.exp (check_effective_target_struct_tail_call): New function. * c-c++-common/musttail1.c: New test. * c-c++-common/musttail2.c: New test. * c-c++-common/musttail3.c: New test. * c-c++-common/musttail4.c: New test. * c-c++-common/musttail7.c: New test. * c-c++-common/musttail8.c: New test. * g++.dg/musttail6.C: New test. * g++.dg/musttail9.C: New test. * g++.dg/musttail10.C: New test. * g++.dg/musttail11.C: New test. --- gcc/testsuite/c-c++-common/musttail1.c | 14 ++++++ gcc/testsuite/c-c++-common/musttail2.c | 33 ++++++++++++++ gcc/testsuite/c-c++-common/musttail3.c | 29 +++++++++++++ gcc/testsuite/c-c++-common/musttail4.c | 17 ++++++++ gcc/testsuite/c-c++-common/musttail5.c | 28 ++++++++++++ gcc/testsuite/c-c++-common/musttail7.c | 14 ++++++ gcc/testsuite/c-c++-common/musttail8.c | 17 ++++++++ gcc/testsuite/g++.dg/musttail10.C | 40 +++++++++++++++++ gcc/testsuite/g++.dg/musttail11.C | 33 ++++++++++++++ gcc/testsuite/g++.dg/musttail6.C | 60 ++++++++++++++++++++++++++ gcc/testsuite/g++.dg/musttail9.C | 10 +++++ gcc/testsuite/lib/target-supports.exp | 9 ++++ 12 files changed, 304 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/musttail1.c create mode 100644 gcc/testsuite/c-c++-common/musttail2.c create mode 100644 gcc/testsuite/c-c++-common/musttail3.c create mode 100644 gcc/testsuite/c-c++-common/musttail4.c create mode 100644 gcc/testsuite/c-c++-common/musttail5.c create mode 100644 gcc/testsuite/c-c++-common/musttail7.c create mode 100644 gcc/testsuite/c-c++-common/musttail8.c create mode 100644 gcc/testsuite/g++.dg/musttail10.C create mode 100644 gcc/testsuite/g++.dg/musttail11.C create mode 100644 gcc/testsuite/g++.dg/musttail6.C create mode 100644 gcc/testsuite/g++.dg/musttail9.C diff --git a/gcc/testsuite/c-c++-common/musttail1.c b/gcc/testsuite/c-c++-common/musttail1.c new file mode 100644 index 000000000000..74efcc2a0bc6 --- /dev/null +++ b/gcc/testsuite/c-c++-common/musttail1.c @@ -0,0 +1,14 @@ +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +int __attribute__((noinline,noclone,noipa)) +callee (int i) +{ + return i * i; +} + +int __attribute__((noinline,noclone,noipa)) +caller (int i) +{ + [[gnu::musttail]] return callee (i + 1); +} diff --git a/gcc/testsuite/c-c++-common/musttail2.c b/gcc/testsuite/c-c++-common/musttail2.c new file mode 100644 index 000000000000..86f2c3d77404 --- /dev/null +++ b/gcc/testsuite/c-c++-common/musttail2.c @@ -0,0 +1,33 @@ +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */ + +struct box { char field[256]; int i; }; + +int __attribute__((noinline,noclone,noipa)) +test_2_callee (int i, struct box b) +{ + if (b.field[0]) + return 5; + return i * i; +} + +int __attribute__((noinline,noclone,noipa)) +test_2_caller (int i) +{ + struct box b; + [[gnu::musttail]] return test_2_callee (i + 1, b); /* { dg-error "cannot tail-call: " } */ +} + +extern void setjmp (void); +void +test_3 (void) +{ + [[gnu::musttail]] return setjmp (); /* { dg-error "cannot tail-call: " } */ +} + +extern float f7(void); + +int +test_6 (void) +{ + [[gnu::musttail]] return f7(); /* { dg-error "cannot tail-call: " } */ +} diff --git a/gcc/testsuite/c-c++-common/musttail3.c b/gcc/testsuite/c-c++-common/musttail3.c new file mode 100644 index 000000000000..ea9589c59ef2 --- /dev/null +++ b/gcc/testsuite/c-c++-common/musttail3.c @@ -0,0 +1,29 @@ +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */ + +extern int foo2 (int x, ...); + +struct str +{ + int a, b; +}; + +struct str +cstruct (int x) +{ + if (x < 10) + [[clang::musttail]] return cstruct (x + 1); + return ((struct str){ x, 0 }); +} + +int +foo (int x) +{ + if (x < 10) + [[clang::musttail]] return foo2 (x, 29); + if (x < 100) + { + int k = foo (x + 1); + [[clang::musttail]] return k; /* { dg-error "cannot tail-call: " } */ + } + return x; +} diff --git a/gcc/testsuite/c-c++-common/musttail4.c b/gcc/testsuite/c-c++-common/musttail4.c new file mode 100644 index 000000000000..23f4b5e1cd68 --- /dev/null +++ b/gcc/testsuite/c-c++-common/musttail4.c @@ -0,0 +1,17 @@ +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */ + +struct box { char field[64]; int i; }; + +struct box __attribute__((noinline,noclone,noipa)) +returns_struct (int i) +{ + struct box b; + b.i = i * i; + return b; +} + +int __attribute__((noinline,noclone)) +test_1 (int i) +{ + [[gnu::musttail]] return returns_struct (i * 5).i; /* { dg-error "cannot tail-call: " } */ +} diff --git a/gcc/testsuite/c-c++-common/musttail5.c b/gcc/testsuite/c-c++-common/musttail5.c new file mode 100644 index 000000000000..234da0d3f2a9 --- /dev/null +++ b/gcc/testsuite/c-c++-common/musttail5.c @@ -0,0 +1,28 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23" { target c } } */ +/* { dg-options "-std=gnu++11" { target c++ } } */ + +[[musttail]] int j; /* { dg-warning "attribute" } */ +__attribute__((musttail)) int k; /* { dg-warning "attribute" } */ + +void foo(void) +{ + [[gnu::musttail]] j++; /* { dg-warning "attribute" } */ + [[gnu::musttail]] if (k > 0) /* { dg-warning "attribute" } */ + [[gnu::musttail]] k++; /* { dg-warning "attribute" } */ +} + +int foo2(int p) +{ + [[gnu::musttail(1)]] return foo2(p + 1); /* { dg-error "\(before numeric constant|attribute\)" } */ +} + +int i; + +int foo3(void) +{ + [[musttail]] i++; /* { dg-warning "attribute" } */ + [[musttail]] if (i > 10) /* { dg-warning "attribute" } */ + [[musttail]] return foo2(i); /* { dg-warning "attribute" } */ + return 0; +} diff --git a/gcc/testsuite/c-c++-common/musttail7.c b/gcc/testsuite/c-c++-common/musttail7.c new file mode 100644 index 000000000000..c753a3fe9b2a --- /dev/null +++ b/gcc/testsuite/c-c++-common/musttail7.c @@ -0,0 +1,14 @@ +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +void __attribute__((noipa)) f() {} + +void f2() +{ + [[gnu::musttail]] return f2(); +} + +void f3() +{ + [[gnu::musttail]] return f(); +} diff --git a/gcc/testsuite/c-c++-common/musttail8.c b/gcc/testsuite/c-c++-common/musttail8.c new file mode 100644 index 000000000000..9fa10e0b54c4 --- /dev/null +++ b/gcc/testsuite/c-c++-common/musttail8.c @@ -0,0 +1,17 @@ +/* { dg-do compile { target { tail_call && { c || c++11 } } } } */ + +float f1(void); + +int f2(void) +{ + [[gnu::musttail]] return f1 (); /* { dg-error "changed after call" } */ +} + + +int f3(int *); + +int f4(void) +{ + int x; + [[gnu::musttail]] return f3(&x); /* { dg-error "\(refers to locals|other reasons\)" } */ +} diff --git a/gcc/testsuite/g++.dg/musttail10.C b/gcc/testsuite/g++.dg/musttail10.C new file mode 100644 index 000000000000..ff7fcc7d8755 --- /dev/null +++ b/gcc/testsuite/g++.dg/musttail10.C @@ -0,0 +1,40 @@ +/* { dg-do compile { target { tail_call } } } */ +/* { dg-options "-std=gnu++11" } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +template T f(); + +double g() { [[gnu::musttail]] return f(); } /* { dg-error "cannot tail-call" } */ + +template +__attribute__((noinline, noclone, noipa)) +T g1() { [[gnu::musttail]] return f(); } /* { dg-error "target is not able" "" { target powerpc*-*-* } } */ + +template +__attribute__((noinline, noclone, noipa)) +T g2() { [[gnu::musttail]] return f(); } + +template +__attribute__((noinline, noclone, noipa)) +/* Would work with -O1. */ +T g3() { [[gnu::musttail]] return f(); } /* { dg-error "cannot tail-call" } */ + +template +__attribute__((noinline, noclone, noipa)) +T g4() { [[gnu::musttail]] return f(); } /* { dg-error "cannot tail-call" } */ + +class C +{ + double x; +public: + C(double x) : x(x) {} + ~C() { asm("":::"memory"); } +}; + +int main() +{ + g1(); + g2(); + g3(); + g4(); +} diff --git a/gcc/testsuite/g++.dg/musttail11.C b/gcc/testsuite/g++.dg/musttail11.C new file mode 100644 index 000000000000..1779e3287a93 --- /dev/null +++ b/gcc/testsuite/g++.dg/musttail11.C @@ -0,0 +1,33 @@ +/* { dg-do compile { target { tail_call } } } */ +/* { dg-options "-std=gnu++11" } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +template T f(); + +class C +{ + double x; +public: + C(double x) : x(x) {} + ~C() { asm("":::"memory"); } + operator int() { return x; } +}; + +template +__attribute__((noinline, noclone, noipa)) +T g5() { [[gnu::musttail]] return f (); } /* { dg-error "cannot tail-call" } */ + +C h(); + +__attribute__((noinline, noclone, noipa)) +int g6() { [[gnu::musttail]] return h (); } /* { dg-error "cannot tail-call" } */ + +__attribute__((noinline, noclone, noipa)) +C g7() { [[gnu::musttail]] return h (); } /* { dg-error "cannot tail-call" } */ + +int main() +{ + g5 (); + g6 (); + g7 (); +} diff --git a/gcc/testsuite/g++.dg/musttail6.C b/gcc/testsuite/g++.dg/musttail6.C new file mode 100644 index 000000000000..434a54a441c8 --- /dev/null +++ b/gcc/testsuite/g++.dg/musttail6.C @@ -0,0 +1,60 @@ +/* { dg-do compile { target { struct_tail_call } } } */ +/* A lot of architectures will not run this due to PR115606 and PR115607 */ +/* { dg-skip-if "powerpc does not support sibcall to templates" { powerpc*-*-* } } */ +/* { dg-options "-std=gnu++11" } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +class Foo { +public: + int a, b; + Foo(int a, int b) : a(a), b(b) {} +}; + +Foo __attribute__((noinline,noclone,noipa)) +callee (int i) +{ + return Foo(i, i+1); +} + +Foo __attribute__((noinline,noclone,noipa)) +caller (int i) +{ + [[gnu::musttail]] return callee (i + 1); +} + +template +T __attribute__((noinline,noclone,noipa)) foo (T i) +{ + return i + 1; +} + +int +caller2 (int k) +{ + [[gnu::musttail]] return foo(1); +} + +template +T caller3 (T v) +{ + [[gnu::musttail]] return foo(v); +} + +int call3(int i) +{ + [[gnu::musttail]] return caller3(i + 1); +} + +struct Bar { + int a; + Bar(int a) : a(a) {} + Bar operator+(Bar o) { return Bar(a + o.a); } +}; + +#if __OPTIMIZE__ >= 1 +Bar +caller4 (Bar k) +{ + [[gnu::musttail]] return caller3(Bar(99)); +} +#endif diff --git a/gcc/testsuite/g++.dg/musttail9.C b/gcc/testsuite/g++.dg/musttail9.C new file mode 100644 index 000000000000..fb0262e751be --- /dev/null +++ b/gcc/testsuite/g++.dg/musttail9.C @@ -0,0 +1,10 @@ +/* { dg-do compile { target { tail_call } } } */ +/* { dg-options "-std=gnu++11" } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +extern void foo(); + +void f() noexcept +{ + [[gnu::musttail]] return foo(); /* { dg-error "call may throw exception that does not propagate" } */ +} diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp index daa0c75d2bc3..37188880f6c2 100644 --- a/gcc/testsuite/lib/target-supports.exp +++ b/gcc/testsuite/lib/target-supports.exp @@ -12744,6 +12744,15 @@ proc check_effective_target_tail_call { } { } {-O2 -fdump-rtl-expand-all}] ;# The "SIBCALL" note requires a detailed dump. } +# Return 1 if the target can perform tail-call optimizations for structures +proc check_effective_target_struct_tail_call { } { + return [check_no_messages_and_pattern tail_call ",SIBCALL" rtl-expand { + struct foo { int a, b }; + __attribute__((__noipa__)) struct foo foo (void) { return {}; } + __attribute__((__noipa__)) struct foo bar (void) { return foo(); } + } {-O2 -fdump-rtl-expand-all}] ;# The "SIBCALL" note requires a detailed dump. +} + # Return 1 if the target's calling sequence or its ABI # create implicit stack probes at or prior to function entry. proc check_effective_target_caller_implicit_probes { } {