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

[Feature] Add Attributes? #45

Open
jtenner opened this issue Jan 28, 2023 · 1 comment
Open

[Feature] Add Attributes? #45

jtenner opened this issue Jan 28, 2023 · 1 comment

Comments

@jtenner
Copy link

jtenner commented Jan 28, 2023

I currently am using llvm-bindings for a toy compiler that I might turn into something bigger.

I can currently generate the following llvm using the node module on ubuntu.

; ModuleID = 'main'
source_filename = "main"
target datalayout = "e"

define void @main() {
entry:
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  store i32 41, i32* %a, align 4
  store i32 1, i32* %b, align 4
  %0 = load i32, i32* %a, align 4
  %1 = load i32, i32* %b, align 4
  %add = call i32 @my_import(i32 %0, i32 %1)
  ret void
}

declare i32 @my_import(i32, i32)

This is genuinely awesome and very accessible for node devs and I want to thank you for this effort.

However when I try to use wasm-ld to make a wasm file like this...

ts-node ./src/index.ts

llc mod.ll --march=wasm32 --filetype=obj

wasm-ld \
  --entry main \
  -o mod.wasm \
  --import-undefined \
  mod.o
(module
  (type (;0;) (func (param i32 i32) (result i32)))
  (type (;1;) (func))
  (import "env" "my_import" (func (;0;) (type 0)))
  (func (;1;) (type 1)
    (local i32)
    ...
    )
  (memory (;0;) 2)
  (global (;0;) (mut i32) (i32.const 66560))
  (export "memory" (memory 0))
  (export "main" (func 1)))

It looks like llc compiles mod.ll just fine to a wasm object, and then wasm-ld actually generates the wasm file. This is really great except I want to target wasi and lunatic imports with proper namespaces.

 (import "env" "lunatic.add" (func (;0;) (type 0)))

In clang, you can designate a symbol as imported from an arbitrary modname and name with syntax like attribute((import_module("wasi_snapshot_preview1"), import_name("path_open"))).

Which in LLVM IR appears to translate to something like:

attributes #2 = { [...] "wasm-import-module"="wasi_snapshot_preview1" "wasm-import-name"="path_open" }

and then stick #2 on the declaration.

Is there currently a way to do this programatically using the bindings or is that a true feature request? I would genuinely love to make a compiler that compiles directly to wasm.

@jtenner
Copy link
Author

jtenner commented Mar 9, 2023

Hey this issue has been open for a month. Is there anything I can do to help?

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