Skip to content

Commit

Permalink
[update #69] SPIRV-Tools v2024.4.rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
tiawl committed Sep 20, 2024
2 parents 5a4feaf + f6e4f98 commit 4f3fd56
Show file tree
Hide file tree
Showing 61 changed files with 2,022 additions and 461 deletions.
2 changes: 1 addition & 1 deletion .references/spirv-tools
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2024.3
v2024.4.rc1
9 changes: 8 additions & 1 deletion spirv-tools/source/binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,11 @@ spv_result_t Parser::parseOperand(size_t inst_offset,
case SPV_OPERAND_TYPE_OVERFLOW_MODES:
case SPV_OPERAND_TYPE_PACKED_VECTOR_FORMAT:
case SPV_OPERAND_TYPE_OPTIONAL_PACKED_VECTOR_FORMAT:
case SPV_OPERAND_TYPE_FPENCODING:
case SPV_OPERAND_TYPE_OPTIONAL_FPENCODING:
case SPV_OPERAND_TYPE_HOST_ACCESS_QUALIFIER:
case SPV_OPERAND_TYPE_LOAD_CACHE_CONTROL:
case SPV_OPERAND_TYPE_STORE_CACHE_CONTROL:
case SPV_OPERAND_TYPE_NAMED_MAXIMUM_NUMBER_OF_REGISTERS: {
// A single word that is a plain enum value.

Expand All @@ -679,6 +684,8 @@ spv_result_t Parser::parseOperand(size_t inst_offset,
parsed_operand.type = SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
if (type == SPV_OPERAND_TYPE_OPTIONAL_PACKED_VECTOR_FORMAT)
parsed_operand.type = SPV_OPERAND_TYPE_PACKED_VECTOR_FORMAT;
if (type == SPV_OPERAND_TYPE_OPTIONAL_FPENCODING)
parsed_operand.type = SPV_OPERAND_TYPE_FPENCODING;

spv_operand_desc entry;
if (grammar_.lookupOperand(type, word, &entry)) {
Expand All @@ -699,7 +706,7 @@ spv_result_t Parser::parseOperand(size_t inst_offset,
<< ", if you are creating a new source language please use "
"value 0 "
"(Unknown) and when ready, add your source language to "
"SPRIV-Headers";
"SPIRV-Headers";
}
// Prepare to accept operands to this operand, if needed.
spvPushOperandTypes(entry->operandTypes, expected_operands);
Expand Down
3 changes: 2 additions & 1 deletion spirv-tools/source/disassemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ uint32_t GetLineLengthWithoutColor(const std::string line) {
if (line[i] == '\x1b') {
do {
++i;
} while (line[i] != 'm');
} while (i < line.size() && line[i] != 'm');
continue;
}

Expand Down Expand Up @@ -944,6 +944,7 @@ void InstructionDisassembler::EmitOperand(std::ostream& stream,
case SPV_OPERAND_TYPE_FPDENORM_MODE:
case SPV_OPERAND_TYPE_FPOPERATION_MODE:
case SPV_OPERAND_TYPE_QUANTIZATION_MODES:
case SPV_OPERAND_TYPE_FPENCODING:
case SPV_OPERAND_TYPE_OVERFLOW_MODES: {
spv_operand_desc entry;
if (grammar_.lookupOperand(operand.type, word, &entry))
Expand Down
21 changes: 9 additions & 12 deletions spirv-tools/source/name_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,15 @@
#include "source/binary.h"
#include "source/latest_version_spirv_header.h"
#include "source/parsed_operand.h"
#include "source/to_string.h"
#include "spirv-tools/libspirv.h"

namespace spvtools {
namespace {

// Converts a uint32_t to its string decimal representation.
std::string to_string(uint32_t id) {
// Use stringstream, since some versions of Android compilers lack
// std::to_string.
std::stringstream os;
os << id;
return os.str();
NameMapper GetTrivialNameMapper() {
return [](uint32_t i) { return spvtools::to_string(i); };
}

} // anonymous namespace

NameMapper GetTrivialNameMapper() { return to_string; }

FriendlyNameMapper::FriendlyNameMapper(const spv_const_context context,
const uint32_t* code,
const size_t wordCount)
Expand Down Expand Up @@ -218,6 +209,7 @@ spv_result_t FriendlyNameMapper::ParseInstruction(
} break;
case spv::Op::OpTypeFloat: {
const auto bit_width = inst.words[2];
// TODO: Handle optional fpencoding enum once actually used.
switch (bit_width) {
case 16:
SaveName(result_id, "half");
Expand Down Expand Up @@ -255,6 +247,11 @@ spv_result_t FriendlyNameMapper::ParseInstruction(
inst.words[2]) +
"_" + NameForId(inst.words[3]));
break;
case spv::Op::OpTypeUntypedPointerKHR:
SaveName(result_id, std::string("_ptr_") +
NameForEnumOperand(SPV_OPERAND_TYPE_STORAGE_CLASS,
inst.words[2]));
break;
case spv::Op::OpTypePipe:
SaveName(result_id,
std::string("Pipe") +
Expand Down
22 changes: 22 additions & 0 deletions spirv-tools/source/opcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ int32_t spvOpcodeIsComposite(const spv::Op opcode) {
case spv::Op::OpTypeMatrix:
case spv::Op::OpTypeArray:
case spv::Op::OpTypeStruct:
case spv::Op::OpTypeRuntimeArray:
case spv::Op::OpTypeCooperativeMatrixNV:
case spv::Op::OpTypeCooperativeMatrixKHR:
return true;
Expand All @@ -287,15 +288,19 @@ int32_t spvOpcodeIsComposite(const spv::Op opcode) {
bool spvOpcodeReturnsLogicalVariablePointer(const spv::Op opcode) {
switch (opcode) {
case spv::Op::OpVariable:
case spv::Op::OpUntypedVariableKHR:
case spv::Op::OpAccessChain:
case spv::Op::OpInBoundsAccessChain:
case spv::Op::OpUntypedAccessChainKHR:
case spv::Op::OpUntypedInBoundsAccessChainKHR:
case spv::Op::OpFunctionParameter:
case spv::Op::OpImageTexelPointer:
case spv::Op::OpCopyObject:
case spv::Op::OpSelect:
case spv::Op::OpPhi:
case spv::Op::OpFunctionCall:
case spv::Op::OpPtrAccessChain:
case spv::Op::OpUntypedPtrAccessChainKHR:
case spv::Op::OpLoad:
case spv::Op::OpConstantNull:
case spv::Op::OpRawAccessChainNV:
Expand All @@ -308,8 +313,11 @@ bool spvOpcodeReturnsLogicalVariablePointer(const spv::Op opcode) {
int32_t spvOpcodeReturnsLogicalPointer(const spv::Op opcode) {
switch (opcode) {
case spv::Op::OpVariable:
case spv::Op::OpUntypedVariableKHR:
case spv::Op::OpAccessChain:
case spv::Op::OpInBoundsAccessChain:
case spv::Op::OpUntypedAccessChainKHR:
case spv::Op::OpUntypedInBoundsAccessChainKHR:
case spv::Op::OpFunctionParameter:
case spv::Op::OpImageTexelPointer:
case spv::Op::OpCopyObject:
Expand Down Expand Up @@ -351,6 +359,7 @@ int32_t spvOpcodeGeneratesType(spv::Op op) {
// spv::Op::OpTypeAccelerationStructureNV
case spv::Op::OpTypeRayQueryKHR:
case spv::Op::OpTypeHitObjectNV:
case spv::Op::OpTypeUntypedPointerKHR:
return true;
default:
// In particular, OpTypeForwardPointer does not generate a type,
Expand Down Expand Up @@ -792,3 +801,16 @@ bool spvOpcodeIsBit(spv::Op opcode) {
return false;
}
}

bool spvOpcodeGeneratesUntypedPointer(spv::Op opcode) {
switch (opcode) {
case spv::Op::OpUntypedVariableKHR:
case spv::Op::OpUntypedAccessChainKHR:
case spv::Op::OpUntypedInBoundsAccessChainKHR:
case spv::Op::OpUntypedPtrAccessChainKHR:
case spv::Op::OpUntypedInBoundsPtrAccessChainKHR:
return true;
default:
return false;
}
}
3 changes: 3 additions & 0 deletions spirv-tools/source/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,7 @@ bool spvOpcodeIsBit(spv::Op opcode);
// Gets the name of an instruction, without the "Op" prefix.
const char* spvOpcodeString(const spv::Op opcode);

// Returns true for opcodes that generate an untyped pointer result.
bool spvOpcodeGeneratesUntypedPointer(spv::Op opcode);

#endif // SOURCE_OPCODE_H_
5 changes: 5 additions & 0 deletions spirv-tools/source/operand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ const char* spvOperandTypeStr(spv_operand_type_t type) {
return "OpenCL.DebugInfo.100 debug operation";
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY:
return "OpenCL.DebugInfo.100 debug imported entity";
case SPV_OPERAND_TYPE_FPENCODING:
case SPV_OPERAND_TYPE_OPTIONAL_FPENCODING:
return "FP encoding";

// The next values are for values returned from an instruction, not actually
// an operand. So the specific strings don't matter. But let's add them
Expand Down Expand Up @@ -366,6 +369,7 @@ bool spvOperandIsConcrete(spv_operand_type_t type) {
case SPV_OPERAND_TYPE_LOAD_CACHE_CONTROL:
case SPV_OPERAND_TYPE_STORE_CACHE_CONTROL:
case SPV_OPERAND_TYPE_NAMED_MAXIMUM_NUMBER_OF_REGISTERS:
case SPV_OPERAND_TYPE_FPENCODING:
return true;
default:
break;
Expand Down Expand Up @@ -407,6 +411,7 @@ bool spvOperandIsOptional(spv_operand_type_t type) {
case SPV_OPERAND_TYPE_OPTIONAL_COOPERATIVE_MATRIX_OPERANDS:
case SPV_OPERAND_TYPE_OPTIONAL_CIV:
case SPV_OPERAND_TYPE_OPTIONAL_RAW_ACCESS_CHAIN_OPERANDS:
case SPV_OPERAND_TYPE_OPTIONAL_FPENCODING:
return true;
default:
break;
Expand Down
13 changes: 11 additions & 2 deletions spirv-tools/source/opt/aggressive_dead_code_elim_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ void AggressiveDCEPass::AddStores(Function* func, uint32_t ptrId) {
}
break;
// If default, assume it stores e.g. frexp, modf, function call
case spv::Op::OpStore:
case spv::Op::OpStore: {
const uint32_t kStoreTargetAddrInIdx = 0;
if (user->GetSingleWordInOperand(kStoreTargetAddrInIdx) == ptrId)
AddToWorklist(user);
break;
}
default:
AddToWorklist(user);
break;
Expand Down Expand Up @@ -262,6 +267,7 @@ void AggressiveDCEPass::AddBreaksAndContinuesToWorklist(
}

bool AggressiveDCEPass::AggressiveDCE(Function* func) {
if (func->IsDeclaration()) return false;
std::list<BasicBlock*> structured_order;
cfg()->ComputeStructuredOrder(func, &*func->begin(), &structured_order);
live_local_vars_.clear();
Expand Down Expand Up @@ -1004,7 +1010,10 @@ void AggressiveDCEPass::InitExtensions() {
"SPV_NV_bindless_texture",
"SPV_EXT_shader_atomic_float_add",
"SPV_EXT_fragment_shader_interlock",
"SPV_NV_compute_shader_derivatives"
"SPV_NV_compute_shader_derivatives",
"SPV_NV_cooperative_matrix",
"SPV_KHR_cooperative_matrix",
"SPV_KHR_ray_tracing_position_fetch"
});
// clang-format on
}
Expand Down
2 changes: 2 additions & 0 deletions spirv-tools/source/opt/copy_prop_arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,8 @@ void CopyPropagateArrays::UpdateUses(Instruction* original_ptr_inst,
uint32_t pointee_type_id =
pointer_type->GetSingleWordInOperand(kTypePointerPointeeInIdx);
uint32_t copy = GenerateCopy(original_ptr_inst, pointee_type_id, use);
assert(copy != 0 &&
"Should not be updating uses unless we know it can be done.");

context()->ForgetUses(use);
use->SetInOperand(index, {copy});
Expand Down
20 changes: 17 additions & 3 deletions spirv-tools/source/opt/debug_info_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,15 +768,29 @@ void DebugInfoManager::ConvertDebugGlobalToLocalVariable(
local_var->opcode() == spv::Op::OpFunctionParameter);

// Convert |dbg_global_var| to DebugLocalVariable
// All of the operands up to the scope operand are the same for the type
// instructions. The flag operand needs to move from operand
// kDebugGlobalVariableOperandFlagsIndex to
// kDebugLocalVariableOperandFlagsIndex. No other operands are needed to
// define the DebugLocalVariable.

// Modify the opcode.
dbg_global_var->SetInOperand(kExtInstInstructionInIdx,
{CommonDebugInfoDebugLocalVariable});

// Move the flags operand.
auto flags = dbg_global_var->GetSingleWordOperand(
kDebugGlobalVariableOperandFlagsIndex);
for (uint32_t i = dbg_global_var->NumInOperands() - 1;
i >= kDebugLocalVariableOperandFlagsIndex; --i) {
dbg_global_var->SetOperand(kDebugLocalVariableOperandFlagsIndex, {flags});

// Remove the extra operands. Starting at the end to avoid copying too much
// data.
for (uint32_t i = dbg_global_var->NumOperands() - 1;
i > kDebugLocalVariableOperandFlagsIndex; --i) {
dbg_global_var->RemoveOperand(i);
}
dbg_global_var->SetOperand(kDebugLocalVariableOperandFlagsIndex, {flags});

// Update the def-use manager.
context()->ForgetUses(dbg_global_var);
context()->AnalyzeUses(dbg_global_var);

Expand Down
7 changes: 5 additions & 2 deletions spirv-tools/source/opt/desc_sroa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ bool IsDecorationBinding(Instruction* inst) {

Pass::Status DescriptorScalarReplacement::Process() {
bool modified = false;

std::vector<Instruction*> vars_to_kill;

for (Instruction& var : context()->types_values()) {
if (descsroautil::IsDescriptorArray(context(), &var)) {
bool is_candidate =
flatten_arrays_ && descsroautil::IsDescriptorArray(context(), &var);
is_candidate |= flatten_composites_ &&
descsroautil::IsDescriptorStruct(context(), &var);
if (is_candidate) {
modified = true;
if (!ReplaceCandidate(&var)) {
return Status::Failure;
Expand Down
16 changes: 13 additions & 3 deletions spirv-tools/source/opt/desc_sroa.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ namespace opt {
// Documented in optimizer.hpp
class DescriptorScalarReplacement : public Pass {
public:
DescriptorScalarReplacement() {}

const char* name() const override { return "descriptor-scalar-replacement"; }
DescriptorScalarReplacement(bool flatten_composites, bool flatten_arrays)
: flatten_composites_(flatten_composites),
flatten_arrays_(flatten_arrays) {}

const char* name() const override {
if (flatten_composites_ && flatten_arrays_)
return "descriptor-scalar-replacement";
if (flatten_composites_) return "descriptor-compososite-scalar-replacement";
return "descriptor-array-scalar-replacement";
}

Status Process() override;

Expand Down Expand Up @@ -141,6 +148,9 @@ class DescriptorScalarReplacement : public Pass {
// array |var|. If the entry is |0|, then the variable has not been
// created yet.
std::map<Instruction*, std::vector<uint32_t>> replacement_variables_;

bool flatten_composites_;
bool flatten_arrays_;
};

} // namespace opt
Expand Down
51 changes: 34 additions & 17 deletions spirv-tools/source/opt/desc_sroa_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,58 @@ uint32_t GetLengthOfArrayType(IRContext* context, Instruction* type) {
return length_const->GetU32();
}

} // namespace

namespace descsroautil {
bool HasDescriptorDecorations(IRContext* context, Instruction* var) {
const auto& decoration_mgr = context->get_decoration_mgr();
return decoration_mgr->HasDecoration(
var->result_id(), uint32_t(spv::Decoration::DescriptorSet)) &&
decoration_mgr->HasDecoration(var->result_id(),
uint32_t(spv::Decoration::Binding));
}

bool IsDescriptorArray(IRContext* context, Instruction* var) {
Instruction* GetVariableType(IRContext* context, Instruction* var) {
if (var->opcode() != spv::Op::OpVariable) {
return false;
return nullptr;
}

uint32_t ptr_type_id = var->type_id();
Instruction* ptr_type_inst = context->get_def_use_mgr()->GetDef(ptr_type_id);
if (ptr_type_inst->opcode() != spv::Op::OpTypePointer) {
return false;
return nullptr;
}

uint32_t var_type_id = ptr_type_inst->GetSingleWordInOperand(1);
Instruction* var_type_inst = context->get_def_use_mgr()->GetDef(var_type_id);
if (var_type_inst->opcode() != spv::Op::OpTypeArray &&
var_type_inst->opcode() != spv::Op::OpTypeStruct) {
return false;
return context->get_def_use_mgr()->GetDef(var_type_id);
}

} // namespace

namespace descsroautil {

bool IsDescriptorArray(IRContext* context, Instruction* var) {
Instruction* var_type_inst = GetVariableType(context, var);
if (var_type_inst == nullptr) return false;
return var_type_inst->opcode() == spv::Op::OpTypeArray &&
HasDescriptorDecorations(context, var);
}

bool IsDescriptorStruct(IRContext* context, Instruction* var) {
Instruction* var_type_inst = GetVariableType(context, var);
if (var_type_inst == nullptr) return false;

while (var_type_inst->opcode() == spv::Op::OpTypeArray) {
var_type_inst = context->get_def_use_mgr()->GetDef(
var_type_inst->GetInOperand(0).AsId());
}

if (var_type_inst->opcode() != spv::Op::OpTypeStruct) return false;

// All structures with descriptor assignments must be replaced by variables,
// one for each of their members - with the exceptions of buffers.
if (IsTypeOfStructuredBuffer(context, var_type_inst)) {
return false;
}

if (!context->get_decoration_mgr()->HasDecoration(
var->result_id(), uint32_t(spv::Decoration::DescriptorSet))) {
return false;
}

return context->get_decoration_mgr()->HasDecoration(
var->result_id(), uint32_t(spv::Decoration::Binding));
return HasDescriptorDecorations(context, var);
}

bool IsTypeOfStructuredBuffer(IRContext* context, const Instruction* type) {
Expand Down
Loading

0 comments on commit 4f3fd56

Please sign in to comment.