2017-11-30 22:03:06 +00:00
|
|
|
// Copyright (c) 2017 Google Inc.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_OPT_SCALAR_REPLACEMENT_PASS_H_
|
|
|
|
#define SOURCE_OPT_SCALAR_REPLACEMENT_PASS_H_
|
2017-11-30 22:03:06 +00:00
|
|
|
|
2022-06-10 21:55:45 +00:00
|
|
|
#include <cassert>
|
2018-04-16 13:58:00 +00:00
|
|
|
#include <cstdio>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <memory>
|
2017-11-30 22:03:06 +00:00
|
|
|
#include <queue>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "source/opt/function.h"
|
2022-02-03 15:51:15 +00:00
|
|
|
#include "source/opt/mem_pass.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/type_manager.h"
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
|
|
|
// Documented in optimizer.hpp
|
2022-02-03 15:51:15 +00:00
|
|
|
class ScalarReplacementPass : public MemPass {
|
2018-04-16 13:58:00 +00:00
|
|
|
private:
|
2022-11-17 18:02:50 +00:00
|
|
|
static constexpr uint32_t kDefaultLimit = 100;
|
2018-04-16 13:58:00 +00:00
|
|
|
|
2017-11-30 22:03:06 +00:00
|
|
|
public:
|
2018-04-16 13:58:00 +00:00
|
|
|
ScalarReplacementPass(uint32_t limit = kDefaultLimit)
|
|
|
|
: max_num_elements_(limit) {
|
2022-06-10 21:55:45 +00:00
|
|
|
const auto num_to_write = snprintf(
|
|
|
|
name_, sizeof(name_), "scalar-replacement=%u", max_num_elements_);
|
|
|
|
assert(size_t(num_to_write) < sizeof(name_));
|
|
|
|
(void)num_to_write; // Mark as unused
|
2022-07-06 00:12:58 +00:00
|
|
|
|
|
|
|
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
|
|
|
// ClusterFuzz/OSS-Fuzz is likely to yield examples with very large arrays.
|
|
|
|
// This can cause timeouts and memouts during fuzzing that
|
|
|
|
// are not classed as bugs. To avoid this noise, we set the
|
|
|
|
// max_num_elements_ to a smaller value for fuzzing.
|
|
|
|
max_num_elements_ =
|
|
|
|
(max_num_elements_ > 0 && max_num_elements_ < 100 ? max_num_elements_
|
|
|
|
: 100);
|
|
|
|
#endif
|
2018-04-16 13:58:00 +00:00
|
|
|
}
|
2017-11-30 22:03:06 +00:00
|
|
|
|
2018-04-16 13:58:00 +00:00
|
|
|
const char* name() const override { return name_; }
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Attempts to scalarize all appropriate function scope variables. Returns
|
|
|
|
// SuccessWithChange if any change is made.
|
2018-07-12 13:08:45 +00:00
|
|
|
Status Process() override;
|
2018-07-09 15:32:29 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
IRContext::Analysis GetPreservedAnalyses() override {
|
|
|
|
return IRContext::kAnalysisDefUse |
|
|
|
|
IRContext::kAnalysisInstrToBlockMapping |
|
|
|
|
IRContext::kAnalysisDecorations | IRContext::kAnalysisCombinators |
|
2018-12-20 18:00:05 +00:00
|
|
|
IRContext::kAnalysisCFG | IRContext::kAnalysisNameMap |
|
|
|
|
IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
|
2017-11-30 22:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Small container for tracking statistics about variables.
|
|
|
|
//
|
|
|
|
// TODO(alanbaker): Develop some useful heuristics to tune this pass.
|
|
|
|
struct VariableStats {
|
|
|
|
uint32_t num_partial_accesses;
|
|
|
|
uint32_t num_full_accesses;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Attempts to scalarize all appropriate function scope variables in
|
|
|
|
// |function|. Returns SuccessWithChange if any changes are mode.
|
2018-07-12 19:14:43 +00:00
|
|
|
Status ProcessFunction(Function* function);
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Returns true if |varInst| can be scalarized.
|
|
|
|
//
|
|
|
|
// Examines the use chain of |varInst| to verify all uses are valid for
|
|
|
|
// scalarization.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CanReplaceVariable(const Instruction* varInst) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Returns true if |typeInst| is an acceptable type to scalarize.
|
|
|
|
//
|
|
|
|
// Allows all aggregate types except runtime arrays. Additionally, checks the
|
|
|
|
// that the number of elements that would be scalarized is within bounds.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CheckType(const Instruction* typeInst) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Returns true if all the decorations for |varInst| are acceptable for
|
|
|
|
// scalarization.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CheckAnnotations(const Instruction* varInst) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Returns true if all the decorations for |typeInst| are acceptable for
|
|
|
|
// scalarization.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CheckTypeAnnotations(const Instruction* typeInst) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Returns true if the uses of |inst| are acceptable for scalarization.
|
|
|
|
//
|
|
|
|
// Recursively checks all the uses of |inst|. For |inst| specifically, only
|
2022-11-04 21:27:10 +00:00
|
|
|
// allows spv::Op::OpAccessChain, spv::Op::OpInBoundsAccessChain,
|
|
|
|
// spv::Op::OpLoad and spv::Op::OpStore. Access chains must have the first
|
|
|
|
// index be a compile-time constant. Subsequent uses of access chains
|
|
|
|
// (including other access chains) are checked in a more relaxed manner.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CheckUses(const Instruction* inst) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Helper function for the above |CheckUses|.
|
|
|
|
//
|
|
|
|
// This version tracks some stats about the current OpVariable. These stats
|
|
|
|
// are used to drive heuristics about when to scalarize.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CheckUses(const Instruction* inst, VariableStats* stats) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Relaxed helper function for |CheckUses|.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CheckUsesRelaxed(const Instruction* inst) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Transfers appropriate decorations from |source| to |replacements|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void TransferAnnotations(const Instruction* source,
|
|
|
|
std::vector<Instruction*>* replacements);
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Scalarizes |inst| and updates its uses.
|
|
|
|
//
|
|
|
|
// |inst| must be an OpVariable. It is replaced with an OpVariable for each
|
|
|
|
// for element of the composite type. Uses of |inst| are updated as
|
|
|
|
// appropriate. If the replacement variables are themselves scalarizable, they
|
|
|
|
// get added to |worklist| for further processing. If any replacement
|
2019-07-26 16:33:40 +00:00
|
|
|
// variable ends up with no uses it is erased. Returns
|
|
|
|
// - Status::SuccessWithoutChange if the variable could not be replaced.
|
|
|
|
// - Status::SuccessWithChange if it made replacements.
|
|
|
|
// - Status::Failure if it couldn't create replacement variables.
|
|
|
|
Pass::Status ReplaceVariable(Instruction* inst,
|
|
|
|
std::queue<Instruction*>* worklist);
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Returns the underlying storage type for |inst|.
|
|
|
|
//
|
|
|
|
// |inst| must be an OpVariable. Returns the type that is pointed to by
|
|
|
|
// |inst|.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* GetStorageType(const Instruction* inst) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Returns true if the load can be scalarized.
|
|
|
|
//
|
|
|
|
// |inst| must be an OpLoad. Returns true if |index| is the pointer operand of
|
|
|
|
// |inst| and the load is not from volatile memory.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CheckLoad(const Instruction* inst, uint32_t index) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Returns true if the store can be scalarized.
|
|
|
|
//
|
|
|
|
// |inst| must be an OpStore. Returns true if |index| is the pointer operand
|
|
|
|
// of |inst| and the store is not to volatile memory.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CheckStore(const Instruction* inst, uint32_t index) const;
|
2021-09-24 14:56:08 +00:00
|
|
|
|
|
|
|
// Returns true if the DebugDeclare can be scalarized at |index|.
|
|
|
|
bool CheckDebugDeclare(uint32_t index) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
2021-03-16 20:40:51 +00:00
|
|
|
// Returns true if |index| is the pointer operand of an OpImageTexelPointer
|
|
|
|
// instruction.
|
|
|
|
bool CheckImageTexelPointer(uint32_t index) const;
|
|
|
|
|
2017-11-30 22:03:06 +00:00
|
|
|
// Creates a variable of type |typeId| from the |index|'th element of
|
2019-08-16 17:15:17 +00:00
|
|
|
// |varInst|. The new variable is added to |replacements|. If the variable
|
|
|
|
// could not be created, then |nullptr| is appended to |replacements|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void CreateVariable(uint32_t typeId, Instruction* varInst, uint32_t index,
|
|
|
|
std::vector<Instruction*>* replacements);
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Populates |replacements| with a new OpVariable for each element of |inst|.
|
2019-05-15 13:29:28 +00:00
|
|
|
// Returns true if the replacement variables were successfully created.
|
2017-11-30 22:03:06 +00:00
|
|
|
//
|
|
|
|
// |inst| must be an OpVariable of a composite type. New variables are
|
|
|
|
// initialized the same as the corresponding index in |inst|. |replacements|
|
|
|
|
// will contain a variable for each element of the composite with matching
|
|
|
|
// indexes (i.e. the 0'th element of |inst| is the 0'th entry of
|
|
|
|
// |replacements|).
|
2019-05-15 13:29:28 +00:00
|
|
|
bool CreateReplacementVariables(Instruction* inst,
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Instruction*>* replacements);
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Returns the array length for |arrayInst|.
|
2019-04-26 16:52:23 +00:00
|
|
|
uint64_t GetArrayLength(const Instruction* arrayInst) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Returns the number of elements in |type|.
|
|
|
|
//
|
|
|
|
// |type| must be a vector or matrix type.
|
2019-04-26 16:52:23 +00:00
|
|
|
uint64_t GetNumElements(const Instruction* type) const;
|
2017-11-30 22:03:06 +00:00
|
|
|
|
2018-10-04 13:00:24 +00:00
|
|
|
// Returns true if |id| is a specialization constant.
|
|
|
|
//
|
|
|
|
// |id| must be registered definition.
|
|
|
|
bool IsSpecConstant(uint32_t id) const;
|
|
|
|
|
2017-11-30 22:03:06 +00:00
|
|
|
// Returns an id for a pointer to |id|.
|
|
|
|
uint32_t GetOrCreatePointerType(uint32_t id);
|
|
|
|
|
|
|
|
// Creates the initial value for the |index| element of |source| in |newVar|.
|
|
|
|
//
|
|
|
|
// If there is an initial value for |source| for element |index|, it is
|
|
|
|
// appended as an operand on |newVar|. If the initial value is OpUndef, no
|
|
|
|
// initial value is added to |newVar|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void GetOrCreateInitialValue(Instruction* source, uint32_t index,
|
|
|
|
Instruction* newVar);
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Replaces the load to the entire composite.
|
|
|
|
//
|
|
|
|
// Generates a load for each replacement variable and then creates a new
|
|
|
|
// composite by combining all of the loads.
|
|
|
|
//
|
2019-08-21 17:12:42 +00:00
|
|
|
// |load| must be a load. Returns true if successful.
|
|
|
|
bool ReplaceWholeLoad(Instruction* load,
|
2018-07-12 19:14:43 +00:00
|
|
|
const std::vector<Instruction*>& replacements);
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Replaces the store to the entire composite.
|
|
|
|
//
|
|
|
|
// Generates a composite extract and store for each element in the scalarized
|
2019-08-21 17:12:42 +00:00
|
|
|
// variable from the original store data input. Returns true if successful.
|
|
|
|
bool ReplaceWholeStore(Instruction* store,
|
2018-07-12 19:14:43 +00:00
|
|
|
const std::vector<Instruction*>& replacements);
|
2017-11-30 22:03:06 +00:00
|
|
|
|
2020-07-27 17:02:25 +00:00
|
|
|
// Replaces the DebugDeclare to the entire composite.
|
|
|
|
//
|
|
|
|
// Generates a DebugValue with Deref operation for each element in the
|
|
|
|
// scalarized variable from the original DebugDeclare. Returns true if
|
|
|
|
// successful.
|
|
|
|
bool ReplaceWholeDebugDeclare(Instruction* dbg_decl,
|
|
|
|
const std::vector<Instruction*>& replacements);
|
|
|
|
|
|
|
|
// Replaces the DebugValue to the entire composite.
|
|
|
|
//
|
|
|
|
// Generates a DebugValue for each element in the scalarized variable from
|
|
|
|
// the original DebugValue. Returns true if successful.
|
|
|
|
bool ReplaceWholeDebugValue(Instruction* dbg_value,
|
|
|
|
const std::vector<Instruction*>& replacements);
|
|
|
|
|
2017-11-30 22:03:06 +00:00
|
|
|
// Replaces an access chain to the composite variable with either a direct use
|
|
|
|
// of the appropriate replacement variable or another access chain with the
|
2019-08-21 17:12:42 +00:00
|
|
|
// replacement variable as the base and one fewer indexes. Returns true if
|
|
|
|
// successful.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool ReplaceAccessChain(Instruction* chain,
|
|
|
|
const std::vector<Instruction*>& replacements);
|
2017-11-30 22:03:06 +00:00
|
|
|
|
2018-05-11 21:02:57 +00:00
|
|
|
// Returns a set containing the which components of the result of |inst| are
|
|
|
|
// potentially used. If the return value is |nullptr|, then every components
|
|
|
|
// is possibly used.
|
2019-07-31 19:39:33 +00:00
|
|
|
std::unique_ptr<std::unordered_set<int64_t>> GetUsedComponents(
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* inst);
|
2018-05-11 21:02:57 +00:00
|
|
|
|
2022-02-03 15:51:15 +00:00
|
|
|
// Returns an instruction defining an undefined value type |type_id|.
|
|
|
|
Instruction* GetUndef(uint32_t type_id);
|
2018-05-11 21:02:57 +00:00
|
|
|
|
2017-11-30 22:03:06 +00:00
|
|
|
// Maps storage type to a pointer type enclosing that type.
|
|
|
|
std::unordered_map<uint32_t, uint32_t> pointee_to_pointer_;
|
|
|
|
|
|
|
|
// Maps type id to OpConstantNull for that type.
|
|
|
|
std::unordered_map<uint32_t, uint32_t> type_to_null_;
|
2018-04-16 13:58:00 +00:00
|
|
|
|
2019-08-21 17:12:42 +00:00
|
|
|
// Returns the number of elements in the variable |var_inst|.
|
|
|
|
uint64_t GetMaxLegalIndex(const Instruction* var_inst) const;
|
|
|
|
|
|
|
|
// Returns true if |length| is larger than limit on the size of the variable
|
|
|
|
// that we will be willing to split.
|
|
|
|
bool IsLargerThanSizeLimit(uint64_t length) const;
|
|
|
|
|
2023-05-08 13:39:14 +00:00
|
|
|
// Copies all relevant decorations from `from` to `to`. This includes
|
|
|
|
// decorations applied to the variable, and to the members of the type.
|
|
|
|
// It is assumed that `to` is a variable that is intended to replace the
|
|
|
|
// `member_index`th member of `from`.
|
|
|
|
void CopyDecorationsToVariable(Instruction* from, Instruction* to,
|
|
|
|
uint32_t member_index);
|
|
|
|
|
|
|
|
// Copies pointer related decoration from `from` to `to` if they exist.
|
|
|
|
void CopyPointerDecorationsToVariable(Instruction* from, Instruction* to);
|
|
|
|
|
|
|
|
// Copies decorations that are needed from the `member_index` of `from` to
|
|
|
|
// `to, if there was one.
|
|
|
|
void CopyNecessaryMemberDecorationsToVariable(Instruction* from,
|
|
|
|
Instruction* to,
|
|
|
|
uint32_t member_index);
|
|
|
|
|
2018-04-16 13:58:00 +00:00
|
|
|
// Limit on the number of members in an object that will be replaced.
|
|
|
|
// 0 means there is no limit.
|
|
|
|
uint32_t max_num_elements_;
|
2023-05-08 13:39:14 +00:00
|
|
|
|
2022-06-10 21:55:45 +00:00
|
|
|
// This has to be big enough to fit "scalar-replacement=" followed by a
|
|
|
|
// uint32_t number written in decimal (so 10 digits), and then a
|
|
|
|
// terminating nul.
|
|
|
|
char name_[30];
|
2017-11-30 22:03:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_SCALAR_REPLACEMENT_PASS_H_
|