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

WIP: Add support for the llvm: use scheme #3924

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

redvers
Copy link
Contributor

@redvers redvers commented Nov 18, 2021

In the ponysync meeting on 2021-11-02 and on Zulip we discussed having a use scheme which would allow inclusion and linking of pre-generated llvm-ir (.ll) files by the ponyc compiler.

@jemc walked me through compiler and the process needed to make these changes and this is the result thus far.

Here is how it works:

use "llvm:addints"
use @addints[I32](a: I32, b: I32)

actor Main
  new create(env: Env) =>
    let x: I32 = @addints(I32(10), I32(32))
    env.out.print("Got: " + x.string())

The use scheme makes ponyc look for the associated file.ll, so in the above example: addints.ll

To generate the addints.ll file, we take the following C file:

int addints(int a, int b) {return a + b;};

… and compile like so:

[nix-shell:~/projects/ponyc]$ clang -emit-llvm -S addints.c
[nix-shell:~/projects/ponyc]$ head addints.ll
; ModuleID = 'addints.c'
source_filename = "addints.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: norecurse nounwind readnone sspstrong uwtable
define i32 @addints(i32, i32) local_unnamed_addr #0 {
  %3 = add i32 %1, %0
  ret i32 %3
}

Pony compilation and execution:

[nix-shell:~/projects/ponyc]$ ./build/debug/ponyc
Building builtin -> /home/red/projects/ponyc/packages/builtin
Building . -> /home/red/projects/ponyc
Generating
 Reachability
 Selector painting
 Data prototypes
 Data types
 Function prototypes
 Functions
 Descriptors
Optimising
warning: Linking two modules of different data layouts: '/home/red/projects/ponyc/addints.ll' is 'e-m:e-i64:64-f80:128-n8:16:32:64-S128' whereas 'ponyc' is 'e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128'

Writing ./ponyc.o
Linking ./ponyc

[nix-shell:~/projects/ponyc]$ ./ponyc 
Got: 42

TODO:
[] Initial Code Review
[] Write Tests
[] Ensure that .ll files from other packages resolve to the correct path
[] Write an example
[] Tutorial Updates
[] Identify if the above warning is NixOS specific and understand what it means.
[] TWIP

Please let me know if I've missed anything.

Thanks,

Red

@SeanTAllen
Copy link
Member

@redvers have you done any more work on this?

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

Successfully merging this pull request may close these issues.

2 participants