[mlir] Create a std op instead of chain of ops.
authorHanhan Wang <hanchung@google.com>
Tue, 10 Mar 2020 21:59:47 +0000 (14:59 -0700)
committerHanhan Wang <hanchung@google.com>
Tue, 10 Mar 2020 22:01:44 +0000 (15:01 -0700)
Summary:
1-bit integer is tricky in different dialects sometimes. E.g., there is no
arithmetic instructions on 1-bit integer in SPIR-V, i.e., `spv.IMul %0, %1 : i1`
is not valid. Instead, `spv.LogicalAnd %0, %1 : i1` is valid. Creating the op
directly makes lowering easier because we don't need to match a complicated
pattern like `!(!lhs && !rhs)`. Also, this matches the semantic better.

Also add assertions on inputs.

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

mlir/lib/Dialect/AffineOps/EDSC/Builders.cpp
mlir/test/EDSC/builder-api-test.cpp

index 301d3fd..e69f3d6 100644 (file)
@@ -209,11 +209,13 @@ ValueHandle mlir::edsc::op::operator!(ValueHandle value) {
 ValueHandle mlir::edsc::op::operator&&(ValueHandle lhs, ValueHandle rhs) {
   assert(lhs.getType().isInteger(1) && "expected boolean expression on LHS");
   assert(rhs.getType().isInteger(1) && "expected boolean expression on RHS");
-  return lhs * rhs;
+  return ValueHandle::create<AndOp>(lhs, rhs);
 }
 
 ValueHandle mlir::edsc::op::operator||(ValueHandle lhs, ValueHandle rhs) {
-  return !(!lhs && !rhs);
+  assert(lhs.getType().isInteger(1) && "expected boolean expression on LHS");
+  assert(rhs.getType().isInteger(1) && "expected boolean expression on RHS");
+  return ValueHandle::create<OrOp>(lhs, rhs);
 }
 
 static ValueHandle createIComparisonExpr(CmpIPredicate predicate,
index ed42278..69f615e 100644 (file)
@@ -480,6 +480,44 @@ TEST_FUNC(zero_and_std_sign_extendi_op_i1_to_i8) {
   f.erase();
 }
 
+TEST_FUNC(operator_or) {
+  auto i1Type = IntegerType::get(/*width=*/1, &globalContext());
+  auto f = makeFunction("operator_or", {}, {i1Type, i1Type});
+
+  OpBuilder builder(f.getBody());
+  ScopedContext scope(builder, f.getLoc());
+
+  using op::operator||;
+  ValueHandle lhs(f.getArgument(0));
+  ValueHandle rhs(f.getArgument(1));
+  lhs || rhs;
+
+  // CHECK-LABEL: @operator_or
+  //       CHECK: [[ARG0:%.*]]: i1, [[ARG1:%.*]]: i1
+  //       CHECK: or [[ARG0]], [[ARG1]]
+  f.print(llvm::outs());
+  f.erase();
+}
+
+TEST_FUNC(operator_and) {
+  auto i1Type = IntegerType::get(/*width=*/1, &globalContext());
+  auto f = makeFunction("operator_and", {}, {i1Type, i1Type});
+
+  OpBuilder builder(f.getBody());
+  ScopedContext scope(builder, f.getLoc());
+
+  using op::operator&&;
+  ValueHandle lhs(f.getArgument(0));
+  ValueHandle rhs(f.getArgument(1));
+  lhs &&rhs;
+
+  // CHECK-LABEL: @operator_and
+  //       CHECK: [[ARG0:%.*]]: i1, [[ARG1:%.*]]: i1
+  //       CHECK: and [[ARG0]], [[ARG1]]
+  f.print(llvm::outs());
+  f.erase();
+}
+
 TEST_FUNC(select_op_i32) {
   using namespace edsc::op;
   auto indexType = IndexType::get(&globalContext());