2016-05-22 18:11:24 +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-05-22 18:11:24 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-05-22 18:11:24 +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-05-22 18:11:24 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_OPT_INSTRUCTION_H_
|
|
|
|
#define SOURCE_OPT_INSTRUCTION_H_
|
2016-05-22 18:11:24 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <functional>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2016-05-22 18:11:24 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-09-15 18:38:53 +00:00
|
|
|
#include "NonSemanticShaderDebugInfo100.h"
|
2020-04-13 13:29:36 +00:00
|
|
|
#include "OpenCLDebugInfo100.h"
|
2021-12-08 17:01:26 +00:00
|
|
|
#include "source/binary.h"
|
2021-07-16 20:28:14 +00:00
|
|
|
#include "source/common_debug_info.h"
|
2020-04-13 13:29:36 +00:00
|
|
|
#include "source/latest_version_glsl_std_450_header.h"
|
|
|
|
#include "source/latest_version_spirv_header.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opcode.h"
|
|
|
|
#include "source/operand.h"
|
2020-04-13 13:29:36 +00:00
|
|
|
#include "source/opt/reflect.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/util/ilist_node.h"
|
|
|
|
#include "source/util/small_vector.h"
|
2021-12-08 17:01:26 +00:00
|
|
|
#include "source/util/string_utils.h"
|
2016-05-22 18:11:24 +00:00
|
|
|
#include "spirv-tools/libspirv.h"
|
|
|
|
|
2022-11-17 18:02:50 +00:00
|
|
|
constexpr uint32_t kNoDebugScope = 0;
|
|
|
|
constexpr uint32_t kNoInlinedAt = 0;
|
2020-03-23 15:01:18 +00:00
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
namespace spvtools {
|
2018-07-09 15:32:29 +00:00
|
|
|
namespace opt {
|
2016-05-22 18:11:24 +00:00
|
|
|
|
|
|
|
class Function;
|
2017-11-14 19:11:50 +00:00
|
|
|
class IRContext;
|
2016-05-22 18:11:24 +00:00
|
|
|
class Module;
|
2017-10-13 18:25:21 +00:00
|
|
|
class InstructionList;
|
2016-05-22 18:11:24 +00:00
|
|
|
|
2018-06-04 18:26:08 +00:00
|
|
|
// Relaxed logical addressing:
|
2017-12-11 18:10:24 +00:00
|
|
|
//
|
|
|
|
// In the logical addressing model, pointers cannot be stored or loaded. This
|
|
|
|
// is a useful assumption because it simplifies the aliasing significantly.
|
|
|
|
// However, for the purpose of legalizing code generated from HLSL, we will have
|
|
|
|
// to allow storing and loading of pointers to opaque objects and runtime
|
|
|
|
// arrays. This relaxation of the rule still implies that function and private
|
|
|
|
// scope variables do not have any aliasing, so we can treat them as before.
|
|
|
|
// This will be call the relaxed logical addressing model.
|
|
|
|
//
|
|
|
|
// This relaxation of the rule will be allowed by |GetBaseAddress|, but it will
|
|
|
|
// enforce that no other pointers are stored or loaded.
|
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
// About operand:
|
|
|
|
//
|
|
|
|
// In the SPIR-V specification, the term "operand" is used to mean any single
|
|
|
|
// SPIR-V word following the leading wordcount-opcode word. Here, the term
|
|
|
|
// "operand" is used to mean a *logical* operand. A logical operand may consist
|
2017-10-13 18:25:21 +00:00
|
|
|
// of multiple SPIR-V words, which together make up the same component. For
|
2016-05-22 18:11:24 +00:00
|
|
|
// example, a logical operand of a 64-bit integer needs two words to express.
|
|
|
|
//
|
|
|
|
// Further, we categorize logical operands into *in* and *out* operands.
|
|
|
|
// In operands are operands actually serve as input to operations, while out
|
|
|
|
// operands are operands that represent ids generated from operations (result
|
|
|
|
// type id or result id). For example, for "OpIAdd %rtype %rid %inop1 %inop2",
|
|
|
|
// "%inop1" and "%inop2" are in operands, while "%rtype" and "%rid" are out
|
|
|
|
// operands.
|
|
|
|
|
|
|
|
// A *logical* operand to a SPIR-V instruction. It can be the type id, result
|
|
|
|
// id, or other additional operands carried in an instruction.
|
|
|
|
struct Operand {
|
2018-06-04 18:26:08 +00:00
|
|
|
using OperandData = utils::SmallVector<uint32_t, 2>;
|
|
|
|
Operand(spv_operand_type_t t, OperandData&& w)
|
2016-05-22 18:11:24 +00:00
|
|
|
: type(t), words(std::move(w)) {}
|
|
|
|
|
2018-06-04 18:26:08 +00:00
|
|
|
Operand(spv_operand_type_t t, const OperandData& w) : type(t), words(w) {}
|
2016-08-12 21:26:35 +00:00
|
|
|
|
2022-02-10 18:31:07 +00:00
|
|
|
template <class InputIt>
|
|
|
|
Operand(spv_operand_type_t t, InputIt firstOperandData,
|
|
|
|
InputIt lastOperandData)
|
|
|
|
: type(t), words(firstOperandData, lastOperandData) {}
|
|
|
|
|
2018-06-04 18:26:08 +00:00
|
|
|
spv_operand_type_t type; // Type of this logical operand.
|
|
|
|
OperandData words; // Binary segments of this logical operand.
|
2016-05-22 18:11:24 +00:00
|
|
|
|
2022-02-02 15:33:18 +00:00
|
|
|
uint32_t AsId() const {
|
|
|
|
assert(spvIsIdType(type));
|
|
|
|
assert(words.size() == 1);
|
|
|
|
return words[0];
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:01:26 +00:00
|
|
|
// Returns a string operand as a std::string.
|
|
|
|
std::string AsString() const {
|
2020-03-19 16:44:28 +00:00
|
|
|
assert(type == SPV_OPERAND_TYPE_LITERAL_STRING);
|
2021-12-08 17:01:26 +00:00
|
|
|
return spvtools::utils::MakeString(words);
|
2020-03-19 16:44:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 13:38:06 +00:00
|
|
|
// Returns a literal integer operand as a uint64_t
|
|
|
|
uint64_t AsLiteralUint64() const {
|
2022-02-02 15:33:18 +00:00
|
|
|
assert(type == SPV_OPERAND_TYPE_LITERAL_INTEGER ||
|
|
|
|
type == SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER ||
|
|
|
|
type == SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER ||
|
|
|
|
type == SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER);
|
2020-04-27 13:38:06 +00:00
|
|
|
assert(1 <= words.size());
|
|
|
|
assert(words.size() <= 2);
|
2021-01-18 18:36:26 +00:00
|
|
|
uint64_t result = 0;
|
|
|
|
if (words.size() > 0) { // Needed to avoid maybe-uninitialized GCC warning
|
|
|
|
uint32_t low = words[0];
|
|
|
|
result = uint64_t(low);
|
|
|
|
}
|
2020-04-27 13:38:06 +00:00
|
|
|
if (words.size() > 1) {
|
2021-01-18 18:36:26 +00:00
|
|
|
uint32_t high = words[1];
|
|
|
|
result = result | (uint64_t(high) << 32);
|
2020-04-27 13:38:06 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
friend bool operator==(const Operand& o1, const Operand& o2) {
|
|
|
|
return o1.type == o2.type && o1.words == o2.words;
|
|
|
|
}
|
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
// TODO(antiagainst): create fields for literal number kind, width, etc.
|
|
|
|
};
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
inline bool operator!=(const Operand& o1, const Operand& o2) {
|
|
|
|
return !(o1 == o2);
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:01:18 +00:00
|
|
|
// This structure is used to represent a DebugScope instruction from
|
2022-01-26 20:13:08 +00:00
|
|
|
// the OpenCL.100.DebugInfo extended instruction set. Note that we can
|
2020-03-23 15:01:18 +00:00
|
|
|
// ignore the result id of DebugScope instruction because it is not
|
|
|
|
// used for anything. We do not keep it to reduce the size of
|
|
|
|
// structure.
|
|
|
|
// TODO: Let validator check that the result id is not used anywhere.
|
|
|
|
class DebugScope {
|
|
|
|
public:
|
|
|
|
DebugScope(uint32_t lexical_scope, uint32_t inlined_at)
|
|
|
|
: lexical_scope_(lexical_scope), inlined_at_(inlined_at) {}
|
|
|
|
|
|
|
|
inline bool operator!=(const DebugScope& d) const {
|
|
|
|
return lexical_scope_ != d.lexical_scope_ || inlined_at_ != d.inlined_at_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accessor functions for |lexical_scope_|.
|
|
|
|
uint32_t GetLexicalScope() const { return lexical_scope_; }
|
|
|
|
void SetLexicalScope(uint32_t scope) { lexical_scope_ = scope; }
|
|
|
|
|
|
|
|
// Accessor functions for |inlined_at_|.
|
|
|
|
uint32_t GetInlinedAt() const { return inlined_at_; }
|
|
|
|
void SetInlinedAt(uint32_t at) { inlined_at_ = at; }
|
|
|
|
|
|
|
|
// Pushes the binary segments for this DebugScope instruction into
|
|
|
|
// the back of *|binary|.
|
|
|
|
void ToBinary(uint32_t type_id, uint32_t result_id, uint32_t ext_set,
|
|
|
|
std::vector<uint32_t>* binary) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// The result id of the lexical scope in which this debug scope is
|
|
|
|
// contained. The value is kNoDebugScope if there is no scope.
|
|
|
|
uint32_t lexical_scope_;
|
|
|
|
|
|
|
|
// The result id of DebugInlinedAt if instruction in this debug scope
|
|
|
|
// is inlined. The value is kNoInlinedAt if it is not inlined.
|
|
|
|
uint32_t inlined_at_;
|
|
|
|
};
|
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
// A SPIR-V instruction. It contains the opcode and any additional logical
|
2016-08-16 15:04:32 +00:00
|
|
|
// operand, including the result id (if any) and result type id (if any). It
|
|
|
|
// may also contain line-related debug instruction (OpLine, OpNoLine) directly
|
|
|
|
// appearing before this instruction. Note that the result id of an instruction
|
|
|
|
// should never change after the instruction being built. If the result id
|
|
|
|
// needs to change, the user should create a new instruction instead.
|
2017-10-13 18:25:21 +00:00
|
|
|
class Instruction : public utils::IntrusiveNodeBase<Instruction> {
|
2016-05-22 18:11:24 +00:00
|
|
|
public:
|
2018-06-04 18:26:08 +00:00
|
|
|
using OperandList = std::vector<Operand>;
|
|
|
|
using iterator = OperandList::iterator;
|
|
|
|
using const_iterator = OperandList::const_iterator;
|
2016-08-25 16:55:44 +00:00
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
// Creates a default OpNop instruction.
|
2017-11-14 19:11:50 +00:00
|
|
|
// This exists solely for containers that can't do without. Should be removed.
|
2017-10-13 18:25:21 +00:00
|
|
|
Instruction()
|
|
|
|
: utils::IntrusiveNodeBase<Instruction>(),
|
2017-11-14 19:11:50 +00:00
|
|
|
context_(nullptr),
|
2022-11-04 21:27:10 +00:00
|
|
|
opcode_(spv::Op::OpNop),
|
2018-07-20 15:09:30 +00:00
|
|
|
has_type_id_(false),
|
|
|
|
has_result_id_(false),
|
2020-03-23 15:01:18 +00:00
|
|
|
unique_id_(0),
|
|
|
|
dbg_scope_(kNoDebugScope, kNoInlinedAt) {}
|
2017-11-14 19:11:50 +00:00
|
|
|
|
|
|
|
// Creates a default OpNop instruction.
|
|
|
|
Instruction(IRContext*);
|
2016-05-22 18:11:24 +00:00
|
|
|
// Creates an instruction with the given opcode |op| and no additional logical
|
|
|
|
// operands.
|
2022-11-04 21:27:10 +00:00
|
|
|
Instruction(IRContext*, spv::Op);
|
2016-05-22 18:11:24 +00:00
|
|
|
// Creates an instruction using the given spv_parsed_instruction_t |inst|. All
|
|
|
|
// the data inside |inst| will be copied and owned in this instance. And keep
|
|
|
|
// record of line-related debug instructions |dbg_line| ahead of this
|
|
|
|
// instruction, if any.
|
2017-11-14 19:11:50 +00:00
|
|
|
Instruction(IRContext* c, const spv_parsed_instruction_t& inst,
|
2016-05-22 18:11:24 +00:00
|
|
|
std::vector<Instruction>&& dbg_line = {});
|
|
|
|
|
2020-03-23 15:01:18 +00:00
|
|
|
Instruction(IRContext* c, const spv_parsed_instruction_t& inst,
|
|
|
|
const DebugScope& dbg_scope);
|
|
|
|
|
2016-08-12 21:26:35 +00:00
|
|
|
// Creates an instruction with the given opcode |op|, type id: |ty_id|,
|
|
|
|
// result id: |res_id| and input operands: |in_operands|.
|
2022-11-04 21:27:10 +00:00
|
|
|
Instruction(IRContext* c, spv::Op op, uint32_t ty_id, uint32_t res_id,
|
2018-06-04 18:26:08 +00:00
|
|
|
const OperandList& in_operands);
|
2016-08-12 21:26:35 +00:00
|
|
|
|
2017-10-13 18:25:21 +00:00
|
|
|
// TODO: I will want to remove these, but will first have to remove the use of
|
|
|
|
// std::vector<Instruction>.
|
2016-07-28 13:09:00 +00:00
|
|
|
Instruction(const Instruction&) = default;
|
|
|
|
Instruction& operator=(const Instruction&) = default;
|
|
|
|
|
2016-08-09 20:57:36 +00:00
|
|
|
Instruction(Instruction&&);
|
|
|
|
Instruction& operator=(Instruction&&);
|
2016-07-28 13:09:00 +00:00
|
|
|
|
2021-02-10 18:21:38 +00:00
|
|
|
~Instruction() override = default;
|
2017-10-13 18:25:21 +00:00
|
|
|
|
|
|
|
// Returns a newly allocated instruction that has the same operands, result,
|
|
|
|
// and type as |this|. The new instruction is not linked into any list.
|
|
|
|
// It is the responsibility of the caller to make sure that the storage is
|
|
|
|
// removed. It is the caller's responsibility to make sure that there is only
|
|
|
|
// one instruction for each result id.
|
2017-11-27 15:16:41 +00:00
|
|
|
Instruction* Clone(IRContext* c) const;
|
2017-11-14 19:11:50 +00:00
|
|
|
|
|
|
|
IRContext* context() const { return context_; }
|
2017-10-13 18:25:21 +00:00
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Op opcode() const { return opcode_; }
|
2016-07-26 16:11:03 +00:00
|
|
|
// Sets the opcode of this instruction to a specific opcode. Note this may
|
|
|
|
// invalidate the instruction.
|
|
|
|
// TODO(qining): Remove this function when instruction building and insertion
|
|
|
|
// is well implemented.
|
2022-11-04 21:27:10 +00:00
|
|
|
void SetOpcode(spv::Op op) { opcode_ = op; }
|
2018-07-20 15:09:30 +00:00
|
|
|
uint32_t type_id() const {
|
|
|
|
return has_type_id_ ? GetSingleWordOperand(0) : 0;
|
|
|
|
}
|
|
|
|
uint32_t result_id() const {
|
|
|
|
return has_result_id_ ? GetSingleWordOperand(has_type_id_ ? 1 : 0) : 0;
|
|
|
|
}
|
2017-11-11 01:26:55 +00:00
|
|
|
uint32_t unique_id() const {
|
|
|
|
assert(unique_id_ != 0);
|
|
|
|
return unique_id_;
|
|
|
|
}
|
2016-05-22 18:11:24 +00:00
|
|
|
// Returns the vector of line-related debug instructions attached to this
|
|
|
|
// instruction and the caller can directly modify them.
|
|
|
|
std::vector<Instruction>& dbg_line_insts() { return dbg_line_insts_; }
|
|
|
|
const std::vector<Instruction>& dbg_line_insts() const {
|
|
|
|
return dbg_line_insts_;
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:09:43 +00:00
|
|
|
const Instruction* dbg_line_inst() const {
|
|
|
|
return dbg_line_insts_.empty() ? nullptr : &dbg_line_insts_[0];
|
|
|
|
}
|
|
|
|
|
2020-01-14 19:35:21 +00:00
|
|
|
// Clear line-related debug instructions attached to this instruction.
|
|
|
|
void clear_dbg_line_insts() { dbg_line_insts_.clear(); }
|
|
|
|
|
2017-10-13 18:25:21 +00:00
|
|
|
// Same semantics as in the base class except the list the InstructionList
|
|
|
|
// containing |pos| will now assume ownership of |this|.
|
|
|
|
// inline void MoveBefore(Instruction* pos);
|
|
|
|
// inline void InsertAfter(Instruction* pos);
|
|
|
|
|
2016-08-25 21:45:08 +00:00
|
|
|
// Begin and end iterators for operands.
|
|
|
|
iterator begin() { return operands_.begin(); }
|
|
|
|
iterator end() { return operands_.end(); }
|
2016-08-26 20:52:16 +00:00
|
|
|
const_iterator begin() const { return operands_.cbegin(); }
|
|
|
|
const_iterator end() const { return operands_.cend(); }
|
2016-08-25 16:55:44 +00:00
|
|
|
// Const begin and end iterators for operands.
|
|
|
|
const_iterator cbegin() const { return operands_.cbegin(); }
|
|
|
|
const_iterator cend() const { return operands_.cend(); }
|
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
// Gets the number of logical operands.
|
|
|
|
uint32_t NumOperands() const {
|
|
|
|
return static_cast<uint32_t>(operands_.size());
|
|
|
|
}
|
|
|
|
// Gets the number of SPIR-V words occupied by all logical operands.
|
|
|
|
uint32_t NumOperandWords() const {
|
|
|
|
return NumInOperandWords() + TypeResultIdCount();
|
|
|
|
}
|
|
|
|
// Gets the |index|-th logical operand.
|
2018-02-20 18:19:57 +00:00
|
|
|
inline Operand& GetOperand(uint32_t index);
|
2016-05-22 18:11:24 +00:00
|
|
|
inline const Operand& GetOperand(uint32_t index) const;
|
2017-10-24 16:28:18 +00:00
|
|
|
// Adds |operand| to the list of operands of this instruction.
|
|
|
|
// It is the responsibility of the caller to make sure
|
|
|
|
// that the instruction remains valid.
|
2017-11-11 01:26:55 +00:00
|
|
|
inline void AddOperand(Operand&& operand);
|
2016-05-22 18:11:24 +00:00
|
|
|
// Gets the |index|-th logical operand as a single SPIR-V word. This method is
|
|
|
|
// not expected to be used with logical operands consisting of multiple SPIR-V
|
|
|
|
// words.
|
|
|
|
uint32_t GetSingleWordOperand(uint32_t index) const;
|
2016-08-16 15:04:32 +00:00
|
|
|
// Sets the |index|-th in-operand's data to the given |data|.
|
2018-06-04 18:26:08 +00:00
|
|
|
inline void SetInOperand(uint32_t index, Operand::OperandData&& data);
|
2018-01-26 12:07:10 +00:00
|
|
|
// Sets the |index|-th operand's data to the given |data|.
|
|
|
|
// This is for in-operands modification only, but with |index| expressed in
|
|
|
|
// terms of operand index rather than in-operand index.
|
2018-06-04 18:26:08 +00:00
|
|
|
inline void SetOperand(uint32_t index, Operand::OperandData&& data);
|
2018-01-24 18:26:33 +00:00
|
|
|
// Replace all of the in operands with those in |new_operands|.
|
2018-06-04 18:26:08 +00:00
|
|
|
inline void SetInOperands(OperandList&& new_operands);
|
2016-08-16 15:04:32 +00:00
|
|
|
// Sets the result type id.
|
|
|
|
inline void SetResultType(uint32_t ty_id);
|
2022-02-02 15:33:18 +00:00
|
|
|
inline bool HasResultType() const { return has_type_id_; }
|
2016-11-10 17:11:50 +00:00
|
|
|
// Sets the result id
|
|
|
|
inline void SetResultId(uint32_t res_id);
|
2018-07-20 15:09:30 +00:00
|
|
|
inline bool HasResultId() const { return has_result_id_; }
|
2020-03-23 15:01:18 +00:00
|
|
|
// Sets DebugScope.
|
|
|
|
inline void SetDebugScope(const DebugScope& scope);
|
|
|
|
inline const DebugScope& GetDebugScope() const { return dbg_scope_; }
|
2021-09-24 14:56:08 +00:00
|
|
|
// Add debug line inst. Renew result id if Debug[No]Line
|
|
|
|
void AddDebugLine(const Instruction* inst);
|
2020-05-21 17:09:43 +00:00
|
|
|
// Updates DebugInlinedAt of DebugScope and OpLine.
|
2020-08-31 14:05:38 +00:00
|
|
|
void UpdateDebugInlinedAt(uint32_t new_inlined_at);
|
2021-09-24 14:56:08 +00:00
|
|
|
// Clear line-related debug instructions attached to this instruction
|
|
|
|
// along with def-use entries.
|
|
|
|
void ClearDbgLineInsts();
|
|
|
|
// Return true if Shader100:Debug[No]Line
|
|
|
|
bool IsDebugLineInst() const;
|
|
|
|
// Return true if Op[No]Line or Shader100:Debug[No]Line
|
|
|
|
bool IsLineInst() const;
|
|
|
|
// Return true if OpLine or Shader100:DebugLine
|
|
|
|
bool IsLine() const;
|
|
|
|
// Return true if OpNoLine or Shader100:DebugNoLine
|
|
|
|
bool IsNoLine() const;
|
2020-05-21 17:09:43 +00:00
|
|
|
inline uint32_t GetDebugInlinedAt() const {
|
|
|
|
return dbg_scope_.GetInlinedAt();
|
|
|
|
}
|
2020-08-26 14:15:36 +00:00
|
|
|
// Updates lexical scope of DebugScope and OpLine.
|
2020-08-31 14:05:38 +00:00
|
|
|
void UpdateLexicalScope(uint32_t scope);
|
2020-05-06 16:57:57 +00:00
|
|
|
// Updates OpLine and DebugScope based on the information of |from|.
|
2020-08-31 14:05:38 +00:00
|
|
|
void UpdateDebugInfoFrom(const Instruction* from);
|
2017-07-13 00:16:51 +00:00
|
|
|
// Remove the |index|-th operand
|
|
|
|
void RemoveOperand(uint32_t index) {
|
|
|
|
operands_.erase(operands_.begin() + index);
|
|
|
|
}
|
2020-07-06 17:48:12 +00:00
|
|
|
// Insert an operand before the |index|-th operand
|
|
|
|
void InsertOperand(uint32_t index, Operand&& operand) {
|
|
|
|
operands_.insert(operands_.begin() + index, operand);
|
|
|
|
}
|
2016-05-22 18:11:24 +00:00
|
|
|
|
|
|
|
// The following methods are similar to the above, but are for in operands.
|
|
|
|
uint32_t NumInOperands() const {
|
|
|
|
return static_cast<uint32_t>(operands_.size() - TypeResultIdCount());
|
|
|
|
}
|
|
|
|
uint32_t NumInOperandWords() const;
|
2018-02-20 18:19:57 +00:00
|
|
|
Operand& GetInOperand(uint32_t index) {
|
|
|
|
return GetOperand(index + TypeResultIdCount());
|
|
|
|
}
|
2016-05-22 18:11:24 +00:00
|
|
|
const Operand& GetInOperand(uint32_t index) const {
|
|
|
|
return GetOperand(index + TypeResultIdCount());
|
|
|
|
}
|
|
|
|
uint32_t GetSingleWordInOperand(uint32_t index) const {
|
|
|
|
return GetSingleWordOperand(index + TypeResultIdCount());
|
|
|
|
}
|
2017-07-13 00:16:51 +00:00
|
|
|
void RemoveInOperand(uint32_t index) {
|
|
|
|
operands_.erase(operands_.begin() + index + TypeResultIdCount());
|
|
|
|
}
|
2016-05-22 18:11:24 +00:00
|
|
|
|
|
|
|
// Returns true if this instruction is OpNop.
|
|
|
|
inline bool IsNop() const;
|
|
|
|
// Turns this instruction to OpNop. This does not clear out all preceding
|
|
|
|
// line-related debug instructions.
|
|
|
|
inline void ToNop();
|
|
|
|
|
2016-08-20 13:47:00 +00:00
|
|
|
// Runs the given function |f| on this instruction and optionally on the
|
|
|
|
// preceding debug line instructions. The function will always be run
|
|
|
|
// if this is itself a debug line instruction.
|
|
|
|
inline void ForEachInst(const std::function<void(Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts = false);
|
|
|
|
inline void ForEachInst(const std::function<void(const Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts = false) const;
|
2016-05-22 18:11:24 +00:00
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
// Runs the given function |f| on this instruction and optionally on the
|
|
|
|
// preceding debug line instructions. The function will always be run
|
|
|
|
// if this is itself a debug line instruction. If |f| returns false,
|
|
|
|
// iteration is terminated and this function returns false.
|
|
|
|
inline bool WhileEachInst(const std::function<bool(Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts = false);
|
|
|
|
inline bool WhileEachInst(const std::function<bool(const Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts = false) const;
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
// Runs the given function |f| on all operand ids.
|
|
|
|
//
|
|
|
|
// |f| should not transform an ID into 0, as 0 is an invalid ID.
|
|
|
|
inline void ForEachId(const std::function<void(uint32_t*)>& f);
|
|
|
|
inline void ForEachId(const std::function<void(const uint32_t*)>& f) const;
|
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
// Runs the given function |f| on all "in" operand ids.
|
2016-11-10 17:11:50 +00:00
|
|
|
inline void ForEachInId(const std::function<void(uint32_t*)>& f);
|
|
|
|
inline void ForEachInId(const std::function<void(const uint32_t*)>& f) const;
|
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
// Runs the given function |f| on all "in" operand ids. If |f| returns false,
|
|
|
|
// iteration is terminated and this function returns false.
|
|
|
|
inline bool WhileEachInId(const std::function<bool(uint32_t*)>& f);
|
|
|
|
inline bool WhileEachInId(
|
|
|
|
const std::function<bool(const uint32_t*)>& f) const;
|
|
|
|
|
|
|
|
// Runs the given function |f| on all "in" operands.
|
2017-10-06 02:37:00 +00:00
|
|
|
inline void ForEachInOperand(const std::function<void(uint32_t*)>& f);
|
2017-11-08 17:40:02 +00:00
|
|
|
inline void ForEachInOperand(
|
|
|
|
const std::function<void(const uint32_t*)>& f) const;
|
2017-10-06 02:37:00 +00:00
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
// Runs the given function |f| on all "in" operands. If |f| returns false,
|
|
|
|
// iteration is terminated and this function return false.
|
|
|
|
inline bool WhileEachInOperand(const std::function<bool(uint32_t*)>& f);
|
|
|
|
inline bool WhileEachInOperand(
|
|
|
|
const std::function<bool(const uint32_t*)>& f) const;
|
|
|
|
|
2020-05-14 10:38:34 +00:00
|
|
|
// Returns true if it's an OpBranchConditional instruction
|
|
|
|
// with branch weights.
|
|
|
|
bool HasBranchWeights() const;
|
|
|
|
|
2016-11-10 17:11:50 +00:00
|
|
|
// Returns true if any operands can be labels
|
2017-05-05 02:55:53 +00:00
|
|
|
inline bool HasLabels() const;
|
2016-11-10 17:11:50 +00:00
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
// Pushes the binary segments for this instruction into the back of *|binary|.
|
2016-08-20 13:47:00 +00:00
|
|
|
void ToBinaryWithoutAttachedDebugInsts(std::vector<uint32_t>* binary) const;
|
2016-05-22 18:11:24 +00:00
|
|
|
|
2017-09-06 12:56:41 +00:00
|
|
|
// Replaces the operands to the instruction with |new_operands|. The caller
|
|
|
|
// is responsible for building a complete and valid list of operands for
|
|
|
|
// this instruction.
|
2018-06-04 18:26:08 +00:00
|
|
|
void ReplaceOperands(const OperandList& new_operands);
|
2017-09-06 12:56:41 +00:00
|
|
|
|
2017-11-13 20:31:43 +00:00
|
|
|
// Returns true if the instruction annotates an id with a decoration.
|
2017-11-11 01:26:55 +00:00
|
|
|
inline bool IsDecoration() const;
|
|
|
|
|
|
|
|
// Returns true if the instruction is known to be a load from read-only
|
|
|
|
// memory.
|
|
|
|
bool IsReadOnlyLoad() const;
|
|
|
|
|
|
|
|
// Returns the instruction that gives the base address of an address
|
2018-01-04 16:37:19 +00:00
|
|
|
// calculation. The instruction must be a load, as defined by |IsLoad|,
|
|
|
|
// store, copy, or access chain instruction. In logical addressing mode, will
|
|
|
|
// return an OpVariable or OpFunctionParameter instruction. For relaxed
|
|
|
|
// logical addressing, it would also return a load of a pointer to an opaque
|
|
|
|
// object. For physical addressing mode, could return other types of
|
|
|
|
// instructions.
|
2017-11-11 01:26:55 +00:00
|
|
|
Instruction* GetBaseAddress() const;
|
|
|
|
|
2018-01-04 16:37:19 +00:00
|
|
|
// Returns true if the instruction loads from memory or samples an image, and
|
|
|
|
// stores the result into an id. It considers only core instructions.
|
|
|
|
// Memory-to-memory instructions are not considered loads.
|
2017-11-11 01:26:55 +00:00
|
|
|
inline bool IsLoad() const;
|
|
|
|
|
2020-04-30 21:47:20 +00:00
|
|
|
// Returns true if the instruction generates a pointer that is definitely
|
|
|
|
// read-only. This is determined by analysing the pointer type's storage
|
|
|
|
// class and decorations that target the pointer's id. It does not analyse
|
|
|
|
// other instructions that the pointer may be derived from. Thus if 'true' is
|
|
|
|
// returned, the pointer is definitely read-only, while if 'false' is returned
|
|
|
|
// it is possible that the pointer may actually be read-only if it is derived
|
|
|
|
// from another pointer that is decorated as read-only.
|
|
|
|
bool IsReadOnlyPointer() const;
|
2017-11-11 01:26:55 +00:00
|
|
|
|
|
|
|
// The following functions check for the various descriptor types defined in
|
|
|
|
// the Vulkan specification section 13.1.
|
|
|
|
|
|
|
|
// Returns true if the instruction defines a pointer type that points to a
|
|
|
|
// storage image.
|
|
|
|
bool IsVulkanStorageImage() const;
|
|
|
|
|
|
|
|
// Returns true if the instruction defines a pointer type that points to a
|
|
|
|
// sampled image.
|
|
|
|
bool IsVulkanSampledImage() const;
|
|
|
|
|
|
|
|
// Returns true if the instruction defines a pointer type that points to a
|
|
|
|
// storage texel buffer.
|
|
|
|
bool IsVulkanStorageTexelBuffer() const;
|
|
|
|
|
|
|
|
// Returns true if the instruction defines a pointer type that points to a
|
|
|
|
// storage buffer.
|
|
|
|
bool IsVulkanStorageBuffer() const;
|
|
|
|
|
2021-10-01 12:31:40 +00:00
|
|
|
// Returns true if the instruction defines a variable in StorageBuffer or
|
|
|
|
// Uniform storage class with a pointer type that points to a storage buffer.
|
|
|
|
bool IsVulkanStorageBufferVariable() const;
|
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
// Returns true if the instruction defines a pointer type that points to a
|
|
|
|
// uniform buffer.
|
|
|
|
bool IsVulkanUniformBuffer() const;
|
|
|
|
|
2018-10-12 12:46:35 +00:00
|
|
|
// Returns true if the instruction is an atom operation that uses original
|
|
|
|
// value.
|
|
|
|
inline bool IsAtomicWithLoad() const;
|
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
// Returns true if the instruction is an atom operation.
|
|
|
|
inline bool IsAtomicOp() const;
|
2017-11-13 20:31:43 +00:00
|
|
|
|
2017-11-17 13:59:25 +00:00
|
|
|
// Returns true if this instruction is a branch or switch instruction (either
|
|
|
|
// conditional or not).
|
|
|
|
bool IsBranch() const { return spvOpcodeIsBranch(opcode()); }
|
|
|
|
|
|
|
|
// Returns true if this instruction causes the function to finish execution
|
|
|
|
// and return to its caller
|
|
|
|
bool IsReturn() const { return spvOpcodeIsReturn(opcode()); }
|
|
|
|
|
2018-01-03 20:25:03 +00:00
|
|
|
// Returns true if this instruction exits this function or aborts execution.
|
|
|
|
bool IsReturnOrAbort() const { return spvOpcodeIsReturnOrAbort(opcode()); }
|
|
|
|
|
2017-11-17 13:59:25 +00:00
|
|
|
// Returns true if this instruction is a basic block terminator.
|
|
|
|
bool IsBlockTerminator() const {
|
|
|
|
return spvOpcodeIsBlockTerminator(opcode());
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:39:25 +00:00
|
|
|
// Returns true if |this| is an instruction that define an opaque type. Since
|
2017-12-11 18:10:24 +00:00
|
|
|
// runtime array have similar characteristics they are included as opaque
|
|
|
|
// types.
|
|
|
|
bool IsOpaqueType() const;
|
|
|
|
|
2017-12-05 16:39:25 +00:00
|
|
|
// Returns true if |this| is an instruction which could be folded into a
|
|
|
|
// constant value.
|
|
|
|
bool IsFoldable() const;
|
|
|
|
|
2018-02-15 15:41:01 +00:00
|
|
|
// Returns true if |this| is an instruction which could be folded into a
|
|
|
|
// constant value by |FoldScalar|.
|
|
|
|
bool IsFoldableByFoldScalar() const;
|
|
|
|
|
2018-02-17 19:55:54 +00:00
|
|
|
// Returns true if we are allowed to fold or otherwise manipulate the
|
|
|
|
// instruction that defines |id| in the given context. This includes not
|
|
|
|
// handling NaN values.
|
|
|
|
bool IsFloatingPointFoldingAllowed() const;
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
inline bool operator==(const Instruction&) const;
|
|
|
|
inline bool operator!=(const Instruction&) const;
|
|
|
|
inline bool operator<(const Instruction&) const;
|
|
|
|
|
2019-07-18 15:37:28 +00:00
|
|
|
// Takes ownership of the instruction owned by |i| and inserts it immediately
|
2019-07-30 23:52:46 +00:00
|
|
|
// before |this|. Returns the inserted instruction.
|
2017-11-21 19:47:46 +00:00
|
|
|
Instruction* InsertBefore(std::unique_ptr<Instruction>&& i);
|
2019-07-18 15:37:28 +00:00
|
|
|
// Takes ownership of the instructions in |list| and inserts them in order
|
|
|
|
// immediately before |this|. Returns the first inserted instruction.
|
|
|
|
// Assumes the list is non-empty.
|
|
|
|
Instruction* InsertBefore(std::vector<std::unique_ptr<Instruction>>&& list);
|
2017-11-21 19:47:46 +00:00
|
|
|
using utils::IntrusiveNodeBase<Instruction>::InsertBefore;
|
|
|
|
|
2018-01-09 17:45:46 +00:00
|
|
|
// Returns true if |this| is an instruction defining a constant, but not a
|
|
|
|
// Spec constant.
|
|
|
|
inline bool IsConstant() const;
|
|
|
|
|
2018-01-29 10:39:55 +00:00
|
|
|
// Returns true if |this| is an instruction with an opcode safe to move
|
|
|
|
bool IsOpcodeCodeMotionSafe() const;
|
|
|
|
|
2018-01-10 19:23:47 +00:00
|
|
|
// Pretty-prints |inst|.
|
|
|
|
//
|
|
|
|
// Provides the disassembly of a specific instruction. Utilizes |inst|'s
|
|
|
|
// context to provide the correct interpretation of types, constants, etc.
|
|
|
|
//
|
|
|
|
// |options| are the disassembly options. SPV_BINARY_TO_TEXT_OPTION_NO_HEADER
|
|
|
|
// is always added to |options|.
|
|
|
|
std::string PrettyPrint(uint32_t options = 0u) const;
|
|
|
|
|
2018-05-02 18:54:26 +00:00
|
|
|
// Returns true if the result can be a vector and the result of each component
|
|
|
|
// depends on the corresponding component of any vector inputs.
|
2018-04-27 17:52:05 +00:00
|
|
|
bool IsScalarizable() const;
|
|
|
|
|
2018-05-02 18:54:26 +00:00
|
|
|
// Return true if the only effect of this instructions is the result.
|
|
|
|
bool IsOpcodeSafeToDelete() const;
|
|
|
|
|
2018-07-19 17:52:24 +00:00
|
|
|
// Returns true if it is valid to use the result of |inst| as the base
|
|
|
|
// pointer for a load or store. In this case, valid is defined by the relaxed
|
|
|
|
// logical addressing rules when using logical addressing. Normal validation
|
|
|
|
// rules for physical addressing.
|
|
|
|
bool IsValidBasePointer() const;
|
|
|
|
|
2020-04-13 13:29:36 +00:00
|
|
|
// Returns debug opcode of an OpenCL.100.DebugInfo instruction. If
|
|
|
|
// it is not an OpenCL.100.DebugInfo instruction, just returns
|
|
|
|
// OpenCLDebugInfo100InstructionsMax.
|
|
|
|
OpenCLDebugInfo100Instructions GetOpenCL100DebugOpcode() const;
|
|
|
|
|
2021-09-15 18:38:53 +00:00
|
|
|
// Returns debug opcode of an NonSemantic.Shader.DebugInfo.100 instruction. If
|
|
|
|
// it is not an NonSemantic.Shader.DebugInfo.100 instruction, just return
|
|
|
|
// NonSemanticShaderDebugInfo100InstructionsMax.
|
|
|
|
NonSemanticShaderDebugInfo100Instructions GetShader100DebugOpcode() const;
|
2021-07-16 20:28:14 +00:00
|
|
|
|
|
|
|
// Returns debug opcode of an OpenCL.100.DebugInfo or
|
2021-09-15 18:38:53 +00:00
|
|
|
// NonSemantic.Shader.DebugInfo.100 instruction. Since these overlap, we
|
2021-07-16 20:28:14 +00:00
|
|
|
// return the OpenCLDebugInfo code
|
|
|
|
CommonDebugInfoInstructions GetCommonDebugOpcode() const;
|
|
|
|
|
2020-07-08 20:34:00 +00:00
|
|
|
// Returns true if it is an OpenCL.DebugInfo.100 instruction.
|
|
|
|
bool IsOpenCL100DebugInstr() const {
|
|
|
|
return GetOpenCL100DebugOpcode() != OpenCLDebugInfo100InstructionsMax;
|
2021-07-16 20:28:14 +00:00
|
|
|
}
|
2021-09-15 18:38:53 +00:00
|
|
|
|
|
|
|
// Returns true if it is an NonSemantic.Shader.DebugInfo.100 instruction.
|
|
|
|
bool IsShader100DebugInstr() const {
|
|
|
|
return GetShader100DebugOpcode() !=
|
|
|
|
NonSemanticShaderDebugInfo100InstructionsMax;
|
2021-07-16 20:28:14 +00:00
|
|
|
}
|
|
|
|
bool IsCommonDebugInstr() const {
|
|
|
|
return GetCommonDebugOpcode() != CommonDebugInfoInstructionsMax;
|
2020-07-08 20:34:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 18:54:14 +00:00
|
|
|
// Returns true if this instructions a non-semantic instruction.
|
|
|
|
bool IsNonSemanticInstruction() const;
|
|
|
|
|
2018-09-14 17:57:12 +00:00
|
|
|
// Dump this instruction on stderr. Useful when running interactive
|
|
|
|
// debuggers.
|
|
|
|
void Dump() const;
|
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
private:
|
2017-09-06 12:56:41 +00:00
|
|
|
// Returns the total count of result type id and result id.
|
2016-05-22 18:11:24 +00:00
|
|
|
uint32_t TypeResultIdCount() const {
|
2018-07-20 15:09:30 +00:00
|
|
|
if (has_type_id_ && has_result_id_) return 2;
|
|
|
|
if (has_type_id_ || has_result_id_) return 1;
|
|
|
|
return 0;
|
2016-05-22 18:11:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 21:47:20 +00:00
|
|
|
// Returns true if the instruction generates a read-only pointer, with the
|
|
|
|
// same caveats documented in the comment for IsReadOnlyPointer. The first
|
|
|
|
// version assumes the module is a shader module. The second assumes a
|
2017-11-11 01:26:55 +00:00
|
|
|
// kernel.
|
2020-04-30 21:47:20 +00:00
|
|
|
bool IsReadOnlyPointerShaders() const;
|
|
|
|
bool IsReadOnlyPointerKernel() const;
|
2017-12-11 18:10:24 +00:00
|
|
|
|
2018-01-04 16:37:19 +00:00
|
|
|
// Returns true if the result of |inst| can be used as the base image for an
|
|
|
|
// instruction that samples a image, reads an image, or writes to an image.
|
|
|
|
bool IsValidBaseImage() const;
|
2017-11-11 01:26:55 +00:00
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
IRContext* context_; // IR Context
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Op opcode_; // Opcode
|
2018-07-20 15:09:30 +00:00
|
|
|
bool has_type_id_; // True if the instruction has a type id
|
|
|
|
bool has_result_id_; // True if the instruction has a result id
|
2017-11-14 19:11:50 +00:00
|
|
|
uint32_t unique_id_; // Unique instruction id
|
2016-05-22 18:11:24 +00:00
|
|
|
// All logical operands, including result type id and result id.
|
2018-06-04 18:26:08 +00:00
|
|
|
OperandList operands_;
|
2021-09-24 14:56:08 +00:00
|
|
|
// Op[No]Line or Debug[No]Line instructions preceding this instruction. Note
|
|
|
|
// that for Instructions representing Op[No]Line or Debug[No]Line themselves,
|
|
|
|
// this field should be empty.
|
2016-05-22 18:11:24 +00:00
|
|
|
std::vector<Instruction> dbg_line_insts_;
|
2017-10-13 18:25:21 +00:00
|
|
|
|
2020-03-23 15:01:18 +00:00
|
|
|
// DebugScope that wraps this instruction.
|
|
|
|
DebugScope dbg_scope_;
|
|
|
|
|
2017-10-13 18:25:21 +00:00
|
|
|
friend InstructionList;
|
2016-05-22 18:11:24 +00:00
|
|
|
};
|
|
|
|
|
2018-01-10 19:23:47 +00:00
|
|
|
// Pretty-prints |inst| to |str| and returns |str|.
|
|
|
|
//
|
|
|
|
// Provides the disassembly of a specific instruction. Utilizes |inst|'s context
|
|
|
|
// to provide the correct interpretation of types, constants, etc.
|
|
|
|
//
|
|
|
|
// Disassembly uses raw ids (not pretty printed names).
|
2018-07-12 19:14:43 +00:00
|
|
|
std::ostream& operator<<(std::ostream& str, const Instruction& inst);
|
2018-01-10 19:23:47 +00:00
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
inline bool Instruction::operator==(const Instruction& other) const {
|
|
|
|
return unique_id() == other.unique_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Instruction::operator!=(const Instruction& other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Instruction::operator<(const Instruction& other) const {
|
|
|
|
return unique_id() < other.unique_id();
|
|
|
|
}
|
|
|
|
|
2018-02-20 18:19:57 +00:00
|
|
|
inline Operand& Instruction::GetOperand(uint32_t index) {
|
|
|
|
assert(index < operands_.size() && "operand index out of bound");
|
|
|
|
return operands_[index];
|
|
|
|
}
|
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
inline const Operand& Instruction::GetOperand(uint32_t index) const {
|
|
|
|
assert(index < operands_.size() && "operand index out of bound");
|
|
|
|
return operands_[index];
|
2018-02-20 18:19:57 +00:00
|
|
|
}
|
2016-05-22 18:11:24 +00:00
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
inline void Instruction::AddOperand(Operand&& operand) {
|
2017-11-17 13:59:25 +00:00
|
|
|
operands_.push_back(std::move(operand));
|
2017-10-24 16:28:18 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 15:04:32 +00:00
|
|
|
inline void Instruction::SetInOperand(uint32_t index,
|
2018-06-04 18:26:08 +00:00
|
|
|
Operand::OperandData&& data) {
|
2018-01-26 12:07:10 +00:00
|
|
|
SetOperand(index + TypeResultIdCount(), std::move(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Instruction::SetOperand(uint32_t index,
|
2018-06-04 18:26:08 +00:00
|
|
|
Operand::OperandData&& data) {
|
2018-01-26 12:07:10 +00:00
|
|
|
assert(index < operands_.size() && "operand index out of bound");
|
|
|
|
assert(index >= TypeResultIdCount() && "operand is not a in-operand");
|
|
|
|
operands_[index].words = std::move(data);
|
2016-08-16 15:04:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 18:26:08 +00:00
|
|
|
inline void Instruction::SetInOperands(OperandList&& new_operands) {
|
2018-01-24 18:26:33 +00:00
|
|
|
// Remove the old in operands.
|
|
|
|
operands_.erase(operands_.begin() + TypeResultIdCount(), operands_.end());
|
|
|
|
// Add the new in operands.
|
|
|
|
operands_.insert(operands_.end(), new_operands.begin(), new_operands.end());
|
|
|
|
}
|
|
|
|
|
2016-11-10 17:11:50 +00:00
|
|
|
inline void Instruction::SetResultId(uint32_t res_id) {
|
2018-07-20 15:09:30 +00:00
|
|
|
// TODO(dsinclair): Allow setting a result id if there wasn't one
|
|
|
|
// previously. Need to make room in the operands_ array to place the result,
|
|
|
|
// and update the has_result_id_ flag.
|
|
|
|
assert(has_result_id_);
|
|
|
|
|
|
|
|
// TODO(dsinclair): Allow removing the result id. This needs to make sure,
|
|
|
|
// if there was a result id previously to remove it from the operands_ array
|
|
|
|
// and reset the has_result_id_ flag.
|
|
|
|
assert(res_id != 0);
|
|
|
|
|
|
|
|
auto ridx = has_type_id_ ? 1 : 0;
|
2016-11-10 17:11:50 +00:00
|
|
|
operands_[ridx].words = {res_id};
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:01:18 +00:00
|
|
|
inline void Instruction::SetDebugScope(const DebugScope& scope) {
|
|
|
|
dbg_scope_ = scope;
|
|
|
|
for (auto& i : dbg_line_insts_) {
|
|
|
|
i.dbg_scope_ = scope;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 15:04:32 +00:00
|
|
|
inline void Instruction::SetResultType(uint32_t ty_id) {
|
2018-07-20 15:09:30 +00:00
|
|
|
// TODO(dsinclair): Allow setting a type id if there wasn't one
|
|
|
|
// previously. Need to make room in the operands_ array to place the result,
|
|
|
|
// and update the has_type_id_ flag.
|
|
|
|
assert(has_type_id_);
|
|
|
|
|
|
|
|
// TODO(dsinclair): Allow removing the type id. This needs to make sure,
|
|
|
|
// if there was a type id previously to remove it from the operands_ array
|
|
|
|
// and reset the has_type_id_ flag.
|
|
|
|
assert(ty_id != 0);
|
|
|
|
|
|
|
|
operands_.front().words = {ty_id};
|
2016-05-22 18:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Instruction::IsNop() const {
|
2022-11-04 21:27:10 +00:00
|
|
|
return opcode_ == spv::Op::OpNop && !has_type_id_ && !has_result_id_ &&
|
2016-05-22 18:11:24 +00:00
|
|
|
operands_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Instruction::ToNop() {
|
2022-11-04 21:27:10 +00:00
|
|
|
opcode_ = spv::Op::OpNop;
|
2018-07-20 15:09:30 +00:00
|
|
|
has_type_id_ = false;
|
|
|
|
has_result_id_ = false;
|
2016-05-22 18:11:24 +00:00
|
|
|
operands_.clear();
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
inline bool Instruction::WhileEachInst(
|
|
|
|
const std::function<bool(Instruction*)>& f, bool run_on_debug_line_insts) {
|
|
|
|
if (run_on_debug_line_insts) {
|
|
|
|
for (auto& dbg_line : dbg_line_insts_) {
|
|
|
|
if (!f(&dbg_line)) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Instruction::WhileEachInst(
|
|
|
|
const std::function<bool(const Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts) const {
|
|
|
|
if (run_on_debug_line_insts) {
|
|
|
|
for (auto& dbg_line : dbg_line_insts_) {
|
|
|
|
if (!f(&dbg_line)) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f(this);
|
|
|
|
}
|
|
|
|
|
2016-08-20 13:47:00 +00:00
|
|
|
inline void Instruction::ForEachInst(const std::function<void(Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts) {
|
2018-01-12 20:05:53 +00:00
|
|
|
WhileEachInst(
|
|
|
|
[&f](Instruction* inst) {
|
|
|
|
f(inst);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
run_on_debug_line_insts);
|
2016-08-20 13:47:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
inline void Instruction::ForEachInst(
|
2016-08-20 13:47:00 +00:00
|
|
|
const std::function<void(const Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts) const {
|
2018-01-12 20:05:53 +00:00
|
|
|
WhileEachInst(
|
|
|
|
[&f](const Instruction* inst) {
|
|
|
|
f(inst);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
run_on_debug_line_insts);
|
2016-05-22 18:11:24 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
inline void Instruction::ForEachId(const std::function<void(uint32_t*)>& f) {
|
2020-08-05 19:28:05 +00:00
|
|
|
for (auto& operand : operands_)
|
|
|
|
if (spvIsIdType(operand.type)) f(&operand.words[0]);
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void Instruction::ForEachId(
|
|
|
|
const std::function<void(const uint32_t*)>& f) const {
|
2020-08-05 19:28:05 +00:00
|
|
|
for (const auto& operand : operands_)
|
|
|
|
if (spvIsIdType(operand.type)) f(&operand.words[0]);
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
inline bool Instruction::WhileEachInId(
|
|
|
|
const std::function<bool(uint32_t*)>& f) {
|
2020-08-05 19:28:05 +00:00
|
|
|
for (auto& operand : operands_) {
|
|
|
|
if (spvIsInIdType(operand.type) && !f(&operand.words[0])) {
|
|
|
|
return false;
|
2017-07-04 20:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
2016-11-10 17:11:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
inline bool Instruction::WhileEachInId(
|
|
|
|
const std::function<bool(const uint32_t*)>& f) const {
|
2020-08-05 19:28:05 +00:00
|
|
|
for (const auto& operand : operands_) {
|
|
|
|
if (spvIsInIdType(operand.type) && !f(&operand.words[0])) {
|
|
|
|
return false;
|
2017-07-04 20:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
2016-11-10 17:11:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
inline void Instruction::ForEachInId(const std::function<void(uint32_t*)>& f) {
|
|
|
|
WhileEachInId([&f](uint32_t* id) {
|
|
|
|
f(id);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Instruction::ForEachInId(
|
|
|
|
const std::function<void(const uint32_t*)>& f) const {
|
|
|
|
WhileEachInId([&f](const uint32_t* id) {
|
|
|
|
f(id);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Instruction::WhileEachInOperand(
|
|
|
|
const std::function<bool(uint32_t*)>& f) {
|
2020-08-05 19:28:05 +00:00
|
|
|
for (auto& operand : operands_) {
|
|
|
|
switch (operand.type) {
|
2017-10-06 02:37:00 +00:00
|
|
|
case SPV_OPERAND_TYPE_RESULT_ID:
|
|
|
|
case SPV_OPERAND_TYPE_TYPE_ID:
|
|
|
|
break;
|
|
|
|
default:
|
2020-08-05 19:28:05 +00:00
|
|
|
if (!f(&operand.words[0])) return false;
|
2017-10-06 02:37:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
2017-10-06 02:37:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
inline bool Instruction::WhileEachInOperand(
|
|
|
|
const std::function<bool(const uint32_t*)>& f) const {
|
2020-08-05 19:28:05 +00:00
|
|
|
for (const auto& operand : operands_) {
|
|
|
|
switch (operand.type) {
|
2017-10-06 02:37:00 +00:00
|
|
|
case SPV_OPERAND_TYPE_RESULT_ID:
|
|
|
|
case SPV_OPERAND_TYPE_TYPE_ID:
|
|
|
|
break;
|
|
|
|
default:
|
2020-08-05 19:28:05 +00:00
|
|
|
if (!f(&operand.words[0])) return false;
|
2017-10-06 02:37:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Instruction::ForEachInOperand(
|
|
|
|
const std::function<void(uint32_t*)>& f) {
|
2020-08-05 19:28:05 +00:00
|
|
|
WhileEachInOperand([&f](uint32_t* operand) {
|
|
|
|
f(operand);
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Instruction::ForEachInOperand(
|
|
|
|
const std::function<void(const uint32_t*)>& f) const {
|
2020-08-05 19:28:05 +00:00
|
|
|
WhileEachInOperand([&f](const uint32_t* operand) {
|
|
|
|
f(operand);
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
|
|
|
});
|
2017-10-06 02:37:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 02:55:53 +00:00
|
|
|
inline bool Instruction::HasLabels() const {
|
2016-11-10 17:11:50 +00:00
|
|
|
switch (opcode_) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSelectionMerge:
|
|
|
|
case spv::Op::OpBranch:
|
|
|
|
case spv::Op::OpLoopMerge:
|
|
|
|
case spv::Op::OpBranchConditional:
|
|
|
|
case spv::Op::OpSwitch:
|
|
|
|
case spv::Op::OpPhi:
|
2016-11-10 17:11:50 +00:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
bool Instruction::IsDecoration() const {
|
2017-11-13 20:31:43 +00:00
|
|
|
return spvOpcodeIsDecoration(opcode());
|
|
|
|
}
|
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
bool Instruction::IsLoad() const { return spvOpcodeIsLoad(opcode()); }
|
|
|
|
|
2018-10-12 12:46:35 +00:00
|
|
|
bool Instruction::IsAtomicWithLoad() const {
|
|
|
|
return spvOpcodeIsAtomicWithLoad(opcode());
|
|
|
|
}
|
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
bool Instruction::IsAtomicOp() const { return spvOpcodeIsAtomicOp(opcode()); }
|
2018-01-09 17:45:46 +00:00
|
|
|
|
2018-01-13 05:28:40 +00:00
|
|
|
bool Instruction::IsConstant() const {
|
2023-02-15 20:09:55 +00:00
|
|
|
return IsConstantInst(opcode()) && !IsSpecConstantInst(opcode());
|
2018-01-13 05:28:40 +00:00
|
|
|
}
|
2018-07-09 15:32:29 +00:00
|
|
|
} // namespace opt
|
2016-05-22 18:11:24 +00:00
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_INSTRUCTION_H_
|