Example #1
0
    def transform(self, trans_stmt):
        '''To transform the "transform" statement'''

        # initialize argument list
        args = {'dtype': None}  # string

        # read all arguments
        for k in trans_stmt.kw_args:
            kname = k.lhs.name
            if kname not in args:
                print 'error:%s: unknown argument: "%s"' % (k.lhs.line_no,
                                                            kname)
                sys.exit(1)
            args[kname] = k.rhs

        # check arguments and update the values of some arguments accordingly
        for kname, exp in args.items():
            if not exp:
                print 'error:%s: missing argument: "%s"' % (trans_stmt.line_no,
                                                            kname)
                sys.exit(1)
            if kname == 'dtype':
                if not isinstance(exp, module.loop.ast.StringLitExp):
                    print('error:%s: data-type must have string-typed values' %
                          exp.line_no)
                    sys.exit(1)
                args[kname] = str(exp.val[1:len(exp.val) - 1])

        # create argument information
        arg_info = [args['dtype']]

        # perform transformation
        transformed_stmt = transformator.transform(trans_stmt.stmt, arg_info)

        return transformed_stmt
Example #2
0
    def transform(self, trans_stmt):
        '''To transform the "transform" statement'''

        # initialize argument list
        args = {'dtype' : None}   # string
        
        # read all arguments
        for k in trans_stmt.kw_args:
            kname = k.lhs.name
            if kname not in args:
                print 'error:%s: unknown argument: "%s"' % (k.lhs.line_no, kname)
                sys.exit(1)
            args[kname] = k.rhs

        # check arguments and update the values of some arguments accordingly
        for kname, exp in args.items():
            if not exp:
                print 'error:%s: missing argument: "%s"' % (trans_stmt.line_no, kname)
                sys.exit(1)
            if kname == 'dtype':
                if not isinstance(exp, module.loop.ast.StringLitExp):
                    print ('error:%s: data-type must have string-typed values'
                           % exp.line_no)
                    sys.exit(1)
                args[kname] = str(exp.val[1:len(exp.val)-1])
                                                            
        # create argument information
        arg_info = [args['dtype']]

        # perform transformation
        transformed_stmt = transformator.transform(trans_stmt.stmt, arg_info)

        return transformed_stmt
Example #3
0
    def transform(self, leader_annot_info, annot_body_code, trailer_annot_code,
                  lang):
        '''To transform the annotated code region'''

        # extract leader annotation information
        leader_annot_code, indent, line_no, module_name, module_body = leader_annot_info

        # parse the module body
        p = parser.getParser(line_no)
        stmts = p.parse(module_body)

        # apply transformation procedure on each statement
        transformed_stmts = transformator.transform(stmts)

        # unparse
        transformed_code = ''
        extra_indent = '  '
        include_orig_loop = False
        if include_orig_loop:
            transformed_code += '\n' + indent + '#if ORIG_LOOP' + '\n'
            transformed_code += annot_body_code.replace(
                '\n', '\n' + extra_indent)
            transformed_code += '\n' + indent + '#else ' + '\n\n'
            for s in transformed_stmts:
                transformed_code += s.unparseToC(indent + extra_indent,
                                                 extra_indent)
            transformed_code += '\n' + indent + '#endif ' + '\n' + indent
        else:
            for s in transformed_stmts:
                transformed_code += s.unparseToC(indent, extra_indent)

        return leader_annot_code + transformed_code + trailer_annot_code
Example #4
0
    def transform(self, leader_annot_info, annot_body_code, trailer_annot_code, lang):
        '''To transform the annotated code region'''

        # extract leader annotation information
        leader_annot_code, indent, line_no, module_name, module_body = leader_annot_info

        # parse the module body
        p = parser.getParser(line_no)
        stmts = p.parse(module_body)

        # apply transformation procedure on each statement
        transformed_stmts = transformator.transform(stmts)
        
        # unparse
        transformed_code = ''
        extra_indent = '  '
        include_orig_loop = False
        if include_orig_loop:
            transformed_code += '\n' + indent + '#if ORIG_LOOP' + '\n'
            transformed_code += annot_body_code.replace('\n', '\n' + extra_indent)
            transformed_code += '\n' + indent + '#else ' + '\n\n'
            for s in transformed_stmts:
                transformed_code += s.unparseToC(indent + extra_indent, extra_indent)
            transformed_code += '\n' + indent + '#endif ' + '\n' + indent
        else:
            for s in transformed_stmts:
                transformed_code += s.unparseToC(indent, extra_indent)

        return leader_annot_code + transformed_code + trailer_annot_code
Example #5
0
    def transform(self, trans_stmt):
        '''To transform the "transform" statement'''

        # initialize argument list
        args = {'order' : None}   # list of tuples (index_name, is_optional)

        # read all arguments
        for k in trans_stmt.kw_args:
            kname = k.lhs.name
            if kname not in args:
                print 'error:%s: unknown argument: "%s"' % (k.lhs.line_no, kname)
                sys.exit(1)
            args[kname] = k.rhs

        # check arguments and update the values of some arguments accordingly
        for kname, exp in args.items():
            if not exp:
                print 'error:%s: missing argument: "%s"' % (trans_stmt.line_no, kname)
                sys.exit(1)
            if kname == 'order':
                args[kname] = extractLoopOrder(exp)
        index_names = [iname for iname, opt in args['order']]
        for i in index_names:
            if index_names.count(i) > 1:
                print ('error:%s: repeated index name in loop order: "%s"'
                       % (trans_stmt.line_no, i))
                sys.exit(1)
                
        # create argument information
        arg_info = [args['order']]
        
        # perform transformation
        transformed_stmt = transformator.transform(trans_stmt.stmt, arg_info)

        return transformed_stmt
Example #6
0
    def transform(self, trans_stmt):
        '''To transform the "transform" statement'''

        # initialize argument list
        args = {'tsize' : None,   # integer
                'tindex' : None}  # IdentExp

        # read all arguments
        for k in trans_stmt.kw_args:
            kname = k.lhs.name
            if kname not in args:
                print 'error:%s: unknown argument: "%s"' % (k.lhs.line_no, kname)
                sys.exit(1)
            args[kname] = k.rhs

        # extract for-loop structure information
        for_loop_info = extractForLoopInfo(trans_stmt.stmt)
        index_id, lbound_exp, ubound_exp, stride_exp, loop_body = for_loop_info

        # check arguments and update the values of some arguments accordingly
        for kname, exp in args.items():
            if not exp:
                print 'error:%s: missing argument: "%s"' % (trans_stmt.line_no, kname)
                sys.exit(1)
            if kname == 'tsize':
                if (not isinstance(exp, module.loop.ast.NumLitExp) or
                    exp.lit_type != module.loop.ast.NumLitExp.INT or
                    exp.val <= 0):
                    print ('error:%s: tile size must be a positive integer: "%s"'
                           % (exp.line_no, exp))
                    sys.exit(1)
                args[kname] = int(exp.val)
            elif kname == 'tindex':
                if not isinstance(exp, module.loop.ast.IdentExp):
                    print ('error:%s: loop iterator must be an identifier: "%s"'
                           % (exp.line_no, exp))
                    sys.exit(1)
        if (not isinstance(stride_exp, module.loop.ast.NumLitExp) or
            stride_exp.lit_type != module.loop.ast.NumLitExp.INT or
            stride_exp.val <= 0):
            print ('error:%s: stride value must be a positive integer: "%s"'
                   % (stride_exp.line_no, stride_exp))
            sys.exit(1)
        tsize = args['tsize']
        stride = stride_exp.val
        if (tsize % stride) != 0:
            print ('error:%s: tile size must be a multiple of the stride value'
                   % trans_stmt.line_no)
            sys.exit(1)

        # create argument information
        arg_info = [args['tsize'], args['tindex']]
        
        # perform transformation
        transformed_stmt = transformator.transform(for_loop_info, arg_info)
        
        return transformed_stmt
Example #7
0
    def transform(self, trans_stmt):
        '''To transform the "transform" statement'''

        # initialize argument list
        args = {'ufactor': None}  # integer

        # read all arguments
        for k in trans_stmt.kw_args:
            kname = k.lhs.name
            if kname not in args:
                print 'error:%s: unknown argument: "%s"' % (k.lhs.line_no,
                                                            kname)
                sys.exit(1)
            args[kname] = k.rhs

        # extract for-loop structure information
        for_loop_info = extractForLoopInfo(trans_stmt.stmt)
        index_id, lbound_exp, ubound_exp, stride_exp, loop_body = for_loop_info

        # check arguments and update the values of some arguments accordingly
        for kname, exp in args.items():
            if not exp:
                print 'error:%s: missing argument: "%s"' % (trans_stmt.line_no,
                                                            kname)
                sys.exit(1)
            if kname == 'ufactor':
                if (not isinstance(exp, module.loop.ast.NumLitExp)
                        or exp.lit_type != module.loop.ast.NumLitExp.INT
                        or exp.val <= 0):
                    print(
                        'error:%s: unrolling factor must be a positive integer: "%"'
                        % (exp.line_no, exp))
                    sys.exit(1)
                args[kname] = int(exp.val)

        # create argument information
        arg_info = [args['ufactor']]

        # perform transformation
        transformed_stmt = transformator.transform(for_loop_info, arg_info)

        return transformed_stmt
Example #8
0
    def transform(self, trans_stmt):
        '''To transform the "transform" statement'''

        # initialize argument list
        args = {'prefix' : None,   # string
                'dtype' : None}    # string

        # read all arguments
        for k in trans_stmt.kw_args:
            kname = k.lhs.name
            if kname not in args:
                print 'error:%s: unknown argument: "%s"' % (k.lhs.line_no, kname)
                sys.exit(1)
            args[kname] = k.rhs

        # extract for-loop structure information
        for_loop_info = extractPerfectlyNestedForLoopInfo(trans_stmt.stmt)

        # check arguments and update the values of some arguments accordingly
        for kname, exp in args.items():
            if not exp:
                print 'error:%s: missing argument: "%s"' % (trans_stmt.line_no, kname)
                sys.exit(1)
            if kname in ('prefix', 'dtype'):
                if not isinstance(exp, module.loop.ast.StringLitExp):
                    print ('error:%s: prefix and data-type must have string-typed values'
                           % exp.line_no)
                    sys.exit(1)
                args[kname] = str(exp.val[1:len(exp.val)-1])
        if not args['prefix']:
            print 'error:%s: prefix name cannot be an empty string' % trans_stmt.line_no
            sys.exit(1)

        # create argument information
        arg_info = [args['prefix'], args['dtype']]

        # perform transformation
        transformed_stmt = transformator.transform(for_loop_info, arg_info)

        return transformed_stmt
Example #9
0
    def transform(self, trans_stmt):
        '''To transform the "transform" statement'''

        # initialize argument list
        args = {'ufactor' : None}  # integer

        # read all arguments
        for k in trans_stmt.kw_args:
            kname = k.lhs.name
            if kname not in args:
                print 'error:%s: unknown argument: "%s"' % (k.lhs.line_no, kname)
                sys.exit(1)
            args[kname] = k.rhs

        # extract for-loop structure information
        for_loop_info = extractForLoopInfo(trans_stmt.stmt)
        index_id, lbound_exp, ubound_exp, stride_exp, loop_body = for_loop_info
        
        # check arguments and update the values of some arguments accordingly
        for kname, exp in args.items():
            if not exp:
                print 'error:%s: missing argument: "%s"' % (trans_stmt.line_no, kname)
                sys.exit(1)
            if kname == 'ufactor':
                if (not isinstance(exp, module.loop.ast.NumLitExp) or
                    exp.lit_type != module.loop.ast.NumLitExp.INT or
                    exp.val <= 0):
                    print ('error:%s: unrolling factor must be a positive integer: "%"'
                           % (exp.line_no, exp))
                    sys.exit(1)
                args[kname] = int(exp.val)

        # create argument information
        arg_info = [args['ufactor']]

        # perform transformation
        transformed_stmt = transformator.transform(for_loop_info, arg_info)

        return transformed_stmt
Example #10
0
    def transform(self, trans_stmt):
        '''To transform the "transform" statement'''

        # initialize argument list
        args = {
            'tsize': None,  # integer
            'tindex': None
        }  # IdentExp

        # read all arguments
        for k in trans_stmt.kw_args:
            kname = k.lhs.name
            if kname not in args:
                print 'error:%s: unknown argument: "%s"' % (k.lhs.line_no,
                                                            kname)
                sys.exit(1)
            args[kname] = k.rhs

        # extract for-loop structure information
        for_loop_info = extractForLoopInfo(trans_stmt.stmt)
        index_id, lbound_exp, ubound_exp, stride_exp, loop_body = for_loop_info

        # check arguments and update the values of some arguments accordingly
        for kname, exp in args.items():
            if not exp:
                print 'error:%s: missing argument: "%s"' % (trans_stmt.line_no,
                                                            kname)
                sys.exit(1)
            if kname == 'tsize':
                if (not isinstance(exp, module.loop.ast.NumLitExp)
                        or exp.lit_type != module.loop.ast.NumLitExp.INT
                        or exp.val <= 0):
                    print(
                        'error:%s: tile size must be a positive integer: "%s"'
                        % (exp.line_no, exp))
                    sys.exit(1)
                args[kname] = int(exp.val)
            elif kname == 'tindex':
                if not isinstance(exp, module.loop.ast.IdentExp):
                    print(
                        'error:%s: loop iterator must be an identifier: "%s"' %
                        (exp.line_no, exp))
                    sys.exit(1)
        if (not isinstance(stride_exp, module.loop.ast.NumLitExp)
                or stride_exp.lit_type != module.loop.ast.NumLitExp.INT
                or stride_exp.val <= 0):
            print('error:%s: stride value must be a positive integer: "%s"' %
                  (stride_exp.line_no, stride_exp))
            sys.exit(1)
        tsize = args['tsize']
        stride = stride_exp.val
        if (tsize % stride) != 0:
            print(
                'error:%s: tile size must be a multiple of the stride value' %
                trans_stmt.line_no)
            sys.exit(1)

        # create argument information
        arg_info = [args['tsize'], args['tindex']]

        # perform transformation
        transformed_stmt = transformator.transform(for_loop_info, arg_info)

        return transformed_stmt