Giter Site home page Giter Site logo

Comments (5)

partouf avatar partouf commented on June 12, 2024 1

GO has a special parser implementation, it takes the functions and adds custom labels https://github.com/compiler-explorer/compiler-explorer/blob/main/lib/compilers/golang.ts#L152 - starting with _pc0

I think based on that information, we can make a custom implementation for CFG

from compiler-explorer.

OfekShilon avatar OfekShilon commented on June 12, 2024

Thanks for reporting this!
Compiler-explorer uses some heuristics to parse the assembly output, among others - to partition it into functions. Unfortunately these heuristics fail for the go compiler gc, and what's worse - I myself cannot distinguish basic-blocks from functions in its output format :( So not sure this is solvable.
(Ideas are very welcome - eg is anyone aware of gc flags that somehow control formatting of assembly output?)

gccgo does produce parsable assembly output (block labels start with a dot), and cfg generation indeed succeeds: https://godbolt.org/z/M9YaY1oT9
Perhaps this is a valid workaround?

from compiler-explorer.

OfekShilon avatar OfekShilon commented on June 12, 2024

Looking at the full asm dump of the default go program https://godbolt.org/z/zx1EvK8zd :

        .file 1 "<source>"
        .loc 1 5 0
        TEXT    main.Square(SB), NOSPLIT|NOFRAME|ABIInternal, $0-8
        FUNCDATA        $0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
        FUNCDATA        $1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
        FUNCDATA        $5, main.Square.arginfo1(SB)
        FUNCDATA        $6, main.Square.argliveinfo(SB)
        PCDATA  $3, $1
        .loc 1 6 0
        IMULQ   AX, AX
        RET
        .loc 1 9 0
        TEXT    main.main(SB), NOSPLIT|NOFRAME|ABIInternal, $0-0
        FUNCDATA        $0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
        FUNCDATA        $1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
        RET

It seems there's little hope of parsing this dump programmatically - this one doesn't even contain labels for function name, let alone basic blocks.

from compiler-explorer.

stapelberg avatar stapelberg commented on June 12, 2024

Hey, sorry for the slow response and thanks for taking a look.

I was wondering how exactly godbolt runs the Go compiler to obtain the dump you showed without function names?

I tried using Go 1.22.1 by setting the GOTOOLCHAIN environment variable, meaning using the standard Go-managed build of Go 1.22.1.

The UI shows it’s using the following compiler flags:

2024-05-12-build

But when I compile the square code like that, I get:

GOTOOLCHAIN=go1.22.1 go build -o output.s -gcflags=-S default.go
# command-line-arguments                                              
main.Square STEXT nosplit size=5 args=0x8 locals=0x0 funcid=0x0 align=0x0
	0x0000 00000 (/tmp/ce/default.go:5)	TEXT	main.Square(SB), NOSPLIT|NOFRAME|ABIInternal, $0-8
	0x0000 00000 (/tmp/ce/default.go:5)	FUNCDATA	$0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0000 00000 (/tmp/ce/default.go:5)	FUNCDATA	$1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0000 00000 (/tmp/ce/default.go:5)	FUNCDATA	$5, main.Square.arginfo1(SB)
	0x0000 00000 (/tmp/ce/default.go:5)	FUNCDATA	$6, main.Square.argliveinfo(SB)
	0x0000 00000 (/tmp/ce/default.go:5)	PCDATA	$3, $1
	0x0000 00000 (/tmp/ce/default.go:6)	IMULQ	AX, AX
	0x0004 00004 (/tmp/ce/default.go:6)	RET
	0x0000 48 0f af c0 c3                                   H....
main.main STEXT nosplit size=1 args=0x0 locals=0x0 funcid=0x0 align=0x0
	0x0000 00000 (/tmp/ce/default.go:9)	TEXT	main.main(SB), NOSPLIT|NOFRAME|ABIInternal, $0-0
	0x0000 00000 (/tmp/ce/default.go:9)	FUNCDATA	$0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0000 00000 (/tmp/ce/default.go:9)	FUNCDATA	$1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0000 00000 (/tmp/ce/default.go:9)	RET
	0x0000 c3                                               .
go:cuinfo.producer.main SDWARFCUINFO dupok size=0
	0x0000 72 65 67 61 62 69                                regabi
go:cuinfo.packagename.main SDWARFCUINFO dupok size=0
	0x0000 6d 61 69 6e                                      main
main..inittask SNOPTRDATA size=8
	0x0000 00 00 00 00 00 00 00 00                          ........
gclocals·g2BeySu+wFnoycgXfElmcg== SRODATA dupok size=8
	0x0000 01 00 00 00 00 00 00 00                          ........
main.Square.arginfo1 SRODATA static dupok size=3
	0x0000 00 08 ff                                         ...
main.Square.argliveinfo SRODATA static dupok size=2
	0x0000 00 00                                            ..

So I’m wondering if the function names get lost somewhere else or if I’m reproducing it wrong.

Regarding the basic blocks, what would we need to change in Go to make that work? Just emit more labels in the assembly output?

Thanks

from compiler-explorer.

OfekShilon avatar OfekShilon commented on June 12, 2024

@partouf is right of course, I missed the custom parser.

from compiler-explorer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.