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

Fixed a couple of indexing type mismatches in QASM3 handler



Also, allow to pass mlir optimization option to qcor command line

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent eed8cabd
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -140,13 +140,18 @@ antlrcpp::Any qasm3_expression_generator::visitTerminal(

        update_current_value(
            builder.create<mlir::quantum::GeneralArrayExtractOp>(
                location, array_type, indexed_variable_value, current_value));
                location, array_type, indexed_variable_value,
                cast_array_index_value_if_required(
                    indexed_variable_value.getType(), current_value, location,
                    builder)));

        // unset the variable name just in case
        indexed_variable_name = "";
      } else {
        // We are loading from a variable
        llvm::ArrayRef<mlir::Value> idx(current_value);
        llvm::ArrayRef<mlir::Value> idx(cast_array_index_value_if_required(
            indexed_variable_value.getType(), current_value, location,
            builder));
        update_current_value(builder.create<mlir::LoadOp>(
            location, indexed_variable_value, idx));
      }
+20 −0
Original line number Diff line number Diff line
@@ -230,6 +230,26 @@ mlir::Type convertQasm3Type(qasm3::qasm3Parser::ClassicalTypeContext* ctx,
  return mlir::Type();
}

mlir::Value cast_array_index_value_if_required(mlir::Type array_type, mlir::Value raw_index, mlir::Location location, mlir::OpBuilder &builder) {
  // Memref must use index type
  if (array_type.isa<mlir::MemRefType>() &&
      !raw_index.getType().isa<mlir::IndexType>()) {
    return builder.create<mlir::IndexCastOp>(location, builder.getIndexType(),
                                             raw_index);
  }
  // QIR arrays: must use I64
  if (array_type.isa<mlir::OpaqueType>() &&
      array_type.cast<mlir::OpaqueType>().getTypeData().str() == "Array" &&
      raw_index.getType().isa<mlir::IndexType>()) {
    return builder.create<mlir::IndexCastOp>(location, builder.getI64Type(),
                                             raw_index);
  }

  // No need to do anything
  return raw_index;
}


std::map<std::string, mlir::CmpIPredicate> antlr_to_mlir_predicate{
    {"==", mlir::CmpIPredicate::eq},  {"!=", mlir::CmpIPredicate::ne},
    {"<=", mlir::CmpIPredicate::sle}, {">=", mlir::CmpIPredicate::sge},
+9 −0
Original line number Diff line number Diff line
@@ -60,6 +60,15 @@ mlir::Value get_or_create_constant_index_value(const std::size_t idx,
                                               int width,
                                               ScopedSymbolTable& symbol_table,
                                               mlir::OpBuilder& builder);

// Construct the correct array indexing type:
// For memref: index type
// For qubit array: I64 type
mlir::Value cast_array_index_value_if_required(mlir::Type array_type,
                                               mlir::Value raw_index,
                                               mlir::Location location,
                                               mlir::OpBuilder &builder);

extern std::map<std::string, mlir::CmpIPredicate> antlr_to_mlir_predicate;
extern std::map<std::string, mlir::CmpFPredicate> antlr_to_mlir_fpredicate;

+4 −0
Original line number Diff line number Diff line
@@ -416,6 +416,10 @@ def main(argv=None):
        if '-no-entrypoint' in sys.argv[1:]:
            sys.argv.remove('-no-entrypoint')
            extra_args.append('-no-entrypoint')
        # FIXME: define pass-mamanegement CLI options for qcor
        if '--q-optimize' in sys.argv[1:]:
            sys.argv.remove('--q-optimize')
            extra_args.append('--q-optimize')
        result = subprocess.run(['@CMAKE_INSTALL_PREFIX@/bin/qcor-mlir-tool', filename] + extra_args, check=True)
        llvm_bin_path = str(pathlib.Path(compiler).parent)
        if verbose: