Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : /* Author(s) Martin Kersten
14 : * This module contains the framework for inclusion query transformers, i.e.
15 : * C-programs geared at optimizing a piece of MAL.
16 : * The query transformer appears at the language level as an ordinary function,
17 : * but it is effective only at a specific execution phase.
18 : *
19 : * Each optimizer function has access to the runtime scope of the
20 : * routine in which it is called. This can be used to maintain status
21 : * information between successive calls.
22 : *
23 : * The routines below are linked with the kernel by default
24 : */
25 : #include "monetdb_config.h"
26 : #include "mal_scenario.h"
27 : #include "optimizer.h"
28 : #include "optimizer_private.h"
29 : #include "opt_pipes.h"
30 : #include "mal_session.h"
31 :
32 : /*
33 : * Upon loading the module it should inspect the scenario table
34 : * for any unresolved references to the MALoptimizer and set the
35 : * callback function.
36 : */
37 : static str
38 332 : optimizer_prelude(void)
39 : {
40 332 : optimizerInit();
41 332 : return MAL_SUCCEED;
42 : }
43 :
44 : str
45 330 : optimizer_epilogue(void *ret)
46 : {
47 330 : (void) ret;
48 330 : opt_pipes_reset();
49 330 : return MAL_SUCCEED;
50 : }
51 :
52 : /*
53 : * MAL functions can be optimized explicitly using the routines below.
54 : * Beware, the function names should be known as literal strings, because
55 : * you may not know the runtime situation.
56 : */
57 :
58 : str
59 0 : QOToptimize(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
60 : {
61 0 : str modnme;
62 0 : str fcnnme;
63 0 : Symbol s;
64 :
65 0 : (void) stk;
66 0 : if (stk != 0) {
67 0 : modnme = *getArgReference_str(stk, pci, 1);
68 0 : fcnnme = *getArgReference_str(stk, pci, 2);
69 : } else {
70 0 : modnme = getArgDefault(mb, pci, 1);
71 0 : fcnnme = getArgDefault(mb, pci, 2);
72 : }
73 0 : s = findSymbol(cntxt->usermodule, putName(modnme), fcnnme);
74 0 : if (s == NULL)
75 0 : throw(MAL, "optimizer.optimize",
76 : SQLSTATE(HY002) SEMANTIC_OPERATION_MISSING);
77 0 : removeInstruction(mb, pci);
78 0 : return optimizeMALBlock(cntxt, s->def);
79 : }
80 :
81 : #define optwrapper_pattern(NAME, DESC) \
82 : pattern("optimizer", NAME, OPTwrapper, false, "", args(1,1, arg("",str))), \
83 : pattern("optimizer", NAME, OPTwrapper, false, DESC, args(1,3, arg("",str),arg("mod",str),arg("fcn",str)))
84 :
85 :
86 : #include "mel.h"
87 : static mel_func optimizer_init_funcs[] = {
88 : optwrapper_pattern("aliases", "Alias removal optimizer"),
89 : optwrapper_pattern("coercions", "Handle simple type coercions"),
90 : optwrapper_pattern("commonTerms", "Common sub-expression optimizer"),
91 : optwrapper_pattern("candidates", "Mark candidate list variables"),
92 : optwrapper_pattern("constants", "Duplicate constant removal optimizer"),
93 : optwrapper_pattern("profiler", "Collect properties for the profiler"),
94 : optwrapper_pattern("costModel",
95 : "Estimate the cost of a relational expression"),
96 : optwrapper_pattern("dataflow", "Dataflow bracket code injection"),
97 : optwrapper_pattern("deadcode", "Dead code optimizer"),
98 : optwrapper_pattern("emptybind", "Evaluate empty set expressions"),
99 : optwrapper_pattern("evaluate", "Evaluate constant expressions once"),
100 : optwrapper_pattern("garbageCollector", "Garbage collector optimizer"),
101 : optwrapper_pattern("generator", "Sequence generator optimizer"),
102 : optwrapper_pattern("querylog", "Collect SQL query statistics"),
103 : optwrapper_pattern("minimalfast", "Fast compound minimal optimizer pipe"),
104 : optwrapper_pattern("defaultfast", "Fast compound default optimizer pipe"),
105 : optwrapper_pattern("wrapper", "Fake optimizer"),
106 : command("optimizer", "epilogue", optimizer_epilogue, false,
107 : "release the resources held by the optimizer module",
108 : args(1, 1, arg("", void))),
109 : pattern("optimizer", "optimize", QOToptimize, false,
110 : "Optimize a specific operation",
111 : args(0, 2, arg("mod", str), arg("fcn", str))),
112 : optwrapper_pattern("inline", "Expand inline functions"),
113 : optwrapper_pattern("projectionpath", "Join path constructor"),
114 : optwrapper_pattern("mergetable", "Resolve the multi-table definitions"),
115 : optwrapper_pattern("mitosis",
116 : "Modify the plan to exploit parallel processing on multiple cores"),
117 : optwrapper_pattern("multiplex", "Compiler for multiplexed instructions"),
118 : optwrapper_pattern("matpack", "Unroll the mat.pack operation"),
119 : optwrapper_pattern("reduce", "Reduce the stack space claims"),
120 : optwrapper_pattern("remap",
121 : "Remapping function calls to a their multiplex variant"),
122 : optwrapper_pattern("remoteQueries", "Resolve the multi-table definitions"),
123 : optwrapper_pattern("reorder", "Reorder by dataflow dependencies"),
124 : pattern("inspect", "optimizer_stats", OPTstatistics, false,
125 : "Get optimizer use statistics, i.e. calls and total time",
126 : args(3, 3, batarg("", str), batarg("", int), batarg("", lng))),
127 : optwrapper_pattern("pushselect", "Push selects down projections"),
128 : optwrapper_pattern("postfix", "Postfix the plan,e.g. pushing projections"),
129 : optwrapper_pattern("strimps", "Use strimps index if appropriate"),
130 : optwrapper_pattern("for", "Push for decompress down"),
131 : optwrapper_pattern("dict", "Push dict decompress down"),
132 : {.imp = NULL}
133 : };
134 :
135 : #include "mal_import.h"
136 : #ifdef _MSC_VER
137 : #undef read
138 : #pragma section(".CRT$XCU",read)
139 : #endif
140 325 : LIB_STARTUP_FUNC(init_optimizer_mal)
141 : {
142 325 : mal_module2("optimizer", NULL, optimizer_init_funcs, optimizer_prelude,
143 : NULL);
144 325 : }
|