42.1 F
Columbus
Tuesday, January 26, 2021

How to dump the GOSSAFUNC graph for a method

This post was originally published on this site

The Go compiler’s SSA backend contains a facility to produce HTML debugging output of the compilation phases. This post covers how to print the SSA output for function and methods.

Let’s start with a sample program which contains a function, a value method, and a pointer method:

package main

import (
    "fmt"
)

type Numbers struct {
    vals []int
}

func (n *Numbers) Add(v int) {
    n.vals = append(n.vals, v)
}

func (n Numbers) Average() float64 {
    sum := 0.0
    for _, num := range n.vals {
        sum += float64(num)
    }
    return sum / float64(len(n.vals))
}


func main() {
    var numbers Numbers
    numbers.Add(200)
    numbers.Add(43)
    numbers.Add(-6)
    fmt.Println(numbers.Average())
}

Control of the SSA debugging output is via the GOSSAFUNC environment variable. This variable takes the name of the function to dump. This is not the functions fully qualified name. For func main above the name of the function is main not main.main.

% env GOSSAFUNC=main go build
runtime
dumped SSA to ../../go/src/runtime/ssa.html
t
dumped SSA to ./ssa.html

In this example GOSSAFUNC=main matched both main.main and a function called runtime.main.1 This is a little unfortunate, but in practice probably not a big deal as, if you’re performance tuning your code, it won’t be in a giant spaghetti blob in func main.

What is more likely is your code will be in a method, so you’ve probably landed on this post looking for the correct incantation to dump the SSA output for a method.

To print the SSA debug for the pointer method func (n *Numbers) Add, the equivalent function name is(*Numbers).Add:2

% env "GOSSAFUNC=(*Numbers).Add" go build
t
dumped SSA to ./ssa.html

To print the SSA debug for a value method func (n Numbers) Average, the equivalent function name is (*Numbers).Average even though this is a value method:

% env "GOSSAFUNC=(*Numbers).Average" go build
t
dumped SSA to ./ssa.html
Previous articleThe Next Step for Generics
Next articleBeginnings

Related Articles

Announcing the launch of our Slack V.3 integration

In 2018, we released an integration between Stack Overflow for Teams and Slack. It’s now one of the most popular ways for users to...

IntelliJ IDEA 2020.3.2 Is Available

Our second bug-fix release for IntelliJ IDEA 2020.3.2 is out! You can upgrade to v2020.3.2 from the IDE, with the Toolbox App, or by...

Answering Your Questions: AMA with the .NET Team on Reddit

Last week, we held our first Ask Me Anything (AMA) session on Reddit. We’d like to thank everyone who participated, and we hope you...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay Connected

21,422FansLike
0FollowersFollow
0SubscribersSubscribe
- Advertisement -

Latest Articles

Announcing the launch of our Slack V.3 integration

In 2018, we released an integration between Stack Overflow for Teams and Slack. It’s now one of the most popular ways for users to...

IntelliJ IDEA 2020.3.2 Is Available

Our second bug-fix release for IntelliJ IDEA 2020.3.2 is out! You can upgrade to v2020.3.2 from the IDE, with the Toolbox App, or by...

Answering Your Questions: AMA with the .NET Team on Reddit

Last week, we held our first Ask Me Anything (AMA) session on Reddit. We’d like to thank everyone who participated, and we hope you...

WebStorm 2020.3.2 Is Available

WebStorm 2020.3.2, the second bug-fix update for WebStorm 2020.3, is now available! You can update to it using the Toolbox App or right from the...

Video: Refactoring Code With PhpStorm

Christoph Rumpel shows PhpStorm’s refactoring features. He goes through 6 refactorings to improve code readability and maintainability. https://youtube.com/watch?v=LamxHRmwK2Q PhpStorm tips with Christoph Rumpel is a video...