[mlir] Allow to specify target type in `convertType`
authorJakub Kuderski <kubak@google.com>
Wed, 19 Apr 2023 17:51:13 +0000 (13:51 -0400)
committerJakub Kuderski <kubak@google.com>
Wed, 19 Apr 2023 17:51:13 +0000 (13:51 -0400)
commit0c7f3d6c39d7dd7d240854ac347a801d62dc938b
tree7579d70b0097eef0ce66b3149e889219982b0281
parentf2f37557e722a4d41378bd53c119ee89cb3ec93d
[mlir] Allow to specify target type in `convertType`

Add a new helper function for the type converter that takes care of
casting to the target type.

This is to avoid bugs where an incorrect cast function is used after
type conversion, e.g., `dyn_cast` or `cast`. These are not guaranteed to
work when type conversion fails, or when type conversion succeeds but
the provided type converted returned a type that a conversion pattern
did not expect.

I saw this being an issue in some SPIR-V passes and in mlir-hlo.

Exercise the new function in a couple of passes. As a side-effect, this
also made the code more concise.

Reviewed By: rriddle, mehdi_amini

Differential Revision: https://reviews.llvm.org/D148725
mlir/include/mlir/Transforms/DialectConversion.h
mlir/lib/Conversion/MemRefToSPIRV/MemRefToSPIRV.cpp
mlir/lib/Dialect/Arith/Transforms/EmulateWideInt.cpp