diff --git a/mlir/include/mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h b/mlir/include/mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h --- a/mlir/include/mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h +++ b/mlir/include/mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h @@ -29,6 +29,9 @@ /// Otherwise, a pass failure is triggered. bool allowReturnAllocs = false; + /// Specifies whether the tensor IR should be annotated with alias sets. + bool dumpAliasSets = false; + /// The heuristic controls the order in which ops are traversed during the /// analysis. AnalysisHeuristic analysisHeuristic = AnalysisHeuristic::BottomUp; diff --git a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td --- a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td +++ b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td @@ -297,6 +297,8 @@ "core bufferization passes.">, ListOption<"dialectFilter", "dialect-filter", "std::string", "Restrict bufferization to ops from these dialects.">, + Option<"dumpAliasSets", "dump-alias-sets", "bool", /*default=*/"false", + "Test only: Annotate tensor IR with alias sets">, ListOption<"noAnalysisFuncFilter", "no-analysis-func-filter", "std::string", "Skip analysis of functions with these symbol names." "Set copyBeforeWrite to true when bufferizing them.">, diff --git a/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp b/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp --- a/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp +++ b/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp @@ -208,6 +208,7 @@ opt.analysisHeuristic = parseHeuristicOption(analysisHeuristic); opt.copyBeforeWrite = copyBeforeWrite; opt.createDeallocs = createDeallocs; + opt.dumpAliasSets = dumpAliasSets; opt.functionBoundaryTypeConversion = parseLayoutMapOption(functionBoundaryTypeConversion); if (mustInferMemorySpace) diff --git a/mlir/lib/Dialect/Bufferization/Transforms/OneShotAnalysis.cpp b/mlir/lib/Dialect/Bufferization/Transforms/OneShotAnalysis.cpp --- a/mlir/lib/Dialect/Bufferization/Transforms/OneShotAnalysis.cpp +++ b/mlir/lib/Dialect/Bufferization/Transforms/OneShotAnalysis.cpp @@ -79,6 +79,8 @@ /// Attribute marker to specify op operands that bufferize in-place. constexpr StringLiteral kInPlaceOperandsAttrName = "__inplace_operands_attr__"; +constexpr StringLiteral kAliasSetAttrName = "__alias_set_attr__"; + /// Mark whether OpOperand will be bufferized inplace. static void setInPlaceOpOperand(OpOperand &opOperand, bool inPlace) { Operation *op = opOperand.getOwner(); @@ -979,6 +981,29 @@ }); } +static void annotateOpsWithAliasSets(Operation *op, + const OneShotAnalysisState &state) { + AsmState asmState(op->getParentOfType()); + Builder b(op->getContext()); + op->walk([&](Operation *op) { + SmallVector aliasSets; + for (OpResult opResult : op->getOpResults()) { + if (opResult.getType().isa()) { + SmallVector aliases; + state.applyOnAliases(opResult, [&](Value alias) { + std::string buffer; + llvm::raw_string_ostream stream(buffer); + alias.printAsOperand(stream, asmState); + aliases.push_back(b.getStringAttr(stream.str())); + }); + aliasSets.push_back(b.getArrayAttr(aliases)); + } + } + if (!aliasSets.empty()) + op->setAttr(kAliasSetAttrName, b.getArrayAttr(aliasSets)); + }); +} + /// Assert that every allocation can be deallocated in the same block. I.e., /// every value that is returned or yielded from a block is: /// * guaranteed to be aliasing a bbArg of that block or a parent block, or @@ -1095,6 +1120,8 @@ // Annotate operations if we only want to report the analysis. if (options.testAnalysisOnly) annotateOpsWithBufferizationMarkers(op, state); + if (options.dumpAliasSets) + annotateOpsWithAliasSets(op, state); return success(!failedAnalysis); }