Operation cloning in graph regions with use before def values (original) (raw)

October 18, 2024, 5:45pm 1

Hi,

I’m working on building a pass to outline operations that are part of strongly connected components within an HwModuleOp into separate submodules. To achieve this, I need to clone the necessary operations into the new module. However, I’ve run into an issue where both cloneWithoutRegions and clone don’t seem to handle graph regions with “use before def” values properly.

When cloning, these problematic operands are showing up as <<UNKNOWN SSA VALUE>> in the cloned operations, leading to invalid IR (error: op using value defined outside the region).

I’ve attempted various workarounds to fix the def/use relationship, but so far, none of my approaches have been successful.

Does anyone have suggestions on how to tackle this issue? Any advice would be greatly appreciated!

Thanks in advance!

Yes, I think for these cases we do need to do some manual bookkeeping. Here is an example that I think should capture what you’d need to do: circt/lib/Dialect/SV/Transforms/SVExtractTestCode.cpp at main · llvm/circt · GitHub

You can use an IRMapping with your clone* calls, and after all the clones are made, manually go through any ops that had “use before def”, and update them to use the cloned op that was defined later in the block (or a block argument).

Normally, IRMapping will take care of this if you have strict def before use and process operations in pre-order, but in the use before def case I think we need to do this. I guess if you always have use before def, you could process operations in post order, but as soon as there is a dataflow cycle, we’ll need to do a second pass to handle it.

sderrien October 21, 2024, 3:24pm 3

Thanks a lot, mike,

I ultimately ended doing that too, and I was able to make it work.

Regards,

Steven