StringLifting?

by ADMIN 15 views

Introduction

In the world of WebAssembly (WASM) and Binaryen, toolchains play a crucial role in optimizing and compiling code. One of the challenges toolchains face is handling string imports, which can be a complex task. In this article, we will explore the concept of StringLifting, a potential solution that could simplify the process of handling string imports for toolchains.

The Problem with String Imports

String imports in WASM can be a challenge for toolchains. Currently, toolchains can emit either JS string imports or stringref, depending on the build type (debug or optimized). However, this process can be time-consuming and requires toolchains to do extra work. To simplify this process, Binaryen could be modified to read JS string imports and turn them into optimizable stringref.

String Lowering and Lifting

We already have a StringLowering pass that turns stringref into JS string imports, which works well. However, the inverse problem, StringLifting, is a lot harder. Consider the following example:

(module
  (import "\'" "foo" (global $string.foo externref))
  (func $use
    (local $temp externref)
    (local.set $temp (global.get $string.foo))
  )
)

In this example, the imported JS string is turned into a string.const, but the type changes from externref to stringref, which no longer fits in the local. The existing lowering pass handles this by turning every stringref into externref, which is fine as the goal is to lower away all native WASM strings. However, this approach is not feasible in a lifting pass, as there might be legitimate and unrelated externref uses to keep.

Inferring Types and Challenges

Inferring the types to change (in locals, globals, params, results, struct and array fields, tags, etc.) would be challenging and likely brittle. This is because the type system of WASM is complex, and making changes to it would require significant effort.

Using Type Imports and Custom Annotations

One potential solution is to use type imports, which would allow toolchains to use something more specific than raw externref. However, this proposal is still in its early stages, and toolchains cannot depend on it yet. Another option is to use custom annotations, which would require significant changes to the parsing level.

Stashing Annotations on the IR or Side

Another potential solution is to stash the annotations on the IR or on the side, like we do with debug info. This would allow a lifting pass to use these annotations to infer the types. However, this approach is not very appealing, as it would require significant work on both the Binaryen side and toolchains.

The Benefit to Toolchains

While StringLifting could save toolchains time, the benefit is not as significant as one might think. Running --string-lowering-magic-imports (and reading and writing the binary) takes 14 seconds on a modest laptop, which is a relatively small amount of time.

Conclusion

StringLifting is a potential solution for toolchains to simplify the process of handling string imports. However, the challenges involved in inferring types and making changes to the type system of WASM are significant. While the benefit to toolchains is not as significant as one might think, it is still worth exploring this solution further.

Future Work

To move forward with StringLifting, we need to:

  • Investigate the feasibility of stashing annotations on the IR or side
  • Explore the use of type imports and custom annotations
  • Make changes to the type system of WASM to support StringLifting
  • Test and optimize the StringLifting pass

By working together, we can simplify the process of handling string imports for toolchains and make WASM development more efficient.

References

Related Articles

Introduction

In our previous article, we explored the concept of StringLifting, a potential solution for toolchains to simplify the process of handling string imports in WebAssembly (WASM). In this article, we will answer some of the most frequently asked questions about StringLifting.

Q: What is StringLifting?

A: StringLifting is a potential solution for toolchains to simplify the process of handling string imports in WASM. It involves modifying the Binaryen compiler to read JS string imports and turn them into optimizable stringref.

Q: Why do we need StringLifting?

A: Currently, toolchains can emit either JS string imports or stringref, depending on the build type (debug or optimized). However, this process can be time-consuming and requires toolchains to do extra work. StringLifting can simplify this process and save toolchains time.

Q: What are the challenges involved in StringLifting?

A: Inferring the types to change (in locals, globals, params, results, struct and array fields, tags, etc.) would be challenging and likely brittle. This is because the type system of WASM is complex, and making changes to it would require significant effort.

Q: Can we use type imports to simplify StringLifting?

A: Yes, we can use type imports to simplify StringLifting. However, this proposal is still in its early stages, and toolchains cannot depend on it yet.

Q: What are the benefits of StringLifting?

A: While the benefit to toolchains is not as significant as one might think, StringLifting can still save toolchains time. Running --string-lowering-magic-imports (and reading and writing the binary) takes 14 seconds on a modest laptop, which is a relatively small amount of time.

Q: How can we implement StringLifting?

A: To implement StringLifting, we need to:

  • Investigate the feasibility of stashing annotations on the IR or side
  • Explore the use of type imports and custom annotations
  • Make changes to the type system of WASM to support StringLifting
  • Test and optimize the StringLifting pass

Q: What are the next steps for StringLifting?

A: To move forward with StringLifting, we need to:

  • Investigate the feasibility of stashing annotations on the IR or side
  • Explore the use of type imports and custom annotations
  • Make changes to the type system of WASM to support StringLifting
  • Test and optimize the StringLifting pass

Q: Who can help with StringLifting?

A: Anyone interested in contributing to the development of WASM can help with StringLifting. This includes toolchain developers, compiler developers, and anyone interested in improving the performance and efficiency of WASM.

References

Related Articles

Get Involved

If you are interested in contributing to the development of WASM and helping with StringLifting, please join the WASM community and get involved. You can find more information about the WASM community and how to get involved on the WASM website.