Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Circuits Performance Improvement #291

Merged
merged 16 commits into from
Mar 13, 2024
3 changes: 2 additions & 1 deletion include/Dialect/QUIR/Transforms/ExtractCircuits.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "Dialect/QUIR/IR/QUIROps.h"

#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Pass/Pass.h"

Expand Down Expand Up @@ -60,8 +61,8 @@ struct ExtractCircuitsPass
llvm::SmallVector<Type> outputTypes;
llvm::SmallVector<Value> outputValues;
std::vector<int> phyiscalIds;
std::unordered_map<uint32_t, int> argToId;

std::unordered_map<uint32_t, BlockArgument> circuitArguments;
std::unordered_map<Operation *, uint32_t> circuitOperands;
llvm::SmallVector<OpResult> originalResults;

Expand Down
31 changes: 17 additions & 14 deletions lib/Dialect/QUIR/Transforms/ExtractCircuits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ OpBuilder ExtractCircuitsPass::startCircuit(Location location,
outputTypes.clear();
outputValues.clear();
originalResults.clear();
circuitArguments.clear();
circuitOperands.clear();
phyiscalIds.clear();
argToId.clear();

std::string const circuitName = "circuit_";
std::string newName = circuitName + std::to_string(circuitCount++);
Expand Down Expand Up @@ -132,19 +132,14 @@ void ExtractCircuitsPass::addToCircuit(
if (search == circuitOperands.end()) {
argumentIndex = inputValues.size();
inputValues.push_back(operand);
inputTypes.push_back(operand.getType());
circuitOperands[defOp] = argumentIndex;

currentCircuitOp.insertArgument(argumentIndex, operand.getType(), {},
currentOp->getLoc());
currentCircuitOp.getBody().addArgument(operand.getType(),
currentOp->getLoc());
if (isa<quir::DeclareQubitOp>(defOp)) {
auto physicalId = defOp->getAttrOfType<IntegerAttr>("id");
phyiscalIds.push_back(physicalId.getInt());
currentCircuitOp.setArgAttrs(
argumentIndex,
ArrayRef({NamedAttribute(
StringAttr::get(&getContext(),
mlir::quir::getPhysicalIdAttrName()),
physicalId)}));
auto id = defOp->getAttrOfType<IntegerAttr>("id").getInt();
phyiscalIds.push_back(id);
argToId[argumentIndex] = id;
}
} else {
argumentIndex = search->second;
Expand Down Expand Up @@ -175,17 +170,25 @@ void ExtractCircuitsPass::endCircuit(
// change the input / output types for the quir.circuit
auto opType = currentCircuitOp.getFunctionType();
currentCircuitOp.setType(topLevelBuilder.getFunctionType(
/*inputs=*/opType.getInputs(),
/*inputs=*/ArrayRef<Type>(inputTypes),
/*results=*/ArrayRef<Type>(outputTypes)));

for (const auto &[key, value] : argToId)
currentCircuitOp.setArgAttrs(
key,
ArrayRef({NamedAttribute(
StringAttr::get(&getContext(), mlir::quir::getPhysicalIdAttrName()),
topLevelBuilder.getI32IntegerAttr(value))}));

std::sort(phyiscalIds.begin(), phyiscalIds.end());
currentCircuitOp->setAttr(
mlir::quir::getPhysicalIdsAttrName(),
topLevelBuilder.getI32ArrayAttr(ArrayRef<int>(phyiscalIds)));

// insert call_circuit
// NOLINTNEXTLINE(misc-const-correctness)
OpBuilder builder(firstOp);
OpBuilder builder(lastOp);
builder.setInsertionPointAfter(lastOp);
newCallCircuitOp = builder.create<mlir::quir::CallCircuitOp>(
currentCircuitOp->getLoc(), currentCircuitOp.getSymName(),
TypeRange(outputTypes), ValueRange(inputValues));
Expand Down
Loading