-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathQCOOps.cpp
More file actions
145 lines (116 loc) · 4.05 KB
/
QCOOps.cpp
File metadata and controls
145 lines (116 loc) · 4.05 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (c) 2023 - 2026 Chair for Design Automation, TUM
* Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH
* All rights reserved.
*
* SPDX-License-Identifier: MIT
*
* Licensed under the MIT License
*/
#include "mlir/Dialect/QCO/IR/QCODialect.h" // IWYU pragma: associated
#include "mlir/IR/OpImplementation.h"
#include "mlir/IR/Region.h"
#include "mlir/Support/LogicalResult.h"
#include "llvm/ADT/SmallVector.h"
// The following headers are needed for some template instantiations.
// IWYU pragma: begin_keep
#include <llvm/ADT/TypeSwitch.h>
#include <mlir/IR/Builders.h>
#include <mlir/IR/DialectImplementation.h>
#include <mlir/IR/PatternMatch.h>
// IWYU pragma: end_keep
using namespace mlir;
using namespace mlir::qco;
//===----------------------------------------------------------------------===//
// Custom Parsers
//===----------------------------------------------------------------------===//
namespace {
ParseResult
parseTargetAliasing(OpAsmParser& parser, Region& region,
SmallVectorImpl<OpAsmParser::UnresolvedOperand>& operands) {
// 1. Parse the opening parenthesis
if (parser.parseLParen()) {
return failure();
}
// Temporary storage for block arguments we are about to create
SmallVector<OpAsmParser::Argument> blockArgs;
// 2. Prepare to parse the list
if (failed(parser.parseOptionalRParen())) {
do {
OpAsmParser::Argument newArg; // The "new" variable name
OpAsmParser::UnresolvedOperand oldOperand; // The "old" input variable
// Parse "%new"
if (parser.parseArgument(newArg)) {
return failure();
}
// Parse "="
if (parser.parseEqual()) {
return failure();
}
// Parse "%old"
if (parser.parseOperand(oldOperand)) {
return failure();
}
operands.push_back(oldOperand);
Type type = qco::QubitType::get(parser.getBuilder().getContext());
newArg.type = type;
blockArgs.push_back(newArg);
} while (succeeded(parser.parseOptionalComma()));
if (parser.parseRParen()) {
return failure();
}
}
// 4. Parse the Region
// We explicitly pass the blockArgs we just parsed so they become the entry
// block!
if (parser.parseRegion(region, blockArgs)) {
return failure();
}
return success();
}
void printTargetAliasing(OpAsmPrinter& printer, Operation* op, Region& region,
OperandRange targets_in) {
printer << "(";
Block& entryBlock = region.front();
auto blockArgs = entryBlock.getArguments();
for (unsigned i = 0; i < targets_in.size(); ++i) {
if (i > 0) {
printer << ", ";
}
printer.printOperand(blockArgs[i]);
printer << " = ";
printer.printOperand(targets_in[i]);
}
printer << ") ";
printer.printRegion(region, /*printEntryBlockArgs=*/false);
}
} // end anonymous namespace
//===----------------------------------------------------------------------===//
// Dialect
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/QCO/IR/QCOOpsDialect.cpp.inc"
void QCODialect::initialize() {
// NOLINTNEXTLINE(clang-analyzer-core.StackAddressEscape)
addTypes<
#define GET_TYPEDEF_LIST
#include "mlir/Dialect/QCO/IR/QCOOpsTypes.cpp.inc"
>();
addOperations<
#define GET_OP_LIST
#include "mlir/Dialect/QCO/IR/QCOOps.cpp.inc"
>();
}
//===----------------------------------------------------------------------===//
// Types
//===----------------------------------------------------------------------===//
#define GET_TYPEDEF_CLASSES
#include "mlir/Dialect/QCO/IR/QCOOpsTypes.cpp.inc"
//===----------------------------------------------------------------------===//
// Interfaces
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/QCO/IR/QCOInterfaces.cpp.inc"
//===----------------------------------------------------------------------===//
// Operations
//===----------------------------------------------------------------------===//
#define GET_OP_CLASSES
#include "mlir/Dialect/QCO/IR/QCOOps.cpp.inc"