-   Notifications  You must be signed in to change notification settings 
- Fork 15k
Closed
Labels
Description
The fold method for arith.addi and similar operations does not define a fold for 0+x-->x, only for x+0-->x. This means that createOrFold for arith.addi(%zero, %a) creates arith.addi(%a, %zero) thanks to the Commutative foldTrait.
 Is this intended? I think it would make sense for folding to fully fold an addition with zero in all cases.
#include "mlir/Dialect/Arith/IR/Arith.h" #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinTypes.h" #include "mlir/IR/Location.h" #include "mlir/IR/MLIRContext.h" #include "mlir/IR/Verifier.h" #include "llvm/Support/raw_ostream.h" using namespace mlir; using namespace mlir::arith; int main() { MLIRContext context; context.getOrLoadDialect<ArithDialect>(); OpBuilder builder(&context); Location loc = builder.getUnknownLoc(); Type i32Type = builder.getI32Type(); Block b = Block(); Value blockArg = b.addArgument(i32Type, loc); Value constantZero = builder.create<ConstantOp>(loc, i32Type, builder.getI32IntegerAttr(0)).getResult(); Value addiResult = builder.createOrFold<AddIOp>(loc, constantZero, blockArg); addiResult.getDefiningOp()->dump(); // 0 + a --> a + 0 Value addiResult2 = builder.createOrFold<AddIOp>(loc, blockArg, constantZero); addiResult2.dump(); // a + 0 --> a return 0; }%0 = arith.addi <<UNKNOWN SSA VALUE>>, <<UNKNOWN SSA VALUE>> : i32 <block argument> of type 'i32' at index: 0