113 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C++
		
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C++
		
	
	
	
//===- AnalysisManagerTest.cpp - AnalysisManager unit tests ---------------===//
 | 
						|
//
 | 
						|
// Copyright 2019 The MLIR Authors.
 | 
						|
//
 | 
						|
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
						|
// you may not use this file except in compliance with the License.
 | 
						|
// You may obtain a copy of the License at
 | 
						|
//
 | 
						|
//   http://www.apache.org/licenses/LICENSE-2.0
 | 
						|
//
 | 
						|
// Unless required by applicable law or agreed to in writing, software
 | 
						|
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
						|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
						|
// See the License for the specific language governing permissions and
 | 
						|
// limitations under the License.
 | 
						|
// =============================================================================
 | 
						|
 | 
						|
#include "mlir/Pass/AnalysisManager.h"
 | 
						|
#include "mlir/IR/Builders.h"
 | 
						|
#include "gtest/gtest.h"
 | 
						|
 | 
						|
using namespace mlir;
 | 
						|
using namespace mlir::detail;
 | 
						|
 | 
						|
namespace {
 | 
						|
/// Minimal class definitions for two analyses.
 | 
						|
struct MyAnalysis {
 | 
						|
  MyAnalysis(Function *) {}
 | 
						|
  MyAnalysis(Module *) {}
 | 
						|
};
 | 
						|
struct OtherAnalysis {
 | 
						|
  OtherAnalysis(Function *) {}
 | 
						|
  OtherAnalysis(Module *) {}
 | 
						|
};
 | 
						|
 | 
						|
TEST(AnalysisManagerTest, FineGrainModuleAnalysisPreservation) {
 | 
						|
  MLIRContext context;
 | 
						|
 | 
						|
  // Test fine grain invalidation of the module analysis manager.
 | 
						|
  std::unique_ptr<Module> module(new Module(&context));
 | 
						|
  ModuleAnalysisManager mam(&*module);
 | 
						|
 | 
						|
  // Query two different analyses, but only preserve one before invalidating.
 | 
						|
  mam.getResult<MyAnalysis>();
 | 
						|
  mam.getResult<OtherAnalysis>();
 | 
						|
 | 
						|
  detail::PreservedAnalyses pa;
 | 
						|
  pa.preserve<MyAnalysis>();
 | 
						|
  mam.invalidate(pa);
 | 
						|
 | 
						|
  // Check that only MyAnalysis is preserved.
 | 
						|
  EXPECT_TRUE(mam.getCachedResult<MyAnalysis>().hasValue());
 | 
						|
  EXPECT_FALSE(mam.getCachedResult<OtherAnalysis>().hasValue());
 | 
						|
}
 | 
						|
 | 
						|
TEST(AnalysisManagerTest, FineGrainFunctionAnalysisPreservation) {
 | 
						|
  MLIRContext context;
 | 
						|
  Builder builder(&context);
 | 
						|
 | 
						|
  // Create a function and a module.
 | 
						|
  std::unique_ptr<Module> module(new Module(&context));
 | 
						|
  Function *func1 =
 | 
						|
      new Function(builder.getUnknownLoc(), "foo",
 | 
						|
                   builder.getFunctionType(llvm::None, llvm::None));
 | 
						|
  module->getFunctions().push_back(func1);
 | 
						|
 | 
						|
  // Test fine grain invalidation of the function analysis manager.
 | 
						|
  ModuleAnalysisManager mam(&*module);
 | 
						|
  FunctionAnalysisManager fam = mam.slice(func1);
 | 
						|
 | 
						|
  // Query two different analyses, but only preserve one before invalidating.
 | 
						|
  fam.getResult<MyAnalysis>();
 | 
						|
  fam.getResult<OtherAnalysis>();
 | 
						|
 | 
						|
  detail::PreservedAnalyses pa;
 | 
						|
  pa.preserve<MyAnalysis>();
 | 
						|
  fam.invalidate(pa);
 | 
						|
 | 
						|
  // Check that only MyAnalysis is preserved.
 | 
						|
  EXPECT_TRUE(fam.getCachedResult<MyAnalysis>().hasValue());
 | 
						|
  EXPECT_FALSE(fam.getCachedResult<OtherAnalysis>().hasValue());
 | 
						|
}
 | 
						|
 | 
						|
TEST(AnalysisManagerTest, FineGrainChildFunctionAnalysisPreservation) {
 | 
						|
  MLIRContext context;
 | 
						|
  Builder builder(&context);
 | 
						|
 | 
						|
  // Create a function and a module.
 | 
						|
  std::unique_ptr<Module> module(new Module(&context));
 | 
						|
  Function *func1 =
 | 
						|
      new Function(builder.getUnknownLoc(), "foo",
 | 
						|
                   builder.getFunctionType(llvm::None, llvm::None));
 | 
						|
  module->getFunctions().push_back(func1);
 | 
						|
 | 
						|
  // Test fine grain invalidation of a function analysis from within a module
 | 
						|
  // analysis manager.
 | 
						|
  ModuleAnalysisManager mam(&*module);
 | 
						|
 | 
						|
  // Query two different analyses, but only preserve one before invalidating.
 | 
						|
  mam.getFunctionResult<MyAnalysis>(func1);
 | 
						|
  mam.getFunctionResult<OtherAnalysis>(func1);
 | 
						|
 | 
						|
  detail::PreservedAnalyses pa;
 | 
						|
  pa.preserve<MyAnalysis>();
 | 
						|
  mam.invalidate(pa);
 | 
						|
 | 
						|
  // Check that only MyAnalysis is preserved.
 | 
						|
  EXPECT_TRUE(mam.getCachedFunctionResult<MyAnalysis>(func1).hasValue());
 | 
						|
  EXPECT_FALSE(mam.getCachedFunctionResult<OtherAnalysis>(func1).hasValue());
 | 
						|
}
 | 
						|
 | 
						|
} // end namespace
 |