Adding Partial Instruction to Wasm Backend

I’m new to LLVM and I’ve been stuck on this problem for a month now.
I was asked to add a part of wasm-gc instruction to the wasm backend(struct series and array series) and I can’t figure out a way to add them.
Can someone give me a rough path on how to add new instruction to a specific backend(Wasm) and use the new instruction on instruction selection part? Thanks for your help!

Hi there! I can help, I did almost exactly the same thing. Broadly speaking, it involves first defining your new instruction opcode in the tables they are stored in. Then modifying the TableGen files of Wasm to lower it. Most (but not all) files would be found in llvm/lib/Target/WebAssembly.

Sorry for replying this late!
I’ve read some documents and the source code. It looks like the additional instructions should be defined in a new .td file(like WasmAggregationInst.td) and thus being recognized by the system.
I am wondering where the template I<…> is defined
Moreover, My goal is to change some load/store insts to Struct series inst, any good way to do so? Write a new pass or other stuff?
Thanks very much!

Could you explain in more detail what your goal compilation from C++ to Wasm looks like, and what struct series and array series are? Especially what kind of C++ code maps to what (new) Wasm instructions.

If your new instructions are similar in function to load/store, I would suggest adding them to the load/store .td file. This will avoid you a lot of trouble in tracing the recurisve macros and templates of TableGen. Most of the rest of the work will be understanding what LLVM IR you are targeting, how those LLVM IR nodes are being compiled now and how you can change that flow. If its a relatively simple new instr with a close mapping to LLVM IR (like a store or load), modifying TableGen would be far easier. If it involves more complex logic, then you might need to write new passes.

Also if you haven’t, install LLVM’s TableGen tooling in VsCode. It enables tracing of TableGen objects, which makes things much easier.

Hello!
Struct series insts:struct.new, struct.get struct.set (also there is a inst called struct.new_default). All these insts start with “struct”
Array series insts: array.new, array.set, array.get(and more, but my goal is not that complicated)

The goal is to add these insts into the system and using them to generate right wasm-gc code.
A simple llvm-ir code is given, and the goal is to use these insts to interpret the ir code file instead of using memory.load and memory.store. In the file there would be several operation on simple structs, arrays or moreover simple classes(without using Inheritance and Polymorphism) . Correctness is the most important.
By the way, when using these insts to generate code, some new type also need to be introduced :frowning: which is really annoying.