From patchwork Sun Jun 2 17:16:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1942531 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=IGkxsA4T; 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 4VskG15hTkz20Q4 for ; Mon, 3 Jun 2024 03:23:05 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 12D563938C3B for ; Sun, 2 Jun 2024 17:23:04 +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 [198.175.65.11]) by sourceware.org (Postfix) with ESMTPS id DC9A5392AC28 for ; Sun, 2 Jun 2024 17:22:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DC9A5392AC28 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 DC9A5392AC28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717348948; cv=none; b=ZD6tSs6upTQLTM5WLDMfhLSE70QKciQEHjUWV6FTeXwukp4K3lXkWACMrm1lbHh7w5rIq9d1TWj3bP/98aktY2KIbwU0qLVZKK1z/76cRDtWMNrzQWUDcEZlJEbfOKxaX816oEawNJS4WkcQs08fKgMva+lRRfssNfeDM1Wvy+o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717348948; c=relaxed/simple; bh=SY0pYlZFDJlTSx+jY3lyBVuuB8pJ5WiGmEF3aygZilA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ENHkJzhAZRI/ml7FKsJJKOZxi6GnWK8N0V3MUQ/wX9Bdz04j67MbHwUFbihOlH6r0GMz1x8yHj/6sShzoejBpedvM2XFEG3yjLfpvyUjFOltaU5vy2Xtkddf2rus26J7kQTzVgceG4tkTPWfgIgQlmPwTcGGfNFHDeS5r01IDi4= 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=1717348947; x=1748884947; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SY0pYlZFDJlTSx+jY3lyBVuuB8pJ5WiGmEF3aygZilA=; b=IGkxsA4TssJ6b5xCNBk4eXCBQBc0akwW7j2Vtt432Hdeel4L1L/OxGEO bYG9yoGYRz2BKTlf5+a8VdIskilGTrVbKvSnJvDmK3ZVhi9OnnDjxxv8r XgtQExPCwg3oXWE8LAGGChigXbxZaZLznGQR3iUVloC6Jai20KOWipQFq aPQsplfnfl/V3mFNAKg3YkjASpinMhoeck4WXdTAm8Npj1V/pDlvcH8rJ 64sNqWLZ9eOnWYu6pkncoPXV4qo+2JoGrYhAo+qLCM8iLRk+Bc6fmJ0Z9 JbWvXU8Y466J/BZEHu5o6XKU58CChsWtjVyagEAOJqzbiPl/oBp+kL4nB Q==; X-CSE-ConnectionGUID: qo7o0YxCQDux4t8n0Si0fg== X-CSE-MsgGUID: Lk5KTZ3gQ4adCGoXogXdYw== X-IronPort-AV: E=McAfee;i="6600,9927,11091"; a="24418941" X-IronPort-AV: E=Sophos;i="6.08,209,1712646000"; d="scan'208";a="24418941" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jun 2024 10:22:18 -0700 X-CSE-ConnectionGUID: yc+39oPcTdSA18S1/nI1Gg== X-CSE-MsgGUID: YVRSa1hGQ56gptB5P+IJig== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,209,1712646000"; d="scan'208";a="41088188" Received: from tassilo.jf.intel.com ([10.54.38.190]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jun 2024 10:22:17 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v7 6/9] Add tests for C/C++ musttail attributes Date: Sun, 2 Jun 2024 10:16:50 -0700 Message-ID: <20240602172205.2151579-7-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240602172205.2151579-1-ak@linux.intel.com> References: <20240602172205.2151579-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.2 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, T_SCC_BODY_TEXT_LINE, 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 Mostly adopted from the existing C musttail plugin tests. gcc/testsuite/ChangeLog: * 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. --- 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 | 20 +++++++++ gcc/testsuite/g++.dg/musttail6.C | 58 ++++++++++++++++++++++++++ gcc/testsuite/g++.dg/musttail9.C | 10 +++++ 10 files changed, 240 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/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..5e4eb1bfbacc --- /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*-*-* } } */ + +extern void 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..6da7e021f826 --- /dev/null +++ b/gcc/testsuite/g++.dg/musttail10.C @@ -0,0 +1,20 @@ +/* { dg-do compile { target { tail_call } } } */ +/* { dg-options "-std=gnu++11" } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +int f(); + +double h() { [[gnu::musttail]] return f(); } /* { dg-error "cannot tail-call" } */ + +/* The error check cannot distinguish between the (valid) int and the invalid + double conversion case because the template context is lost when tree-tailcall + finally runs. */ + +template +T g() { [[gnu::musttail]] return f(); } /* { dg-error "cannot tail-call" } */ + +int main() +{ + g(); + g(); +} diff --git a/gcc/testsuite/g++.dg/musttail6.C b/gcc/testsuite/g++.dg/musttail6.C new file mode 100644 index 000000000000..e0e478e08d58 --- /dev/null +++ b/gcc/testsuite/g++.dg/musttail6.C @@ -0,0 +1,58 @@ +/* { dg-do compile { target { tail_call } } } */ +/* { 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..8095bdf6fe62 --- /dev/null +++ b/gcc/testsuite/g++.dg/musttail9.C @@ -0,0 +1,10 @@ +/* { dg-do compile { target { tail_call } } } */ +/* { dg-options "-std=gnu++11 -Wno-deprecated" } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +extern void foo() throw(const char *); + +void f() throw() +{ + [[gnu::musttail]] return foo(); /* { dg-error "call may throw exception that does not propagate" } */ +}