[Lldb-commits] [llvm] [mlir] [lldb] [mlir] Introduce replaceWithZeroTripCheck in LoopLikeOpInterface (PR #80331)

Jerry Wu via lldb-commits lldb-commits at lists.llvm.org
Mon Feb 5 15:01:54 PST 2024


================
@@ -0,0 +1,101 @@
+//===- LoopLikeInterfaceTest.cpp - Unit tests for Loop Like Interfaces. ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Interfaces/LoopLikeInterface.h"
+#include "mlir/IR/BuiltinOps.h"
+#include "mlir/IR/Dialect.h"
+#include "mlir/IR/DialectImplementation.h"
+#include "mlir/IR/OpDefinition.h"
+#include "mlir/IR/OpImplementation.h"
+#include "mlir/IR/PatternMatch.h"
+#include "mlir/Parser/Parser.h"
+
+#include <gtest/gtest.h>
+
+using namespace mlir;
+
+struct NoZeroTripCheckLoopOp
+    : public Op<NoZeroTripCheckLoopOp, LoopLikeOpInterface::Trait> {
+  using Op::Op;
+
+  static ArrayRef<StringRef> getAttributeNames() { return {}; }
+
+  static StringRef getOperationName() {
+    return "looptest.no_zero_trip_check_loop_op";
+  }
+
+  SmallVector<Region *> getLoopRegions() { return {}; }
+};
+
+struct ImplZeroTripCheckLoopOp
+    : public Op<ImplZeroTripCheckLoopOp, LoopLikeOpInterface::Trait> {
+  using Op::Op;
+
+  static ArrayRef<StringRef> getAttributeNames() { return {}; }
+
+  static StringRef getOperationName() {
+    return "looptest.impl_zero_trip_check_loop_op";
+  }
+
+  SmallVector<Region *> getLoopRegions() { return {}; }
+
+  FailureOr<LoopLikeOpInterface>
+  replaceWithZeroTripCheck(RewriterBase &rewriter) {
+    return cast<LoopLikeOpInterface>(this->getOperation());
+  }
+};
+
+/// A dialect putting all the above together.
+struct LoopTestDialect : Dialect {
+  explicit LoopTestDialect(MLIRContext *ctx)
+      : Dialect(getDialectNamespace(), ctx, TypeID::get<LoopTestDialect>()) {
+    addOperations<NoZeroTripCheckLoopOp, ImplZeroTripCheckLoopOp>();
+  }
+  static StringRef getDialectNamespace() { return "looptest"; }
+};
+
+TEST(LoopLikeOpInterface, NoReplaceWithZeroTripCheck) {
+  const char *ir = R"MLIR(
+  "looptest.no_zero_trip_check_loop_op"() : () -> ()
+  )MLIR";
+
+  DialectRegistry registry;
+  registry.insert<LoopTestDialect>();
+  MLIRContext ctx(registry);
+
+  OwningOpRef<ModuleOp> module = parseSourceString<ModuleOp>(ir, &ctx);
+  LoopLikeOpInterface testOp =
+      cast<LoopLikeOpInterface>(module->getBody()->getOperations().front());
+
+  IRRewriter rewriter(&ctx);
+  FailureOr<LoopLikeOpInterface> result =
+      testOp.replaceWithZeroTripCheck(rewriter);
+
+  EXPECT_TRUE(failed(result));
+}
+
+TEST(LoopLikeOpInterface, ImplReplaceWithZeroTripCheck) {
+  const char *ir = R"MLIR(
+  "looptest.impl_zero_trip_check_loop_op"() : () -> ()
+  )MLIR";
+
+  DialectRegistry registry;
+  registry.insert<LoopTestDialect>();
+  MLIRContext ctx(registry);
+
+  OwningOpRef<ModuleOp> module = parseSourceString<ModuleOp>(ir, &ctx);
+  LoopLikeOpInterface testOp =
+      cast<LoopLikeOpInterface>(module->getBody()->getOperations().front());
+
+  IRRewriter rewriter(&ctx);
+  FailureOr<LoopLikeOpInterface> result =
+      testOp.replaceWithZeroTripCheck(rewriter);
+
+  EXPECT_TRUE(succeeded(result));
+  EXPECT_EQ(*result, testOp);
+}
----------------
pzread wrote:

Done. PTAL. I prefer to keep this change focusing on the interface definition so I only add a `scf.while` test with no transformation. The actual transformation and tests will be added in the follow-up change #80349

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


More information about the lldb-commits mailing list