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

Fixes a bug in chasing use-def chain with region-based modifer ops inside a kernel def



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 10ad175a
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -490,6 +490,33 @@ pow(2) @ ctrl @ t q, qq;
  std::cout << "LLVM:\n" << llvm << "\n";
}

// Modifier block in a kernel definition:
// Note: we chase use-def for gate def:
TEST(qasm3VisitorTester, checkModifierInKernelDef) {
  const std::string check_nested = R"#(OPENQASM 3;
gate test1 q, qq {
  h q;
  ctrl @ x q, qq;
  x qq;
}

gate test2 q, qq {
  h q;
  ctrl @ pow(4) @ x q, qq;
  x qq;
}

gate test3 q, qq {
  h q;
  inv @ ctrl @ pow(4) @ t q, qq;
  x qq;
}
)#";
  auto llvm = qcor::mlir_compile(check_nested, "nested",
                                 qcor::OutputType::LLVMIR, false, 0);
  std::cout << "LLVM:\n" << llvm << "\n";
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
+16 −6
Original line number Diff line number Diff line
@@ -170,10 +170,20 @@ antlrcpp::Any qasm3_visitor::visitQuantumGateDefinition(
    }
    mlir::Value last_user = arg;
    auto users = last_user.getUsers();

    while (!users.empty()) {
      // Get the first and only user
      auto only_user = *users.begin();
      auto user_iter = users.begin();
      for (const auto &user : users) {
        // Only chase use-def chain on the top-level ops
        // i.e., direct children of this kernel (FuncOp)
        // e.g., skip ops inside a modifier region.
        if (llvm::dyn_cast_or_null<mlir::FuncOp>(user->getParentOp())) {
          break;
        }
        ++user_iter;
      }
      // Get the first and only user (quantum ops: value semantics ops or
      // modifier regions, kernel calls)
      auto only_user = *user_iter;

      // figure out which operand idx last_user 
      // corresponds to