Commit fac11e47 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Comparison Op needs to handle index type



AffineOp loop var is of index type; the comparison op doesn't handle that.

Hence, the following code isn't handled correctly:

for i in [0:10] {
	if (i == 2) {
	....
        }
}

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent ee616ae7
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -232,8 +232,10 @@ antlrcpp::Any qasm3_expression_generator::visitComparsionExpression(
      auto lhs_bw = lhs.getType().getIntOrFloatBitWidth();
      auto rhs_bw = rhs.getType().getIntOrFloatBitWidth();

      if (lhs.getType().isa<mlir::IntegerType>()) {
        if (!rhs.getType().isa<mlir::IntegerType>()) {
      if (lhs.getType().isa<mlir::IntegerType>() ||
          lhs.getType().isa<mlir::IndexType>()) {
        if (!rhs.getType().isa<mlir::IntegerType>() &&
            !rhs.getType().isa<mlir::IndexType>()) {
          printErrorMessage("for comparison " + op +
                                " lhs was an integer type, but rhs was not.",
                            compare, {lhs, rhs});
@@ -259,6 +261,10 @@ antlrcpp::Any qasm3_expression_generator::visitComparsionExpression(
        }
        update_current_value(builder.create<mlir::CmpFOp>(
            location, antlr_to_mlir_fpredicate[op], lhs, rhs));
      } else {
        // Sth wrong, we cannot handle this atm.
        printErrorMessage("Unhandled comparison " + op + ": ", compare,
                          {lhs, rhs});
      }
      return 0;
    } else {
+5 −3
Original line number Diff line number Diff line
@@ -206,11 +206,13 @@ antlrcpp::Any qasm3_visitor::visitBranchingStatement(
  exp_generator.visit(conditional_expr);
  // Boolean check value:
  auto expr_value = exp_generator.current_value;

  // Must be an i1 (bool)
  assert(expr_value.getType().isa<mlir::IntegerType>() &&
         expr_value.getType().getIntOrFloatBitWidth() == 1);
  // Create SCF If Op:
  // SCF IfOp (switching on a boolean value) matches what we need here,
  // an AffineIfOp requires an integer set and will be lowered to SCF's IfOp later,
  // hence is not a good solution.
  // an AffineIfOp requires an integer set and will be lowered to SCF's IfOp
  // later, hence is not a good solution.
  const bool hasElseBlock = context->programBlock().size() == 2;
  auto scfIfOp = builder.create<mlir::scf::IfOp>(location, mlir::TypeRange(),
                                           expr_value, hasElseBlock);