Source file src/cmd/link/internal/ld/optimized_inline_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ld
     6  
     7  import (
     8  	"debug/dwarf"
     9  	"internal/testenv"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"cmd/link/internal/dwtest"
    14  )
    15  
    16  // Keep the line numbers of the issue 82076 reproducer: the initializer call
    17  // is on line 17 and the two inlineThis calls are on lines 18 and 19.
    18  const optimizedInlineSource = `package main
    19  
    20  import "fmt"
    21  
    22  func inlineThis(a int) int {
    23  	z := a * a
    24  	return f(z + a)
    25  }
    26  
    27  func initialize(a, b *int) {
    28  	*a = 3
    29  	*b = 4
    30  }
    31  
    32  func main() {
    33  	var a, b int
    34  	initialize(&a, &b)
    35  	a = inlineThis(a)
    36  	b = inlineThis(b)
    37  	fmt.Printf("%d %d\n", a, b)
    38  }
    39  
    40  //go:noinline
    41  func f(x int) int {
    42  	return x
    43  }
    44  `
    45  
    46  // Check concrete inline instances and their declarations, even when their
    47  // code or values have been optimized away. Value locations are tested separately.
    48  func TestOptimizedInline(t *testing.T) {
    49  	testenv.MustHaveGoBuild(t)
    50  	mustHaveDWARF(t)
    51  	t.Parallel()
    52  
    53  	d, ex := gobuildAndExamine(t, optimizedInlineSource, DefaultOpt)
    54  	main := findSubprogramDIE(t, ex, "main.main")
    55  	mainIdx := ex.IdxFromOffset(main.Offset)
    56  	mainRanges, err := d.Ranges(main)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	lr, err := d.LineReader(ex.ParentCU(mainIdx))
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	if lr == nil {
    65  		t.Fatal("missing line table")
    66  	}
    67  
    68  	wantCalls := map[int64]string{17: "main.initialize", 18: "main.inlineThis", 19: "main.inlineThis"}
    69  	for _, call := range ex.Children(mainIdx) {
    70  		if call.Tag != dwarf.TagInlinedSubroutine {
    71  			continue
    72  		}
    73  		line, _ := call.Val(dwarf.AttrCallLine).(int64)
    74  		want, ok := wantCalls[line]
    75  		if !ok {
    76  			continue // e.g., fmt.Printf
    77  		}
    78  		origin := inlineOrigin(t, ex, call)
    79  		if got := origin.Val(dwarf.AttrName); got != want {
    80  			t.Errorf("call on line %d: origin name = %v, want %s", line, got, want)
    81  		}
    82  		delete(wantCalls, line)
    83  		ranges, err := d.Ranges(call)
    84  		if err != nil {
    85  			t.Fatal(err)
    86  		}
    87  		if len(ranges) == 0 {
    88  			t.Errorf("call on line %d has no ranges", line)
    89  		}
    90  		for _, r := range ranges {
    91  			if r[0] >= r[1] || !inlineRangeContained(mainRanges, r) {
    92  				t.Errorf("call on line %d: invalid range %x in main ranges %x", line, r, mainRanges)
    93  			}
    94  		}
    95  		if line == 17 {
    96  			// The otherwise empty initialize call must still provide a
    97  			// breakpoint location, not just an empty inline DIE.
    98  			statement := false
    99  			for _, r := range ranges {
   100  				var row dwarf.LineEntry
   101  				if err := lr.SeekPC(r[0], &row); err != nil {
   102  					t.Fatal(err)
   103  				}
   104  				statement = statement || row.IsStmt && row.Line == 17 &&
   105  					row.File != nil && filepath.Base(row.File.Name) == "test.go"
   106  			}
   107  			if !statement {
   108  				t.Error("initialize has no executable call-site statement")
   109  			}
   110  			continue
   111  		}
   112  
   113  		// Each inlineThis instance must describe its own parameter and local,
   114  		// even when constant folding removes all SSA names for them.
   115  		wantVars := map[string]dwarf.Tag{"a": dwarf.TagFormalParameter, "z": dwarf.TagVariable}
   116  		for _, v := range ex.Children(ex.IdxFromOffset(call.Offset)) {
   117  			if v.Tag != dwarf.TagVariable && v.Tag != dwarf.TagFormalParameter {
   118  				continue
   119  			}
   120  			vo := inlineOrigin(t, ex, v)
   121  			name, _ := vo.Val(dwarf.AttrName).(string)
   122  			if tag, ok := wantVars[name]; ok {
   123  				if v.Tag != tag || ex.Parent(ex.IdxFromOffset(vo.Offset)) != origin {
   124  					t.Errorf("call on line %d: incorrect tag or abstract parent for %s", line, name)
   125  				}
   126  				delete(wantVars, name)
   127  			}
   128  		}
   129  		if len(wantVars) != 0 {
   130  			t.Errorf("call on line %d: missing concrete declarations: %v", line, wantVars)
   131  		}
   132  	}
   133  	for line, name := range wantCalls {
   134  		t.Errorf("missing concrete %s inline on line %d", name, line)
   135  	}
   136  }
   137  
   138  func inlineOrigin(t *testing.T, ex *dwtest.Examiner, e *dwarf.Entry) *dwarf.Entry {
   139  	t.Helper()
   140  	off, ok := e.Val(dwarf.AttrAbstractOrigin).(dwarf.Offset)
   141  	if !ok {
   142  		t.Fatalf("DIE at %x has no abstract origin", e.Offset)
   143  	}
   144  	o := ex.EntryFromOffset(off)
   145  	if o == nil {
   146  		t.Fatalf("DIE at %x has unresolved abstract origin %x", e.Offset, off)
   147  	}
   148  	return o
   149  }
   150  
   151  func inlineRangeContained(ranges [][2]uint64, r [2]uint64) bool {
   152  	for _, outer := range ranges {
   153  		if outer[0] <= r[0] && r[1] <= outer[1] {
   154  			return true
   155  		}
   156  	}
   157  	return false
   158  }
   159  

View as plain text