yuzu/src/shader_recompiler/backend/spirv/emit_spirv.cpp

201 lines
7.0 KiB
C++
Raw Normal View History

2021-02-08 05:54:35 +00:00
// Copyright 2021 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
2021-02-17 03:59:28 +00:00
#include <span>
#include <tuple>
2021-02-08 05:54:35 +00:00
#include <type_traits>
2021-02-17 03:59:28 +00:00
#include <utility>
#include <vector>
2021-02-08 05:54:35 +00:00
#include "shader_recompiler/backend/spirv/emit_spirv.h"
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/function.h"
#include "shader_recompiler/frontend/ir/microinstruction.h"
#include "shader_recompiler/frontend/ir/program.h"
namespace Shader::Backend::SPIRV {
2021-02-16 07:10:22 +00:00
namespace {
template <class Func>
2021-02-17 03:59:28 +00:00
struct FuncTraits : FuncTraits<Func> {};
2021-02-08 05:54:35 +00:00
2021-02-17 03:59:28 +00:00
template <class ReturnType_, class... Args>
struct FuncTraits<ReturnType_ (*)(Args...)> {
2021-02-16 07:10:22 +00:00
using ReturnType = ReturnType_;
2021-02-08 05:54:35 +00:00
2021-02-16 07:10:22 +00:00
static constexpr size_t NUM_ARGS = sizeof...(Args);
2021-02-08 05:54:35 +00:00
2021-02-16 07:10:22 +00:00
template <size_t I>
using ArgType = std::tuple_element_t<I, std::tuple<Args...>>;
};
2021-02-14 04:24:32 +00:00
2021-02-17 03:59:28 +00:00
template <auto func, typename... Args>
void SetDefinition(EmitContext& ctx, IR::Inst* inst, Args... args) {
2021-02-16 07:10:22 +00:00
const Id forward_id{inst->Definition<Id>()};
const bool has_forward_id{Sirit::ValidId(forward_id)};
Id current_id{};
if (has_forward_id) {
current_id = ctx.ExchangeCurrentId(forward_id);
}
2021-02-17 03:59:28 +00:00
const Id new_id{func(ctx, std::forward<Args>(args)...)};
2021-02-16 07:10:22 +00:00
if (has_forward_id) {
ctx.ExchangeCurrentId(current_id);
} else {
inst->SetDefinition<Id>(new_id);
}
}
template <typename ArgType>
ArgType Arg(EmitContext& ctx, const IR::Value& arg) {
if constexpr (std::is_same_v<ArgType, Id>) {
return ctx.Def(arg);
} else if constexpr (std::is_same_v<ArgType, const IR::Value&>) {
return arg;
} else if constexpr (std::is_same_v<ArgType, u32>) {
return arg.U32();
} else if constexpr (std::is_same_v<ArgType, IR::Block*>) {
return arg.Label();
}
}
2021-02-17 03:59:28 +00:00
template <auto func, bool is_first_arg_inst, size_t... I>
void Invoke(EmitContext& ctx, IR::Inst* inst, std::index_sequence<I...>) {
using Traits = FuncTraits<decltype(func)>;
2021-02-16 07:10:22 +00:00
if constexpr (std::is_same_v<Traits::ReturnType, Id>) {
if constexpr (is_first_arg_inst) {
2021-02-17 03:59:28 +00:00
SetDefinition<func>(ctx, inst, inst, Arg<Traits::ArgType<I + 2>>(ctx, inst->Arg(I))...);
2021-02-16 07:10:22 +00:00
} else {
2021-02-17 03:59:28 +00:00
SetDefinition<func>(ctx, inst, Arg<Traits::ArgType<I + 1>>(ctx, inst->Arg(I))...);
2021-02-16 07:10:22 +00:00
}
} else {
if constexpr (is_first_arg_inst) {
2021-02-17 03:59:28 +00:00
func(ctx, inst, Arg<Traits::ArgType<I + 2>>(ctx, inst->Arg(I))...);
2021-02-16 07:10:22 +00:00
} else {
2021-02-17 03:59:28 +00:00
func(ctx, Arg<Traits::ArgType<I + 1>>(ctx, inst->Arg(I))...);
2021-02-08 05:54:35 +00:00
}
}
}
2021-02-17 03:59:28 +00:00
template <auto func>
void Invoke(EmitContext& ctx, IR::Inst* inst) {
using Traits = FuncTraits<decltype(func)>;
2021-02-16 07:10:22 +00:00
static_assert(Traits::NUM_ARGS >= 1, "Insufficient arguments");
if constexpr (Traits::NUM_ARGS == 1) {
2021-02-17 03:59:28 +00:00
Invoke<func, false>(ctx, inst, std::make_index_sequence<0>{});
2021-02-16 07:10:22 +00:00
} else {
using FirstArgType = typename Traits::template ArgType<1>;
static constexpr bool is_first_arg_inst = std::is_same_v<FirstArgType, IR::Inst*>;
using Indices = std::make_index_sequence<Traits::NUM_ARGS - (is_first_arg_inst ? 2 : 1)>;
2021-02-17 03:59:28 +00:00
Invoke<func, is_first_arg_inst>(ctx, inst, Indices{});
}
}
void EmitInst(EmitContext& ctx, IR::Inst* inst) {
switch (inst->Opcode()) {
#define OPCODE(name, result_type, ...) \
case IR::Opcode::name: \
return Invoke<&Emit##name>(ctx, inst);
#include "shader_recompiler/frontend/ir/opcodes.inc"
#undef OPCODE
}
throw LogicError("Invalid opcode {}", inst->Opcode());
}
Id TypeId(const EmitContext& ctx, IR::Type type) {
switch (type) {
case IR::Type::U1:
return ctx.U1;
case IR::Type::U32:
return ctx.U32[1];
default:
throw NotImplementedException("Phi node type {}", type);
2021-02-16 07:10:22 +00:00
}
}
} // Anonymous namespace
2021-02-08 05:54:35 +00:00
2021-02-17 03:59:28 +00:00
std::vector<u32> EmitSPIRV(Environment& env, IR::Program& program) {
2021-02-08 05:54:35 +00:00
EmitContext ctx{program};
const Id void_function{ctx.TypeFunction(ctx.void_id)};
// FIXME: Forward declare functions (needs sirit support)
Id func{};
for (IR::Function& function : program.functions) {
func = ctx.OpFunction(ctx.void_id, spv::FunctionControlMask::MaskNone, void_function);
for (IR::Block* const block : function.blocks) {
2021-02-16 07:10:22 +00:00
ctx.AddLabel(block->Definition<Id>());
2021-02-08 05:54:35 +00:00
for (IR::Inst& inst : block->Instructions()) {
EmitInst(ctx, &inst);
}
}
ctx.OpFunctionEnd();
}
2021-02-16 07:10:22 +00:00
boost::container::small_vector<Id, 32> interfaces;
if (program.info.uses_workgroup_id) {
interfaces.push_back(ctx.workgroup_id);
}
if (program.info.uses_local_invocation_id) {
interfaces.push_back(ctx.local_invocation_id);
}
const std::span interfaces_span(interfaces.data(), interfaces.size());
2021-02-17 03:59:28 +00:00
ctx.AddEntryPoint(spv::ExecutionModel::GLCompute, func, "main", interfaces_span);
2021-02-08 05:54:35 +00:00
2021-02-17 03:59:28 +00:00
const std::array<u32, 3> workgroup_size{env.WorkgroupSize()};
ctx.AddExecutionMode(func, spv::ExecutionMode::LocalSize, workgroup_size[0], workgroup_size[1],
workgroup_size[2]);
2021-02-08 05:54:35 +00:00
2021-02-17 03:59:28 +00:00
return ctx.Assemble();
}
2021-02-17 03:59:28 +00:00
Id EmitPhi(EmitContext& ctx, IR::Inst* inst) {
const size_t num_args{inst->NumArgs()};
boost::container::small_vector<Id, 32> operands;
operands.reserve(num_args * 2);
for (size_t index = 0; index < num_args; ++index) {
// Phi nodes can have forward declarations, if an argument is not defined provide a forward
// declaration of it. Invoke will take care of giving it the right definition when it's
// actually defined.
const IR::Value arg{inst->Arg(index)};
Id def{};
if (arg.IsImmediate()) {
// Let the context handle immediate definitions, as it already knows how
def = ctx.Def(arg);
} else {
IR::Inst* const arg_inst{arg.Inst()};
def = arg_inst->Definition<Id>();
if (!Sirit::ValidId(def)) {
// If it hasn't been defined, get a forward declaration
def = ctx.ForwardDeclarationId();
arg_inst->SetDefinition<Id>(def);
}
}
IR::Block* const phi_block{inst->PhiBlock(index)};
operands.push_back(def);
2021-02-16 07:10:22 +00:00
operands.push_back(phi_block->Definition<Id>());
}
const Id result_type{TypeId(ctx, inst->Arg(0).Type())};
return ctx.OpPhi(result_type, std::span(operands.data(), operands.size()));
2021-02-08 05:54:35 +00:00
}
2021-02-17 03:59:28 +00:00
void EmitVoid(EmitContext&) {}
2021-02-08 05:54:35 +00:00
2021-02-17 03:59:28 +00:00
Id EmitIdentity(EmitContext& ctx, const IR::Value& value) {
2021-02-16 22:48:58 +00:00
return ctx.Def(value);
2021-02-08 05:54:35 +00:00
}
2021-02-17 03:59:28 +00:00
void EmitGetZeroFromOp(EmitContext&) {
2021-02-08 05:54:35 +00:00
throw LogicError("Unreachable instruction");
}
2021-02-17 03:59:28 +00:00
void EmitGetSignFromOp(EmitContext&) {
2021-02-08 05:54:35 +00:00
throw LogicError("Unreachable instruction");
}
2021-02-17 03:59:28 +00:00
void EmitGetCarryFromOp(EmitContext&) {
2021-02-08 05:54:35 +00:00
throw LogicError("Unreachable instruction");
}
2021-02-17 03:59:28 +00:00
void EmitGetOverflowFromOp(EmitContext&) {
2021-02-08 05:54:35 +00:00
throw LogicError("Unreachable instruction");
}
} // namespace Shader::Backend::SPIRV