Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 1 | //===- 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" |
| 19 | #include "llvm/ADT/StringMap.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | using namespace mlir; |
| 22 | using llvm::StringMap; |
| 23 | |
| 24 | static StringMap<AbstractOperation> &getImpl(void *pImpl) { |
| 25 | return *static_cast<StringMap<AbstractOperation> *>(pImpl); |
| 26 | } |
| 27 | |
| 28 | OperationSet::OperationSet() { |
| 29 | pImpl = new StringMap<AbstractOperation>(); |
| 30 | } |
| 31 | |
| 32 | OperationSet::~OperationSet() { |
| 33 | delete &getImpl(pImpl); |
| 34 | } |
| 35 | |
| 36 | void OperationSet::addOperation(StringRef prefix, AbstractOperation opInfo) { |
| 37 | assert(opInfo.name.startswith(prefix) && "op name doesn't start with prefix"); |
| 38 | |
| 39 | if (!getImpl(pImpl).insert({opInfo.name, opInfo}).second) { |
| 40 | llvm::errs() << "error: ops named '" << opInfo.name |
| 41 | << "' is already registered.\n"; |
| 42 | abort(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// Look up the specified operation in the operation set and return a pointer |
| 47 | /// to it if present. Otherwise, return a null pointer. |
| 48 | const AbstractOperation *OperationSet::lookup(StringRef opName) const { |
| 49 | auto &map = getImpl(pImpl); |
| 50 | auto it = map.find(opName); |
| 51 | if (it != map.end()) |
| 52 | return &it->second; |
| 53 | return nullptr; |
| 54 | } |