[Mlir-commits] [mlir] e767ef4 - [mlir] Enable converting properties during C create

Jacques Pienaar llvmlistbot at llvm.org
Sun Jul 23 21:40:18 PDT 2023


Author: Jacques Pienaar
Date: 2023-07-23T21:40:12-07:00
New Revision: e767ef47a27975a3231b0d1611030923dbafd4a2

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

LOG: [mlir] Enable converting properties during C create

This enables querying properties passed as attributes during
construction time. In particular needed for type inference where the
Operation has not been created at this point. This allows Python
construction of operations whose type inference depends on properties.

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

Added: 
    

Modified: 
    mlir/lib/CAPI/IR/IR.cpp
    mlir/lib/Dialect/Shape/IR/Shape.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp
index dedae3ddd2c1d9..b140a463982f9b 100644
--- a/mlir/lib/CAPI/IR/IR.cpp
+++ b/mlir/lib/CAPI/IR/IR.cpp
@@ -15,10 +15,12 @@
 #include "mlir/CAPI/Support.h"
 #include "mlir/CAPI/Utils.h"
 #include "mlir/IR/Attributes.h"
+#include "mlir/IR/BuiltinAttributes.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/Dialect.h"
 #include "mlir/IR/Location.h"
 #include "mlir/IR/Operation.h"
+#include "mlir/IR/OperationSupport.h"
 #include "mlir/IR/Types.h"
 #include "mlir/IR/Value.h"
 #include "mlir/IR/Verifier.h"
@@ -26,6 +28,7 @@
 #include "mlir/Parser/Parser.h"
 
 #include <cstddef>
+#include <memory>
 #include <optional>
 
 using namespace mlir;
@@ -345,25 +348,43 @@ static LogicalResult inferOperationTypes(OperationState &state) {
   if (!info) {
     emitError(state.location)
         << "type inference was requested for the operation " << state.name
-        << ", but the operation was not registered. Ensure that the dialect "
+        << ", but the operation was not registered; ensure that the dialect "
            "containing the operation is linked into MLIR and registered with "
            "the context";
     return failure();
   }
 
-  // Fallback to inference via an op interface.
   auto *inferInterface = info->getInterface<InferTypeOpInterface>();
   if (!inferInterface) {
     emitError(state.location)
         << "type inference was requested for the operation " << state.name
-        << ", but the operation does not support type inference. Result "
-           "types must be specified explicitly.";
+        << ", but the operation does not support type inference; result "
+           "types must be specified explicitly";
+    return failure();
+  }
+
+  DictionaryAttr attributes = state.attributes.getDictionary(context);
+  OpaqueProperties properties = state.getRawProperties();
+
+  if (!properties && info->getOpPropertyByteSize() > 0 && !attributes.empty()) {
+    auto prop = std::make_unique<char[]>(info->getOpPropertyByteSize());
+    properties = OpaqueProperties(prop.get());
+    if (failed(info->setOpPropertiesFromAttribute(state.name, properties,
+                                                  attributes, nullptr))) {
+      return failure();
+    }
+
+    if (succeeded(inferInterface->inferReturnTypes(
+            context, state.location, state.operands, attributes, properties,
+            state.regions, state.types))) {
+      return success();
+    }
+    // Diagnostic emitted by interface.
     return failure();
   }
 
   if (succeeded(inferInterface->inferReturnTypes(
-          context, state.location, state.operands,
-          state.attributes.getDictionary(context), state.getRawProperties(),
+          context, state.location, state.operands, attributes, properties,
           state.regions, state.types)))
     return success();
 
@@ -405,8 +426,7 @@ MlirOperation mlirOperationCreate(MlirOperationState *state) {
       return {nullptr};
   }
 
-  MlirOperation result = wrap(Operation::create(cppState));
-  return result;
+  return wrap(Operation::create(cppState));
 }
 
 MlirOperation mlirOperationCreateParse(MlirContext context,

diff  --git a/mlir/lib/Dialect/Shape/IR/Shape.cpp b/mlir/lib/Dialect/Shape/IR/Shape.cpp
index 1c529929a401d2..3862b889723e5e 100644
--- a/mlir/lib/Dialect/Shape/IR/Shape.cpp
+++ b/mlir/lib/Dialect/Shape/IR/Shape.cpp
@@ -919,15 +919,8 @@ LogicalResult mlir::shape::ConstShapeOp::inferReturnTypes(
     ConstShapeOp::Adaptor adaptor, SmallVectorImpl<Type> &inferredReturnTypes) {
   Builder b(context);
   const Properties prop = adaptor.getProperties();
-  DenseIntElementsAttr shape;
-  // TODO: this is only exercised by the Python bindings codepath which does not
-  // support properties
-  shape = prop.shape ? prop.shape : 
-            adaptor.getAttributes().getAs<DenseIntElementsAttr>("shape");
-  if (!shape)
-    return emitOptionalError(location, "missing shape attribute");
   inferredReturnTypes.assign({RankedTensorType::get(
-      {static_cast<int64_t>(shape.size())}, b.getIndexType())});
+      {static_cast<int64_t>(prop.shape.size())}, b.getIndexType())});
   return success();
 }
 


        


More information about the Mlir-commits mailing list