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

More fixes to make MLIR-level IR fully validated



Added a couple of missing Result cast in Measure handler.

Handled the case of single-bit memref (zero dimension)

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 62488e66
Loading
Loading
Loading
Loading
+24 −5
Original line number Diff line number Diff line
@@ -181,9 +181,10 @@ antlrcpp::Any qasm3_visitor::visitQuantumMeasurementAssignment(
              location, result_type, str_attr,
              llvm::makeArrayRef(extract.qbit()),
              llvm::makeArrayRef(std::vector<mlir::Value>{}));

          auto cast_bit_op = builder.create<mlir::quantum::ResultCastOp>(
              location, builder.getIntegerType(1), instop.bit());
          builder.create<mlir::StoreOp>(
              location, instop.bit(), bit_value,
              location, cast_bit_op.bit_result(), bit_value,
              llvm::makeArrayRef(std::vector<mlir::Value>{bit_idx_val}));
        }

@@ -212,15 +213,33 @@ antlrcpp::Any qasm3_visitor::visitQuantumMeasurementAssignment(
                                                        file_name);
        equals_exp_generator.visit(index_list->expression(0));
        v = equals_exp_generator.current_value;
        // Make sure v is of Index type (to be used w/ StoreOp)
        v = builder.create<mlir::IndexCastOp>(location, builder.getIndexType(),
                                              v);
      } else {
        v = get_or_create_constant_index_value(
          0, location, 64, symbol_table, builder);
      }

      assert(v.getType().isa<mlir::IndexType>());

      // Cast Measure Result -> Bit (i1)
      auto cast_bit_op = builder.create<mlir::quantum::ResultCastOp>(
          location, builder.getIntegerType(1), instop.bit());

      if (bit_value.getType().isa<mlir::MemRefType>() &&
          bit_value.getType().cast<mlir::MemRefType>().getShape().empty()) {
        // If the array is a **zero-dimemsion** Memref *without* shape
        // we don't send on the index (probably v = 0).
        // This will fail to validate at the MLIR level (Memref dimension mismatches)
        // (at LLVM level, it doesn't matter b/w a memref<i1> vs. memref<1xi1>).
        builder.create<mlir::StoreOp>(location, cast_bit_op.bit_result(),
                                      bit_value);
      } else {
        builder.create<mlir::StoreOp>(
          location, instop.bit(), bit_value,
            location, cast_bit_op.bit_result(), bit_value,
            llvm::makeArrayRef(std::vector<mlir::Value>{v}));
      }
    } else {
      // This is the case where we are measuring an entire qubit array
      // to a bit array