[Mlir-commits] [mlir] Add OneTypedResult::getResultOfType to simplify the result type casting logic (PR #120381)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Dec 18 18:21:15 PST 2024


================
@@ -694,11 +694,16 @@ class OneTypedResult {
   class Impl
       : public TraitBase<ConcreteType, OneTypedResult<ResultType>::Impl> {
   public:
-    mlir::TypedValue<ResultType> getResult() {
-      return cast<mlir::TypedValue<ResultType>>(
+    template <typename ValTy>
+    mlir::TypedValue<ValTy> getResultOfType() {
+      return mlir::cast<mlir::TypedValue<ValTy>>(
           this->getOperation()->getResult(0));
     }
 
+    mlir::TypedValue<ResultType> getResult() {
+      return getResultOfType<ResultType>();
+    }
+
----------------
xiaoleis-nv wrote:

> Would this work by templating getResult?

This is a nice suggestion. I have tried it, but simply templating the getResult would cause a compilation error, as shown below.
```
llvm-project/mlir/lib/Dialect/Mesh/Transforms/Spmdization.cpp:94:32: error: expected primary-expression before '>' token
   94 |           .getResult<ShapedType>();
      |                                ^
llvm-project/mlir/lib/Dialect/Mesh/Transforms/Spmdization.cpp:94:34: error: expected primary-expression before ')' token
   94 |           .getResult<ShapedType>();
      |                                  ^
```

> I would name it getResultAs instead, because other methods like getParentOfType are not behaving the same

Thanks for this note. I have updated it to getResultAs.

https://github.com/llvm/llvm-project/pull/120381


More information about the Mlir-commits mailing list