Permalink
Browse files

added special instructions DICT_GET DICT_GET_DEFAULT

  • Loading branch information...
1 parent 9a75e6a commit 337a959592a1227b796bdfcc8bbdf411230ba33a @iskandr iskandr committed Aug 14, 2013
View
9 Makefile
@@ -22,15 +22,18 @@ TOPDIR := ../..
SRCDIR := $(TOPDIR)/src
VPATH := $(SRCDIR)/falcon
-CPPFLAGS := -I$(SRCDIR) -I$(TOPDIR)/include/python2.7 -I$(SRCDIR)/sparsehash-2.0.2/src
# -fno-gcse -fno-crossjumping
-CFLAGS := $(CPPFLAGS) -Wall -pthread -fno-strict-aliasing -fwrapv -Wall -fPIC -ggdb2 -std=c++0x -funroll-loops
-CXXFLAGS := $(CFLAGS)
INCLUDES := $(shell find $(SRCDIR) -name '*.h') ../../Makefile
opt : COPT := -O3 -funroll-loops
+opt : CPPFLAGS := -I$(SRCDIR) -I$(SRCDIR)/sparsehash-2.0.2/src -I/usr/include/python2.7
+
dbg : COPT := -DFALCON_DEBUG=1 -O0 -fno-omit-frame-pointer
+dbg : CPPFLAGS := -I$(SRCDIR) -I$(SRCDIR)/sparsehash-2.0.2/src -I/usr/include/python2.7
+
+CFLAGS = $(CPPFLAGS) -Wall -pthread -fno-strict-aliasing -fwrapv -Wall -fPIC -ggdb2 -std=c++0x -funroll-loops
+CXXFLAGS = $(CFLAGS)
opt: _falcon_core.so
dbg: _falcon_core.so
View
5 benchmarks/fannkuch.py
@@ -55,4 +55,9 @@ def test_f(self):
self.time_compare(fannkuch, 9, repeat=1)
if __name__ == '__main__':
+ if len(sys.argv) > 2:
+ n = int(sys.argv[2])
+ else:
+ n =
+ fannkuch(9
unittest.main()
View
1 benchmarks/timed_test.py
@@ -4,6 +4,7 @@
import sys
import time
import unittest
+
import numpy as np
logging.basicConfig(
format='%(asctime)s %(filename)s:%(funcName)s %(message)s',
View
28 benchmarks/wordcount.py
@@ -1,22 +1,17 @@
import unittest
from timed_test import TimedTest
-def wc(s):
+#import falcon
+
+#@falcon.wrap
+def wc(s, n_repeat = 200):
counts = {}
get = counts.get
- for line in s.splitlines():
- for word in line.split(" "):
- #if word in counts:
- # counts[word] += 1
- #else:
- # counts[word] = 1
- counts[word] = get(word, 0) + 1
+ for _ in xrange(n_repeat):
+ for line in s.splitlines():
+ for word in line.split(" "):
+ counts[word] = get(word, 0) + 1
return counts
-class TestWordCount(TimedTest):
- def test_wordcount(self):
- reptext = text * 200
- self.time_compare(wc, reptext, repeat=1)
-
text = """
Fellow Workers,
We come before you as Anarchist Communists to explain our principles. We are aware that the minds of many of you have been poisoned by the lies which all parties have diligently spread about us. But surely the persecutions to which we have been and are subjected by the governing classes of all countries should open the eyes of those who love fair play. Thousands of our comrades are suffering in prison or are driven homeless from one country to the other. Free speech - almost the only part of British liberty that can be of any use to the people - is denied to us in many instances, as the events of the last few years have shown.
@@ -136,7 +131,12 @@ def test_wordcount(self):
Study our principles, our movement, and if they convince you join us in our struggle against authority and exploitation, for freedom and happiness for all.
"""
+
if __name__ == '__main__':
- unittest.main()
+ import argparse
+ parser = argparse.ArgumentParser(description="Word counting benchmark")
+ parser.add_argument('--repeat', type=int, default = 200)
+ args = parser.parse_args()
+ wc(text, args.repeat)
View
10 src/falcon/__init__.py
@@ -6,22 +6,18 @@
evaluator = Evaluator()
def run_function(f, *args, **kw):
- print "NO WRAPPER", "ARGS = ", args, "KW =", kw
+ # print "NO WRAPPER", "ARGS = ", args, "KW =", kw
return evaluator.eval_python(f, args, kw)
- #frame = evaluator.frame_from_pyfunc(f, args, kw)
- #return evaluator.eval(frame)
-
+
def wrap(f):
'''Function decorator.
Functions wrapped in this decorator will be compiled and run via falcon.
'''
def wrapper(*args, **kw):
- print "CALLING with args =", args, "kw =", kw
+ # print "CALLING with args =", args, "kw =", kw
return evaluator.eval_python(f, args, kw)
- #frame = evaluator.frame_from_pyfunc(f, args, kw)
- #return evaluator.eval(frame)
wrapper.func_name = f.func_name
return wrapper
View
8 src/falcon/__main__.py
@@ -14,11 +14,9 @@ def main():
sys.path.insert(0, os.path.dirname(script))
with open(script, 'rb') as fp:
code = compile(fp.read(), script, 'exec')
-
-# import dis
-# dis.dis(code)
- f_frame = falcon.evaluator.frame_from_codeobj(code)
- falcon.evaluator.eval(f_frame)
+
+ e = falcon.Evaluator()
+ e.eval_python(code, (), {})
if __name__ == '__main__':
main()
View
54 src/falcon/optimizations.h
@@ -506,7 +506,8 @@ class DeadCodeElim: public BackwardPass, UseCounts, TypeInference {
enum KnownMethod {
METHOD_UNKNOWN,
- METHOD_LIST_APPEND
+ METHOD_LIST_APPEND,
+ METHOD_DICT_GET,
};
class LocalTypeSpecialization: public CompilerPass, protected TypeInference {
@@ -530,30 +531,61 @@ class LocalTypeSpecialization: public CompilerPass, protected TypeInference {
void visit_op(CompilerOp* op) {
switch (op->code) {
case LOAD_ATTR: {
+ StaticType t = this->get_type(op->regs[0]);
PyObject* attr_name_obj = PyTuple_GetItem(this->names, op->arg);
char* attr_name = PyString_AsString(attr_name_obj);
- if (strcmp(attr_name, "append") == 0) {
+ if (t == LIST && strcmp(attr_name, "append") == 0) {
this->known_methods[op->regs[1]] = METHOD_LIST_APPEND;
this->known_bound_objects[op->regs[1]] = op->regs[0];
+ } else if (t == DICT && strcmp(attr_name, "get") == 0) {
+ this->known_methods[op->regs[1]] = METHOD_DICT_GET;
+ this->known_bound_objects[op->regs[1]] = op->regs[0];
}
break;
}
case CALL_FUNCTION: {
int fn_reg = op->regs[0];
- if (this->find_method(fn_reg) == METHOD_LIST_APPEND) {
- op->code = LIST_APPEND;
- int item = op->regs[1];
- op->has_dest = false;
- op->arg = 0;
- op->regs.clear();
-
- op->regs.push_back(this->known_bound_objects[fn_reg]);
- op->regs.push_back(item);
+ KnownMethod method = this->find_method(fn_reg);
+ if (method != METHOD_UNKNOWN) {
+ int n_args = op->regs.size();
+ if (method == METHOD_LIST_APPEND) {
+ op->code = LIST_APPEND;
+ int item = op->regs[1];
+ op->has_dest = false;
+ op->arg = 0;
+ op->regs.clear();
+
+ op->regs.push_back(this->known_bound_objects[fn_reg]);
+ op->regs.push_back(item);
+ } else if (method == METHOD_DICT_GET && n_args == 3) {
+ op->code = DICT_GET;
+ int key = op->regs[1];
+ int dest = op->regs[2];
+ op->has_dest = true;
+ op->arg = 0;
+ op->regs.clear();
+ op->regs.push_back(this->known_bound_objects[fn_reg]);
+ op->regs.push_back(key);
+ op->regs.push_back(dest);
+ } else if (method == METHOD_DICT_GET && n_args == 4) {
+ op->code = DICT_GET_DEFAULT;
+ int key = op->regs[1];
+ int value = op->regs[2];
+ int dest = op->regs[3];
+ op->has_dest = true;
+ op->arg = 0;
+ op->regs.clear();
+ op->regs.push_back(this->known_bound_objects[fn_reg]);
+ op->regs.push_back(key);
+ op->regs.push_back(value);
+ op->regs.push_back(dest);
+ }
}
break;
+
}
case BINARY_SUBSCR: {
StaticType t = this->get_type(op->regs[0]);
View
5 src/falcon/oputil.cc
@@ -127,10 +127,11 @@ const char* OpUtil::name(int opcode) {
case CONST_INDEX: return "CONST_INDEX";
case BINARY_SUBSCR_LIST : return "BINARY_SUBSCR_LIST";
case BINARY_SUBSCR_DICT : return "BINARY_SUBSCR_DICT";
- case DICT_CONTAINS : return "DICT_CONTAINS";
case STORE_SUBSCR_LIST : return "STORE_SUBSCR_LIST";
case STORE_SUBSCR_DICT : return "STORE_SUBSCR_DICT";
-
+ case DICT_CONTAINS : return "DICT_CONTAINS";
+ case DICT_GET : return "DICT_GET";
+ case DICT_GET_DEFAULT : return "DICT_GET_DEFAULT";
}
return "BAD_OP";
View
8 src/falcon/oputil.h
@@ -11,9 +11,11 @@
#define CONST_INDEX 150
#define BINARY_SUBSCR_LIST 151
#define BINARY_SUBSCR_DICT 152
-#define DICT_CONTAINS 153
-#define STORE_SUBSCR_LIST 154
-#define STORE_SUBSCR_DICT 155
+#define STORE_SUBSCR_LIST 153
+#define STORE_SUBSCR_DICT 154
+#define DICT_CONTAINS 155
+#define DICT_GET 156
+#define DICT_GET_DEFAULT 157
struct OpUtil {
static const char* name(int opcode);
View
53 src/falcon/reval.cc
@@ -808,6 +808,40 @@ struct DictContains : public RegOpImpl<RegOp<3>, DictContains> {
}
};
+struct DictGet : public RegOpImpl<RegOp<3>, DictGet> {
+ static f_inline void _eval(Evaluator *eval, RegisterFrame* frame, RegOp<3>& op, Register* registers) {
+ PyObject* dict = LOAD_OBJ(op.reg[0]);
+ CHECK_VALID(dict);
+
+ PyObject* key = LOAD_OBJ(op.reg[1]);
+ CHECK_VALID(key);
+
+ PyObject* result = PyDict_GetItem(dict, key);
+ if (result == NULL) {
+ result = Py_None;
+ }
+ Py_INCREF(result);
+ STORE_REG(op.reg[2], result);
+ }
+};
+
+struct DictGetDefault : public RegOpImpl<RegOp<4>, DictGetDefault> {
+ static f_inline void _eval(Evaluator *eval, RegisterFrame* frame, RegOp<4>& op, Register* registers) {
+ PyObject* dict = LOAD_OBJ(op.reg[0]);
+ CHECK_VALID(dict);
+
+ PyObject* key = LOAD_OBJ(op.reg[1]);
+ CHECK_VALID(key);
+
+ PyObject* result = PyDict_GetItem(dict, key);
+ if (result == NULL) {
+ result = LOAD_OBJ(op.reg[2]);
+ }
+ Py_INCREF(result);
+ STORE_REG(op.reg[3], result);
+ }
+};
+
struct IncRef: public RegOpImpl<RegOp<1>, IncRef> {
static f_inline void _eval(Evaluator *eval, RegisterFrame* frame, RegOp<1>& op, Register* registers) {
CHECK_VALID(LOAD_OBJ(op.reg[0]));
@@ -1596,6 +1630,13 @@ struct ImportName: public RegOpImpl<RegOp<3>, ImportName> {
}
PyObject* res = PyEval_CallObject(import, args);
+ if (res == NULL) {
+ PyErr_Print();
+ throw RException(PyExc_ImportError, "Failed to import name %s",
+ PyString_AsString(name));
+ }
+ // band-aid to prevent segfaults, not sure why this incref makes things work
+ Py_IncRef(res);
STORE_REG(op.reg[2], res);
}
};
@@ -1853,9 +1894,11 @@ static const void* labels[] = {
OFFSET(CONST_INDEX),
OFFSET(BINARY_SUBSCR_LIST),
OFFSET(BINARY_SUBSCR_DICT),
- OFFSET(DICT_CONTAINS),
OFFSET(STORE_SUBSCR_LIST),
OFFSET(STORE_SUBSCR_DICT),
+ OFFSET(DICT_CONTAINS),
+ OFFSET(DICT_GET),
+ OFFSET(DICT_GET_DEFAULT),
}
;
@@ -1895,7 +1938,7 @@ DEFINE_OP(BINARY_SUBSCR_DICT, BinarySubscrDict);
DEFINE_OP(CONST_INDEX, ConstIndex);
-DEFINE_OP(DICT_CONTAINS, DictContains);
+
BINARY_OP3(INPLACE_MULTIPLY, PyNumber_InPlaceMultiply, IntegerOps::mul, true);
BINARY_OP3(INPLACE_DIVIDE, PyNumber_InPlaceDivide, IntegerOps::div, true);
@@ -1974,6 +2017,12 @@ DEFINE_OP(INCREF, IncRef);
DEFINE_OP(DECREF, DecRef);
DEFINE_OP(LIST_APPEND, ListAppend);
+
+DEFINE_OP(DICT_CONTAINS, DictContains);
+DEFINE_OP(DICT_GET, DictGet);
+DEFINE_OP(DICT_GET_DEFAULT, DictGetDefault);
+
+
DEFINE_OP(SLICE, Slice);
DEFINE_OP(IMPORT_STAR, ImportStar);

0 comments on commit 337a959

Please sign in to comment.