Skip to content

[Feature] Add Attributes? #45

@jtenner

Description

@jtenner

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions