Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: arm64 cross-compilation errors "branch too far" #12540

Closed
matt2909 opened this issue Sep 8, 2015 · 19 comments
Closed

cmd/compile: arm64 cross-compilation errors "branch too far" #12540

matt2909 opened this issue Sep 8, 2015 · 19 comments
Milestone

Comments

@matt2909
Copy link
Contributor

matt2909 commented Sep 8, 2015

When using go1.5 to cross compile a large body of code on amd64/linux and targetting GOOS=linux GOARCH=arm64 I am getting the following errors:

00012 (my_code.go:246) BEQ 2907044
other_code.go:1: branch too far
00032 (my_code.go:246) BLS 2907044
other_code.go:1: branch too far
00224 (my_code.go:7) BNE 2907016
other_code.go:1: branch too far
00284 (my_code.go:8) BNE 2906988
other_code.go:1: branch too far
00344 (my_code.go:9) BNE 2906960
other_code.go:1: branch too far
00404 (my_code.go:10) BNE 2906932
other_code.go:1: branch too far
00464 (my_code.go:11) BNE 2906904
other_code.go:1: branch too far
00524 (my_code.go:12) BNE 2906876
other_code.go:1: branch too far
00584 (my_code.go:13) BNE 2906848
other_code.go:1: branch too far
00644 (my_code.go:14) BNE 2906820
other_code.go:1: too many errors

The code in question (name anonymised in this dump), is basically building a big slice of function pointers. Each function pointer points to a closure. The closure is generated within the same package, but there are a lot of closures (>5000).

This looks odd, the asm7.go code is being called with pc relative offsets greater than 19 bits. Happy to dig a bit further if someone can point me in the correct direction.

@bradfitz bradfitz changed the title Linux/arm64: cross-compilation errors "branch too far" cmd/compile: arm64 cross-compilation errors "branch too far" Sep 8, 2015
@bradfitz
Copy link
Contributor

bradfitz commented Sep 8, 2015

/cc @minux

@ianlancetaylor ianlancetaylor added this to the Go1.6 milestone Sep 8, 2015
@mwhudson
Copy link
Contributor

mwhudson commented Sep 8, 2015

Code that could be used to reproduce this would be helpful.

@matt2909
Copy link
Contributor Author

matt2909 commented Sep 8, 2015

I'll try and generate a test case for you tomorrow, I can't upload the code under test yet sadly

@matt2909
Copy link
Contributor Author

Sorry for the delay... gist link to the generator code (it builds a tester.go with 20000 function stubs and a map pointing to them):

https://gist.github.com/matt2909/7c1c5880621f9c6d8775

To reproduce the compiler error I see on head:

$> go run generator.go > tester.go

/* ARM64 compiler breaks as follows */
$> GOOS=linux GOARCH=arm64 go build tester.go
//# command-line-arguments
./tester.go:1: branch too far
00012 (/xxxxx/tester.go:120015) BLS 1360280
./tester.go:1: branch too far
00172 (/xxxxx/tester.go:20007) BNE 1360252

/* AMD64 compiler behaves as expected and generates a tester executable */
$> GOOS=linux GOARCH=amd64 go build tester.go

@matt2909
Copy link
Contributor Author

Let me know if that is sufficient for your purposes @mwhudson ?

@mwhudson
Copy link
Contributor

I can reproduce the problem, so that's good. Unfortunately I'm a little confused about what's going on and don't really have time to investigate now.

I guess some code somewhere needs to switch to a model of loading the offset into a register and branching to that rather than attempting to encode the offset as an immediate.

@matt2909
Copy link
Contributor Author

is it worth me taking a look? Is there anyway to understand the path though the compiler to the error? I couldn't find a good way of getting a stack trace (or any other trace) at the point of the error message.

@davecheney
Copy link
Contributor

It will most likely be in cmd/internal/obj/arm64/asm7.go

On Tue, Oct 13, 2015 at 7:28 AM, Matt Horsnell notifications@github.com
wrote:

is it worth me taking a look? Is there anyway to understand the path
though the compiler to the error? I couldn't find a good way of getting a
stack trace (or any other trace) at the point of the error message.


Reply to this email directly or view it on GitHub
#12540 (comment).

@matt2909
Copy link
Contributor Author

possibly the bit commented out about "very large branches" ?

@davecheney
Copy link
Contributor

I'm sorry, I haven't looked at that code in months, but that does sound
like a smoking gun.

On Tue, 13 Oct 2015 07:57 Matt Horsnell notifications@github.com wrote:

possibly the bit commented out about "very large branches" ?


Reply to this email directly or view it on GitHub
#12540 (comment).

@matt2909
Copy link
Contributor Author

anyway to get more context printed out as the compiler generates makes decisions on branching etc?

@mwhudson
Copy link
Contributor

Nothing systematic I know about, I suspect this is the land of debugging by adding print statements.

@matt2909
Copy link
Contributor Author

Do you know of any documents that explain some of the compiler phases, it's APIs or structs? Flying by fmt statements is fine if I have a map of the terrain

@minux
Copy link
Member

minux commented Oct 13, 2015 via email

@matt2909
Copy link
Contributor Author

OK, fmt statements it is then ;)

@matt2909
Copy link
Contributor Author

having spent a bit of time looking at this, I'm really not making progress and can't invest more effort without some guidance or compiler documentation...

@rsc
Copy link
Contributor

rsc commented Nov 4, 2015

“Branch too far” means that the code has generated a conditional branch to a target too far away in the code. The usual fix is to generate the opposite branch, jumping over an unconditional branch to the actual target. The code generator could and probably should do this. The x86 already does something similar to decide whether to use the long or short forms of conditional branches.

@matt2909
Copy link
Contributor Author

matt2909 commented Nov 4, 2015

@rsc if you could point me in the right direction, i.e. which file and which part of that file, or step in the process of asm etc I need to go hunt for then I'd happily take another look.

/* My suspicion having reduced my actual code base so that it avoids this issue is that there are other bugs lurking in the arm64 branch, for serious sized applications. */

@minux minux self-assigned this Nov 12, 2015
@gopherbot
Copy link

CL https://golang.org/cl/16902 mentions this issue.

@minux minux closed this as completed in 2a031e6 Nov 13, 2015
@golang golang locked and limited conversation to collaborators Nov 16, 2016
@rsc rsc unassigned minux Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants