2019-03-07 03:04:22 +08:00
|
|
|
//===- AnalysisManagerTest.cpp - AnalysisManager unit tests ---------------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-03-07 03:04:22 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
#include "mlir/Pass/AnalysisManager.h"
|
|
|
|
#include "mlir/IR/Builders.h"
|
2019-08-29 06:10:37 +08:00
|
|
|
#include "mlir/IR/Function.h"
|
2020-08-14 11:43:47 +08:00
|
|
|
#include "mlir/Pass/Pass.h"
|
|
|
|
#include "mlir/Pass/PassManager.h"
|
2019-03-07 03:04:22 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::detail;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// Minimal class definitions for two analyses.
|
|
|
|
struct MyAnalysis {
|
2019-08-29 06:10:37 +08:00
|
|
|
MyAnalysis(Operation *) {}
|
2019-03-07 03:04:22 +08:00
|
|
|
};
|
|
|
|
struct OtherAnalysis {
|
2019-08-29 06:10:37 +08:00
|
|
|
OtherAnalysis(Operation *) {}
|
2019-03-07 03:04:22 +08:00
|
|
|
};
|
2020-08-14 11:43:47 +08:00
|
|
|
struct OpSpecificAnalysis {
|
|
|
|
OpSpecificAnalysis(ModuleOp) {}
|
|
|
|
};
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
TEST(AnalysisManagerTest, FineGrainModuleAnalysisPreservation) {
|
2020-10-24 04:19:35 +08:00
|
|
|
MLIRContext context;
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
// Test fine grain invalidation of the module analysis manager.
|
2019-07-11 01:07:49 +08:00
|
|
|
OwningModuleRef module(ModuleOp::create(UnknownLoc::get(&context)));
|
2019-07-03 01:49:17 +08:00
|
|
|
ModuleAnalysisManager mam(*module, /*passInstrumentor=*/nullptr);
|
2019-08-29 06:10:37 +08:00
|
|
|
AnalysisManager am = mam;
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
// Query two different analyses, but only preserve one before invalidating.
|
2019-08-29 06:10:37 +08:00
|
|
|
am.getAnalysis<MyAnalysis>();
|
|
|
|
am.getAnalysis<OtherAnalysis>();
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
detail::PreservedAnalyses pa;
|
|
|
|
pa.preserve<MyAnalysis>();
|
2019-08-29 06:10:37 +08:00
|
|
|
am.invalidate(pa);
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
// Check that only MyAnalysis is preserved.
|
2019-08-29 06:10:37 +08:00
|
|
|
EXPECT_TRUE(am.getCachedAnalysis<MyAnalysis>().hasValue());
|
|
|
|
EXPECT_FALSE(am.getCachedAnalysis<OtherAnalysis>().hasValue());
|
2019-03-07 03:04:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AnalysisManagerTest, FineGrainFunctionAnalysisPreservation) {
|
2020-10-24 04:19:35 +08:00
|
|
|
MLIRContext context;
|
2019-03-07 03:04:22 +08:00
|
|
|
Builder builder(&context);
|
|
|
|
|
|
|
|
// Create a function and a module.
|
2019-07-11 01:07:49 +08:00
|
|
|
OwningModuleRef module(ModuleOp::create(UnknownLoc::get(&context)));
|
2019-07-10 07:17:55 +08:00
|
|
|
FuncOp func1 =
|
|
|
|
FuncOp::create(builder.getUnknownLoc(), "foo",
|
|
|
|
builder.getFunctionType(llvm::None, llvm::None));
|
2019-07-02 01:29:09 +08:00
|
|
|
module->push_back(func1);
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
// Test fine grain invalidation of the function analysis manager.
|
2019-07-03 01:49:17 +08:00
|
|
|
ModuleAnalysisManager mam(*module, /*passInstrumentor=*/nullptr);
|
2019-08-29 06:10:37 +08:00
|
|
|
AnalysisManager am = mam;
|
2020-08-29 04:17:38 +08:00
|
|
|
AnalysisManager fam = am.nest(func1);
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
// Query two different analyses, but only preserve one before invalidating.
|
2019-03-07 04:03:14 +08:00
|
|
|
fam.getAnalysis<MyAnalysis>();
|
|
|
|
fam.getAnalysis<OtherAnalysis>();
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
detail::PreservedAnalyses pa;
|
|
|
|
pa.preserve<MyAnalysis>();
|
|
|
|
fam.invalidate(pa);
|
|
|
|
|
|
|
|
// Check that only MyAnalysis is preserved.
|
2019-03-07 04:03:14 +08:00
|
|
|
EXPECT_TRUE(fam.getCachedAnalysis<MyAnalysis>().hasValue());
|
|
|
|
EXPECT_FALSE(fam.getCachedAnalysis<OtherAnalysis>().hasValue());
|
2019-03-07 03:04:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AnalysisManagerTest, FineGrainChildFunctionAnalysisPreservation) {
|
2020-10-24 04:19:35 +08:00
|
|
|
MLIRContext context;
|
2019-03-07 03:04:22 +08:00
|
|
|
Builder builder(&context);
|
|
|
|
|
|
|
|
// Create a function and a module.
|
2019-07-11 01:07:49 +08:00
|
|
|
OwningModuleRef module(ModuleOp::create(UnknownLoc::get(&context)));
|
2019-07-10 07:17:55 +08:00
|
|
|
FuncOp func1 =
|
|
|
|
FuncOp::create(builder.getUnknownLoc(), "foo",
|
|
|
|
builder.getFunctionType(llvm::None, llvm::None));
|
2019-07-02 01:29:09 +08:00
|
|
|
module->push_back(func1);
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
// Test fine grain invalidation of a function analysis from within a module
|
|
|
|
// analysis manager.
|
2019-07-03 01:49:17 +08:00
|
|
|
ModuleAnalysisManager mam(*module, /*passInstrumentor=*/nullptr);
|
2019-08-29 06:10:37 +08:00
|
|
|
AnalysisManager am = mam;
|
|
|
|
|
|
|
|
// Check that the analysis cache is initially empty.
|
|
|
|
EXPECT_FALSE(am.getCachedChildAnalysis<MyAnalysis>(func1).hasValue());
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
// Query two different analyses, but only preserve one before invalidating.
|
2019-08-29 06:10:37 +08:00
|
|
|
am.getChildAnalysis<MyAnalysis>(func1);
|
|
|
|
am.getChildAnalysis<OtherAnalysis>(func1);
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
detail::PreservedAnalyses pa;
|
|
|
|
pa.preserve<MyAnalysis>();
|
2019-08-29 06:10:37 +08:00
|
|
|
am.invalidate(pa);
|
2019-03-07 03:04:22 +08:00
|
|
|
|
|
|
|
// Check that only MyAnalysis is preserved.
|
2019-08-29 06:10:37 +08:00
|
|
|
EXPECT_TRUE(am.getCachedChildAnalysis<MyAnalysis>(func1).hasValue());
|
|
|
|
EXPECT_FALSE(am.getCachedChildAnalysis<OtherAnalysis>(func1).hasValue());
|
2019-03-07 03:04:22 +08:00
|
|
|
}
|
|
|
|
|
2019-12-04 03:13:39 +08:00
|
|
|
/// Test analyses with custom invalidation logic.
|
|
|
|
struct TestAnalysisSet {};
|
|
|
|
|
|
|
|
struct CustomInvalidatingAnalysis {
|
|
|
|
CustomInvalidatingAnalysis(Operation *) {}
|
|
|
|
|
|
|
|
bool isInvalidated(const AnalysisManager::PreservedAnalyses &pa) {
|
|
|
|
return !pa.isPreserved<TestAnalysisSet>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(AnalysisManagerTest, CustomInvalidation) {
|
2020-10-24 04:19:35 +08:00
|
|
|
MLIRContext context;
|
2019-12-04 03:13:39 +08:00
|
|
|
Builder builder(&context);
|
|
|
|
|
|
|
|
// Create a function and a module.
|
|
|
|
OwningModuleRef module(ModuleOp::create(UnknownLoc::get(&context)));
|
|
|
|
ModuleAnalysisManager mam(*module, /*passInstrumentor=*/nullptr);
|
|
|
|
AnalysisManager am = mam;
|
|
|
|
|
|
|
|
detail::PreservedAnalyses pa;
|
|
|
|
|
|
|
|
// Check that the analysis is invalidated properly.
|
|
|
|
am.getAnalysis<CustomInvalidatingAnalysis>();
|
|
|
|
am.invalidate(pa);
|
|
|
|
EXPECT_FALSE(am.getCachedAnalysis<CustomInvalidatingAnalysis>().hasValue());
|
|
|
|
|
|
|
|
// Check that the analysis is preserved properly.
|
|
|
|
am.getAnalysis<CustomInvalidatingAnalysis>();
|
|
|
|
pa.preserve<TestAnalysisSet>();
|
|
|
|
am.invalidate(pa);
|
|
|
|
EXPECT_TRUE(am.getCachedAnalysis<CustomInvalidatingAnalysis>().hasValue());
|
|
|
|
}
|
2020-08-14 11:43:47 +08:00
|
|
|
|
|
|
|
TEST(AnalysisManagerTest, OpSpecificAnalysis) {
|
|
|
|
MLIRContext context;
|
|
|
|
|
|
|
|
// Create a module.
|
|
|
|
OwningModuleRef module(ModuleOp::create(UnknownLoc::get(&context)));
|
|
|
|
ModuleAnalysisManager mam(*module, /*passInstrumentor=*/nullptr);
|
|
|
|
AnalysisManager am = mam;
|
|
|
|
|
|
|
|
// Query the op specific analysis for the module and verify that its cached.
|
|
|
|
am.getAnalysis<OpSpecificAnalysis, ModuleOp>();
|
|
|
|
EXPECT_TRUE(am.getCachedAnalysis<OpSpecificAnalysis>().hasValue());
|
|
|
|
}
|
|
|
|
|
2019-03-07 03:04:22 +08:00
|
|
|
} // end namespace
|