Taichi: [refactor] [ir] IR system refactorings

Created on 14 May 2020  路  11Comments  路  Source: taichi-dev/taichi

  • [x] Remove Kernel::ir; Rename Kernel::ir_holder to Kernel::ir.
  • [x] Add a member kernel to Block
  • [x] Set kernel of the Kernel::ir
  • [x] Implement Block::get_kernel: follow Block::parent to fetch the top-level block and return kernel
  • [x] Implement Stmt::get_kernel via its containing Block
  • [x] Implement virtual Kernel *IRNode::get_kernel()
  • [ ] Try to remove dependency on get_current_program in irpasses

    • Use X.get_kernel().program instead

  • [x] Get rid of the CompileConfig arguments in passes like alg_simp(IRNode *root, const CompileConfig &config)
c++ ir welcome contribution

Most helpful comment

I assume that the added Block::kernel is of Kernel* and every get_kernel() return a Kernel*, so what I originally mean is that if setting Block::kernel to be the same type of Kernel::ir, it would change the type from Kernel* to something like IRNode*

I see. For now, we'd better not treat Kernel as an IRNode :-)

Thanks for the elaboration, then I am curious about when would this happen, maybe a rough hint?

Immediately after this line looks like a good place:
https://github.com/taichi-dev/taichi/blob/ae785ad4bc267c84396392da4a71167f082ab4c4/taichi/program/kernel.cpp#L46

All 11 comments

Thank for purposing this. I also want to make compilerconfig contains stuffs like ti_enable_opengl, ti_gdb_teigger, and rename that to be a more generic name.

Get rid of the CompileConfig arguments in passes like alg_simp(IRNode *root, const CompileConfig &config

Can't we just make it global, or make alg_simp a class member func?

Thank for purposing this. I also want to make compilerconfig contains stuffs like ti_enable_opengl, ti_gdb_teigger, and rename that to be a more generic name.

Get rid of the CompileConfig arguments in passes like alg_simp(IRNode *root, const CompileConfig &config

Can't we just make it global, or make alg_simp a class member func?

I think it's better to get rid of the global get_current_program and the state variable Program::get_current_kernel so that we can support multiple programs and parallel kernel compilation.

A straightforward one can be make current_kernel a per-thread variable like errno. Or you move all functions with a get_current_program a member functiom of Program?

Change the type of Kernel::ir to std::unique_ptr<Block>

Now I think this may be unnecessary, as all IR passes take as input IRNode * rather than Block *.

I suggest using std::unique_ptr<IRNode> while implementing more functions in IRNode like IRNode::clone, e.g., IRNode::get_kernel.

  • [ ] Set kernel of the Kernel::ir

This point is a little ambiguous to me. If it means to set the Block::kernel member to kernel::ir, it then changes Block::kernel's type from Kernel* to IRNode*(and this breaks unique_ptr's idea). Could you please elaborate more on this? @yuanming-hu

Sorry about the ambiguity. Here I actually meant setting something like dynamic_cast<Block *>(kernel->ir.get())->kernel = kernel.

from Kernel* to IRNode*

You mean from IRNode * to Block *? I'm not sure if I understand you correctly...

You mean _from IRNode * to Block *_? I'm not sure if I understand you correctly...

I assume that the added Block::kernel is of Kernel* and every get_kernel() return a Kernel*, so what I originally mean is that if setting Block::kernel to be the same type of Kernel::ir, it would change the type from Kernel* to something like IRNode*

Sorry about the ambiguity. Here I actually meant setting something like dynamic_cast(kernel->ir.get())->kernel = kernel.

Thanks for the elaboration, then I am curious about when would this happen, maybe a rough hint?

I assume that the added Block::kernel is of Kernel* and every get_kernel() return a Kernel*, so what I originally mean is that if setting Block::kernel to be the same type of Kernel::ir, it would change the type from Kernel* to something like IRNode*

I see. For now, we'd better not treat Kernel as an IRNode :-)

Thanks for the elaboration, then I am curious about when would this happen, maybe a rough hint?

Immediately after this line looks like a good place:
https://github.com/taichi-dev/taichi/blob/ae785ad4bc267c84396392da4a71167f082ab4c4/taichi/program/kernel.cpp#L46

Immediately after this line looks like a good place:
https://github.com/taichi-dev/taichi/blob/ae785ad4bc267c84396392da4a71167f082ab4c4/taichi/program/kernel.cpp#L46

Thanks! This is greatly helpful!

Actually we may utilize some helper functions in the codebase to simplify a bit:

dynamic_cast<Block *>(kernel->ir.get())->kernel = kernel

is equivalent with

kernel->ir->cast<Block>()->kernel = kernel

or (with assertion -- I prefer this one):

kernel->ir->as<Block>()->kernel = kernel
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Xayahp picture Xayahp  路  3Comments

g1n0st picture g1n0st  路  3Comments

xumingkuan picture xumingkuan  路  3Comments

yuanming-hu picture yuanming-hu  路  4Comments

jackalcooper picture jackalcooper  路  4Comments