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

gollvm: cross compiling #42388

Open
wyw900807 opened this issue Nov 5, 2020 · 17 comments
Open

gollvm: cross compiling #42388

wyw900807 opened this issue Nov 5, 2020 · 17 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@wyw900807
Copy link

Dear Sir,
I want to compile a program in the x86_64 environment, this program can run in the aarch64 environment, and I try " go build -o output -gccgoflags="--target=aarch64-linux"", but it cannot work.

@ianlancetaylor
Copy link
Contributor

CC @thanm

I believe that GoLLVM does not not current support aarch64-linux anyhow.

@ianlancetaylor ianlancetaylor added this to the gollvm milestone Nov 5, 2020
@wyw900807
Copy link
Author

Thank you for your reply, but the documents of gollvm writes that gollvm is supported for aarch64 linux.

Which architectures and operating systems are supported for gollvm?
Gollvm is currently supported only for x86_64 and aarch64 Linux"

@ianlancetaylor
Copy link
Contributor

OK, thanks.

What is the exact output that you get from your go build command?

@wyw900807
Copy link
Author

It outputs as follows:

/usr/local/bin/llvm-goc: unable to determine target CPU features for target aarch64-unknown-linux

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 5, 2020
@erifan
Copy link

erifan commented Nov 5, 2020

This is caused by the inconsistency between the triple_ lists in ArchCpusAttrs.h and the normalized triple_ that we get through the --target option, and the root cause is clang. In fact, if you set --target=x86_64-linux, the same error will be reported. I can fix this problem.

@wyw900807
Copy link
Author

Thank you very much!

@thanm
Copy link
Contributor

thanm commented Nov 5, 2020

Thanks @erifan. I took a look and I can reproduce as well. I assume this will involve changing and/or rerunning tools/capture-fcn-attributes.go -- are you planning of sending a patch?

@erifan
Copy link

erifan commented Nov 6, 2020

@thanm Yes, I plan to send a patch, but it will be next week, is that ok?

@thanm
Copy link
Contributor

thanm commented Nov 6, 2020

Yes, I plan to send a patch, but it will be next week, is that ok?

Yup, that's fine. Thanks.

@advancedwebdeveloper
Copy link

advancedwebdeveloper commented Nov 7, 2020

@wyw900807 , there are some common files for both Clang and llvm-goc.
They define which CPU architectures are supported and how specific CPU features/instruction sets are related.
Some other files are responsible for the calling conventions, in LLVM.

As for now - you can do the following (this duplicates what Clang itself would do):

  1. Create a C++ source code file named, say, llvm_cpu_features_investigation.cpp
  2. Populate it with the following content (you could improve a bit, to format the output):

#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/MC/SubtargetFeature.h"
#include "llvm/Support/Host.h"

using namespace llvm;
SubtargetFeatures Features1;

int main (int argc, char **argv)
{
sys::getHostCPUName();
StringMap HostFeatures;
if (sys::getHostCPUFeatures(HostFeatures))
for (auto &F : HostFeatures)
Features1.AddFeature(F.first(), F.second);

printf("test %s", Features1.getString().c_str());
printf("\nsomething else\n");
return 0;
}

  1. Compile it:

$ clang++-11 -I/usr/include/llvm-c-11 -I/usr/include/llvm-11 -L
then there is a path to your LLVM libs
-lLLVM-11 ~/workarea/llvm_cpu_features_investigation.cpp

  1. Run your resulting executable:

$ ./a.out
test
(...your CPU instruction sets are listed here...)

something else

  1. Run the following command:

$ llc-11 --version
LLVM (http://llvm.org/):
LLVM version 11.0.0

Optimized build.
Default target:
(...that would be your current target, not for cross-compiling)
Host CPU:
(...there would be your CPU model, as LLVM understands it...)

You shall see some CPU features, show during (4), with a "-" prefix. Those are non-supported features, for a specific CPU model (5). You can't expected that to be used during -mtune

Since you are cross-compiling - it would be required to get corresponding CPU features (4) and the CPU model(5) definitions, on Aarch64 (probably you would either run under qemu or would use a system-on-chip/micro-controller, for that purpose).
Please provide specific information about targeted Aarch64 hardware.

@advancedwebdeveloper
Copy link

advancedwebdeveloper commented Nov 8, 2020

Github Actions give access to a relic Clang 9 - so I used it for an example.

Here is the workflow configuration file:
https://gist.github.com/advancedwebdeveloper/2927ae765f44e387a804d01523bc572f

Screenshot from 2020-11-08 05-11-12

@wyw900807
Copy link
Author

@advancedwebdeveloper Thank you for your help, I will try it

@erifan
Copy link

erifan commented Nov 9, 2020

Hi, @thanm I thought gollvm already supports cross compiling, but it doesn't seem to be. Because we did not generate standard libraries for multiple architectures, the corresponding libraries can't be found during cross compiling. Updating ArchCpusAttrs.h is easy, but adding support for cross compiling requires much more effort. It seems that gccgo does not currently support cross compiling ?
@wyw900807 Maybe you need to compile the gollvm toolchain on Linux aarch64, and then compile your program.

@wyw900807
Copy link
Author

OK, Thank you!

@thanm
Copy link
Contributor

thanm commented Nov 9, 2020

@erifan yes, there is almost certainly a lot of work to do to set up Gollvm as a cross compiler with respect to the libraries. The first step is getting the compiler to work properly however.

Gccgo does support cross compilation, but not using the same model as vlang-- with gccgo you need a separate installation and compiler binary per target (as opposed to the clang model in which a single compiler binary supports multiple targets).

@gopherbot
Copy link

Change https://golang.org/cl/269857 mentions this issue: gollvm: update capture-fcn-attributes.go for cross compilation

@gopherbot
Copy link

Change https://golang.org/cl/270677 mentions this issue: gollvm: report that gollvm doesn't support cross compilation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants