Skip to content

Commit

Permalink
Fix the QIR base profile lowering to handle single qubit allocations and
Browse files Browse the repository at this point in the history
multiple allocations.

This does not perform any live range analysis and assumes that all
qubits for the kernel are allocated, logically, "in parallel" making
them all distinct and assigning each a unique integer.

These changes harden the QIR base profile generation pass, making it
correct even when other preconditioning passes are absent.

Add test.

Remove singleton deallocs. Handle singleton allocations.
  • Loading branch information
schweitzpgi committed Jul 25, 2023
1 parent 9e11972 commit cb39942
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 154 deletions.
12 changes: 0 additions & 12 deletions include/cudaq/Optimizer/CodeGen/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,6 @@ def QIRToBaseQIR : Pass<"qir-to-base-qir"> {
}];

let constructor = "cudaq::opt::createQIRToBaseProfilePass()";

let options = [
Option<"topDownProcessingEnabled", "top-down", "bool",
/*default=*/"true",
"Seed the worklist in general top-down order">,
Option<"enableRegionSimplification", "region-simplify", "bool",
/*default=*/"true",
"Seed the worklist in general top-down order">,
Option<"maxIterations", "max-iterations", "int64_t",
/*default=*/"10",
"Seed the worklist in general top-down order">
];
}

#endif // CUDAQ_OPT_OPTIMIZER_CODEGEN_PASSES
24 changes: 5 additions & 19 deletions include/cudaq/Optimizer/CodeGen/Peephole.td
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,6 @@ def CalleeConv : Pat<
def IsArrayGetElementPtrId : Constraint<CPred<
"$0.getValue().str() == cudaq::opt::QIRArrayGetElementPtr1d">>;

def CreateIntToPtrOp : NativeCodeCall<
"$_builder.create<mlir::LLVM::IntToPtrOp>($_loc, $0.getType(), $1[1]);">;

// %5 = ?
// %6 = call @QIRArrayGetElementPtr %2, %5
// %7 = bitcast %6
// %8 = load %7, ...
// ───────────────────────────────────────
// %8 = inttoptr %5
def ArrayGetElementPtrConv : Pat<
(LLVM_LoadOp:$load (LLVM_BitcastOp (LLVM_CallOp $callee, $args, $_, $_)), $_,
$_, $_, $_, $_, $_),
(CreateIntToPtrOp $load, $args), [(IsArrayGetElementPtrId $callee)]>;

//===----------------------------------------------------------------------===//

def EraseArrayGEPOp : NativeCodeCall<
"$_builder.create<mlir::LLVM::UndefOp>($_loc,"
" cudaq::opt::getQubitType($_builder.getContext()))">;
Expand All @@ -111,19 +95,21 @@ def EraseArrayAllocateOp : NativeCodeCall<
//
// %0 = call @allocate ... : ... -> T*
// ───────────────────────────────────
// %0 = inttoptr ~0 : i64 -> T*
// %0 = undef : T*
def EraseArrayAlloc : Pat<
(LLVM_CallOp $callee, $_, $_, $_), (EraseArrayAllocateOp),
[(IsaAllocateCall $callee)]>;

//===----------------------------------------------------------------------===//

def IsaReleaseCall : Constraint<CPred<
"$0.getValue().str() == cudaq::opt::QIRArrayQubitReleaseArray">>;
"$0.getValue().str() == cudaq::opt::QIRArrayQubitReleaseArray || "
"$0.getValue().str() == cudaq::opt::QIRArrayQubitReleaseQubit">>;

def EraseArrayReleaseOp : NativeCodeCall<"static_cast<std::size_t>(0)">;

// Remove the release calls.
// Remove the release calls. This removes both array allocations as well as
// qubit singletons.
//
// call @release %5 : (!Qubit) -> ()
// ─────────────────────────────────
Expand Down
Loading

0 comments on commit cb39942

Please sign in to comment.