From 40a6e2dae5a9c75f1ddc6f97b48b9442c118653f Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Fri, 10 Mar 2023 16:28:57 -0500 Subject: [PATCH] cmd/compile: tighten for huge functions in -N mode Currently, in -N mode we skip the tighten pass. However, for very large functions, many values live across blocks can cause pathological behavior in the register allocator, which could use a huge amount of memory or cause the program to hang. For functions that large, debugging using a debugger is unlikely to be very useful (the function is probably generated anyway). So we do a little optimization to make fewer values live across blocks and make it easier for the compiler. Fixes #52180. Change-Id: I355fe31bb87ea5d0870bb52dd06405dd5d791dab Reviewed-on: https://go-review.googlesource.com/c/go/+/475339 Reviewed-by: Cuong Manh Le TryBot-Result: Gopher Robot Run-TryBot: Cherry Mui Reviewed-by: Keith Randall Reviewed-by: Keith Randall --- src/cmd/compile/internal/ssa/compile.go | 2 +- src/cmd/compile/internal/ssa/tighten.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cmd/compile/internal/ssa/compile.go b/src/cmd/compile/internal/ssa/compile.go index 423564caf9..54de1746b7 100644 --- a/src/cmd/compile/internal/ssa/compile.go +++ b/src/cmd/compile/internal/ssa/compile.go @@ -496,7 +496,7 @@ var passes = [...]pass{ {name: "checkLower", fn: checkLower, required: true}, {name: "late phielim", fn: phielim}, {name: "late copyelim", fn: copyelim}, - {name: "tighten", fn: tighten}, // move values closer to their uses + {name: "tighten", fn: tighten, required: true}, // move values closer to their uses {name: "late deadcode", fn: deadcode}, {name: "critical", fn: critical, required: true}, // remove critical edges {name: "phi tighten", fn: phiTighten}, // place rematerializable phi args near uses to reduce value lifetimes diff --git a/src/cmd/compile/internal/ssa/tighten.go b/src/cmd/compile/internal/ssa/tighten.go index edae6a1cb7..048532a4ca 100644 --- a/src/cmd/compile/internal/ssa/tighten.go +++ b/src/cmd/compile/internal/ssa/tighten.go @@ -4,12 +4,21 @@ package ssa +import "cmd/compile/internal/base" + // tighten moves Values closer to the Blocks in which they are used. // This can reduce the amount of register spilling required, // if it doesn't also create more live values. // A Value can be moved to any block that // dominates all blocks in which it is used. func tighten(f *Func) { + if base.Flag.N != 0 && len(f.Blocks) < 10000 { + // Skip the optimization in -N mode, except for huge functions. + // Too many values live across blocks can cause pathological + // behavior in the register allocator (see issue 52180). + return + } + canMove := f.Cache.allocBoolSlice(f.NumValues()) defer f.Cache.freeBoolSlice(canMove) for _, b := range f.Blocks { -- 2.48.1