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

Mehdi Amini llvmlistbot at llvm.org
Wed Dec 18 03:34:05 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>();
+    }
+
----------------
joker-eph wrote:

Would this work by templating `getResult`?
```
    template<typename ValTy = ResultType>
    mlir::TypedValue<ValTy> getResult() {
       return mlir::cast<mlir::TypedValue<ValTy>>(
          this->getOperation()->getResult(0));
    }
```

If that does not work, I would name it `getResultAs` instead, because other methods like `getParentOfType` are not behaving the same: they are not just a cast but a "find" instead.

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


More information about the Mlir-commits mailing list