blob: 98bf5fef51eb8308d2aaae3ce13ef5e6c88cd719 [file] [log] [blame]
Chris Lattnerff0d5902018-07-05 09:12:11 -07001//===- OperationSet.cpp - OperationSet implementation ---------------------===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17
18#include "mlir/IR/OperationSet.h"
Chris Lattnerdd0c2ca2018-07-24 16:07:22 -070019#include "mlir/IR/OpDefinition.h"
20#include "mlir/IR/OpImplementation.h"
Chris Lattnerff0d5902018-07-05 09:12:11 -070021#include "llvm/ADT/StringMap.h"
Chris Lattner85ee1512018-07-25 11:15:20 -070022#include "llvm/ADT/Twine.h"
Chris Lattnerff0d5902018-07-05 09:12:11 -070023#include "llvm/Support/raw_ostream.h"
24using namespace mlir;
25using llvm::StringMap;
26
Chris Lattner85ee1512018-07-25 11:15:20 -070027OpAsmParser::~OpAsmParser() {}
28
29// The fallback for the printer is to reject the short form.
Chris Lattnereed6c4d2018-08-07 09:12:35 -070030bool OpBaseState::parse(OpAsmParser *parser, OperationState *result) {
31 return parser->emitError(parser->getNameLoc(), "has no concise form");
Chris Lattner85ee1512018-07-25 11:15:20 -070032}
33
Chris Lattner9361fb32018-07-24 08:34:58 -070034// The fallback for the printer is to print it the longhand form.
Chris Lattnerc87fcb72018-07-30 08:48:18 -070035void OpBaseState::print(OpAsmPrinter *p) const {
Chris Lattnerdd0c2ca2018-07-24 16:07:22 -070036 p->printDefaultOp(getOperation());
Chris Lattner9361fb32018-07-24 08:34:58 -070037}
38
Chris Lattnerff0d5902018-07-05 09:12:11 -070039static StringMap<AbstractOperation> &getImpl(void *pImpl) {
40 return *static_cast<StringMap<AbstractOperation> *>(pImpl);
41}
42
43OperationSet::OperationSet() {
44 pImpl = new StringMap<AbstractOperation>();
45}
46
47OperationSet::~OperationSet() {
48 delete &getImpl(pImpl);
49}
50
51void OperationSet::addOperation(StringRef prefix, AbstractOperation opInfo) {
52 assert(opInfo.name.startswith(prefix) && "op name doesn't start with prefix");
53
54 if (!getImpl(pImpl).insert({opInfo.name, opInfo}).second) {
55 llvm::errs() << "error: ops named '" << opInfo.name
56 << "' is already registered.\n";
57 abort();
58 }
59}
60
61/// Look up the specified operation in the operation set and return a pointer
62/// to it if present. Otherwise, return a null pointer.
63const AbstractOperation *OperationSet::lookup(StringRef opName) const {
64 auto &map = getImpl(pImpl);
65 auto it = map.find(opName);
66 if (it != map.end())
67 return &it->second;
68 return nullptr;
69}