forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateRegisterNames.cpp
More file actions
68 lines (60 loc) · 2.65 KB
/
Copy pathUpdateRegisterNames.cpp
File metadata and controls
68 lines (60 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*******************************************************************************
* Copyright (c) 2022 - 2026 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/
#include "LoopAnalysis.h"
#include "PassDetails.h"
#include "cudaq/Optimizer/Transforms/Passes.h"
#include "mlir/IR/Dominance.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Transforms/Passes.h"
#include "mlir/Transforms/RegionUtils.h"
namespace cudaq::opt {
#define GEN_PASS_DEF_UPDATEREGISTERNAMES
#include "cudaq/Optimizer/Transforms/Passes.h.inc"
} // namespace cudaq::opt
#define DEBUG_TYPE "update-register-names"
using namespace mlir;
namespace {
/// After unrolling the loops, there may be duplicate registerName attributes in
/// use. This pass will assign them unique names by appending a counter.
class UpdateRegisterNamesPass
: public cudaq::opt::impl::UpdateRegisterNamesBase<
UpdateRegisterNamesPass> {
public:
using UpdateRegisterNamesBase::UpdateRegisterNamesBase;
void runOnOperation() override {
auto *mod = getOperation();
// First save the op's that contain a registerName attribute
DenseMap<StringRef, SmallVector<Operation *>> regOps;
mod->walk([&](mlir::Operation *walkOp) {
if (auto prevAttr = walkOp->getAttr("registerName")) {
auto registerName = cast<StringAttr>(prevAttr).getValue();
regOps[registerName].push_back(walkOp);
}
return WalkResult::advance();
});
// Now apply new labels, appending a counter if necessary
for (auto &[registerName, opVec] : regOps) {
if (opVec.size() == 1)
continue; // don't rename individual qubit measurements
auto strLen = std::to_string(opVec.size()).size();
int bit = 0;
for (auto ®Op : opVec)
if (auto prevAttr = regOp->getAttr("registerName")) {
auto suffix = std::to_string(bit++);
if (suffix.size() < strLen)
suffix = std::string(strLen - suffix.size(), '0') + suffix;
// Note Quantinuum can't support a ":" delimiter, so use '%'
auto newAttr = OpBuilder(&getContext())
.getStringAttr(registerName + "%" + suffix);
regOp->setAttr("registerName", newAttr);
}
}
}
};
} // namespace