[Mlir-commits] [mlir] [mlir][Ptr] Add the `MemorySpaceAttrInterface` interface and dependencies. (PR #86870)

Tobias Gysi llvmlistbot at llvm.org
Mon Apr 1 01:24:09 PDT 2024


================
@@ -39,8 +39,77 @@ void PtrDialect::initialize() {
 // Pointer API.
 //===----------------------------------------------------------------------===//
 
+// Returns a pair containing:
+// The underlying type of a vector or the type itself if it's not a vector.
+// The number of elements in the vector or an error code if the type is not
+// supported.
+static std::pair<Type, int64_t> getVecOrScalarInfo(Type ty) {
+  if (auto vecTy = dyn_cast<VectorType>(ty)) {
+    auto elemTy = vecTy.getElementType();
+    // Vectors of rank greater than one or with scalable dimensions are not
+    // supported.
+    if (vecTy.getRank() != 1)
+      return {elemTy, -1};
+    else if (vecTy.getScalableDims()[0])
+      return {elemTy, -2};
+    return {elemTy, vecTy.getShape()[0]};
+  }
+  // `ty` is a scalar type.
+  return {ty, 0};
+}
+
+LogicalResult mlir::ptr::isValidAddrSpaceCastImpl(Type tgt, Type src,
+                                                  Operation *op) {
+  std::pair<Type, int64_t> tgtInfo = getVecOrScalarInfo(tgt);
+  std::pair<Type, int64_t> srcInfo = getVecOrScalarInfo(src);
+  if (!isa<PtrType>(tgtInfo.first) || !isa<PtrType>(srcInfo.first))
+    return op ? op->emitError("invalid ptr-like operand") : failure();
+  // Check shape validity.
+  if (tgtInfo.second == -1 || srcInfo.second == -1)
+    return op ? op->emitError("vectors of rank != 1 are not supported")
+              : failure();
+  if (tgtInfo.second == -2 || srcInfo.second == -2)
+    return op ? op->emitError(
+                    "vectors with scalable dimensions are not supported")
+              : failure();
+  if (tgtInfo.second != srcInfo.second)
+    return op ? op->emitError("incompatible operand shapes") : failure();
+  return success();
+}
+
+LogicalResult mlir::ptr::isValidPtrIntCastImpl(Type intLikeTy, Type ptrLikeTy,
+                                               Operation *op) {
+  // Check int-like type.
+  std::pair<Type, int64_t> intInfo = getVecOrScalarInfo(intLikeTy);
+  if (!intInfo.first.isSignlessIntOrIndex())
+    /// The int-like operand is invalid.
+    return op ? op->emitError("invalid int-like type") : failure();
----------------
gysit wrote:

nit: Please use braces around such multi-line ifs here and below. Also no need for triple slash doc comments inside the function.

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


More information about the Mlir-commits mailing list