-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…#2361) * Fixes #2358. Added to the reducer the ability to remove a function that is not directly called. Factored out some code from the optimizer to help with this.
- Loading branch information
Showing
14 changed files
with
551 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2019 Google LLC | ||
// | ||
// 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 "eliminate_dead_functions_util.h" | ||
|
||
namespace spvtools { | ||
namespace opt { | ||
|
||
namespace eliminatedeadfunctionsutil { | ||
|
||
Module::iterator EliminateFunction(IRContext* context, | ||
Module::iterator* func_iter) { | ||
(*func_iter) | ||
->ForEachInst([context](Instruction* inst) { context->KillInst(inst); }, | ||
true); | ||
return func_iter->Erase(); | ||
} | ||
|
||
} // namespace eliminatedeadfunctionsutil | ||
} // namespace opt | ||
} // namespace spvtools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) 2019 Google LLC | ||
// | ||
// 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. | ||
|
||
#ifndef SOURCE_OPT_ELIMINATE_DEAD_FUNCTIONS_UTIL_H_ | ||
#define SOURCE_OPT_ELIMINATE_DEAD_FUNCTIONS_UTIL_H_ | ||
|
||
#include "source/opt/ir_context.h" | ||
|
||
namespace spvtools { | ||
namespace opt { | ||
|
||
// Provides functionality for eliminating functions that are not needed, for use | ||
// by various analyses and passes. | ||
namespace eliminatedeadfunctionsutil { | ||
|
||
// Removes all of the function's instructions, removes the function from the | ||
// module, and returns the next iterator. | ||
Module::iterator EliminateFunction(IRContext* context, | ||
Module::iterator* func_iter); | ||
|
||
} // namespace eliminatedeadfunctionsutil | ||
} // namespace opt | ||
} // namespace spvtools | ||
|
||
#endif // SOURCE_OPT_ELIMINATE_DEAD_FUNCTIONS_UTIL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2019 Google LLC | ||
// | ||
// 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 "remove_function_reduction_opportunity.h" | ||
#include "source/opt/eliminate_dead_functions_util.h" | ||
|
||
namespace spvtools { | ||
namespace reduce { | ||
|
||
bool RemoveFunctionReductionOpportunity::PreconditionHolds() { | ||
// Removing one function cannot influence whether another function can be | ||
// removed. | ||
return true; | ||
} | ||
|
||
void RemoveFunctionReductionOpportunity::Apply() { | ||
for (opt::Module::iterator function_it = context_->module()->begin(); | ||
function_it != context_->module()->end(); ++function_it) { | ||
if (&*function_it == function_) { | ||
opt::eliminatedeadfunctionsutil::EliminateFunction(context_, | ||
&function_it); | ||
return; | ||
} | ||
} | ||
assert(0 && "Function to be removed was not found."); | ||
} | ||
|
||
} // namespace reduce | ||
} // namespace spvtools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2019 Google LLC | ||
// | ||
// 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. | ||
|
||
#ifndef SOURCE_REDUCE_REMOVE_FUNCTION_REDUCTION_OPPORTUNITY_H_ | ||
#define SOURCE_REDUCE_REMOVE_FUNCTION_REDUCTION_OPPORTUNITY_H_ | ||
|
||
#include "reduction_opportunity.h" | ||
#include "source/opt/function.h" | ||
|
||
namespace spvtools { | ||
namespace reduce { | ||
|
||
// An opportunity to remove an unreferenced function. | ||
class RemoveFunctionReductionOpportunity : public ReductionOpportunity { | ||
public: | ||
// Creates an opportunity to remove |function| from the module represented by | ||
// |context|. | ||
RemoveFunctionReductionOpportunity(opt::IRContext* context, | ||
opt::Function* function) | ||
: context_(context), function_(function) {} | ||
|
||
bool PreconditionHolds() override; | ||
|
||
protected: | ||
void Apply() override; | ||
|
||
private: | ||
// The IR context for the module under analysis. | ||
opt::IRContext* context_; | ||
|
||
// The function that can be removed. | ||
opt::Function* function_; | ||
}; | ||
|
||
} // namespace reduce | ||
} // namespace spvtools | ||
|
||
#endif // SOURCE_REDUCE_REMOVE_FUNCTION_REDUCTION_OPPORTUNITY_H_ |
42 changes: 42 additions & 0 deletions
42
source/reduce/remove_function_reduction_opportunity_finder.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2019 Google LLC | ||
// | ||
// 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 "remove_function_reduction_opportunity_finder.h" | ||
#include "remove_function_reduction_opportunity.h" | ||
|
||
namespace spvtools { | ||
namespace reduce { | ||
|
||
std::vector<std::unique_ptr<ReductionOpportunity>> | ||
RemoveFunctionReductionOpportunityFinder::GetAvailableOpportunities( | ||
opt::IRContext* context) const { | ||
std::vector<std::unique_ptr<ReductionOpportunity>> result; | ||
// Consider each function. | ||
for (auto& function : *context->module()) { | ||
if (context->get_def_use_mgr()->NumUses(function.result_id()) > 0) { | ||
// If the function is referenced, ignore it. | ||
continue; | ||
} | ||
result.push_back( | ||
MakeUnique<RemoveFunctionReductionOpportunity>(context, &function)); | ||
} | ||
return result; | ||
} | ||
|
||
std::string RemoveFunctionReductionOpportunityFinder::GetName() const { | ||
return "RemoveFunctionReductionOpportunityFinder"; | ||
} | ||
|
||
} // namespace reduce | ||
} // namespace spvtools |
42 changes: 42 additions & 0 deletions
42
source/reduce/remove_function_reduction_opportunity_finder.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2019 Google LLC | ||
// | ||
// 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. | ||
|
||
#ifndef SOURCE_REDUCE_REMOVE_FUNCTION_REDUCTION_OPPORTUNITY_FINDER_H_ | ||
#define SOURCE_REDUCE_REMOVE_FUNCTION_REDUCTION_OPPORTUNITY_FINDER_H_ | ||
|
||
#include "source/reduce/reduction_opportunity_finder.h" | ||
|
||
namespace spvtools { | ||
namespace reduce { | ||
|
||
// A finder of opportunities to remove unreferenced functions. | ||
class RemoveFunctionReductionOpportunityFinder | ||
: public ReductionOpportunityFinder { | ||
public: | ||
RemoveFunctionReductionOpportunityFinder() = default; | ||
|
||
~RemoveFunctionReductionOpportunityFinder() override = default; | ||
|
||
std::string GetName() const final; | ||
|
||
std::vector<std::unique_ptr<ReductionOpportunity>> GetAvailableOpportunities( | ||
opt::IRContext* context) const final; | ||
|
||
private: | ||
}; | ||
|
||
} // namespace reduce | ||
} // namespace spvtools | ||
|
||
#endif // SOURCE_REDUCE_REMOVE_FUNCTION_REDUCTION_OPPORTUNITY_FINDER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.