Add support for math.ctlz in convert-math-to-funcs
authorJeremy Kun <j2kun@users.noreply.github.com>
Mon, 10 Apr 2023 15:57:34 +0000 (08:57 -0700)
committerSlava Zakharin <szakharin@nvidia.com>
Mon, 10 Apr 2023 17:02:00 +0000 (10:02 -0700)
commitbfbccfa17c97f29993555fcc4d9f191ba49d9606
treeed2807c71acff1b7cabcbe100a6cec4603ee2f05
parentbc8e87ef4ae94323fdb17e9c916d3e491d82b215
Add support for math.ctlz in convert-math-to-funcs

This change adds a software implementation of the `math.ctlz` operation
and includes it in `--convert-math-to-funcs`.

This is my first change to MLIR, so please bear with me as I'm still learning
the idioms of the codebase.

The context for this change is that I have some larger scale project in which
I'd like to lower from a mix of MLIR dialects to CIRCT, but many of the CIRCT
passes don't support the `math` dialect.

I noticed the content of `convert-math-to-funcs` was limited entirely to
the `pow` functions, but otherwise provided the needed structure to implement
this feature with minimal changes.

Highlight of the changes:

- Add a dependence on the SCF dialect for this lower. I could have lowered
  directly to cf, following the pow lowerings in the same pass, but I felt it
  was not necessary given the existing support for lowering scf to cf.
- Generalize the DenseMap storing op implementations: modify the callback
  function hashmap to be keyed by both OperationType (for me this effectively
  means the name of the op being implemented in software) and the type
  signature of the resulting function.
- Implement the ctlz function as a loop. I had researched a variety of
  implementations that claimed to be more efficient (such as those based on a
  de Bruijn sequence), but it seems to me that the simplest approach would make
  it easier for later compiler optimizations to do a better (platform-aware)
  job optimizing this than I could do by hand.

Questions I had for the reviewer:

- [edit: found mlir-cpu-runner and added two tests] What would I add to the filecheck invocation to actually run the resulting MLIR on a value and assert the output is correct? I have done this manually with the C implementation but I'm not confident my port to MLIR is correct.
- Should I add a test for a vectorized version of this lowering? I followed suit with the ` VecOpToScalarOp` but I admit I don't fully understand what it's doing.

Reviewed By: vzakhari

Differential Revision: https://reviews.llvm.org/D146261
mlir/include/mlir/Conversion/Passes.td
mlir/lib/Conversion/MathToFuncs/CMakeLists.txt
mlir/lib/Conversion/MathToFuncs/MathToFuncs.cpp
mlir/test/Conversion/MathToFuncs/ctlz.mlir [new file with mode: 0644]
mlir/test/Integration/Dialect/Math/CPU/mathtofuncs_ctlz.mlir [new file with mode: 0644]
utils/bazel/llvm-project-overlay/mlir/BUILD.bazel