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

Temp. workaround for the uncompute adjoint in compute-action handler



We need to handle this case without SSA argument tracking in region-based modifier ops since we don't know the list of arguments used by the block (i.e., no signature information)

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent b4cd2d1d
Loading
Loading
Loading
Loading
+30 −8
Original line number Diff line number Diff line
@@ -23,15 +23,37 @@ antlrcpp::Any qasm3_visitor::visitCompute_action_stmt(

  builder.create<mlir::quantum::ComputeMarkerOp>(location);

  // TODO: make sure we handle SSA use-def for this case.
  auto adjUOp = builder.create<mlir::quantum::AdjURegion>(location, llvm::None);
  {
    mlir::OpBuilder::InsertionGuard guard(builder);
    builder.setInsertionPointToStart(&adjUOp.body().front());
    visit(context->compute_block);
    builder.create<mlir::quantum::ModifierEndOp>(location, llvm::None);
  const auto createFuncCall = [](const std::string &func_name,
                                 mlir::OpBuilder &opBuilder,
                                 mlir::ModuleOp &parentModule) {
    mlir::FlatSymbolRefAttr funcRef = [&]() {
      mlir::OpBuilder::InsertionGuard guard(opBuilder);
      opBuilder.setInsertionPointToStart(
          &parentModule.getRegion().getBlocks().front());
      if (parentModule.lookupSymbol<mlir::FuncOp>(func_name)) {
        auto fnNameAttr = opBuilder.getSymbolRefAttr(func_name);
        return fnNameAttr;
      }

      auto func_decl = opBuilder.create<mlir::FuncOp>(
          opBuilder.getUnknownLoc(), func_name,
          opBuilder.getFunctionType(llvm::None, llvm::None));
      func_decl.setVisibility(mlir::SymbolTable::Visibility::Private);
      return mlir::SymbolRefAttr::get(func_name, parentModule->getContext());
    }();

    opBuilder.create<mlir::CallOp>(opBuilder.getUnknownLoc(), funcRef,
                                   llvm::None, llvm::None);
  };

  // Direct injection of Adj start/end functions.
  // !!FIXME!!: This compute_block is every generic, it'd be hard to infer all the qubit operands.
  // hence, we cannot enclose it in a modifier-scoped block yet.
  // SSA tracking (for optimization) for compute/action will not be guaranteed.
  createFuncCall("__quantum__rt__start_adj_u_region", builder, m_module);
  visit(context->compute_block);
  createFuncCall("__quantum__rt__end_adj_u_region", builder, m_module);

  builder.create<mlir::quantum::ComputeUnMarkerOp>(location);

  return 0;