2016-08-09 20:02:21 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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
|
2016-08-09 20:02:21 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-09 20:02:21 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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.
|
2016-08-09 20:02:21 +00:00
|
|
|
|
|
|
|
#ifndef LIBSPIRV_OPT_PASS_H_
|
|
|
|
#define LIBSPIRV_OPT_PASS_H_
|
|
|
|
|
2017-08-10 22:42:16 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
|
|
|
#include <queue>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2016-09-21 21:16:31 +00:00
|
|
|
#include <utility>
|
2016-08-09 20:02:21 +00:00
|
|
|
|
2017-10-30 21:42:26 +00:00
|
|
|
#include "basic_block.h"
|
2017-10-25 17:26:25 +00:00
|
|
|
#include "def_use_manager.h"
|
2017-10-30 21:42:26 +00:00
|
|
|
#include "ir_context.h"
|
2016-08-09 20:02:21 +00:00
|
|
|
#include "module.h"
|
2016-09-21 21:16:31 +00:00
|
|
|
#include "spirv-tools/libspirv.hpp"
|
2016-08-09 20:02:21 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
|
|
|
// Abstract class of a pass. All passes should implement this abstract class
|
|
|
|
// and all analysis and transformation is done via the Process() method.
|
|
|
|
class Pass {
|
|
|
|
public:
|
2016-09-12 16:39:44 +00:00
|
|
|
// The status of processing a module using a pass.
|
|
|
|
//
|
|
|
|
// The numbers for the cases are assigned to make sure that Failure & anything
|
|
|
|
// is Failure, SuccessWithChange & any success is SuccessWithChange.
|
|
|
|
enum class Status {
|
|
|
|
Failure = 0x00,
|
|
|
|
SuccessWithChange = 0x10,
|
|
|
|
SuccessWithoutChange = 0x11,
|
|
|
|
};
|
|
|
|
|
2017-08-10 22:42:16 +00:00
|
|
|
using ProcessFunction = std::function<bool(ir::Function*)>;
|
|
|
|
|
2016-09-20 20:48:00 +00:00
|
|
|
// Constructs a new pass.
|
2016-08-20 03:17:44 +00:00
|
|
|
//
|
2016-09-20 20:48:00 +00:00
|
|
|
// The constructed instance will have an empty message consumer, which just
|
|
|
|
// ignores all messages from the library. Use SetMessageConsumer() to supply
|
|
|
|
// one if messages are of concern.
|
2017-10-25 17:26:25 +00:00
|
|
|
Pass();
|
2016-08-20 03:17:44 +00:00
|
|
|
|
2017-02-28 19:34:18 +00:00
|
|
|
// Destructs the pass.
|
|
|
|
virtual ~Pass() = default;
|
|
|
|
|
2016-08-09 20:02:21 +00:00
|
|
|
// Returns a descriptive name for this pass.
|
2017-08-30 18:19:22 +00:00
|
|
|
//
|
|
|
|
// NOTE: When deriving a new pass class, make sure you make the name
|
|
|
|
// compatible with the corresponding spirv-opt command-line flag. For example,
|
|
|
|
// if you add the flag --my-pass to spirv-opt, make this function return
|
|
|
|
// "my-pass" (no leading hyphens).
|
2016-08-09 20:02:21 +00:00
|
|
|
virtual const char* name() const = 0;
|
2016-09-20 20:48:00 +00:00
|
|
|
|
|
|
|
// Sets the message consumer to the given |consumer|. |consumer| which will be
|
|
|
|
// invoked every time there is a message to be communicated to the outside.
|
|
|
|
void SetMessageConsumer(MessageConsumer c) { consumer_ = std::move(c); }
|
2017-10-25 17:26:25 +00:00
|
|
|
|
2016-08-20 03:17:44 +00:00
|
|
|
// Returns the reference to the message consumer for this pass.
|
|
|
|
const MessageConsumer& consumer() const { return consumer_; }
|
|
|
|
|
2017-10-25 17:26:25 +00:00
|
|
|
// Returns the def-use manager used for this pass. TODO(dnovillo): This should
|
|
|
|
// be handled by the pass manager.
|
|
|
|
analysis::DefUseManager* get_def_use_mgr() const {
|
2017-11-02 18:25:48 +00:00
|
|
|
return context()->get_def_use_mgr();
|
2017-10-25 17:26:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 20:31:43 +00:00
|
|
|
analysis::DecorationManager* get_decoration_mgr() const {
|
|
|
|
return context()->get_decoration_mgr();
|
|
|
|
}
|
|
|
|
|
2018-02-17 19:55:54 +00:00
|
|
|
FeatureManager* get_feature_mgr() const {
|
|
|
|
return context()->get_feature_mgr();
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:26:25 +00:00
|
|
|
// Returns a pointer to the current module for this pass.
|
2017-10-30 15:13:24 +00:00
|
|
|
ir::Module* get_module() const { return context_->module(); }
|
|
|
|
|
|
|
|
// Returns a pointer to the current context for this pass.
|
|
|
|
ir::IRContext* context() const { return context_; }
|
2017-10-25 17:26:25 +00:00
|
|
|
|
2017-11-27 19:20:32 +00:00
|
|
|
// Returns a pointer to the CFG for current module.
|
|
|
|
ir::CFG* cfg() const { return context()->cfg(); }
|
2017-10-30 21:42:26 +00:00
|
|
|
|
2017-08-10 22:42:16 +00:00
|
|
|
// Add to |todo| all ids of functions called in |func|.
|
|
|
|
void AddCalls(ir::Function* func, std::queue<uint32_t>* todo);
|
|
|
|
|
2017-09-19 14:12:13 +00:00
|
|
|
// Applies |pfn| to every function in the call trees that are rooted at the
|
|
|
|
// entry points. Returns true if any call |pfn| returns true. By convention
|
|
|
|
// |pfn| should return true if it modified the module.
|
2017-08-10 22:42:16 +00:00
|
|
|
bool ProcessEntryPointCallTree(ProcessFunction& pfn, ir::Module* module);
|
|
|
|
|
2017-09-19 14:12:13 +00:00
|
|
|
// Applies |pfn| to every function in the call trees rooted at the entry
|
|
|
|
// points and exported functions. Returns true if any call |pfn| returns
|
|
|
|
// true. By convention |pfn| should return true if it modified the module.
|
2017-10-30 15:13:24 +00:00
|
|
|
bool ProcessReachableCallTree(ProcessFunction& pfn, ir::IRContext* irContext);
|
2017-09-19 14:12:13 +00:00
|
|
|
|
|
|
|
// Applies |pfn| to every function in the call trees rooted at the elements of
|
|
|
|
// |roots|. Returns true if any call to |pfn| returns true. By convention
|
|
|
|
// |pfn| should return true if it modified the module. After returning
|
|
|
|
// |roots| will be empty.
|
|
|
|
bool ProcessCallTreeFromRoots(
|
|
|
|
ProcessFunction& pfn,
|
|
|
|
const std::unordered_map<uint32_t, ir::Function*>& id2function,
|
|
|
|
std::queue<uint32_t>* roots);
|
|
|
|
|
2017-11-08 17:40:02 +00:00
|
|
|
// Run the pass on the given |module|. Returns Status::Failure if errors occur
|
2018-04-10 17:38:33 +00:00
|
|
|
// when processing. Returns the corresponding Status::Success if processing is
|
2017-11-02 18:25:48 +00:00
|
|
|
// successful to indicate whether changes are made to the module. If there
|
|
|
|
// were any changes it will also invalidate the analyses in the IRContext
|
|
|
|
// that are not preserved.
|
2018-04-10 17:38:33 +00:00
|
|
|
//
|
|
|
|
// It is an error if |Run| is called twice with the same instance of the pass.
|
|
|
|
// If this happens the return value will be |Failure|.
|
2017-11-02 18:25:48 +00:00
|
|
|
virtual Status Run(ir::IRContext* ctx) final;
|
|
|
|
|
|
|
|
// Returns the set of analyses that the pass is guaranteed to preserve.
|
|
|
|
virtual ir::IRContext::Analysis GetPreservedAnalyses() {
|
|
|
|
return ir::IRContext::kAnalysisNone;
|
|
|
|
}
|
2016-08-20 03:17:44 +00:00
|
|
|
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
// Return type id for |ptrInst|'s pointee
|
|
|
|
uint32_t GetPointeeTypeId(const ir::Instruction* ptrInst) const;
|
|
|
|
|
2017-10-25 17:26:25 +00:00
|
|
|
protected:
|
|
|
|
// Initialize basic data structures for the pass. This sets up the def-use
|
2017-12-07 21:42:27 +00:00
|
|
|
// manager, module and other attributes.
|
|
|
|
virtual void InitializeProcessing(ir::IRContext* c) { context_ = c; }
|
2017-10-25 17:26:25 +00:00
|
|
|
|
2017-11-02 18:25:48 +00:00
|
|
|
// Processes the given |module|. Returns Status::Failure if errors occur when
|
|
|
|
// processing. Returns the corresponding Status::Success if processing is
|
|
|
|
// succesful to indicate whether changes are made to the module.
|
|
|
|
virtual Status Process(ir::IRContext* context) = 0;
|
|
|
|
|
2017-12-07 21:42:27 +00:00
|
|
|
// Return the next available SSA id and increment it.
|
|
|
|
uint32_t TakeNextId() { return context_->TakeNextId(); }
|
2017-10-25 17:26:25 +00:00
|
|
|
|
2016-08-20 03:17:44 +00:00
|
|
|
private:
|
2016-09-20 20:48:00 +00:00
|
|
|
MessageConsumer consumer_; // Message consumer.
|
2017-10-25 17:26:25 +00:00
|
|
|
|
2017-10-30 21:42:26 +00:00
|
|
|
// The context that this pass belongs to.
|
2017-10-30 15:13:24 +00:00
|
|
|
ir::IRContext* context_;
|
2018-04-10 17:38:33 +00:00
|
|
|
|
|
|
|
// An instance of a pass can only be run once because it is too hard to
|
|
|
|
// enforce proper resetting of internal state for each instance. This member
|
|
|
|
// is used to check that we do not run the same instance twice.
|
|
|
|
bool already_run_;
|
2016-08-09 20:02:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|
|
|
|
|
|
|
|
#endif // LIBSPIRV_OPT_PASS_H_
|