Skip to content

Commit edaebd2

Browse files
authored
Merge pull request #708 from zickgraf/compiler_improvements
Some improvements to the compiler
2 parents 3a97766 + 7463598 commit edaebd2

22 files changed

+271
-206
lines changed

CAP/gap/CAP.gi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ InstallMethod( CreateCapCategory,
595595
category!.enable_compilation := enable_compilation;
596596

597597
category!.compiled_functions := rec( );
598+
category!.compiled_functions_trees := rec( );
598599

599600
CREATE_CAP_CATEGORY_FILTERS( category );
600601

CAP/gap/CategoriesCategory.gi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ InstallGlobalFunction( CAP_INTERNAL_CREATE_Cat,
3232

3333
function( )
3434

35-
InstallValue( CapCat, rec( caching_info := rec( ), overhead := true, is_computable := true, enable_compilation := false, compiled_functions := rec( ) ) );
35+
InstallValue( CapCat, rec( caching_info := rec( ), overhead := true, is_computable := true, enable_compilation := false, compiled_functions := rec( ), compiled_functions_trees := rec( ) ) );
3636

3737
CREATE_CAP_CATEGORY_OBJECT( CapCat, [ [ "Name", "Cat" ] ] );
3838

CAP/gap/InstallAdds.gi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ InstallGlobalFunction( CapInternalInstallAdd,
571571
if not IsBound( category!.compiled_functions.( function_name ) ) then
572572

573573
category!.compiled_functions.( function_name ) := [ ];
574+
category!.compiled_functions_trees.( function_name ) := [ ];
574575

575576
fi;
576577

CompilerForCAP/PackageInfo.g

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SetPackageInfo( rec(
1010

1111
PackageName := "CompilerForCAP",
1212
Subtitle := "Speed up computations in CAP categories",
13-
Version := "2021.09-02",
13+
Version := "2021.09-03",
1414
Date := Concatenation( "01/", ~.Version{[ 6, 7 ]}, "/", ~.Version{[ 1 .. 4 ]} ),
1515
License := "GPL-2.0-or-later",
1616

CompilerForCAP/doc/Extension.autodoc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ a completely new one, see <Ref Sect="Section_CompilationSteps" />.
77

88
For debugging you can:
99
* use <Ref Func="CapJitPrettyPrintSyntaxTree" />,
10-
* set `debug` to `true` in <Ref Func="CapJitCompiledFunction" /> (Note: this causes informational break loops which are not actual errors),
10+
* set `debug` to `true` in the code of <Ref Func="CapJitCompiledFunctionAsEnhancedSyntaxTree" />
11+
(Note: this causes informational break loops which are not actual errors),
1112
* use the `debug` and `debug_path` record entries of logic templates (see <Ref Func="CapJitAddLogicTemplate" />).
1213

1314
@Section Logic
@@ -26,11 +27,6 @@ To simplify the handling of syntax trees, the CAP compiler enhances syntax trees
2627
* Branches of STAT_IF etc. are given the type BRANCH_IF.
2728
* If the body of a BRANCH_IF is not a STAT_SEQ_STAT, the body is wrapped in a STAT_SEQ_STAT.
2829
* The key-value pairs of EXPR_RECs are given the type REC_KEY_VALUE_PAIR.
29-
* Statements of the form `if condition then return expr_if_true; else return expr_if_false; fi` and
30-
`if condition then var := expr_if_true; else var := expr_if_false; fi` are coded using
31-
a new expression type `EXPR_CONDITIONAL` with components `condition`, `expr_if_true`, and `expr_if_false`.
32-
This makes such statements easier to handle. Hopefully, GAP will support conditional expressions
33-
(i.e. `condition ? expr_if_true : expr_if_false`) natively in the future.
3430
* A globally unique ID is assigned to each function.
3531
* The handling of local variables and higher variables is unified by the concept of function variables:
3632
function variables (FVARs) reference local variables in functions via the function id (`func_id`) and

CompilerForCAP/examples/CAP_JIT_NEXT_FUNCCALL_DOES_NOT_RETURN_FAIL.g

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,6 @@ func2 := function( x )
4141
#% CAP_JIT_RESOLVE_FUNCTION
4242
if x = 1 then return fail; else return 1; fi; end;;
4343

44-
# we have to work hard to not write semicolons so AutoDoc
45-
# does not begin a new statement
46-
func3 := EvalString( ReplacedString( """function( x )
47-
local y@
48-
#% CAP_JIT_RESOLVE_FUNCTION
49-
if x = 1 then
50-
y := fail@
51-
else
52-
y := 1@
53-
fi@
54-
return y@
55-
end""", "@", ";" ) );;
56-
5744
call_func1 := function( x )
5845
#% CAP_JIT_NEXT_FUNCCALL_DOES_NOT_RETURN_FAIL
5946
return func1( x ); end;;
@@ -62,10 +49,6 @@ call_func2 := function( x )
6249
#% CAP_JIT_NEXT_FUNCCALL_DOES_NOT_RETURN_FAIL
6350
return func2( x ); end;;
6451

65-
call_func3 := function( x )
66-
#% CAP_JIT_NEXT_FUNCCALL_DOES_NOT_RETURN_FAIL
67-
return func3( x ); end;;
68-
6952
Display( CapJitCompiledFunction( call_func1, [ 2 ] ) );
7053
#! function ( x_1 )
7154
#! return 1;
@@ -74,9 +57,5 @@ Display( CapJitCompiledFunction( call_func2, [ 2 ] ) );
7457
#! function ( x_1 )
7558
#! return 1;
7659
#! end
77-
Display( CapJitCompiledFunction( call_func3, [ 2 ] ) );
78-
#! function ( x_1 )
79-
#! return 1;
80-
#! end
8160

8261
#! @EndExample

CompilerForCAP/examples/EXPR_CONDITIONAL.g

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,28 @@
22

33
#! @Section Tests
44

5+
#! @Example
6+
57
LoadPackage( "CompilerForCAP" );
8+
#! true
69

7-
#! @Example
10+
MY_ID_FUNC := x -> x;;
811

912
func1 := function( x )
1013
if x = 1 then return 1; else return 2; fi; end;;
1114

1215
tree1 := ENHANCED_SYNTAX_TREE( func1 );;
16+
tree1 := CapJitDetectedTernaryConditionalExpressions( tree1 );;
1317
tree1.stats.statements[1].obj.type = "EXPR_CONDITIONAL";
1418
#! true
1519

1620
coded_func1 := ENHANCED_SYNTAX_TREE_CODE( tree1 );;
1721
String( func1 ) = ReplacedString( String( coded_func1 ), "_1", "" );
1822
#! true
1923

20-
func2 := function( x )
21-
local y; if x = 1 then y := 1; else y := 2; fi; return y; end;;
2224

23-
tree2 := ENHANCED_SYNTAX_TREE( func2 );;
24-
tree2.stats.statements[1].rhs.type = "EXPR_CONDITIONAL";
25-
#! true
2625

27-
coded_func2 := ENHANCED_SYNTAX_TREE_CODE( tree2 );;
28-
String( func2 ) = ReplacedString( String( coded_func2 ), "_1", "" );
29-
#! true
30-
31-
tree3 := rec(
26+
tree2 := rec(
3227
type := "EXPR_FUNC",
3328
id := 1,
3429
nams := [ ],
@@ -44,7 +39,7 @@ tree3 := rec(
4439
type := "EXPR_FUNCCALL",
4540
funcref := rec(
4641
type := "EXPR_REF_GVAR",
47-
gvar := "IdFunc",
42+
gvar := "MY_ID_FUNC",
4843
),
4944
args := [
5045
rec(
@@ -68,10 +63,10 @@ tree3 := rec(
6863
),
6964
);;
7065

71-
coded_func3 := ENHANCED_SYNTAX_TREE_CODE( tree3 );;
72-
Display( coded_func3 );
66+
coded_func2 := ENHANCED_SYNTAX_TREE_CODE( tree2 );;
67+
Display( coded_func2 );
7368
#! function ( )
74-
#! return IdFunc( function ( )
69+
#! return MY_ID_FUNC( function ( )
7570
#! if false then
7671
#! return 1;
7772
#! else
@@ -81,28 +76,48 @@ Display( coded_func3 );
8176
#! end( ) );
8277
#! end
8378

84-
coded_func3();
79+
coded_func2();
8580
#! 2
8681

87-
func4 := function( x )
88-
local y; if x then y := 1; else y := 2; fi; return IdFunc( y ); end;;
8982

90-
compiled_func4 := CapJitCompiledFunction( func4, [ true ] );;
91-
Display( compiled_func4 );
83+
84+
# we have to work hard to not write semicolons so AutoDoc
85+
# does not begin a new statement
86+
func3 := EvalString( ReplacedString( """function( x )
87+
local inner_func@
88+
89+
inner_func := function( )
90+
local y@
91+
if x then
92+
return 1@
93+
else
94+
y := 2@
95+
return y@
96+
fi@
97+
end@
98+
99+
return MY_ID_FUNC( inner_func( ) )@
100+
101+
end""", "@", ";" ) );;
102+
103+
compiled_func3 := CapJitCompiledFunction( func3, [ true ] );;
104+
Display( compiled_func3 );
92105
#! function ( x_1 )
93106
#! if x_1 then
94-
#! return ID_FUNC( 1 );
107+
#! return MY_ID_FUNC( 1 );
95108
#! else
96-
#! return ID_FUNC( 2 );
109+
#! return MY_ID_FUNC( 2 );
97110
#! fi;
98111
#! return;
99112
#! end
100113

101-
func5 := function( x )
114+
115+
116+
func4 := function( x )
102117
local y; if x then return 1; else return 1; fi; end;;
103118

104-
compiled_func5 := CapJitCompiledFunction( func5, [ true ] );;
105-
Display( compiled_func5 );
119+
compiled_func4 := CapJitCompiledFunction( func4, [ true ] );;
120+
Display( compiled_func4 );
106121
#! function ( x_1 )
107122
#! return 1;
108123
#! end

CompilerForCAP/examples/LinearAlgebraForCAP.g

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ Display( SYNTAX_TREE_CODE( tree2 ) );
5959
#! function ( logic_new_func_x_2, logic_new_func_y_2 )
6060
#! return UnionOfColumns( UnderlyingRing( cat_1 ),
6161
#! Dimension( logic_new_func_x_2 ),
62-
#! List( logic_new_func_y_2, function ( s_2_3 )
63-
#! return UnderlyingMatrix( s_2_3 );
62+
#! List( logic_new_func_y_2, function ( s_3 )
63+
#! return UnderlyingMatrix( s_3 );
6464
#! end ) );
6565
#! end ) ) );
6666
#! end

CompilerForCAP/gap/CompilerForCAP.gd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ DeclareInfoClass( "InfoCapJit" );
2626
#! @Section Compiling a function manually
2727

2828
#! @Description
29-
#! Returns a compiled version of the function <A>func</A>.
29+
#! Returns a compiled version of the function <A>func</A> (if <A>func</A> is an operation or a kernel function, it is returned unchanged).
3030
#! The list of arguments <A>jit_args</A> is used to infer the types of variables.
3131
#! If <A>jit_args</A> is shorter than the number of arguments accepted by <A>func</A>,
3232
#! all steps which require knowledge about the types of variables are skipped.
@@ -35,3 +35,10 @@ DeclareInfoClass( "InfoCapJit" );
3535
#! @Returns a function
3636
#! @Arguments func, jit_args
3737
DeclareGlobalFunction( "CapJitCompiledFunction" );
38+
39+
#! @Description
40+
#! Like <Ref Func="CapJitCompiledFunction" />, but returns an enhanced syntax tree of the compiled function.
41+
#! <A>func</A> must not be an operation or a kernel function because those cannot properly be represented as a syntax tree.
42+
#! @Returns a record
43+
#! @Arguments func, jit_args
44+
DeclareGlobalFunction( "CapJitCompiledFunctionAsEnhancedSyntaxTree" );

CompilerForCAP/gap/CompilerForCAP.gi

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,27 @@ InstallGlobalFunction( ContinueCompilationAtCategory, function ( category )
2121
end );
2222

2323
InstallGlobalFunction( CapJitCompiledFunction, function ( func, jit_args )
24+
25+
if IsOperation( func ) or IsKernelFunction( func ) then
26+
27+
Info( InfoCapJit, 1, "<func> is a operation or kernel function, this is not supported yet." );
28+
return func;
29+
30+
fi;
31+
32+
return ENHANCED_SYNTAX_TREE_CODE( CapJitCompiledFunctionAsEnhancedSyntaxTree( func, jit_args ) );
33+
34+
end );
35+
36+
InstallGlobalFunction( CapJitCompiledFunctionAsEnhancedSyntaxTree, function ( func, jit_args )
2437
local debug, tree, orig_tree, compiled_func;
2538

2639
Info( InfoCapJit, 1, "####" );
2740
Info( InfoCapJit, 1, "Start compilation." );
2841

2942
if IsOperation( func ) or IsKernelFunction( func ) then
3043

31-
Info( InfoCapJit, 1, "<func> is a operation or kernel function, this is not supported yet." );
32-
return func;
44+
Error( "<func> is a operation or kernel function, this is not supported yet." );
3345

3446
fi;
3547

@@ -197,6 +209,14 @@ InstallGlobalFunction( CapJitCompiledFunction, function ( func, jit_args )
197209

198210
tree := CapJitInlinedVariableAssignments( tree );
199211

212+
if debug then
213+
compiled_func := ENHANCED_SYNTAX_TREE_CODE( tree );
214+
Display( compiled_func );
215+
Error( "apply CapJitDetectedTernaryConditionalExpressions" );
216+
fi;
217+
218+
tree := CapJitDetectedTernaryConditionalExpressions( tree );
219+
200220
od;
201221

202222
# post-processing
@@ -233,11 +253,9 @@ InstallGlobalFunction( CapJitCompiledFunction, function ( func, jit_args )
233253

234254
fi;
235255

236-
compiled_func := ENHANCED_SYNTAX_TREE_CODE( tree );
237-
238256
Info( InfoCapJit, 1, "####" );
239257
Info( InfoCapJit, 1, "Compilation finished." );
240258

241-
return compiled_func;
259+
return tree;
242260

243261
end );

0 commit comments

Comments
 (0)