[Mlir-commits] [mlir] 5b98e4e - [mlir][linalg][bufferize] Add analysis fuzzer option
Matthias Springer
llvmlistbot at llvm.org
Wed Oct 27 01:38:03 PDT 2021
Author: Matthias Springer
Date: 2021-10-27T17:37:56+09:00
New Revision: 5b98e4ed163b3712fc96a995717129256562f8df
URL: https://github.com/llvm/llvm-project/commit/5b98e4ed163b3712fc96a995717129256562f8df
DIFF: https://github.com/llvm/llvm-project/commit/5b98e4ed163b3712fc96a995717129256562f8df.diff
LOG: [mlir][linalg][bufferize] Add analysis fuzzer option
Analyze ops in a pseudo-random order to see if any assertions are triggered. Randomizing the order of analysis likely worsens the quality of the bufferization result (more out-of-place bufferizations). However, assertions should never fail, as that would indicate a problem with our implementation.
Differential Revision: https://reviews.llvm.org/D112581
Added:
Modified:
mlir/include/mlir/Dialect/Linalg/Passes.td
mlir/include/mlir/Dialect/Linalg/Transforms/ComprehensiveBufferize.h
mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferize.cpp
mlir/test/Dialect/Linalg/comprehensive-module-bufferize-analysis.mlir
mlir/test/Dialect/Linalg/comprehensive-module-bufferize.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/Linalg/Passes.td b/mlir/include/mlir/Dialect/Linalg/Passes.td
index d75437ed6077b..2322de36be989 100644
--- a/mlir/include/mlir/Dialect/Linalg/Passes.td
+++ b/mlir/include/mlir/Dialect/Linalg/Passes.td
@@ -43,7 +43,10 @@ def LinalgComprehensiveModuleBufferize :
"Allows the return of memrefs (for testing purposes only)">,
Option<"useAlloca", "use-alloca", "bool",
/*default=*/"false",
- "Use stack allocations for memrefs (for testing purposes only)">
+ "Use stack allocations for memrefs (for testing purposes only)">,
+ Option<"analysisFuzzerSeed", "analysis-fuzzer-seed", "unsigned",
+ /*default=*/"0",
+ "Analyze ops in random order with a given seed (fuzzer)">
];
let constructor = "mlir::createLinalgComprehensiveModuleBufferizePass()";
}
diff --git a/mlir/include/mlir/Dialect/Linalg/Transforms/ComprehensiveBufferize.h b/mlir/include/mlir/Dialect/Linalg/Transforms/ComprehensiveBufferize.h
index da55055049999..5baec15446df6 100644
--- a/mlir/include/mlir/Dialect/Linalg/Transforms/ComprehensiveBufferize.h
+++ b/mlir/include/mlir/Dialect/Linalg/Transforms/ComprehensiveBufferize.h
@@ -173,7 +173,8 @@ class BufferizationAliasInfo {
/// Analyze the `ops` to determine which OpResults are inplaceable.
LogicalResult inPlaceAnalysis(SmallVector<Operation *> &ops,
BufferizationAliasInfo &aliasInfo,
- const DominanceInfo &domInfo);
+ const DominanceInfo &domInfo,
+ unsigned analysisFuzzerSeed = 0);
/// Default allocation function that is used by the comprehensive bufferization
/// pass. The default currently creates a ranked memref using `memref.alloc`.
diff --git a/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferize.cpp b/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferize.cpp
index 3d60c70137668..4730ff86ab623 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferize.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferize.cpp
@@ -107,6 +107,8 @@
#include "mlir/Dialect/Linalg/Transforms/ComprehensiveBufferize.h"
+#include <random>
+
#include "PassDetail.h"
#include "mlir/Dialect/Linalg/IR/LinalgOps.h"
#include "mlir/Dialect/Linalg/Passes.h"
@@ -2359,7 +2361,16 @@ bufferizableInPlaceAnalysis(OpOperand &operand,
/// ExtractSliceOps are interleaved with other ops in traversal order.
LogicalResult mlir::linalg::inPlaceAnalysis(SmallVector<Operation *> &ops,
BufferizationAliasInfo &aliasInfo,
- const DominanceInfo &domInfo) {
+ const DominanceInfo &domInfo,
+ unsigned analysisFuzzerSeed) {
+ if (analysisFuzzerSeed) {
+ // This is a fuzzer. For testing purposes only. Randomize the order in which
+ // operations are analyzed. The bufferization quality is likely worse, but
+ // we want to make sure that no assertions are triggered anywhere.
+ std::mt19937 g(analysisFuzzerSeed);
+ llvm::shuffle(ops.begin(), ops.end(), g);
+ }
+
// Walk ops in reverse for better interference analysis.
for (Operation *op : reverse(ops)) {
for (OpOperand &opOperand : op->getOpOperands())
@@ -2383,7 +2394,8 @@ LogicalResult mlir::linalg::inPlaceAnalysis(SmallVector<Operation *> &ops,
/// Analyze the `funcOp` body to determine which OpResults are inplaceable.
static LogicalResult
inPlaceAnalysisFuncOpBody(FuncOp funcOp, BufferizationAliasInfo &aliasInfo,
- const DominanceInfo &domInfo) {
+ const DominanceInfo &domInfo,
+ unsigned analysisFuzzerSeed = 0) {
LLVM_DEBUG(llvm::dbgs() << "\n\n");
LDBG("Begin InPlaceAnalysisFuncOpInternals:\n" << funcOp << '\n');
assert(funcOp && funcOp->getNumRegions() > 0 && !funcOp.body().empty() &&
@@ -2408,7 +2420,8 @@ inPlaceAnalysisFuncOpBody(FuncOp funcOp, BufferizationAliasInfo &aliasInfo,
aliasInfo.setBufferizesToWritableMemory(bbArg);
}
- LogicalResult res = inPlaceAnalysis(ops, aliasInfo, domInfo);
+ LogicalResult res =
+ inPlaceAnalysis(ops, aliasInfo, domInfo, analysisFuzzerSeed);
LDBG("End InPlaceAnalysisFuncOpInternals:\n" << funcOp << '\n');
return res;
@@ -3099,7 +3112,8 @@ void LinalgComprehensiveModuleBufferize::runOnOperation() {
setInPlaceFuncArgument(bbArg);
// If the analysis fails, just return.
- if (failed(inPlaceAnalysisFuncOpBody(funcOp, aliasInfo, domInfo))) {
+ if (failed(inPlaceAnalysisFuncOpBody(funcOp, aliasInfo, domInfo,
+ analysisFuzzerSeed))) {
signalPassFailure();
return;
}
diff --git a/mlir/test/Dialect/Linalg/comprehensive-module-bufferize-analysis.mlir b/mlir/test/Dialect/Linalg/comprehensive-module-bufferize-analysis.mlir
index 12897e2b4faaf..f2c56c3f75f2e 100644
--- a/mlir/test/Dialect/Linalg/comprehensive-module-bufferize-analysis.mlir
+++ b/mlir/test/Dialect/Linalg/comprehensive-module-bufferize-analysis.mlir
@@ -1,5 +1,10 @@
// RUN: mlir-opt %s -linalg-comprehensive-module-bufferize=test-analysis-only -split-input-file | FileCheck %s
+// Run fuzzer with
diff erent seeds.
+// RUN: mlir-opt %s -linalg-comprehensive-module-bufferize="test-analysis-only analysis-fuzzer-seed=23" -split-input-file -o /dev/null
+// RUN: mlir-opt %s -linalg-comprehensive-module-bufferize="test-analysis-only analysis-fuzzer-seed=59" -split-input-file -o /dev/null
+// RUN: mlir-opt %s -linalg-comprehensive-module-bufferize="test-analysis-only analysis-fuzzer-seed=91" -split-input-file -o /dev/null
+
//===----------------------------------------------------------------------===//
// Simple cases
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/Linalg/comprehensive-module-bufferize.mlir b/mlir/test/Dialect/Linalg/comprehensive-module-bufferize.mlir
index 9d6227462c490..bf46c1120bfaa 100644
--- a/mlir/test/Dialect/Linalg/comprehensive-module-bufferize.mlir
+++ b/mlir/test/Dialect/Linalg/comprehensive-module-bufferize.mlir
@@ -1,5 +1,10 @@
// RUN: mlir-opt %s -linalg-comprehensive-module-bufferize=allow-return-memref -split-input-file | FileCheck %s
+// Run fuzzer with
diff erent seeds.
+// RUN: mlir-opt %s -linalg-comprehensive-module-bufferize="test-analysis-only analysis-fuzzer-seed=23" -split-input-file -o /dev/null
+// RUN: mlir-opt %s -linalg-comprehensive-module-bufferize="test-analysis-only analysis-fuzzer-seed=59" -split-input-file -o /dev/null
+// RUN: mlir-opt %s -linalg-comprehensive-module-bufferize="test-analysis-only analysis-fuzzer-seed=91" -split-input-file -o /dev/null
+
// CHECK-LABEL: func @transfer_read(%{{.*}}: memref<?xf32, #map>) -> vector<4xf32> {
func @transfer_read(%A : tensor<?xf32>) -> (vector<4xf32>) {
%c0 = arith.constant 0 : index
More information about the Mlir-commits
mailing list