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