This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Add arith.addi_carry op
ClosedPublic

Authored by kuhar on Aug 15 2022, 7:44 AM.

Details

Summary

The arith.addi_carry op implements integer addition with overflows. The carry is returned via the second result, as i1.

Diff Detail

Event Timeline

kuhar created this revision.Aug 15 2022, 7:44 AM
kuhar requested review of this revision.Aug 15 2022, 7:44 AM
antiagainst requested changes to this revision.Aug 15 2022, 10:47 AM

This patch actually contains both the new arith op definition and lowering to SPIR-V. What about splitting them into two patches for more targeted patches and a clean history?

This revision now requires changes to proceed.Aug 15 2022, 10:47 AM

This patch actually contains both the new arith op definition and lowering to SPIR-V. What about splitting them into two patches for more targeted patches and a clean history?

Sure, it makes sense to split it.

kuhar updated this revision to Diff 452746.Aug 15 2022, 11:05 AM
kuhar edited the summary of this revision. (Show Details)

Removed conversion to spv.IAddCarry

antiagainst requested changes to this revision.Aug 15 2022, 2:40 PM

Cool, just two small issues.

mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
242

Use mlir::builder::getZeroAttr(carryTy) directly here?

mlir/test/Dialect/Arithmetic/canonicalize.mlir
549

Need to capture %arg0 behind a FileCheck symbol here.

This revision now requires changes to proceed.Aug 15 2022, 2:40 PM
kuhar added inline comments.Aug 15 2022, 2:52 PM
mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
242

I'm not sure I can access the builder here, can I? This is a non-static method.

mlir/test/Dialect/Arithmetic/canonicalize.mlir
549

Other test cases in this file rely on the argument name being preserved. Do you think this is too fragile?

antiagainst added inline comments.Aug 15 2022, 3:02 PM
mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
242

It's fine to construct a temporary mlir::Builder using the context with mlir::Builder(getContext()) and then call the getZeroAttr method. In MLIR attributes are uniqued and kept in the context and have a lifelime as a context. So it's fine. (mlir::OpBuilder is different---it can build operations and thus would need an insertion point and are stateful, etc.)

mlir/test/Dialect/Arithmetic/canonicalize.mlir
549

Normally we won't want to directly use the symbols from printer given they can be changed and thus break these tests. It's unlikely that we are gonna change how function arguments are printed though. So it's fine then, given other tests already use it.

kuhar updated this revision to Diff 452819.Aug 15 2022, 3:06 PM
kuhar retitled this revision from [WIP][mlir] Add arith.addi_carry op to [mlir] Add arith.addi_carry op.
kuhar edited the summary of this revision. (Show Details)
kuhar marked 3 inline comments as done.
kuhar added inline comments.
mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
242

Ah, makes sense, thanks!

antiagainst accepted this revision.Aug 15 2022, 3:26 PM
This revision is now accepted and ready to land.Aug 15 2022, 3:26 PM
bondhugula requested changes to this revision.Aug 15 2022, 7:56 PM
bondhugula added a subscriber: bondhugula.
bondhugula added inline comments.
mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
20

Not needed.

21

Not needed.

23

You don't need this include here.

225

No need of getSum(); just getType().

236–286

This is completely missing code comments. Please see another op's folding hook for reference.

265–266

Please add assert messages.

This revision now requires changes to proceed.Aug 15 2022, 7:56 PM
kuhar updated this revision to Diff 452883.Aug 15 2022, 8:44 PM
kuhar marked 7 inline comments as done.

Added comments, removed includes.

mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
23

I removed all of these.
I'm used to a convention that roughly says that if you use something directly, it should be included in the same file, instead of relying on transitive includes that may change. Is there some relevant coding convention on includes documented somewhere?

236–286

I added some prose. Is this what you expected? Other fold functions in this file don't seem to explain much when it comes to the default case that calls the default folder (constFoldBinaryOp).

265–266

Actually I don't think these are needed/useful, as constFoldBinaryOp must already check the same things. I removed them.

bondhugula added inline comments.Aug 16 2022, 7:32 AM
mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
23
bondhugula accepted this revision.Aug 16 2022, 7:33 AM
bondhugula added inline comments.
mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
260

Typo: calculate

This revision is now accepted and ready to land.Aug 16 2022, 7:33 AM
This revision was automatically updated to reflect the committed changes.