Skip to content

Commit

Permalink
[JIT] Autogenerate tile predeclarations
Browse files Browse the repository at this point in the history
It turns out this was only used for the tile templates, which means we
can happily inline this.
  • Loading branch information
bdw committed Jan 6, 2019
1 parent 939a738 commit 0afa896
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 122 deletions.
2 changes: 1 addition & 1 deletion build/Makefile.in
Expand Up @@ -643,7 +643,7 @@ src/jit/core_templates.h: src/jit/core_templates.expr src/jit/macro.expr \
src/jit/x64/tile_pattern.h: src/jit/x64/tile_pattern.tile tools/tiler-table-generator.pl src/jit/expr_ops.h

src/jit/expr@obj@: src/jit/core_templates.h
src/jit/tile@obj@: src/jit/x64/tile_pattern.h src/jit/x64/tile_decl.h
src/jit/tile@obj@: src/jit/x64/tile_pattern.h

src/jit/compile@obj@ src/jit/linear_scan@obj@ src/jit/x64/arch@obj@ @jit_obj@: src/jit/internal.h src/jit/x64/arch.h

Expand Down
4 changes: 1 addition & 3 deletions docs/jit/how-to-add-an-missing-expression-operator.org
Expand Up @@ -99,9 +99,7 @@ MVM_JIT_TILE_DECL(add_reg) {
The =MVM_JIT_TILE_DECL= macro expands to a tile function declaration
(which is assigned to a tile *object* in the tile *list*, which is
what we use to represent the compiled code just prior to emitting it
to the DynASM buffer). There is a second (redundant) pre-declaration
of the tile function in the =src/jit/x64/tile_decl.h= which you'll
also need to add.
to the DynASM buffer).

For x64, common tile functions are:

Expand Down
22 changes: 4 additions & 18 deletions docs/jit/tiles.md
Expand Up @@ -210,24 +210,10 @@ machine code for it's architecture. For machine code output we use the
library. Please read that link for detailed information on DynASM.
With DynASM, tile implementation becomes simple.

For x64, the tile name is declared in
`src/jit/x64/tile_decl.h`. This is necessary as the tiler
tables (which themselves reside in the generated header file
`src/jit/x64/tile_tables.h` refer to the tile function
symbol, and so require the name to be predeclared. Because the tile
function signature is rather long, I use a macro to define the
function:

/* in src/jit/x64/tile_decl.h */
MVM_JIT_TILE_DECL(xor);
MVM_JIT_TILE_DECL(xor_const);
MVM_JIT_TILE_DECL(xor_addr);

The implementation is defined in
`src/jit/x64/tiles.dasc`. This file is included from
`src/jit/x64/emit.dasc`, which is the file that includes
the original JIT architecture-specific code. A tile implementation
looks much like this:
The tile implementation is defined in `src/jit/x64/tiles.dasc`. This
file is included from `src/jit/x64/emit.dasc`, which is the file that
includes the original JIT architecture-specific code. A tile
implementation looks much like this:

/* in src/jit/x64/tiles.dasc, supposing we only care about 8 byte xor */
MVM_JIT_TILE_DECL(xor) {
Expand Down
1 change: 0 additions & 1 deletion src/jit/tile.c
Expand Up @@ -3,7 +3,6 @@
#include <math.h>

#if MVM_JIT_ARCH == MVM_JIT_ARCH_X64
#include "jit/x64/tile_decl.h"
#include "jit/x64/tile_pattern.h"
#endif

Expand Down
4 changes: 3 additions & 1 deletion src/jit/tile.h
Expand Up @@ -67,5 +67,7 @@ void MVM_jit_tile_list_edit(MVMThreadContext *tc, MVMJitTileList *list);
void MVM_jit_tile_list_destroy(MVMThreadContext *tc, MVMJitTileList *list);

#define MVM_JIT_TILE_YIELDS_VALUE(t) ((t)->register_spec & 1)

#define MVM_JIT_TILE_NAME(name) MVM_jit_tile ## name
#define MVM_JIT_TILE_DECL(name) \
void MVM_jit_tile_ ## name (MVMThreadContext *tc, MVMJitCompiler *compiler, MVMJitTile *tile, MVMJitExprTree *tree)
void MVM_JIT_TILE_NAME(name) (MVMThreadContext *tc, MVMJitCompiler *compiler, MVMJitTile *tile, MVMJitExprTree *tree)
60 changes: 0 additions & 60 deletions src/jit/x64/tile_decl.h

This file was deleted.

2 changes: 0 additions & 2 deletions src/jit/x64/tiles.dasc
@@ -1,6 +1,4 @@
/* -*-C-*- */
#include "tile_decl.h"

#define DIE(...) do { MVM_oops(tc, __VA_ARGS__); } while (0)

/* NB: The rax/eax/ax/al/ah register is *reserved* for internal use in tiles by
Expand Down
70 changes: 34 additions & 36 deletions tools/tiler-table-generator.pl
Expand Up @@ -101,7 +101,7 @@ sub decompose {
# value symbol
push @{$ctx->{path}}, @trace, $i, '.';
# this is a value symbol, so add it to the bitmap
$ctx->{refs} += (1 << $ctx->{num});
$ctx->{refs} += (1 << $ctx->{num});
$ctx->{num}++;
push @{$ctx->{spec}}, register_spec($item);
} # else head
Expand Down Expand Up @@ -398,6 +398,15 @@ sub compute_costs {
my %table = generate_table(\@rules, \@rulesets);
my %min_cost = compute_costs(\@rules, \@rulesets, \%table);

# Tiling works by selecting *possible* rules bottom-up and picking
# the *optimum* rules top-down. So we need to know, starting from
# a rule and it's children's rulesets, how to select the best rules.
my @symbols = uniq(map { $_->{sym} } @rules);
my %symnum;
for (my $i = 0; $i < @symbols; $i++) {
$symnum{$symbols[$i]} = $i;
}


sub bits {
my $i = 0;
Expand All @@ -411,38 +420,29 @@ sub bits {




# Write tables
my $output;
if (defined $OUTFILE) {
open $output, '>', $OUTFILE or die "Could not open $OUTFILE";
} else {
$output = \*STDOUT;
open my $output, '>', $OUTFILE or die "Could not open $OUTFILE";
select $output;
}

print $output <<"HEADER";
/* FILE AUTOGENERATED BY $0. DO NOT EDIT.
* Define tables for tiler DFA. */
HEADER

# Tiling works by selecting *possible* rules bottom-up and picking
# the *optimum* rules top-down. So we need to know, starting from
# a rule and it's children's rulesets, how to select the best rules.
my @symbols = uniq(map { $_->{sym} } @rules);
my %symnum;
for (my $i = 0; $i < @symbols; $i++) {
$symnum{$symbols[$i]} = $i;
local $\ = "\n";
print "/* FILE AUTOGENERATED BY $0. DO NOT EDIT. */";
print '/* Tile function declarations */';
for my $tile (grep $_, map $_->{name}, @rules) {
print "${PREFIX}TILE_DECL($tile);";
}


print $output "static const MVMJitTileTemplate ${VARNAME}templates[] = {\n";
print '/* Tile template declarations */';
print "static const MVMJitTileTemplate ${VARNAME}templates[] = {";
for (my $rule_nr = 0; $rule_nr < @rules; $rule_nr++) {
my $rule = $rules[$rule_nr];
my ($head, $sym1, $sym2) = @{$rule->{pat}};
my $sn1 = defined $sym1 ? $symnum{$sym1} : -1;
my $sn2 = defined $sym2 ? $symnum{$sym2} : -1;
my ($func, $path, $text, $refs, $nval, $spec);
if (exists $rule->{name}) {
$func = defined $rule->{name} ? $VARNAME . $rule->{name} : "NULL";
$func = defined $rule->{name} ? "${PREFIX}TILE_NAME($rule->{name})" : "NULL";
$path = sprintf('"%s"', $rule->{path});
$text = sprintf('"%s"', $rule->{text});
$refs = $rule->{refs};
Expand All @@ -455,8 +455,7 @@ sub bits {
$nval = 0;
$spec = 0;
}
print $output qq(
{
print qq( {
$func,
$path,
$text,
Expand All @@ -468,20 +467,20 @@ sub bits {
},);

}
print $output "};\n\n";

print "};";

print $output "static const MVMint32 ${VARNAME}select[][3] = {\n";
print '/* Tiler tables */';
print "static const MVMint32 ${VARNAME}select[][3] = {";
for (my $ruleset_nr = 0; $ruleset_nr < @rulesets; $ruleset_nr++) {
for (my $sym_nr = 0; $sym_nr < @symbols; $sym_nr++) {
my $rule = $min_cost{$ruleset_nr,$symbols[$sym_nr]};
next unless defined $rule;
print $output " { $ruleset_nr, $sym_nr, $rule },\n";
print " { $ruleset_nr, $sym_nr, $rule },";
}
}
print $output "};\n\n";
print "};";

print $output <<"COMMENT";
print <<"COMMENT";
/* Each table item consists of 5 integers:
* 0..3 -> lookup key (nodenr, ruleset_1, ruleset_2)
Expand All @@ -494,22 +493,21 @@ sub bits {
* intermediates. */
COMMENT
print $output "static MVMint32 ".$VARNAME."state[][6] = {\n";
print "static MVMint32 ".$VARNAME."state[][6] = {";
for my $expr_op (@EXPR_OPS) {
my $head = lc($expr_op->[0]);
for my $rs1 (sortn keys %{$table{$head}}) {
for my $rs2 (sortn keys %{$table{$head}{$rs1}}) {
my $state = $table{$head}{$rs1}{$rs2};
my $best = $min_cost{$state,'reg'} // $min_cost{$state,'void'} // -1;
printf $output ' { %s%s, %s, %s, %d, %d },' . "\n",
printf ' { %s%s, %s, %s, %d, %d },',
$PREFIX, $expr_op->[0], $rs1, $rs2, $state, $best;
}
}
}
print $output "};\n\n";

print $output <<"LOOKUP";
print "};";

print <<"LOOKUP";
/* Lookup routines. Implemented here so that we may change it
* independently from tiler */
Expand Down Expand Up @@ -576,9 +574,9 @@ sub bits {
return -1;
return ${VARNAME}select[mid][2];
}
LOOKUP
close $output;

close STDOUT;



Expand Down

0 comments on commit 0afa896

Please sign in to comment.