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

Proposal: LLGo-CGo Implementation #840

Closed
cpunion opened this issue Oct 21, 2024 · 0 comments
Closed

Proposal: LLGo-CGo Implementation #840

cpunion opened this issue Oct 21, 2024 · 0 comments

Comments

@cpunion
Copy link
Contributor

cpunion commented Oct 21, 2024

Background

CGo is Go's mechanism for calling C code. The current LLGo compiler primarily uses go:linkname and llgo:link to call C libraries, with additional requirements like LLGoPackage and LLGoFiles. Compared to CGo, this approach lacks static checking and uses a different mechanism, potentially causing a split between Go and LLGo libraries.

CGo is mature but has additional overhead due to Goroutine compatibility.

We aim to implement CGo functionality for compatibility with the standard Go compiler. This proposal explores two potential implementation approaches.

Approach 1: Reuse cgo tool, implement runtime.cgocall

Overview

Reuse existing cgo tool, reimplement runtime.cgocall function, and implement packed args functionality.

Pros

  1. Relatively simple implementation, leveraging existing cgo toolchain.
  2. High compatibility with existing cgo usage.
  3. Manageable risks.

Cons

  1. Potentially average performance due to extra Go-C conversions, but still better than Go's CGo calls.
  2. Complexity in handling packed args, though theoretically low-risk.

Implementation Steps

  1. Implement packed args functionality.
  2. Implement runtime.cgocall function for LLGo.

Approach 2: Reuse Go compilation process, implement LLGo as a GO's compile tool

Experimental implementation: #841

Overview

Deeper modifications, reusing Go's compilation process, rewriting cgo tool, and implementing a new compile stage.

Go Compilation Process

  1. cgo generates C code
  2. Compile .c, .asm, etc. to .o files
  3. Compile .go to .o files
  4. Package each package's .o files into .a files
  5. Link .a files to create executable

Implementation Steps

  1. Modify cgo tool to generate LLGo-compatible wrapper functions
  2. Implement new compile tool, handling -pack, -asmhdr

Pros

  1. Potentially better performance, nearly equivalent to direct C function calls.
  2. Better long-term integration with Go toolchain.

Cons

  1. Some potential risks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant