github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/test/lib/TestDialect/TestDialect.cpp (about) 1 //===- TestDialect.cpp - MLIR Dialect for Testing -------------------------===// 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 "TestDialect.h" 19 #include "mlir/IR/PatternMatch.h" 20 #include "mlir/IR/TypeUtilities.h" 21 22 using namespace mlir; 23 24 //===----------------------------------------------------------------------===// 25 // TestDialect 26 //===----------------------------------------------------------------------===// 27 28 TestDialect::TestDialect(MLIRContext *context) 29 : Dialect(getDialectName(), context) { 30 addOperations< 31 #define GET_OP_LIST 32 #include "TestOps.cpp.inc" 33 >(); 34 allowUnknownOperations(); 35 } 36 37 //===----------------------------------------------------------------------===// 38 // Test IsolatedRegionOp - parse passthrough region arguments. 39 //===----------------------------------------------------------------------===// 40 41 static ParseResult parseIsolatedRegionOp(OpAsmParser *parser, 42 OperationState *result) { 43 OpAsmParser::OperandType argInfo; 44 Type argType = parser->getBuilder().getIndexType(); 45 46 // Parse the input operand. 47 if (parser->parseOperand(argInfo) || 48 parser->resolveOperand(argInfo, argType, result->operands)) 49 return failure(); 50 51 // Parse the body region, and reuse the operand info as the argument info. 52 Region *body = result->addRegion(); 53 return parser->parseRegion(*body, argInfo, argType, 54 /*enableNameShadowing=*/true); 55 } 56 57 static void print(OpAsmPrinter *p, IsolatedRegionOp op) { 58 *p << "test.isolated_region "; 59 p->printOperand(op.getOperand()); 60 p->shadowRegionArgs(op.region(), op.getOperand()); 61 p->printRegion(op.region(), /*printEntryBlockArgs=*/false); 62 } 63 64 //===----------------------------------------------------------------------===// 65 // Test PolyForOp - parse list of region arguments. 66 //===----------------------------------------------------------------------===// 67 static ParseResult parsePolyForOp(OpAsmParser *parser, OperationState *result) { 68 SmallVector<OpAsmParser::OperandType, 4> ivsInfo; 69 // Parse list of region arguments without a delimiter. 70 if (parser->parseRegionArgumentList(ivsInfo)) 71 return failure(); 72 73 // Parse the body region. 74 Region *body = result->addRegion(); 75 auto &builder = parser->getBuilder(); 76 SmallVector<Type, 4> argTypes(ivsInfo.size(), builder.getIndexType()); 77 return parser->parseRegion(*body, ivsInfo, argTypes); 78 } 79 80 //===----------------------------------------------------------------------===// 81 // Test removing op with inner ops. 82 //===----------------------------------------------------------------------===// 83 84 namespace { 85 struct TestRemoveOpWithInnerOps 86 : public OpRewritePattern<TestOpWithRegionPattern> { 87 using OpRewritePattern<TestOpWithRegionPattern>::OpRewritePattern; 88 89 PatternMatchResult matchAndRewrite(TestOpWithRegionPattern op, 90 PatternRewriter &rewriter) const override { 91 rewriter.replaceOp(op, llvm::None); 92 return matchSuccess(); 93 } 94 }; 95 } // end anonymous namespace 96 97 void TestOpWithRegionPattern::getCanonicalizationPatterns( 98 OwningRewritePatternList &results, MLIRContext *context) { 99 results.insert<TestRemoveOpWithInnerOps>(context); 100 } 101 102 OpFoldResult TestOpWithRegionFold::fold(ArrayRef<Attribute> operands) { 103 return operand(); 104 } 105 106 // Static initialization for Test dialect registration. 107 static mlir::DialectRegistration<mlir::TestDialect> testDialect; 108 109 #define GET_OP_CLASSES 110 #include "TestOps.cpp.inc"