diff --git a/mlir/include/mlir/Analysis/AffineStructuresParser.h b/mlir/include/mlir/Analysis/AffineStructuresParser.h new file mode 100644 --- /dev/null +++ b/mlir/include/mlir/Analysis/AffineStructuresParser.h @@ -0,0 +1,32 @@ +//===- AffineStructuresParser.h - MLIR Parser Library Interface -*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This header file defines an interface to parse AffineStructures. +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_ANALYSIS_AFFINE_STRUCTURES_PARSER_H +#define MLIR_ANALYSIS_AFFINE_STRUCTURES_PARSER_H + +#include "mlir/Analysis/AffineStructures.h" + +namespace llvm { +class StringRef; +} // end namespace llvm + +namespace mlir { + +/// This parses a single IntegerSet to an MLIR context and transforms it to +/// FlatAffineConstraints if it was valid. If not, a failure is returned. If the +/// passed `str` has additional tokens that were not part of the IntegerSet, a +/// failure is returned. +FailureOr +parseFlatAffineConstraints(llvm::StringRef, MLIRContext *context); +} // namespace mlir + +#endif // MLIR_ANALYSIS_AFFINE_STRUCTURES_PARSER_H diff --git a/mlir/include/mlir/Parser.h b/mlir/include/mlir/Parser.h --- a/mlir/include/mlir/Parser.h +++ b/mlir/include/mlir/Parser.h @@ -13,7 +13,6 @@ #ifndef MLIR_PARSER_H #define MLIR_PARSER_H -#include "mlir/Analysis/AffineStructures.h" #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinOps.h" #include @@ -258,12 +257,12 @@ /// returned in `numRead`. Type parseType(llvm::StringRef typeStr, MLIRContext *context, size_t &numRead); -/// This parses a single IntegerSet to an MLIR context and transforms it to -/// FlatAffineConstraints if it was valid. If not, a failure is returned. If the -/// passed `str` has additional tokens that were not part of the IntegerSet, a -/// failure is returned. -FailureOr -parseFlatAffineConstraints(llvm::StringRef str, MLIRContext *context); +/// This parses a single IntegerSet to an MLIR context if it was valid. If not, +/// an error message is emitted through a new SourceMgrDiagnosticHandler +/// constructed from a new SourceMgr with a single MemoryBuffer wrapping +/// `str`. If the passed `str` has additional tokens that were not part of the +/// IntegerSet, a failure is returned. +IntegerSet parseIntegerSet(llvm::StringRef str, MLIRContext *context); } // end namespace mlir diff --git a/mlir/lib/Analysis/AffineStructuresParser.cpp b/mlir/lib/Analysis/AffineStructuresParser.cpp new file mode 100644 --- /dev/null +++ b/mlir/lib/Analysis/AffineStructuresParser.cpp @@ -0,0 +1,27 @@ +//===- AffineStructuresParser.cpp - MLIR Affine Parser --------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements parsiing functionality for AffineStructures. +// +//===----------------------------------------------------------------------===// + +#include "mlir/Analysis/AffineStructuresParser.h" +#include "mlir/IR/IntegerSet.h" +#include "mlir/Parser.h" + +using namespace mlir; + +FailureOr +mlir::parseFlatAffineConstraints(llvm::StringRef str, MLIRContext *context) { + IntegerSet set = parseIntegerSet(str, context); + + if (!set) + return failure(); + + return FlatAffineConstraints(set); +} diff --git a/mlir/lib/Analysis/CMakeLists.txt b/mlir/lib/Analysis/CMakeLists.txt --- a/mlir/lib/Analysis/CMakeLists.txt +++ b/mlir/lib/Analysis/CMakeLists.txt @@ -2,6 +2,7 @@ AliasAnalysis.cpp AffineAnalysis.cpp AffineStructures.cpp + AffineStructuresParser.cpp BufferViewFlowAnalysis.cpp CallGraph.cpp DataFlowAnalysis.cpp @@ -49,6 +50,7 @@ add_mlir_library(MLIRLoopAnalysis AffineAnalysis.cpp AffineStructures.cpp + AffineStructuresParser.cpp LinearTransform.cpp LoopAnalysis.cpp NestedMatcher.cpp diff --git a/mlir/lib/Parser/AffineParser.cpp b/mlir/lib/Parser/AffineParser.cpp --- a/mlir/lib/Parser/AffineParser.cpp +++ b/mlir/lib/Parser/AffineParser.cpp @@ -61,7 +61,6 @@ ParseResult parseAffineExprOfSSAIds(AffineExpr &expr); void getDimsAndSymbolSSAIds(SmallVectorImpl &dimAndSymbolSSAIds, unsigned &numDims); - ParseResult parseFlatAffineConstraints(FlatAffineConstraints &fac); private: // Binary affine op parsing. @@ -675,15 +674,6 @@ return IntegerSet::get(numDims, numSymbols, constraints, isEqs); } -ParseResult -AffineParser::parseFlatAffineConstraints(FlatAffineConstraints &fac) { - IntegerSet set; - if (parseIntegerSetReference(set)) - return failure(); - fac = FlatAffineConstraints(set); - return success(); -} - //===----------------------------------------------------------------------===// // Parser //===----------------------------------------------------------------------===// @@ -731,9 +721,7 @@ .parseAffineExprOfSSAIds(expr); } -FailureOr -mlir::parseFlatAffineConstraints(StringRef inputStr, MLIRContext *context) { - +IntegerSet mlir::parseIntegerSet(StringRef inputStr, MLIRContext *context) { llvm::SourceMgr sourceMgr; auto memBuffer = llvm::MemoryBuffer::getMemBuffer( inputStr, /*BufferName=*/"", @@ -741,16 +729,17 @@ sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc()); SymbolState symbolState; ParserState state(sourceMgr, context, symbolState, /*asmState=*/nullptr); - AffineParser parser(state); - FlatAffineConstraints fac; - if (parser.parseFlatAffineConstraints(fac)) - return failure(); + Parser parser(state); + SourceMgrDiagnosticHandler handler(sourceMgr, context); + IntegerSet set; + if (parser.parseIntegerSetReference(set)) + return IntegerSet(); Token endTok = parser.getToken(); if (endTok.isNot(Token::eof)) { parser.emitError(endTok.getLoc(), "encountered unexpected token"); - return failure(); + return IntegerSet(); } - return fac; + return set; } diff --git a/mlir/unittests/Analysis/AffineStructuresParserTest.cpp b/mlir/unittests/Analysis/AffineStructuresParserTest.cpp --- a/mlir/unittests/Analysis/AffineStructuresParserTest.cpp +++ b/mlir/unittests/Analysis/AffineStructuresParserTest.cpp @@ -1,5 +1,5 @@ +#include "mlir/Analysis/AffineStructuresParser.h" #include "mlir/Analysis/PresburgerSet.h" -#include "mlir/Parser.h" #include @@ -57,11 +57,8 @@ "that do not fit into int64_t"; } -static bool facEquality(FlatAffineConstraints &fac1, - FlatAffineConstraints &fac2) { - return PresburgerSet(fac1).isEqual(PresburgerSet(fac2)); -} - +/// Parses and compares the `str` to the `ex`. The equality check is performed +/// by using PresburgerSet::isEqual static bool parseAndCompare(StringRef str, FlatAffineConstraints ex) { MLIRContext context; FailureOr fac = @@ -69,7 +66,7 @@ EXPECT_TRUE(succeeded(fac)); - return facEquality(ex, *fac); + return PresburgerSet(*fac).isEqual(PresburgerSet(ex)); } TEST(ParseFACTest, ParseAndCompareTest) { diff --git a/mlir/unittests/Analysis/AffineStructuresTest.cpp b/mlir/unittests/Analysis/AffineStructuresTest.cpp --- a/mlir/unittests/Analysis/AffineStructuresTest.cpp +++ b/mlir/unittests/Analysis/AffineStructuresTest.cpp @@ -7,9 +7,9 @@ //===----------------------------------------------------------------------===// #include "mlir/Analysis/AffineStructures.h" +#include "mlir/Analysis/AffineStructuresParser.h" #include "mlir/IR/IntegerSet.h" #include "mlir/IR/MLIRContext.h" -#include "mlir/Parser.h" #include #include @@ -99,6 +99,9 @@ } while (std::next_permutation(perm.begin(), perm.end())); } +/// Parses a FlatAffineConstraints from a StringRef. It is expected that the +/// string represents a valid IntegerSet, otherwise it will violate a gtest +/// assertion. static FlatAffineConstraints parseFAC(StringRef str) { MLIRContext context; FailureOr fac = diff --git a/mlir/unittests/Analysis/CMakeLists.txt b/mlir/unittests/Analysis/CMakeLists.txt --- a/mlir/unittests/Analysis/CMakeLists.txt +++ b/mlir/unittests/Analysis/CMakeLists.txt @@ -1,8 +1,8 @@ add_mlir_unittest(MLIRAnalysisTests AffineStructuresTest.cpp + AffineStructuresParserTest.cpp LinearTransformTest.cpp PresburgerSetTest.cpp - AffineStructuresParserTest.cpp ) target_link_libraries(MLIRAnalysisTests