Don't rebuilt valid analyses. (#4733)

The function `BuildInvalideAnalyses` will be rebuilt for every analysis that
has been requested, but it is not necessary.  It also can cause problems
because if the CFG needs to be rebuilt, so do the dominator trees.

This change will make the functionality match the description of the
function.
This commit is contained in:
Steven Perron 2022-03-04 20:16:42 +00:00 committed by GitHub
parent d1addc44b2
commit 0b8426346d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 2 deletions

View File

@ -41,6 +41,8 @@ namespace spvtools {
namespace opt { namespace opt {
void IRContext::BuildInvalidAnalyses(IRContext::Analysis set) { void IRContext::BuildInvalidAnalyses(IRContext::Analysis set) {
set = Analysis(set & ~valid_analyses_);
if (set & kAnalysisDefUse) { if (set & kAnalysisDefUse) {
BuildDefUseManager(); BuildDefUseManager();
} }

View File

@ -867,8 +867,7 @@ inline IRContext::Analysis operator|(IRContext::Analysis lhs,
inline IRContext::Analysis& operator|=(IRContext::Analysis& lhs, inline IRContext::Analysis& operator|=(IRContext::Analysis& lhs,
IRContext::Analysis rhs) { IRContext::Analysis rhs) {
lhs = static_cast<IRContext::Analysis>(static_cast<int>(lhs) | lhs = lhs | rhs;
static_cast<int>(rhs));
return lhs; return lhs;
} }

View File

@ -431,6 +431,7 @@ bool MergeReturnPass::BreakFromConstruct(
std::list<BasicBlock*>* order, Instruction* break_merge_inst) { std::list<BasicBlock*>* order, Instruction* break_merge_inst) {
// Make sure the CFG is build here. If we don't then it becomes very hard // Make sure the CFG is build here. If we don't then it becomes very hard
// to know which new blocks need to be updated. // to know which new blocks need to be updated.
context()->InvalidateAnalyses(IRContext::kAnalysisCFG);
context()->BuildInvalidAnalyses(IRContext::kAnalysisCFG); context()->BuildInvalidAnalyses(IRContext::kAnalysisCFG);
// When predicating, be aware of whether this block is a header block, a // When predicating, be aware of whether this block is a header block, a

View File

@ -90,6 +90,21 @@ TEST_F(IRContextTest, IndividualValidAfterBuild) {
} }
} }
TEST_F(IRContextTest, DontRebuildValidAnalysis) {
std::unique_ptr<Module> module(new Module());
IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
spvtools::MessageConsumer());
auto* oldCfg = localContext.cfg();
auto* oldDefUse = localContext.get_def_use_mgr();
localContext.BuildInvalidAnalyses(IRContext::kAnalysisCFG |
IRContext::kAnalysisDefUse);
auto* newCfg = localContext.cfg();
auto* newDefUse = localContext.get_def_use_mgr();
EXPECT_EQ(oldCfg, newCfg);
EXPECT_EQ(oldDefUse, newDefUse);
}
TEST_F(IRContextTest, AllValidAfterBuild) { TEST_F(IRContextTest, AllValidAfterBuild) {
std::unique_ptr<Module> module = MakeUnique<Module>(); std::unique_ptr<Module> module = MakeUnique<Module>();
IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module), IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),