-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathCatalystDialect.cpp
More file actions
150 lines (114 loc) · 5.33 KB
/
CatalystDialect.cpp
File metadata and controls
150 lines (114 loc) · 5.33 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
146
147
148
149
150
// Copyright 2023 Xanadu Quantum Technologies Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "Catalyst/IR/CatalystDialect.h"
#include "llvm/ADT/TypeSwitch.h" // needed for generated type parser
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/DialectImplementation.h" // needed for generated type parser
#include "mlir/Interfaces/FunctionImplementation.h"
#include "mlir/Transforms/InliningUtils.h"
#include "Catalyst/IR/CatalystOps.h"
using namespace mlir;
using namespace catalyst;
#include "Catalyst/IR/CatalystOpsDialect.cpp.inc"
#define GET_ATTRDEF_CLASSES
#include "Catalyst/IR/CatalystAttributes.cpp.inc"
//===----------------------------------------------------------------------===//
// Catalyst dialect.
//===----------------------------------------------------------------------===//
void CatalystDialect::initialize()
{
addTypes<
#define GET_TYPEDEF_LIST
#include "Catalyst/IR/CatalystOpsTypes.cpp.inc"
>();
addAttributes<
#define GET_ATTRDEF_LIST
#include "Catalyst/IR/CatalystAttributes.cpp.inc"
>();
addOperations<
#define GET_OP_LIST
#include "Catalyst/IR/CatalystOps.cpp.inc"
>();
declarePromisedInterfaces<bufferization::BufferizableOpInterface, PrintOp, CustomCallOp,
CallbackCallOp, CallbackOp>();
}
//===----------------------------------------------------------------------===//
// MemSpaceAttr
//===----------------------------------------------------------------------===//
LogicalResult MemSpaceAttr::verify(function_ref<InFlightDiagnostic()> emitError,
llvm::StringRef domain, IntegerAttr addrSpace)
{
if (domain.empty()) {
return emitError() << "catalyst.memspace: `domain` must be non-empty";
}
if (addrSpace) {
if (!addrSpace.getType().isSignlessInteger()) {
return emitError()
<< "catalyst.memspace: addr_space must be a signless integer attribute";
}
if (addrSpace.getValue().isNegative()) {
return emitError() << "catalyst.memspace: addr_space must be non-negative";
}
}
return success();
}
//===----------------------------------------------------------------------===//
// CallbackOp
//===----------------------------------------------------------------------===//
ParseResult CallbackOp::parse(OpAsmParser &parser, OperationState &result)
{
auto buildFuncType = [](Builder &builder, ArrayRef<Type> argTypes, ArrayRef<Type> results,
function_interface_impl::VariadicFlag,
std::string &) { return builder.getFunctionType(argTypes, results); };
return function_interface_impl::parseFunctionOp(
parser, result, /*allowVariadic=*/false, getFunctionTypeAttrName(result.name),
buildFuncType, getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
}
void CallbackOp::print(OpAsmPrinter &p)
{
function_interface_impl::printFunctionOp(p, *this, /*isVariadic=*/false,
getFunctionTypeAttrName(), getArgAttrsAttrName(),
getResAttrsAttrName());
}
//===----------------------------------------------------------------------===//
// CallbackCallOp
//===----------------------------------------------------------------------===//
CallInterfaceCallable CallbackCallOp::getCallableForCallee()
{
return (*this)->getAttrOfType<SymbolRefAttr>("callee");
}
void CallbackCallOp::setCalleeFromCallable(CallInterfaceCallable callee)
{
(*this)->setAttr("callee", cast<SymbolRefAttr>(callee));
}
Operation::operand_range CallbackCallOp::getArgOperands() { return getInputs(); }
MutableOperandRange CallbackCallOp::getArgOperandsMutable() { return getInputsMutable(); }
//===----------------------------------------------------------------------===//
// LaunchKernelOp
//===----------------------------------------------------------------------===//
CallInterfaceCallable LaunchKernelOp::getCallableForCallee()
{
return (*this)->getAttrOfType<SymbolRefAttr>("callee");
}
void LaunchKernelOp::setCalleeFromCallable(CallInterfaceCallable callee)
{
(*this)->setAttr("callee", cast<SymbolRefAttr>(callee));
}
Operation::operand_range LaunchKernelOp::getArgOperands() { return getInputs(); }
MutableOperandRange LaunchKernelOp::getArgOperandsMutable() { return getInputsMutable(); }
StringAttr LaunchKernelOp::getCalleeModuleName() { return getCallee().getRootReference(); }
StringAttr LaunchKernelOp::getCalleeName() { return getCallee().getLeafReference(); }
//===----------------------------------------------------------------------===//
// Catalyst type definitions.
//===----------------------------------------------------------------------===//
#define GET_TYPEDEF_CLASSES
#include "Catalyst/IR/CatalystOpsTypes.cpp.inc"