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 | dd0c2ca | 2018-07-24 16:07:22 -0700 | [diff] [blame] | 19 | #include "mlir/IR/OpDefinition.h" |
| 20 | #include "mlir/IR/OpImplementation.h" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 21 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 22 | #include "llvm/ADT/Twine.h" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | using namespace mlir; |
| 25 | using llvm::StringMap; |
| 26 | |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 27 | OpAsmParser::~OpAsmParser() {} |
| 28 | |
| 29 | // The fallback for the printer is to reject the short form. |
Chris Lattner | c87fcb7 | 2018-07-30 08:48:18 -0700 | [diff] [blame] | 30 | OpAsmParserResult OpBaseState::parse(OpAsmParser *parser) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 31 | parser->emitError(parser->getNameLoc(), "has no concise form"); |
| 32 | return {}; |
| 33 | } |
| 34 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 35 | // The fallback for the printer is to print it the longhand form. |
Chris Lattner | c87fcb7 | 2018-07-30 08:48:18 -0700 | [diff] [blame] | 36 | void OpBaseState::print(OpAsmPrinter *p) const { |
Chris Lattner | dd0c2ca | 2018-07-24 16:07:22 -0700 | [diff] [blame] | 37 | p->printDefaultOp(getOperation()); |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 40 | static StringMap<AbstractOperation> &getImpl(void *pImpl) { |
| 41 | return *static_cast<StringMap<AbstractOperation> *>(pImpl); |
| 42 | } |
| 43 | |
| 44 | OperationSet::OperationSet() { |
| 45 | pImpl = new StringMap<AbstractOperation>(); |
| 46 | } |
| 47 | |
| 48 | OperationSet::~OperationSet() { |
| 49 | delete &getImpl(pImpl); |
| 50 | } |
| 51 | |
| 52 | void OperationSet::addOperation(StringRef prefix, AbstractOperation opInfo) { |
| 53 | assert(opInfo.name.startswith(prefix) && "op name doesn't start with prefix"); |
| 54 | |
| 55 | if (!getImpl(pImpl).insert({opInfo.name, opInfo}).second) { |
| 56 | llvm::errs() << "error: ops named '" << opInfo.name |
| 57 | << "' is already registered.\n"; |
| 58 | abort(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /// Look up the specified operation in the operation set and return a pointer |
| 63 | /// to it if present. Otherwise, return a null pointer. |
| 64 | const AbstractOperation *OperationSet::lookup(StringRef opName) const { |
| 65 | auto &map = getImpl(pImpl); |
| 66 | auto it = map.find(opName); |
| 67 | if (it != map.end()) |
| 68 | return &it->second; |
| 69 | return nullptr; |
| 70 | } |