Commit 76dfed8a authored by Rob Clark's avatar Rob Clark

nir: mako all the intrinsics

I threatened to do this a long time ago.. I probably *should* have done
it a long time ago when there where many fewer intrinsics.  But the
system of macro/#include magic for dealing with intrinsics is a bit
annoying, and python has the nice property of optional fxn params,
making it possible to define new intrinsics while ignoring parameters
that are not applicable (and naming optional params).  And not having to
specify various array lengths explicitly is nice too.

I think the end result makes it easier to add new intrinsics.

v2: couple small fixes found with a test program to compare the old and
    new tables
v3: misc comments, don't rely on capture=true for meson.build, get rid
    of system_values table to avoid return value of intrinsic() and
    *mostly* remove side-effects, add autotools build support
v4: scons build
Signed-off-by: Rob Clark's avatarRob Clark <robdclark@gmail.com>
Acked-by: Dylan Baker's avatarDylan Baker <dylan@pnwbakers.com>
Acked-by: Jason Ekstrand's avatarJason Ekstrand <jason@jlekstrand.net>
parent cc3a88e8
......@@ -32,7 +32,7 @@ nir_libnir_la_SOURCES = \
$(SPIRV_GENERATED_FILES) \
$(NIR_GENERATED_FILES)
nir/nir_builder_opcodes.h: nir/nir_opcodes.py nir/nir_builder_opcodes_h.py
nir/nir_builder_opcodes.h: nir/nir_opcodes.py nir/nir_builder_opcodes_h.py nir/nir_intrinsics.py
$(MKDIR_GEN)
$(PYTHON_GEN) $(srcdir)/nir/nir_builder_opcodes_h.py > $@ || ($(RM) $@; false)
......@@ -40,6 +40,14 @@ nir/nir_constant_expressions.c: nir/nir_opcodes.py nir/nir_constant_expressions.
$(MKDIR_GEN)
$(PYTHON_GEN) $(srcdir)/nir/nir_constant_expressions.py > $@ || ($(RM) $@; false)
nir/nir_intrinsics.h: nir/nir_intrinsics.py nir/nir_intrinsics_h.py
$(MKDIR_GEN)
$(PYTHON_GEN) $(srcdir)/nir/nir_intrinsics_h.py --outdir nir || ($(RM) $@; false)
nir/nir_intrinsics.c: nir/nir_intrinsics.py nir/nir_intrinsics_c.py
$(MKDIR_GEN)
$(PYTHON_GEN) $(srcdir)/nir/nir_intrinsics_c.py --outdir nir || ($(RM) $@; false)
nir/nir_opcodes.h: nir/nir_opcodes.py nir/nir_opcodes_h.py
$(MKDIR_GEN)
$(PYTHON_GEN) $(srcdir)/nir/nir_opcodes_h.py > $@ || ($(RM) $@; false)
......@@ -112,6 +120,9 @@ EXTRA_DIST += \
nir/nir_algebraic.py \
nir/nir_builder_opcodes_h.py \
nir/nir_constant_expressions.py \
nir/nir_intrinsics.py \
nir/nir_intrinsics_c.py \
nir/nir_intrinsics_h.py \
nir/nir_opcodes.py \
nir/nir_opcodes_c.py \
nir/nir_opcodes_h.py \
......
......@@ -182,6 +182,8 @@ LIBGLCPP_GENERATED_FILES = \
NIR_GENERATED_FILES = \
nir/nir_builder_opcodes.h \
nir/nir_constant_expressions.c \
nir/nir_intrinsics.c \
nir/nir_intrinsics.h \
nir/nir_opcodes.c \
nir/nir_opcodes.h \
nir/nir_opt_algebraic.c
......@@ -202,8 +204,6 @@ NIR_FILES = \
nir/nir_inline_functions.c \
nir/nir_instr_set.c \
nir/nir_instr_set.h \
nir/nir_intrinsics.c \
nir/nir_intrinsics.h \
nir/nir_linking_helpers.c \
nir/nir_liveness.c \
nir/nir_loop_analyze.c \
......
......@@ -59,6 +59,22 @@ env.CodeGenerate(
command = python_cmd + ' $SCRIPT > $TARGET'
)
bldroot = Dir('.').abspath
env.CodeGenerate(
target = 'nir/nir_intrinsics.h',
script = 'nir/nir_intrinsics_h.py',
source = [],
command = python_cmd + ' $SCRIPT --outdir ' + bldroot + '/nir'
)
env.CodeGenerate(
target = 'nir/nir_intrinsics.c',
script = 'nir/nir_intrinsics_c.py',
source = [],
command = python_cmd + ' $SCRIPT --outdir ' + bldroot + '/nir'
)
# parse Makefile.sources
source_lists = env.ParseSourceList('Makefile.sources')
......
......@@ -65,6 +65,24 @@ nir_opt_algebraic_c = custom_target(
depend_files : files('nir_algebraic.py'),
)
nir_intrinsics_h = custom_target(
'nir_intrinsics.h',
input : 'nir_intrinsics_h.py',
output : 'nir_intrinsics.h',
command : [prog_python2, '@INPUT@', '--outdir', meson.current_build_dir()],
capture : false,
depend_files : files('nir_intrinsics.py'),
)
nir_intrinsics_c = custom_target(
'nir_intrinsic.c',
input : 'nir_intrinsics_c.py',
output : 'nir_intrinsics.c',
command : [prog_python2, '@INPUT@', '--outdir', meson.current_build_dir()],
capture: false,
depend_files : files('nir_intrinsics.py'),
)
spirv_info_c = custom_target(
'spirv_info.c',
input : files('../spirv/spirv_info_c.py', '../spirv/spirv.core.grammar.json'),
......@@ -96,8 +114,6 @@ files_libnir = files(
'nir_inline_functions.c',
'nir_instr_set.c',
'nir_instr_set.h',
'nir_intrinsics.c',
'nir_intrinsics.h',
'nir_linking_helpers.c',
'nir_liveness.c',
'nir_loop_analyze.c',
......@@ -202,7 +218,7 @@ libnir = static_library(
'nir',
[files_libnir, spirv_info_c, nir_opt_algebraic_c, nir_opcodes_c,
nir_opcodes_h, nir_constant_expressions_c, nir_builder_opcodes_h,
vtn_gather_types_c],
vtn_gather_types_c, nir_intrinsics_c, nir_intrinsics_h],
include_directories : [inc_common, inc_compiler, include_directories('../spirv')],
c_args : [c_vis_args, c_msvc_compat_args, no_override_init_args],
link_with : libcompiler,
......@@ -211,7 +227,7 @@ libnir = static_library(
# Headers-only dependency
idep_nir_headers = declare_dependency(
sources : [nir_opcodes_h, nir_builder_opcodes_h],
sources : [nir_opcodes_h, nir_builder_opcodes_h, nir_intrinsics_h],
include_directories : include_directories('.'),
)
......
......@@ -945,16 +945,7 @@ typedef struct {
struct nir_function *callee;
} nir_call_instr;
#define INTRINSIC(name, num_srcs, src_components, has_dest, dest_components, \
num_variables, num_indices, idx0, idx1, idx2, flags) \
nir_intrinsic_##name,
#define LAST_INTRINSIC(name) nir_last_intrinsic = nir_intrinsic_##name,
typedef enum {
#include "nir_intrinsics.h"
nir_num_intrinsics = nir_last_intrinsic + 1
} nir_intrinsic_op;
#define NIR_INTRINSIC_MAX_CONST_INDEX 3
......
......@@ -610,32 +610,7 @@ nir_copy_var(nir_builder *build, nir_variable *dest, nir_variable *src)
nir_builder_instr_insert(build, &copy->instr);
}
/* Generic builder for system values. */
static inline nir_ssa_def *
nir_load_system_value(nir_builder *build, nir_intrinsic_op op, int index)
{
nir_intrinsic_instr *load = nir_intrinsic_instr_create(build->shader, op);
load->num_components = nir_intrinsic_infos[op].dest_components;
load->const_index[0] = index;
nir_ssa_dest_init(&load->instr, &load->dest,
nir_intrinsic_infos[op].dest_components, 32, NULL);
nir_builder_instr_insert(build, &load->instr);
return &load->dest.ssa;
}
/* Generate custom builders for system values. */
#define INTRINSIC(name, num_srcs, src_components, has_dest, dest_components, \
num_variables, num_indices, idx0, idx1, idx2, flags)
#define LAST_INTRINSIC(name)
#define DEFINE_SYSTEM_VALUE(name) \
static inline nir_ssa_def * \
nir_load_##name(nir_builder *build) \
{ \
return nir_load_system_value(build, nir_intrinsic_load_##name, 0); \
}
#include "nir_intrinsics.h"
#include "nir_builder_opcodes.h"
static inline nir_ssa_def *
nir_load_barycentric(nir_builder *build, nir_intrinsic_op op,
......
......@@ -41,9 +41,31 @@ nir_${name}(nir_builder *build, ${src_decl_list(opcode.num_inputs)})
}
% endfor
/* Generic builder for system values. */
static inline nir_ssa_def *
nir_load_system_value(nir_builder *build, nir_intrinsic_op op, int index)
{
nir_intrinsic_instr *load = nir_intrinsic_instr_create(build->shader, op);
load->num_components = nir_intrinsic_infos[op].dest_components;
load->const_index[0] = index;
nir_ssa_dest_init(&load->instr, &load->dest,
nir_intrinsic_infos[op].dest_components, 32, NULL);
nir_builder_instr_insert(build, &load->instr);
return &load->dest.ssa;
}
% for name, opcode in filter(lambda v: v[1].sysval, sorted(INTR_OPCODES.iteritems())):
static inline nir_ssa_def *
nir_${name}(nir_builder *build)
{
return nir_load_system_value(build, nir_intrinsic_${name}, 0);
}
% endfor
#endif /* _NIR_BUILDER_OPCODES_ */"""
from nir_opcodes import opcodes
from nir_intrinsics import INTR_OPCODES
from mako.template import Template
print Template(template).render(opcodes=opcodes)
print Template(template).render(opcodes=opcodes, INTR_OPCODES=INTR_OPCODES)
This diff is collapsed.
This diff is collapsed.
template = """\
/* Copyright (C) 2018 Red Hat
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "nir.h"
const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics] = {
% for name, opcode in sorted(INTR_OPCODES.iteritems()):
{
.name = "${name}",
.num_srcs = ${opcode.num_srcs},
.src_components = {
${", ".join(str(comp) for comp in opcode.src_components)}
},
.has_dest = ${"true" if opcode.has_dest else "false"},
.dest_components = ${opcode.dest_components},
.num_variables = ${opcode.num_variables},
.num_indices = ${opcode.num_indices},
.index_map = {
% for i in range(len(opcode.indices)):
[${opcode.indices[i]}] = ${i + 1},
% endfor
},
.flags = ${"0" if len(opcode.flags) == 0 else " | ".join(opcode.flags)},
},
% endfor
};
"""
from nir_intrinsics import INTR_OPCODES
from mako.template import Template
import argparse
import os
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--outdir', required=True,
help='Directory to put the generated files in')
args = parser.parse_args()
path = os.path.join(args.outdir, 'nir_intrinsics.c')
with open(path, 'wb') as f:
f.write(Template(template).render(INTR_OPCODES=INTR_OPCODES))
if __name__ == '__main__':
main()
/*
* Copyright © 2014 Intel Corporation
template = """\
/* Copyright (C) 2018 Red Hat
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
......@@ -19,39 +20,41 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Connor Abbott (cwabbott0@gmail.com)
*
*/
#include "nir.h"
#define OPCODE(name) nir_intrinsic_##name
#define INTRINSIC(_name, _num_srcs, _src_components, _has_dest, \
_dest_components, _num_variables, _num_indices, \
idx0, idx1, idx2, _flags) \
{ \
.name = #_name, \
.num_srcs = _num_srcs, \
.src_components = _src_components, \
.has_dest = _has_dest, \
.dest_components = _dest_components, \
.num_variables = _num_variables, \
.num_indices = _num_indices, \
.index_map = { \
[NIR_INTRINSIC_ ## idx0] = 1, \
[NIR_INTRINSIC_ ## idx1] = 2, \
[NIR_INTRINSIC_ ## idx2] = 3, \
}, \
.flags = _flags \
},
#define NIR_INTRINSIC_xx 0
#define LAST_INTRINSIC(name)
const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics] = {
#include "nir_intrinsics.h"
};
\ No newline at end of file
#ifndef _NIR_INTRINSICS_
#define _NIR_INTRINSICS_
<% opcode_names = sorted(INTR_OPCODES) %>
typedef enum {
% for name in opcode_names:
nir_intrinsic_${name},
% endfor
nir_last_intrinsic = nir_intrinsic_${opcode_names[-1]},
nir_num_intrinsics = nir_last_intrinsic + 1
} nir_intrinsic_op;
#endif /* _NIR_INTRINSICS_ */"""
from nir_intrinsics import INTR_OPCODES
from mako.template import Template
import argparse
import os
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--outdir', required=True,
help='Directory to put the generated files in')
args = parser.parse_args()
path = os.path.join(args.outdir, 'nir_intrinsics.h')
with open(path, 'wb') as f:
f.write(Template(template).render(INTR_OPCODES=INTR_OPCODES))
if __name__ == '__main__':
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment