clangir icon indicating copy to clipboard operation
clangir copied to clipboard

add names to generated IR

Open ChuanqiXu9 opened this issue 1 year ago • 7 comments

Now when we generate IR from CIR, the generated codes are named by numbers, which is pretty hard to read. While we lower from the traditional pipeline, we can see the names pretty clearly. It is pretty helpful for debugging.

ChuanqiXu9 avatar Oct 14 '24 06:10 ChuanqiXu9

@lanza and I have discussed this too. This will also be pretty important in the future when we want to start running some original CodeGen tests with ClangIR, since they assume value names will be present.

From what I understand, MLIR doesn't have the concept of value names the same way LLVM does; they're not stored in the bitcode format, for example, and just generated by the AsmPrinter on the fly instead. We'll probably need to extend the LLVMIR dialect to support this; one idea we were thinking of was an optional attribute on all operations to name the result (similar to how cir.alloca stores the variable names) that can be accessed during translation. It'd need to be discussed with upstream MLIR though.

smeenai avatar Oct 14 '24 17:10 smeenai

Beside SSA value names, what about basic block names? MLIR does not support block names either.

Lancern avatar Oct 16 '24 05:10 Lancern

Good point, I'd forgotten about those. I'm not sure at all about the right approach for those, but hopefully the MLIR folks will have ideas.

smeenai avatar Oct 16 '24 17:10 smeenai

One interesting bit is that operations with multiple return values actually get names based on tablegen, so some of the mechanism is already there.

This discussion has happened in MLIR related forums before, I don't have a digest to give, but it might be worth asking there to get an informed status quo

bcardosolopes avatar Oct 16 '24 18:10 bcardosolopes

I found some relevant reading: https://discourse.llvm.org/t/rfc-better-support-for-dialects-that-want-to-make-ssa-names-load-bearing/674 and https://discourse.llvm.org/t/names-in-ssa-values-for-debuggability/1041. In particular https://github.com/llvm/llvm-project/commit/596da62d21ede197dd95eca5146d2ddf0497275c actually landed, but I'm not sure what the API looks like in practice.

smeenai avatar Oct 17 '24 04:10 smeenai

Haven't looked in depth, but seems like it requires changing the printers to custom ones? This is from 2020, so maybe there's something more streamlined we could use?

bcardosolopes avatar Oct 17 '24 21:10 bcardosolopes

https://github.com/llvm/llvm-project/pull/119996 landed recently, where we can attach a NameLoc to an op to set its value name. I haven't looked into it too much, but it seems like it could be useful for us.

smeenai avatar Jan 14 '25 00:01 smeenai