[qfcc] Clean up spirv instruction generation

This should make it easier to declare local variables on the fly (needed
for function arguments).
This commit is contained in:
Bill Currie 2024-11-12 01:26:20 +09:00
parent 8a3a39be68
commit f9345caeb3
2 changed files with 220 additions and 170 deletions

View file

@ -47,13 +47,20 @@ typedef struct module_s {
ex_list_t extinst_imports; ex_list_t extinst_imports;
const expr_t *addressing_model; const expr_t *addressing_model;
const expr_t *memory_model; const expr_t *memory_model;
entrypoint_t *entry_points; //entrypoint_t *entry_points;
defspace_t *entry_points;
defspace_t *exec_modes;
// debug
defspace_t *strings; defspace_t *strings;
defspace_t *names; defspace_t *names;
defspace_t *module_processed;
// annotations
defspace_t *decorations; defspace_t *decorations;
// types, non-function variables, undefs
defspace_t *globals; defspace_t *globals;
defspace_t *func_import; // functions
defspace_t *func_code; defspace_t *func_declarations;
defspace_t *func_definitions;
} module_t; } module_t;
void spirv_add_capability (module_t *module, SpvCapability capability); void spirv_add_capability (module_t *module, SpvCapability capability);

View file

@ -1,5 +1,5 @@
/* /*
spirv.c target_spirv.c
qfcc spir-v file support qfcc spir-v file support
@ -47,16 +47,15 @@
#include "tools/qfcc/include/target.h" #include "tools/qfcc/include/target.h"
#include "tools/qfcc/include/type.h" #include "tools/qfcc/include/type.h"
typedef struct spirvctx_s { #define INSN(i,o) D_var_o(int,&(i),o)
defspace_t *space; #define ADD_DATA(d,s) defspace_add_data((d), (s)->data, (s)->size)
defspace_t *linkage;
defspace_t *strings; typedef struct spirvctx_s {
defspace_t *names; module_t *module;
defspace_t *annotations;
defspace_t *types; defspace_t *decl_space;
defspace_t *code; defspace_t *code_space;
defspace_t *current;
struct DARRAY_TYPE (unsigned) type_ids; struct DARRAY_TYPE (unsigned) type_ids;
unsigned id; unsigned id;
} spirvctx_t; } spirvctx_t;
@ -83,26 +82,28 @@ spirv_add_type_id (const type_t *type, unsigned id, spirvctx_t *ctx)
ctx->type_ids.a[type->id] = id; ctx->type_ids.a[type->id] = id;
} }
static def_t * static def_t
spirv_new_insn (int op, int size, defspace_t *space) spirv_new_insn (int op, int size, defspace_t *space)
{ {
auto def = new_def (nullptr, nullptr, space, sc_static); def_t insn = {
def->offset = defspace_alloc_loc (space, size); .space = space,
.offset = defspace_alloc_highwater (space, size),
};
D_INT (def) = (op & SpvOpCodeMask) | (size << SpvWordCountShift); INSN (insn, 0) = (op & SpvOpCodeMask) | (size << SpvWordCountShift);
return def; return insn;
} }
static def_t * static def_t
spirv_str_insn (int op, int offs, int extra, const char *str, defspace_t *space) spirv_str_insn (int op, int offs, int extra, const char *str, defspace_t *space)
{ {
int len = strlen (str) + 1; int len = strlen (str) + 1;
int str_size = RUP(len, 4) / 4; int str_size = RUP(len, 4) / 4;
int size = offs + str_size + extra; int size = offs + str_size + extra;
auto def = spirv_new_insn (op, size, space); auto insn = spirv_new_insn (op, size, space);
D_var_o(int, def, str_size - 1) = 0; INSN (insn, str_size - 1) = 0;
memcpy (&D_var_o(int, def, offs), str, len); memcpy (&INSN (insn, offs), str, len);
return def; return insn;
} }
static unsigned static unsigned
@ -114,8 +115,8 @@ spirv_id (spirvctx_t *ctx)
static void static void
spirv_Capability (SpvCapability capability, defspace_t *space) spirv_Capability (SpvCapability capability, defspace_t *space)
{ {
auto def = spirv_new_insn (SpvOpCapability, 2, space); auto insn = spirv_new_insn (SpvOpCapability, 2, space);
D_var_o(int, def, 1) = capability; INSN (insn, 1) = capability;
} }
static void static void
@ -127,9 +128,10 @@ spirv_Extension (const char *ext, defspace_t *space)
static unsigned static unsigned
spirv_ExtInstImport (const char *imp, spirvctx_t *ctx) spirv_ExtInstImport (const char *imp, spirvctx_t *ctx)
{ {
auto def = spirv_str_insn (SpvOpExtInstImport, 2, 0, imp, ctx->space); auto strings = ctx->module->strings;
auto insn = spirv_str_insn (SpvOpExtInstImport, 2, 0, imp, strings);
int id = spirv_id (ctx); int id = spirv_id (ctx);
D_var_o(int, def, 1) = id; INSN (insn, 1) = id;
return id; return id;
} }
@ -137,17 +139,18 @@ static void
spirv_MemoryModel (SpvAddressingModel addressing, SpvMemoryModel memory, spirv_MemoryModel (SpvAddressingModel addressing, SpvMemoryModel memory,
defspace_t *space) defspace_t *space)
{ {
auto def = spirv_new_insn (SpvOpMemoryModel, 3, space); auto insn = spirv_new_insn (SpvOpMemoryModel, 3, space);
D_var_o(int, def, 1) = addressing; INSN (insn, 1) = addressing;
D_var_o(int, def, 2) = memory; INSN (insn, 2) = memory;
} }
static unsigned static unsigned
spirv_String (const char *name, spirvctx_t *ctx) spirv_String (const char *name, spirvctx_t *ctx)
{ {
auto def = spirv_str_insn (SpvOpString, 2, 0, name, ctx->strings); auto strings = ctx->module->strings;
auto insn = spirv_str_insn (SpvOpString, 2, 0, name, strings);
int id = spirv_id (ctx); int id = spirv_id (ctx);
D_var_o(int, def, 1) = id; INSN (insn, 1) = id;
return id; return id;
} }
@ -155,10 +158,11 @@ static void
spirv_Source (unsigned lang, unsigned version, unsigned srcid, spirv_Source (unsigned lang, unsigned version, unsigned srcid,
const char *src_str, spirvctx_t *ctx) const char *src_str, spirvctx_t *ctx)
{ {
auto def = spirv_new_insn (SpvOpSource, 4, ctx->strings); auto strings = ctx->module->strings;
D_var_o(int, def, 1) = lang; auto insn = spirv_new_insn (SpvOpSource, 4, strings);
D_var_o(int, def, 2) = version; INSN (insn, 1) = lang;
D_var_o(int, def, 3) = srcid; INSN (insn, 2) = version;
INSN (insn, 3) = srcid;
} }
static void static void
@ -168,9 +172,10 @@ spirv_Name (unsigned id, const char *name, spirvctx_t *ctx)
name = ""; name = "";
} }
int len = strlen (name) + 1; int len = strlen (name) + 1;
auto def = spirv_new_insn (SpvOpName, 2 + RUP(len, 4) / 4, ctx->names); auto names = ctx->module->names;
D_var_o(int, def, 1) = id; auto insn = spirv_new_insn (SpvOpName, 2 + RUP(len, 4) / 4, names);
memcpy (&D_var_o(int, def, 2), name, len); INSN (insn, 1) = id;
memcpy (&INSN (insn, 2), name, len);
} }
static void static void
@ -181,11 +186,12 @@ spirv_Decorate (unsigned id, SpvDecoration decoration, void *literal,
internal_error (0, "unexpected type"); internal_error (0, "unexpected type");
} }
int size = pr_type_size[type]; int size = pr_type_size[type];
auto def = spirv_new_insn (SpvOpDecorate, 3 + size, ctx->annotations); auto decorations = ctx->module->decorations;
D_var_o(int, def, 1) = id; auto insn = spirv_new_insn (SpvOpDecorate, 3 + size, decorations);
D_var_o(int, def, 2) = decoration; INSN (insn, 1) = id;
INSN (insn, 2) = decoration;
if (type == ev_int) { if (type == ev_int) {
D_var_o(int, def, 3) = *(int *)literal; INSN (insn, 3) = *(int *)literal;
} }
} }
@ -194,11 +200,11 @@ spirv_MemberName (unsigned id, unsigned member, const char *name,
spirvctx_t *ctx) spirvctx_t *ctx)
{ {
int len = strlen (name) + 1; int len = strlen (name) + 1;
auto def = spirv_new_insn (SpvOpMemberName, 3 + RUP(len, 4) / 4, auto names = ctx->module->names;
ctx->names); auto insn = spirv_new_insn (SpvOpMemberName, 3 + RUP(len, 4) / 4, names);
D_var_o(int, def, 1) = id; INSN (insn, 1) = id;
D_var_o(int, def, 2) = member; INSN (insn, 2) = member;
memcpy (&D_var_o(int, def, 3), name, len); memcpy (&INSN (insn, 3), name, len);
} }
static unsigned type_id (const type_t *type, spirvctx_t *ctx); static unsigned type_id (const type_t *type, spirvctx_t *ctx);
@ -206,48 +212,53 @@ static unsigned type_id (const type_t *type, spirvctx_t *ctx);
static unsigned static unsigned
spirv_TypeVoid (spirvctx_t *ctx) spirv_TypeVoid (spirvctx_t *ctx)
{ {
auto def = spirv_new_insn (SpvOpTypeVoid, 2, ctx->types); auto globals = ctx->module->globals;
D_var_o(int, def, 1) = spirv_id (ctx); auto insn = spirv_new_insn (SpvOpTypeVoid, 2, globals);
return D_var_o(int, def, 1); INSN (insn, 1) = spirv_id (ctx);
return INSN (insn, 1);
} }
static unsigned static unsigned
spirv_TypeInt (unsigned bitwidth, bool is_signed, spirvctx_t *ctx) spirv_TypeInt (unsigned bitwidth, bool is_signed, spirvctx_t *ctx)
{ {
auto def = spirv_new_insn (SpvOpTypeInt, 4, ctx->types); auto globals = ctx->module->globals;
D_var_o(int, def, 1) = spirv_id (ctx); auto insn = spirv_new_insn (SpvOpTypeInt, 4, globals);
D_var_o(int, def, 2) = bitwidth; INSN (insn, 1) = spirv_id (ctx);
D_var_o(int, def, 3) = is_signed; INSN (insn, 2) = bitwidth;
return D_var_o(int, def, 1); INSN (insn, 3) = is_signed;
return INSN (insn, 1);
} }
static unsigned static unsigned
spirv_TypeFloat (unsigned bitwidth, spirvctx_t *ctx) spirv_TypeFloat (unsigned bitwidth, spirvctx_t *ctx)
{ {
auto def = spirv_new_insn (SpvOpTypeFloat, 3, ctx->types); auto globals = ctx->module->globals;
D_var_o(int, def, 1) = spirv_id (ctx); auto insn = spirv_new_insn (SpvOpTypeFloat, 3, globals);
D_var_o(int, def, 2) = bitwidth; INSN (insn, 1) = spirv_id (ctx);
return D_var_o(int, def, 1); INSN (insn, 2) = bitwidth;
return INSN (insn, 1);
} }
static unsigned static unsigned
spirv_TypeVector (unsigned base, unsigned width, spirvctx_t *ctx) spirv_TypeVector (unsigned base, unsigned width, spirvctx_t *ctx)
{ {
auto def = spirv_new_insn (SpvOpTypeVector, 4, ctx->types); auto globals = ctx->module->globals;
D_var_o(int, def, 1) = spirv_id (ctx); auto insn = spirv_new_insn (SpvOpTypeVector, 4, globals);
D_var_o(int, def, 2) = base; INSN (insn, 1) = spirv_id (ctx);
D_var_o(int, def, 3) = width; INSN (insn, 2) = base;
return D_var_o(int, def, 1); INSN (insn, 3) = width;
return INSN (insn, 1);
} }
static unsigned static unsigned
spirv_TypeMatrix (unsigned col_type, unsigned columns, spirvctx_t *ctx) spirv_TypeMatrix (unsigned col_type, unsigned columns, spirvctx_t *ctx)
{ {
auto def = spirv_new_insn (SpvOpTypeMatrix, 4, ctx->types); auto globals = ctx->module->globals;
D_var_o(int, def, 1) = spirv_id (ctx); auto insn = spirv_new_insn (SpvOpTypeMatrix, 4, globals);
D_var_o(int, def, 2) = col_type; INSN (insn, 1) = spirv_id (ctx);
D_var_o(int, def, 3) = columns; INSN (insn, 2) = col_type;
return D_var_o(int, def, 1); INSN (insn, 3) = columns;
return INSN (insn, 1);
} }
static unsigned static unsigned
@ -256,11 +267,12 @@ spirv_TypePointer (const type_t *type, spirvctx_t *ctx)
auto rtype = dereference_type (type); auto rtype = dereference_type (type);
unsigned rid = type_id (rtype, ctx); unsigned rid = type_id (rtype, ctx);
auto def = spirv_new_insn (SpvOpTypePointer, 4, ctx->types); auto globals = ctx->module->globals;
D_var_o(int, def, 1) = spirv_id (ctx); auto insn = spirv_new_insn (SpvOpTypePointer, 4, globals);
D_var_o(int, def, 2) = SpvStorageClassFunction;//FIXME INSN (insn, 1) = spirv_id (ctx);
D_var_o(int, def, 3) = rid; INSN (insn, 2) = SpvStorageClassFunction;//FIXME
return D_var_o(int, def, 1); INSN (insn, 3) = rid;
return INSN (insn, 1);
} }
static unsigned static unsigned
@ -278,9 +290,10 @@ spirv_TypeStruct (const type_t *type, spirvctx_t *ctx)
} }
unsigned id = spirv_id (ctx); unsigned id = spirv_id (ctx);
auto def = spirv_new_insn (SpvOpTypeStruct, 2 + num_members, ctx->types); auto globals = ctx->module->globals;
D_var_o(int, def, 1) = id; auto insn = spirv_new_insn (SpvOpTypeStruct, 2 + num_members, globals);
memcpy (&D_var_o(int, def, 2), member_types, sizeof (member_types)); INSN (insn, 1) = id;
memcpy (&INSN (insn, 2), member_types, sizeof (member_types));
spirv_Name (id, type->name + 4, ctx); spirv_Name (id, type->name + 4, ctx);
@ -316,11 +329,12 @@ spirv_TypeFunction (symbol_t *fsym, spirvctx_t *ctx)
} }
unsigned ft_id = spirv_id (ctx); unsigned ft_id = spirv_id (ctx);
auto def = spirv_new_insn (SpvOpTypeFunction, 3 + num_params, ctx->types); auto globals = ctx->module->globals;
D_var_o(int, def, 1) = ft_id; auto insn = spirv_new_insn (SpvOpTypeFunction, 3 + num_params, globals);
D_var_o(int, def, 2) = ret_type; INSN (insn, 1) = ft_id;
INSN (insn, 2) = ret_type;
for (int i = 0; i < num_params; i++) { for (int i = 0; i < num_params; i++) {
D_var_o(int, def, 3 + i) = param_types[i]; INSN (insn, 3 + i) = param_types[i];
} }
spirv_add_type_id (type, ft_id, ctx); spirv_add_type_id (type, ft_id, ctx);
return ft_id; return ft_id;
@ -380,33 +394,41 @@ type_id (const type_t *type, spirvctx_t *ctx)
return id; return id;
} }
static unsigned
spirv_DeclLabel (spirvctx_t *ctx)
{
auto insn = spirv_new_insn (SpvOpLabel, 2, ctx->decl_space);
INSN (insn, 1) = spirv_id (ctx);
return INSN (insn, 1);
}
static unsigned static unsigned
spirv_Label (spirvctx_t *ctx) spirv_Label (spirvctx_t *ctx)
{ {
auto def = spirv_new_insn (SpvOpLabel, 2, ctx->code); auto insn = spirv_new_insn (SpvOpLabel, 2, ctx->code_space);
D_var_o(int, def, 1) = spirv_id (ctx); INSN (insn, 1) = spirv_id (ctx);
return D_var_o(int, def, 1); return INSN (insn, 1);
} }
static void static void
spirv_Unreachable (spirvctx_t *ctx) spirv_Unreachable (spirvctx_t *ctx)
{ {
spirv_new_insn (SpvOpUnreachable, 1, ctx->code); spirv_new_insn (SpvOpUnreachable, 1, ctx->code_space);
} }
static void static void
spirv_FunctionEnd (spirvctx_t *ctx) spirv_FunctionEnd (spirvctx_t *ctx)
{ {
spirv_new_insn (SpvOpFunctionEnd, 1, ctx->code); spirv_new_insn (SpvOpFunctionEnd, 1, ctx->code_space);
} }
static unsigned static unsigned
spirv_FunctionParameter (const char *name, const type_t *type, spirvctx_t *ctx) spirv_FunctionParameter (const char *name, const type_t *type, spirvctx_t *ctx)
{ {
unsigned id = spirv_id (ctx); unsigned id = spirv_id (ctx);
auto def = spirv_new_insn (SpvOpFunctionParameter, 3, ctx->code); auto insn = spirv_new_insn (SpvOpFunctionParameter, 3, ctx->decl_space);
D_var_o(int, def, 1) = type_id (type, ctx); INSN (insn, 1) = type_id (type, ctx);
D_var_o(int, def, 2) = id; INSN (insn, 2) = id;
spirv_Name (id, name, ctx); spirv_Name (id, name, ctx);
return id; return id;
} }
@ -416,10 +438,10 @@ spirv_variable (symbol_t *sym, SpvStorageClass storage_class, spirvctx_t *ctx)
{ {
unsigned id = spirv_id (ctx); unsigned id = spirv_id (ctx);
//FIXME init value //FIXME init value
auto def = spirv_new_insn (SpvOpVariable, 4, ctx->code); auto insn = spirv_new_insn (SpvOpVariable, 4, ctx->decl_space);
D_var_o(int, def, 1) = type_id (sym->type, ctx); INSN (insn, 1) = type_id (sym->type, ctx);
D_var_o(int, def, 2) = id; INSN (insn, 2) = id;
D_var_o(int, def, 3) = storage_class; INSN (insn, 3) = storage_class;
spirv_Name (id, sym->name, ctx); spirv_Name (id, sym->name, ctx);
return id; return id;
} }
@ -431,12 +453,15 @@ spirv_function (function_t *func, spirvctx_t *ctx)
{ {
unsigned ft_id = spirv_TypeFunction (func->sym, ctx); unsigned ft_id = spirv_TypeFunction (func->sym, ctx);
defspace_reset (ctx->code_space);
defspace_reset (ctx->decl_space);
unsigned func_id = spirv_id (ctx); unsigned func_id = spirv_id (ctx);
auto def = spirv_new_insn (SpvOpFunction, 5, ctx->code); auto insn = spirv_new_insn (SpvOpFunction, 5, ctx->decl_space);
D_var_o(int, def, 1) = type_id (func->type->func.ret_type, ctx); INSN (insn, 1) = type_id (func->type->func.ret_type, ctx);
D_var_o(int, def, 2) = func_id; INSN (insn, 2) = func_id;
D_var_o(int, def, 3) = 0; INSN (insn, 3) = 0;
D_var_o(int, def, 4) = ft_id; INSN (insn, 4) = ft_id;
spirv_Name (func_id, GETSTR (func->s_name), ctx); spirv_Name (func_id, GETSTR (func->s_name), ctx);
for (auto p = func->sym->params; p; p = p->next) { for (auto p = func->sym->params; p; p = p->next) {
@ -457,21 +482,25 @@ spirv_function (function_t *func, spirvctx_t *ctx)
} }
if (func->locals) { if (func->locals) {
spirv_Label (ctx); spirv_DeclLabel (ctx);
for (auto sym = func->locals->symbols; sym; sym = sym->next) { for (auto sym = func->locals->symbols; sym; sym = sym->next) {
sym->id = spirv_variable (sym, SpvStorageClassFunction, ctx); sym->id = spirv_variable (sym, SpvStorageClassFunction, ctx);
} }
} else {
spirv_Label (ctx);
} }
int start_size = ctx->code->size; int start_size = ctx->code_space->size;
if (func->exprs) { if (func->exprs) {
spirv_emit_expr (func->exprs, ctx); spirv_emit_expr (func->exprs, ctx);
} else { } else {
spirv_Unreachable (ctx); spirv_Unreachable (ctx);
} }
if (ctx->code->size == start_size) { if (ctx->code_space->size == start_size) {
spirv_Unreachable (ctx); spirv_Unreachable (ctx);
} }
spirv_FunctionEnd (ctx); spirv_FunctionEnd (ctx);
ADD_DATA (ctx->module->func_definitions, ctx->decl_space);
ADD_DATA (ctx->module->func_definitions, ctx->code_space);
return func_id; return func_id;
} }
@ -480,11 +509,11 @@ spirv_EntryPoint (unsigned func_id, const char *func_name,
SpvExecutionModel model, spirvctx_t *ctx) SpvExecutionModel model, spirvctx_t *ctx)
{ {
int len = strlen (func_name) + 1; int len = strlen (func_name) + 1;
auto def = spirv_new_insn (SpvOpEntryPoint, 3 + RUP(len, 4) / 4, auto linkage = ctx->module->entry_points;
ctx->linkage); auto insn = spirv_new_insn (SpvOpEntryPoint, 3 + RUP(len, 4) / 4, linkage);
D_var_o(int, def, 1) = model; INSN (insn, 1) = model;
D_var_o(int, def, 2) = func_id; INSN (insn, 2) = func_id;
memcpy (&D_var_o(int, def, 3), func_name, len); memcpy (&INSN (insn, 3), func_name, len);
} }
typedef struct { typedef struct {
@ -647,16 +676,17 @@ spirv_uexpr (const expr_t *e, spirvctx_t *ctx)
unsigned tid = type_id (get_type (e), ctx); unsigned tid = type_id (get_type (e), ctx);
unsigned id; unsigned id;
if (e->expr.constant) { if (e->expr.constant) {
auto def = spirv_new_insn (SpvOpSpecConstantOp, 5, ctx->types); auto globals = ctx->module->globals;
D_var_o(int, def, 1) = tid; auto insn = spirv_new_insn (SpvOpSpecConstantOp, 5, globals);
D_var_o(int, def, 2) = id = spirv_id (ctx); INSN (insn, 1) = tid;
D_var_o(int, def, 3) = spv_op->op; INSN (insn, 2) = id = spirv_id (ctx);
D_var_o(int, def, 4) = uid; INSN (insn, 3) = spv_op->op;
INSN (insn, 4) = uid;
} else { } else {
auto def = spirv_new_insn (spv_op->op, 4, ctx->current); auto insn = spirv_new_insn (spv_op->op, 4, ctx->code_space);
D_var_o(int, def, 1) = tid; INSN (insn, 1) = tid;
D_var_o(int, def, 2) = id = spirv_id (ctx); INSN (insn, 2) = id = spirv_id (ctx);
D_var_o(int, def, 3) = uid; INSN (insn, 3) = uid;
} }
return id; return id;
} }
@ -701,18 +731,19 @@ spirv_expr (const expr_t *e, spirvctx_t *ctx)
unsigned tid = type_id (get_type (e), ctx); unsigned tid = type_id (get_type (e), ctx);
unsigned id; unsigned id;
if (e->expr.constant) { if (e->expr.constant) {
auto def = spirv_new_insn (SpvOpSpecConstantOp, 6, ctx->types); auto globals = ctx->module->globals;
D_var_o(int, def, 1) = tid; auto insn = spirv_new_insn (SpvOpSpecConstantOp, 6, globals);
D_var_o(int, def, 2) = id = spirv_id (ctx); INSN (insn, 1) = tid;
D_var_o(int, def, 3) = spv_op->op; INSN (insn, 2) = id = spirv_id (ctx);
D_var_o(int, def, 4) = bid1; INSN (insn, 3) = spv_op->op;
D_var_o(int, def, 5) = bid2; INSN (insn, 4) = bid1;
INSN (insn, 5) = bid2;
} else { } else {
auto def = spirv_new_insn (spv_op->op, 5, ctx->current); auto insn = spirv_new_insn (spv_op->op, 5, ctx->code_space);
D_var_o(int, def, 1) = tid; INSN (insn, 1) = tid;
D_var_o(int, def, 2) = id = spirv_id (ctx); INSN (insn, 2) = id = spirv_id (ctx);
D_var_o(int, def, 3) = bid1; INSN (insn, 3) = bid1;
D_var_o(int, def, 4) = bid2; INSN (insn, 4) = bid2;
} }
return id; return id;
} }
@ -774,18 +805,19 @@ spirv_value (const expr_t *e, spirvctx_t *ctx)
op += SpvOpSpecConstantTrue - SpvOpConstantTrue; op += SpvOpSpecConstantTrue - SpvOpConstantTrue;
} }
if (op == SpvOpConstant && !val) { if (op == SpvOpConstant && !val) {
auto def = spirv_new_insn (SpvOpConstantNull, 3, ctx->types); auto globals = ctx->module->globals;
D_var_o(int, def, 1) = tid; auto insn = spirv_new_insn (SpvOpConstantNull, 3, globals);
D_var_o(int, def, 2) = value->id = spirv_id (ctx); INSN (insn, 1) = tid;
INSN (insn, 2) = value->id = spirv_id (ctx);
} else { } else {
auto def = spirv_new_insn (op, 3 + val_size, ctx->current); auto insn = spirv_new_insn (op, 3 + val_size, ctx->code_space);
D_var_o(int, def, 1) = tid; INSN (insn, 1) = tid;
D_var_o(int, def, 2) = value->id = spirv_id (ctx); INSN (insn, 2) = value->id = spirv_id (ctx);
if (val_size > 0) { if (val_size > 0) {
D_var_o(int, def, 3) = val; INSN (insn, 3) = val;
} }
if (val_size > 1) { if (val_size > 1) {
D_var_o(int, def, 4) = val >> 32; INSN (insn, 4) = val >> 32;
} }
} }
} }
@ -815,9 +847,9 @@ spirv_assign (const expr_t *e, spirvctx_t *ctx)
internal_error (e, "invalid assignment?"); internal_error (e, "invalid assignment?");
} }
auto def = spirv_new_insn (SpvOpStore, 3, ctx->current); auto insn = spirv_new_insn (SpvOpStore, 3, ctx->code_space);
D_var_o(int, def, 1) = dst; INSN (insn, 1) = dst;
D_var_o(int, def, 2) = src; INSN (insn, 2) = src;
return 0; return 0;
} }
@ -833,10 +865,10 @@ spirv_return (const expr_t *e, spirvctx_t *ctx)
{ {
if (e->retrn.ret_val) { if (e->retrn.ret_val) {
unsigned ret_id = spirv_emit_expr (e->retrn.ret_val, ctx); unsigned ret_id = spirv_emit_expr (e->retrn.ret_val, ctx);
auto def = spirv_new_insn (SpvOpReturnValue, 2, ctx->current); auto insn = spirv_new_insn (SpvOpReturnValue, 2, ctx->code_space);
D_var_o(int, def, 1) = ret_id; INSN (insn, 1) = ret_id;
} else { } else {
spirv_new_insn (SpvOpReturn, 1, ctx->current); spirv_new_insn (SpvOpReturn, 1, ctx->code_space);
} }
return 0; return 0;
} }
@ -893,11 +925,12 @@ spirv_extend (const expr_t *e, spirvctx_t *ctx)
} }
int tid = type_id (res_type, ctx); int tid = type_id (res_type, ctx);
int id = spirv_id (ctx); int id = spirv_id (ctx);
auto def = spirv_new_insn (SpvOpCompositeConstruct, 3 + nids, ctx->current); auto insn = spirv_new_insn (SpvOpCompositeConstruct, 3 + nids,
D_var_o(int, def, 1) = tid; ctx->code_space);
D_var_o(int, def, 2) = id; INSN (insn, 1) = tid;
INSN (insn, 2) = id;
for (int i = 0; i < nids; i++) { for (int i = 0; i < nids; i++) {
D_var_o(int, def, 3 + i) = cids[start + i]; INSN (insn, 3 + i) = cids[start + i];
} }
return id; return id;
} }
@ -937,29 +970,36 @@ spirv_emit_expr (const expr_t *e, spirvctx_t *ctx)
bool bool
spirv_write (struct pr_info_s *pr, const char *filename) spirv_write (struct pr_info_s *pr, const char *filename)
{ {
pr->module->entry_points = defspace_new (ds_backed);
pr->module->exec_modes = defspace_new (ds_backed);
pr->module->strings = defspace_new (ds_backed);
pr->module->names = defspace_new (ds_backed);
pr->module->module_processed = defspace_new (ds_backed);
pr->module->decorations = defspace_new (ds_backed);
pr->module->globals = defspace_new (ds_backed);
pr->module->func_declarations = defspace_new (ds_backed);
pr->module->func_definitions = defspace_new (ds_backed);
spirvctx_t ctx = { spirvctx_t ctx = {
.space = defspace_new (ds_backed), .module = pr->module,
.linkage = defspace_new (ds_backed), .code_space = defspace_new (ds_backed),
.strings = defspace_new (ds_backed), .decl_space = defspace_new (ds_backed),
.names = defspace_new (ds_backed),
.annotations = defspace_new (ds_backed),
.types = defspace_new (ds_backed),
.code = defspace_new (ds_backed),
.type_ids = DARRAY_STATIC_INIT (64), .type_ids = DARRAY_STATIC_INIT (64),
.id = 0, .id = 0,
}; };
auto header = spirv_new_insn (0, 5, ctx.space); auto space = defspace_new (ds_backed);
D_var_o(int, header, 0) = SpvMagicNumber; auto header = spirv_new_insn (0, 5, space);
D_var_o(int, header, 1) = SpvVersion; INSN (header, 0) = SpvMagicNumber;
D_var_o(int, header, 2) = 0; // FIXME get a magic number for QFCC INSN (header, 1) = SpvVersion;
D_var_o(int, header, 3) = 0; // Filled in later INSN (header, 2) = 0; // FIXME get a magic number for QFCC
D_var_o(int, header, 4) = 0; // Reserved INSN (header, 3) = 0; // Filled in later
INSN (header, 4) = 0; // Reserved
for (auto cap = pr->module->capabilities.head; cap; cap = cap->next) { for (auto cap = pr->module->capabilities.head; cap; cap = cap->next) {
spirv_Capability (expr_uint (cap->expr), ctx.space); spirv_Capability (expr_uint (cap->expr), space);
} }
for (auto ext = pr->module->extensions.head; ext; ext = ext->next) { for (auto ext = pr->module->extensions.head; ext; ext = ext->next) {
spirv_Extension (expr_string (ext->expr), ctx.space); spirv_Extension (expr_string (ext->expr), space);
} }
for (auto imp = pr->module->extinst_imports.head; imp; imp = imp->next) { for (auto imp = pr->module->extinst_imports.head; imp; imp = imp->next) {
//FIXME need to store id where it can be used for instructions //FIXME need to store id where it can be used for instructions
@ -967,12 +1007,11 @@ spirv_write (struct pr_info_s *pr, const char *filename)
} }
spirv_MemoryModel (expr_uint (pr->module->addressing_model), spirv_MemoryModel (expr_uint (pr->module->addressing_model),
expr_uint (pr->module->memory_model), ctx.space); expr_uint (pr->module->memory_model), space);
auto srcid = spirv_String (pr->src_name, &ctx); auto srcid = spirv_String (pr->src_name, &ctx);
spirv_Source (0, 1, srcid, nullptr, &ctx); spirv_Source (0, 1, srcid, nullptr, &ctx);
ctx.current = ctx.code;
for (auto func = pr->func_head; func; func = func->next) for (auto func = pr->func_head; func; func = func->next)
{ {
auto func_id = spirv_function (func, &ctx); auto func_id = spirv_function (func, &ctx);
@ -986,16 +1025,20 @@ spirv_write (struct pr_info_s *pr, const char *filename)
if (main_sym && main_sym->sy_type == sy_func) { if (main_sym && main_sym->sy_type == sy_func) {
} }
D_var_o(int, header, 3) = ctx.id + 1; auto mod = pr->module;
defspace_add_data (ctx.space, ctx.linkage->data, ctx.linkage->size); INSN (header, 3) = ctx.id + 1;
defspace_add_data (ctx.space, ctx.strings->data, ctx.strings->size); ADD_DATA (space, mod->entry_points);
defspace_add_data (ctx.space, ctx.names->data, ctx.names->size); ADD_DATA (space, mod->exec_modes);
defspace_add_data (ctx.space, ctx.annotations->data, ctx.annotations->size); ADD_DATA (space, mod->strings);
defspace_add_data (ctx.space, ctx.types->data, ctx.types->size); ADD_DATA (space, mod->names);
defspace_add_data (ctx.space, ctx.code->data, ctx.code->size); ADD_DATA (space, mod->module_processed);
ADD_DATA (space, mod->decorations);
ADD_DATA (space, mod->globals);
ADD_DATA (space, mod->func_declarations);
ADD_DATA (space, mod->func_definitions);
QFile *file = Qopen (filename, "wb"); QFile *file = Qopen (filename, "wb");
Qwrite (file, ctx.space->data, ctx.space->size * sizeof (pr_type_t)); Qwrite (file, space->data, space->size * sizeof (pr_type_t));
Qclose (file); Qclose (file);
return false; return false;
} }