[Mlir-commits] [mlir] e84589c - Specialize OwningOpRef::operator-> to work with pointers like Operation*

Mehdi Amini llvmlistbot at llvm.org
Thu May 25 00:28:16 PDT 2023


Author: Mehdi Amini
Date: 2023-05-25T00:27:59-07:00
New Revision: e84589c9cce0915b223d1c6eda3320f498b6a598

URL: https://github.com/llvm/llvm-project/commit/e84589c9cce0915b223d1c6eda3320f498b6a598
DIFF: https://github.com/llvm/llvm-project/commit/e84589c9cce0915b223d1c6eda3320f498b6a598.diff

LOG: Specialize OwningOpRef::operator-> to work with pointers like Operation*

This just simplifies user code.

Differential Revision: https://reviews.llvm.org/D151407

Added: 
    

Modified: 
    mlir/include/mlir/IR/OwningOpRef.h
    mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/OwningOpRef.h b/mlir/include/mlir/IR/OwningOpRef.h
index 0ca960b90ab0d..eb4bf2dc67e3e 100644
--- a/mlir/include/mlir/IR/OwningOpRef.h
+++ b/mlir/include/mlir/IR/OwningOpRef.h
@@ -49,7 +49,14 @@ class OwningOpRef {
   /// Allow accessing the internal op.
   OpTy get() const { return op; }
   OpTy operator*() const { return op; }
-  OpTy *operator->() { return &op; }
+  auto operator->() {
+    // Specialize for the case where OpTy is a pointer, to allow using
+    // OwningOpRef<Operation*>.
+    if constexpr (std::is_pointer<OpTy>::value)
+      return op;
+    else
+      return &op;
+  }
   explicit operator bool() const { return op; }
 
   /// Downcast to generic operation.

diff  --git a/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp b/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp
index 7278aba438b96..c84dd92e23acc 100644
--- a/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp
+++ b/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp
@@ -1322,13 +1322,13 @@ lsp::MLIRServer::convertFromBytecode(const URIForFile &uri) {
     // Extract the top-level op so that aliases get printed.
     // FIXME: We should be able to enable aliases without having to do this!
     OwningOpRef<Operation *> topOp = &parsedBlock.front();
-    (*topOp)->remove();
+    topOp->remove();
 
     AsmState state(*topOp, OpPrintingFlags().enableDebugInfo().assumeVerified(),
                    /*locationMap=*/nullptr, &fallbackResourceMap);
 
     llvm::raw_string_ostream os(result.output);
-    (*topOp)->print(os, state);
+    topOp->print(os, state);
   }
   return std::move(result);
 }


        


More information about the Mlir-commits mailing list