From patchwork Fri Aug 23 22:39:08 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: 1152463 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-507652-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="ccNmwMmm"; 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="zigjRXN5"; 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 46FbtX615rz9s3Z for ; Sat, 24 Aug 2019 08:39:32 +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=wpGvbwTsV1vtvieEiexMINWyOk6uJ+962Uuy+E0HCqDPDh SdXlexCXU6UWO/1VQ2ux2reZIInWYAM4RJDoYmPJ7NdOJ52wTB2aN8XyCJoub+DD H7lynfHie6/vLAvKimEeWaklCUaIGmz5wIM32pHIpd24mhxxNLg5TIYdJyZII= 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=AfhOCqCHAONnHBDVS6hR1LRrDkg=; b=ccNmwMmm9/BD1J5os1Kd pmjmZJrkz2G/lVKMgY3SJ0I8L3r9nghU+juGUa5Iru/p0Nqi4xjA6A8k+j0ffbL2 Z+69xC9bIiIWPUNYJ7ozfx8Hqb1Hje+/+KclKaSNSplwUSJNOLHxGRaj50+81k6R hmN5sLLwUBXccWJ4g6TBgY4= Received: (qmail 38949 invoked by alias); 23 Aug 2019 22:39:24 -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 38931 invoked by uid 89); 23 Aug 2019 22:39:24 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-10.7 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=Statement, Statements, 274803, 274800 X-HELO: mail-lj1-f173.google.com Received: from mail-lj1-f173.google.com (HELO mail-lj1-f173.google.com) (209.85.208.173) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 23 Aug 2019 22:39:21 +0000 Received: by mail-lj1-f173.google.com with SMTP id f9so10184919ljc.13 for ; Fri, 23 Aug 2019 15:39:21 -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=A4zrgAlQT+1MHXrkkzNKb9LS+WGAUKooOUKI8Q3EUro=; b=zigjRXN58XeVxWx5iOLecKAuynwhPr2cCTDgyerq4UzsCkKV0Sii7nDUT+OchggPqv Qg6naV6gI68uynqB0tR5jZQcrEtV+YXuvvpjpR3DIVDookgW33HrJ4VCSQm/kLxfP88O O0zfbYHkGfhDYLtio8lvo3HJTv9nTu1cJ6ZjsDB4cDV0k4S046U3AAPPwVxLIlcOaqT2 3v9fwmbE6f5d+GEnh+p5L0z8cckANVXe9toGOP8nmfmq6MHYfJmqQhBhJR+0MXWqAVSk /b25ENyQmenzc6lO+ow9F68bMG88euF2WbnNABvq4/Q4Vgs48JqowJy+h6RWNctitxoX u0bQ== MIME-Version: 1.0 From: Ian Lance Taylor Date: Fri, 23 Aug 2019 15:39:08 -0700 Message-ID: Subject: Go patch committed: Record pointer var values to remove write barriers To: gcc-patches , gofrontend-dev This Go patch records pointer var values to remove write barriers. We record when a local pointer variable is set to a value such that indirecting through the pointer does not require a write barrier. We use that to eliminate write barriers when indirecting through that local pointer variable. We only keep this information per-block, so it's not all that applicable. This reduces the number of write barriers generated when compiling the runtime package from 553 to 524. The point of this is to eliminate a bad write barrier in the bytes function in runtime/print.go. Mark that function nowritebarrier so that the problem does not recur. Bootstrapped and ran Go testsuite on x86_64-pc-linux-gnu. Committed to mainline. Ian Index: gcc/go/gofrontend/MERGE =================================================================== --- gcc/go/gofrontend/MERGE (revision 274803) +++ gcc/go/gofrontend/MERGE (working copy) @@ -1,4 +1,4 @@ -82d27f0f140f33406cf59c0fb262f6dba3077f8e +c9ca1c6bf887c752cc75cf1ddaec8ddd1ec962d4 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 274803) +++ gcc/go/gofrontend/expressions.cc (working copy) @@ -9039,7 +9039,7 @@ Builtin_call_expression::flatten_append( // directly before the write barrier pass. Statement* assign; if (assign_lhs != NULL - || !gogo->assign_needs_write_barrier(lhs)) + || !gogo->assign_needs_write_barrier(lhs, NULL)) assign = Statement::make_assignment(lhs, elem, loc); else { Index: gcc/go/gofrontend/gogo.h =================================================================== --- gcc/go/gofrontend/gogo.h (revision 274800) +++ gcc/go/gofrontend/gogo.h (working copy) @@ -771,7 +771,14 @@ class Gogo // Return whether an assignment that sets LHS to RHS needs a write // barrier. bool - assign_needs_write_barrier(Expression* lhs); + assign_needs_write_barrier(Expression* lhs, + Unordered_set(const Named_object*)*); + + // Return whether EXPR is the address of a variable that can be set + // without a write barrier. That is, if this returns true, then an + // assignment to *EXPR does not require a write barrier. + bool + is_nonwb_pointer(Expression* expr, Unordered_set(const Named_object*)*); // Return an assignment that sets LHS to RHS using a write barrier. // This returns an if statement that checks whether write barriers Index: gcc/go/gofrontend/wb.cc =================================================================== --- gcc/go/gofrontend/wb.cc (revision 274169) +++ gcc/go/gofrontend/wb.cc (working copy) @@ -402,14 +402,21 @@ class Write_barriers : public Traverse { public: Write_barriers(Gogo* gogo) - : Traverse(traverse_functions | traverse_variables | traverse_statements), - gogo_(gogo), function_(NULL), statements_added_() + : Traverse(traverse_functions + | traverse_blocks + | traverse_variables + | traverse_statements), + gogo_(gogo), function_(NULL), statements_added_(), + nonwb_pointers_() { } int function(Named_object*); int + block(Block*); + + int variable(Named_object*); int @@ -422,6 +429,9 @@ class Write_barriers : public Traverse Function* function_; // Statements introduced. Statement_inserter::Statements statements_added_; + // Within a single block, pointer variables that point to values + // that do not need write barriers. + Unordered_set(const Named_object*) nonwb_pointers_; }; // Traverse a function. Just record it for later. @@ -439,6 +449,16 @@ Write_barriers::function(Named_object* n return TRAVERSE_SKIP_COMPONENTS; } +// Traverse a block. Clear anything we know about local pointer +// variables. + +int +Write_barriers::block(Block*) +{ + this->nonwb_pointers_.clear(); + return TRAVERSE_CONTINUE; +} + // Insert write barriers for a global variable: ensure that variable // initialization is handled correctly. This is rarely needed, since // we currently don't enable background GC until after all global @@ -533,7 +553,16 @@ Write_barriers::statement(Block* block, // local variables get declaration statements, and local // variables on the stack do not require write barriers. if (!var->is_in_heap()) - break; + { + // If this is a pointer variable, and assigning through + // the initializer does not require a write barrier, + // record that fact. + if (var->type()->points_to() != NULL + && this->gogo_->is_nonwb_pointer(init, &this->nonwb_pointers_)) + this->nonwb_pointers_.insert(no); + + break; + } // Nothing to do if the variable does not contain any pointers. if (!var->type()->has_pointer()) @@ -578,15 +607,27 @@ Write_barriers::statement(Block* block, { Assignment_statement* as = s->assignment_statement(); - if (as->omit_write_barrier()) - break; - Expression* lhs = as->lhs(); Expression* rhs = as->rhs(); + // Keep track of variables whose values do not escape. + Var_expression* lhsve = lhs->var_expression(); + if (lhsve != NULL && lhsve->type()->points_to() != NULL) + { + Named_object* no = lhsve->named_object(); + if (this->gogo_->is_nonwb_pointer(rhs, &this->nonwb_pointers_)) + this->nonwb_pointers_.insert(no); + else + this->nonwb_pointers_.erase(no); + } + + if (as->omit_write_barrier()) + break; + // We may need to emit a write barrier for the assignment. - if (!this->gogo_->assign_needs_write_barrier(lhs)) + if (!this->gogo_->assign_needs_write_barrier(lhs, + &this->nonwb_pointers_)) break; // Change the assignment to use a write barrier. @@ -667,9 +708,13 @@ Gogo::write_barrier_variable() } // Return whether an assignment that sets LHS needs a write barrier. +// NONWB_POINTERS is a set of variables that point to values that do +// not need write barriers. bool -Gogo::assign_needs_write_barrier(Expression* lhs) +Gogo::assign_needs_write_barrier( + Expression* lhs, + Unordered_set(const Named_object*)* nonwb_pointers) { // Nothing to do if the variable does not contain any pointers. if (!lhs->type()->has_pointer()) @@ -738,22 +783,10 @@ Gogo::assign_needs_write_barrier(Express // Nothing to do for an assignment to *(convert(&x)) where // x is local variable or a temporary variable. Unary_expression* ue = lhs->unary_expression(); - if (ue != NULL && ue->op() == OPERATOR_MULT) - { - Expression* expr = ue->operand(); - while (true) - { - if (expr->conversion_expression() != NULL) - expr = expr->conversion_expression()->expr(); - else if (expr->unsafe_conversion_expression() != NULL) - expr = expr->unsafe_conversion_expression()->expr(); - else - break; - } - ue = expr->unary_expression(); - if (ue != NULL && ue->op() == OPERATOR_AND) - return this->assign_needs_write_barrier(ue->operand()); - } + if (ue != NULL + && ue->op() == OPERATOR_MULT + && this->is_nonwb_pointer(ue->operand(), nonwb_pointers)) + return false; // For a struct assignment, we don't need a write barrier if all the // pointer types can not be in the heap. @@ -784,6 +817,40 @@ Gogo::assign_needs_write_barrier(Express return true; } +// Return whether EXPR is the address of a variable that can be set +// without a write barrier. That is, if this returns true, then an +// assignment to *EXPR does not require a write barrier. +// NONWB_POINTERS is a set of variables that point to values that do +// not need write barriers. + +bool +Gogo::is_nonwb_pointer(Expression* expr, + Unordered_set(const Named_object*)* nonwb_pointers) +{ + while (true) + { + if (expr->conversion_expression() != NULL) + expr = expr->conversion_expression()->expr(); + else if (expr->unsafe_conversion_expression() != NULL) + expr = expr->unsafe_conversion_expression()->expr(); + else + break; + } + + Var_expression* ve = expr->var_expression(); + if (ve != NULL + && nonwb_pointers != NULL + && nonwb_pointers->find(ve->named_object()) != nonwb_pointers->end()) + return true; + + Unary_expression* ue = expr->unary_expression(); + if (ue == NULL || ue->op() != OPERATOR_AND) + return false; + if (this->assign_needs_write_barrier(ue->operand(), nonwb_pointers)) + return false; + return true; +} + // Return a statement that sets LHS to RHS using a write barrier. // ENCLOSING is the enclosing block. Index: libgo/go/runtime/print.go =================================================================== --- libgo/go/runtime/print.go (revision 274169) +++ libgo/go/runtime/print.go (working copy) @@ -35,6 +35,7 @@ import ( // should use printhex instead of printuint (decimal). type hex uint64 +//go:nowritebarrier func bytes(s string) (ret []byte) { rp := (*slice)(unsafe.Pointer(&ret)) sp := stringStructOf(&s)