From patchwork Tue Jun 18 23:55:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 1118385 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-503235-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=golang.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="gd5U41EQ"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=golang-org.20150623.gappssmtp.com header.i=@golang-org.20150623.gappssmtp.com header.b="00JeX26a"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45T4jb3nK8z9s9y for ; Wed, 19 Jun 2019 09:56:19 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=UcKL/2ACfVArI9lupoW6SC3TTazg8OxBKthv+jVHaQRGAh rCZY/sbIL2EICutzZLhfByh9BoVPjQhOcn+Vt5sXudQ1KSiMw2sSnOqnGIg5y+gj F6Fr69M1oMeKgRPAu0Kf5cUUDjDKyB4tGx6ugGECQPHKLHe2g4cY+2c1PBCmg= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=R0uHO0ATreQTxiQxr+OnJvy1yqg=; b=gd5U41EQOfOOVJzK0XEf +cmbcXlYmVjiWSu9VBbhZGCpuMUqZ8gUVRbeEbiDic1VSSlqZMKf97Czf4L28MWY Bj/qGgKYmgkziUb+4MeKlQ3qaCQNJbRd4AUQpBoYwGLNQ+Cpaih3+YLkXM6h5Sft eoQ2WLVZHjPqx8nk1E7fFO0= Received: (qmail 5661 invoked by alias); 18 Jun 2019 23:56:12 -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 5650 invoked by uid 89); 18 Jun 2019 23:56:11 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-10.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=cherry, HTo:D*googlegroups.com, H*Ad:D*googlegroups.com, backing X-HELO: mail-lf1-f53.google.com Received: from mail-lf1-f53.google.com (HELO mail-lf1-f53.google.com) (209.85.167.53) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 18 Jun 2019 23:56:10 +0000 Received: by mail-lf1-f53.google.com with SMTP id z15so10569394lfh.13 for ; Tue, 18 Jun 2019 16:56:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=golang-org.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=x+p4fWm4DiE4LY6vqXdiI+AVQMwAM2Yy6s1t6XZwZwY=; b=00JeX26aa0TXkY/T3tNYgNix0N5ue8Tnr/BgJ03894VNB5USPkBZCqv4h417qVnbp1 q2au54ZUKzeLBcNui7halXXf8lzzn/hbqW96IXIhtjJFr5UHAgMzwEMX4zAbJHvR+zq+ GvBKxOqXIoOaCsKqRycf4ZCVOQtVyIFSEOcEdXh0evvul7M76Llv/CIt0OEndjBk59AU z1SDZGt5eYt7gC9OGwCs/RC+H/IolU1A3nC9ESc4JULUYIyDkjGLJESWb/J3o6oBHpav RisG/Z4+SRStseEnhikpewdvm5i5EIIofV+nxRVAZcuHBJmDWg0uGYme9Cbofo/EFOum 0+IQ== MIME-Version: 1.0 From: Ian Lance Taylor Date: Tue, 18 Jun 2019 16:55:56 -0700 Message-ID: Subject: Go patch committed: Avoid copy for string([]byte) conversion in string concat To: gcc-patches , gofrontend-dev This patch to the Go frontend by Cherry Zhang avoids a copy for a string([]byte) conversion used in string concatenation. If a string([]byte) conversion is used immediately in a string concatenation, we don't need to copy the backing store of the byte slice, as the runtime function doesn't hold any reference to it. Bootstrapped and ran Go testsuite on x86_64-pc-linux-gnu. Committed to mainline. Ian 2019-06-18 Cherry Zhang * go.dg/concatstring.go: New test. Index: gcc/go/gofrontend/MERGE =================================================================== --- gcc/go/gofrontend/MERGE (revision 272133) +++ gcc/go/gofrontend/MERGE (working copy) @@ -1,4 +1,4 @@ -b1ae35965cadac235d7d218e689944286cccdd90 +62d1b667f3e85f72a186b04aad36d701160a4611 The first line of this file holds the git revision number of the last merge done from the gofrontend repository. Index: gcc/go/gofrontend/expressions.cc =================================================================== --- gcc/go/gofrontend/expressions.cc (revision 272133) +++ gcc/go/gofrontend/expressions.cc (working copy) @@ -7408,6 +7408,26 @@ String_concat_expression::do_flatten(Gog return this; Location loc = this->location(); Type* type = this->type(); + + // Mark string([]byte) operands to reuse the backing store. + // runtime.concatstrings does not keep the reference. + // + // Note: in the gc runtime, if all but one inputs are empty, + // concatstrings returns the only nonempty input without copy. + // So it is not safe to reuse the backing store if it is a + // string([]byte) conversion. So the gc compiler does the + // no-copy optimization only when there is at least one + // constant nonempty input. Currently the gccgo runtime + // doesn't do this, so we don't do the check. + for (Expression_list::iterator p = this->exprs_->begin(); + p != this->exprs_->end(); + ++p) + { + Type_conversion_expression* tce = (*p)->conversion_expression(); + if (tce != NULL) + tce->set_no_copy(true); + } + Expression* nil_arg = Expression::make_nil(loc); Expression* call; switch (this->exprs_->size()) Index: gcc/testsuite/go.dg/concatstring.go =================================================================== --- gcc/testsuite/go.dg/concatstring.go (nonexistent) +++ gcc/testsuite/go.dg/concatstring.go (working copy) @@ -0,0 +1,8 @@ +// { dg-do compile } +// { dg-options "-fgo-debug-optimization" } + +package p + +func F(b []byte, x string) string { + return "hello " + string(b) + x // { dg-error "no copy string\\(\\\[\\\]byte\\)" } +}