forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeephole.h
More file actions
67 lines (59 loc) · 2.89 KB
/
Peephole.h
File metadata and controls
67 lines (59 loc) · 2.89 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
/****************************************************************-*- C++ -*-****
* Copyright (c) 2022 - 2025 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. *
******************************************************************************/
#pragma once
#include "cudaq/Optimizer/CodeGen/QIRFunctionNames.h"
#include "cudaq/Optimizer/CodeGen/QIROpaqueStructTypes.h"
#include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h"
#include "llvm/ADT/StringRef.h"
#include "mlir/IR/ValueRange.h"
#include "mlir/Support/LLVM.h"
inline bool needsToBeRenamed(mlir::StringRef name) {
return name.startswith(cudaq::opt::QIRQISPrefix) &&
!name.endswith("__body") && !name.endswith("__adj") &&
!name.endswith("__ctl");
}
inline bool callToInvokeWithXCtrlOneTarget(mlir::StringRef callee,
mlir::ValueRange args) {
if ((args.size() == 4) && (callee == cudaq::opt::NVQIRInvokeWithControlBits))
if (auto addrOf = dyn_cast_or_null<mlir::LLVM::AddressOfOp>(
args[1].getDefiningOp())) {
return addrOf.getGlobalName().startswith(
std::string(cudaq::opt::QIRQISPrefix) + "x__ctl");
}
return false;
}
inline bool isIntToPtrOp(mlir::Value operand) {
return dyn_cast_or_null<mlir::LLVM::IntToPtrOp>(operand.getDefiningOp());
}
static constexpr char resultIndexName[] = "result.index";
inline mlir::Value createMeasureCall(mlir::PatternRewriter &builder,
mlir::Location loc, mlir::LLVM::CallOp op,
mlir::ValueRange args) {
auto ptrTy = cudaq::opt::getResultType(builder.getContext());
if (auto intAttr =
dyn_cast_or_null<mlir::IntegerAttr>(op->getAttr(resultIndexName))) {
auto constOp = builder.create<mlir::LLVM::ConstantOp>(loc, intAttr);
auto cast = builder.create<mlir::LLVM::IntToPtrOp>(loc, ptrTy, constOp);
builder.create<mlir::LLVM::CallOp>(
loc, mlir::TypeRange{}, cudaq::opt::QIRMeasureBody,
mlir::ArrayRef<mlir::Value>{args[0], cast});
return cast;
}
op.emitError("mz op must have an associated result index.");
return {};
}
inline mlir::Value createReadResultCall(mlir::PatternRewriter &builder,
mlir::Location loc,
mlir::Value result) {
auto i1Ty = mlir::IntegerType::get(builder.getContext(), 1);
return builder
.create<mlir::LLVM::CallOp>(loc, mlir::TypeRange{i1Ty},
cudaq::opt::QIRReadResultBody,
mlir::ArrayRef<mlir::Value>{result})
.getResult();
}