1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
7 */
8
9/*
10 * This simple module unrolls the mat.pack into an incremental sequence.
11 * This could speedup parallel processing and releases resources faster.
12 */
13#include "monetdb_config.h"
14#include "opt_matpack.h"
15
16str
17OPTmatpackImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
18{
19 int v, i, j, limit, slimit;
20 InstrPtr p,q;
21 int actions = 0;
22 InstrPtr *old;
23 char buf[256];
24 lng usec = GDKusec();
25
26 //if ( !optimizerIsApplied(mb,"multiplex") )
27 //return 0;
28 (void) pci;
29 (void) cntxt;
30 (void) stk; /* to fool compilers */
31 for( i = 1; i < mb->stop; i++)
32 if( getModuleId(getInstrPtr(mb,i)) == matRef && getFunctionId(getInstrPtr(mb,i)) == packRef && isaBatType(getArgType(mb,getInstrPtr(mb,i),1)))
33 break;
34 if( i == mb->stop)
35 goto wrapup;
36
37 old= mb->stmt;
38 limit= mb->stop;
39 slimit = mb->ssize;
40 if ( newMalBlkStmt(mb,mb->stop) < 0)
41 throw(MAL,"optimizer.matpack", SQLSTATE(HY001) MAL_MALLOC_FAIL);
42 for (i = 0; i < limit; i++) {
43 p = old[i];
44 if( getModuleId(p) == matRef && getFunctionId(p) == packRef && isaBatType(getArgType(mb,p,1))) {
45 q = newInstruction(0, matRef, packIncrementRef);
46 setDestVar(q, newTmpVariable(mb, getArgType(mb,p,1)));\
47 q = pushArgument(mb, q, getArg(p,1));
48 v = getArg(q,0);
49 q = pushInt(mb,q, p->argc - p->retc);
50 pushInstruction(mb,q);
51 typeChecker(cntxt->usermodule,mb,q,TRUE);
52
53 for ( j = 2; j < p->argc; j++) {
54 q = newInstruction(0, matRef, packIncrementRef);
55 q = pushArgument(mb, q, v);
56 q = pushArgument(mb, q, getArg(p,j));
57 setDestVar(q, newTmpVariable(mb, getVarType(mb,v)));
58 v = getArg(q,0);
59 pushInstruction(mb,q);
60 typeChecker(cntxt->usermodule,mb,q,TRUE);
61 }
62 getArg(q,0) = getArg(p,0);
63 freeInstruction(p);
64 actions++;
65 continue;
66 }
67 pushInstruction(mb,p);
68 }
69 for(; i<slimit; i++)
70 if (old[i])
71 freeInstruction(old[i]);
72 GDKfree(old);
73
74 /* Defense line against incorrect plans */
75 if( actions > 0){
76 //chkTypes(cntxt->usermodule, mb, FALSE);
77 //chkFlow(mb);
78 //chkDeclarations(mb);
79 }
80 /* keep all actions taken as a post block comment */
81wrapup:
82 usec = GDKusec()- usec;
83 snprintf(buf,256,"%-20s actions=%2d time=" LLFMT " usec","matpack",actions, usec);
84 newComment(mb,buf);
85 if( actions >= 0)
86 addtoMalBlkHistory(mb);
87
88 if( OPTdebug & OPTmatpack){
89 fprintf(stderr, "#MATPACK optimizer exit\n");
90 fprintFunction(stderr, mb, 0, LIST_MAL_ALL);
91 }
92 return MAL_SUCCEED;
93}
94