LLVM Discussion Forums

[RTL] Flattening operations

The next piece of the puzzle is how to flatten operations in the following manner:
// and(x, and(...)) -> and(x, ...) -- flatten
Given inputs.back(), how do we determine if its another operation, and more specifically, an and operation?

Maybe I’m looking to do something like .getType(), but not entirely sure, nor was I able to get a working prototype. The nearby dialects and StandardOps don’t seem to have anything similar to this either, at least from what I saw.

Given a Value named x you should be able to something like:

if (auto andOp = dyn_cast<rtl::AndOp>(x)) {

Check the LLVM programmers manual for info about dyn_cast and isa.

Thanks Chris. However, given that my input x is of type mlir::Value, this cast isn’t exactly possible, from what I’ve tried. I’m assuming that a mlir::Value can be an operator. If so, I’m assuming I need to call some function to access the operator stored within. Looking at the documentation for mlir::Value, I tried x.getDefiningOp(), with no success.

A Value is either a block argument (this is how function argument are modeled) or one of the results of an Operation.
So x.getDefiningOp() will give you an operation when the Value isn’t a block argument, otherwise it’ll give you a nullptr.

Good point, try this Chris:

if (auto andOp = dyn_cast_or_null<rtl::AndOp>(x.getDefiningOp())) {

The dyn_cast_or_null piece is important to handle the possibly null return from getDefiningOp()

Yep that was it. Thanks! I saw that in the manual too, but misread the use case for it.

There is a shortcut FYI:

if (auto andOp = x.getDefiningOp<rtl::AndOp>()) {
1 Like