コード例 #1
0
 def test_populate_function_pass_manager(self):
     mod = self.module()
     pmb = self.pmb()
     pm = llvm.create_function_pass_manager(mod)
     pmb.populate(pm)
     pmb.close()
     pm.close()
コード例 #2
0
def compile_ir(engine, llvm_ir, should_optimize):
    """
    Compile the LLVM IR string with the given engine.
    The compiled module object is returned.
    """
    # Create a LLVM module object from the IR
    mod = llvm.parse_assembly(str(llvm_ir))

    if should_optimize:
        pmb = llvm.create_pass_manager_builder()
        pmb.opt_level = 3

        fpm = llvm.create_function_pass_manager(mod)
        pmb.populate(fpm)

        pm = llvm.create_module_pass_manager()
        pmb.populate(pm)

        pm.run(mod)

    mod.verify()
    # Now add the module and make sure it is ready for execution
    engine.add_module(mod)
    engine.finalize_object()
    engine.run_static_constructors()
    return str(mod)
コード例 #3
0
ファイル: codegen.py プロジェクト: stuartarchibald/numba
 def _function_pass_manager(self, llvm_module):
     pm = ll.create_function_pass_manager(llvm_module)
     self._target_data.add_pass(pm)
     self._tm.add_analysis_passes(pm)
     with self._pass_manager_builder() as pmb:
         pmb.populate(pm)
     return pm
コード例 #4
0
ファイル: codegen.py プロジェクト: rxist525/numba
 def _function_pass_manager(self, llvm_module):
     pm = ll.create_function_pass_manager(llvm_module)
     self._target_data.add_pass(pm)
     self._tm.add_analysis_passes(pm)
     with self._pass_manager_builder() as pmb:
         pmb.populate(pm)
     return pm
コード例 #5
0
 def _function_pass_manager(self, llvm_module):
     pm = ll.create_function_pass_manager(llvm_module)
     self._tm.add_analysis_passes(pm)
     with self._pass_manager_builder() as pmb:
         pmb.populate(pm)
     if config.LLVM_REFPRUNE_PASS:
         pm.add_refprune_pass(_parse_refprune_flags())
     return pm
コード例 #6
0
ファイル: llvm_binder.py プロジェクト: GuilhermeSSouza/SimNor
def bind(module, *args, optimize=False):
    module = inject_built_in(module)

    llvm_ir_parsed = llvm.parse_assembly(str(module))
    if False:

        pmb = llvm.create_pass_manager_builder()
        pmb.opt_level = 3

        fpm = llvm.create_function_pass_manager(llvm_ir_parsed)
        pmb.populate(fpm)

        pm = llvm.create_module_pass_manager()
        pmb.populate(pm)
        a = pm.run(llvm_ir_parsed)

    if optimize:
        opt_manager = llvm.PassManagerBuilder()
        mod_manager = llvm.ModulePassManager()

        mod_manager.add_constant_merge_pass()
        mod_manager.add_dead_arg_elimination_pass()
        mod_manager.add_function_inlining_pass(225)
        mod_manager.add_global_dce_pass()
        mod_manager.add_global_optimizer_pass()
        mod_manager.add_ipsccp_pass()
        mod_manager.add_dead_code_elimination_pass()
        mod_manager.add_cfg_simplification_pass()
        mod_manager.add_gvn_pass()
        mod_manager.add_instruction_combining_pass()
        mod_manager.add_licm_pass()
        mod_manager.add_sccp_pass()
        mod_manager.add_type_based_alias_analysis_pass()
        mod_manager.add_basic_alias_analysis_pass()

        mod_manager.run(llvm_ir_parsed)

    ####################################################################

    llvm_ir_parsed.verify()

    # JIT
    target_machine = llvm.Target.from_default_triple().create_target_machine()
    engine = llvm.create_mcjit_compiler(llvm_ir_parsed, target_machine)
    engine.finalize_object()

    entry = engine.get_function_address("main")

    cfunc = CFUNCTYPE(c_int64)(entry)

    result = cfunc()
    #print()
    #print("Programa main:: {}".format(result))
    return [llvm_ir_parsed, result]
コード例 #7
0
ファイル: transformer.py プロジェクト: mattpaletta/numpile
def compile_ir(engine, llvm_ir):
    """
    Compile the LLVM IR string with the given engine.
    The compiled module object is returned.
    """
    # Create a LLVM module object from the IR
    import llvmlite.binding as llvm

    #     llvm_ir = """
    #     define double @add4531207233431041901(double %a, double %b) {
    # entry:
    #   %0 = alloca double
    #   store double %a, double* %0
    #   %1 = alloca double
    #   store double %b, double* %1
    #   %retval = alloca double
    #   %2 = load double, double* %0
    #   %3 = load double, double* %1
    #   %4 = fadd double %2, %3
    #   store double %4, double* %retval
    #   br label %exit
    #
    # exit:
    #   %5 = load double, double* %retval
    #   ret double %5
    # }

    llvm.initialize()
    llvm.initialize_native_target()
    llvm.initialize_native_asmprinter()  # yes, even this one

    mod = llvm.parse_assembly(llvm_ir)
    mod.verify()

    # Optionally, could use passManagerBuilder to reuse the same options across JIT.
    funcPass = llvm.create_function_pass_manager(module=mod)
    funcPass.initialize()

    funcPass.add_dead_arg_elimination_pass()
    funcPass.add_dead_code_elimination_pass()

    passManager = llvm.create_pass_manager_builder()
    passManager.opt_level = 3
    passManager.loop_vectorize = True
    passManager.slp_vectorize = False

    funcPass.finalize()

    # Now add the module and make sure it is ready for execution
    engine.add_module(mod)
    engine.finalize_object()
    engine.run_static_constructors()
    return mod
コード例 #8
0
def build_pass_managers(**kws):
    mod = kws.get('mod')
    if not mod:
        raise NameError("module must be provided")

    pm = llvm.create_module_pass_manager()

    if kws.get('fpm', True):
        assert isinstance(mod, llvm.ModuleRef)
        fpm = llvm.create_function_pass_manager(mod)
    else:
        fpm = None

    with llvm.create_pass_manager_builder() as pmb:
        pmb.opt_level = opt = kws.get('opt', 2)
        pmb.loop_vectorize = kws.get('loop_vectorize', False)
        pmb.slp_vectorize = kws.get('slp_vectorize', False)
        pmb.inlining_threshold = _inlining_threshold(optlevel=opt)

        if mod:
            dl = llvm.create_target_data(mod.data_layout)
            dl.add_pass(pm)
            if fpm is not None:
                dl.add_pass(fpm)

            tli = llvm.create_target_library_info(mod.triple)
            if kws.get('nobuiltins', False):
                # Disable all builtins (-fno-builtins)
                tli.disable_all()
            else:
                # Disable a list of builtins given
                for k in kws.get('disable_builtins', ()):
                    libf = tli.get_libfunc(k)
                    tli.set_unavailable(libf)

            tli.add_pass(pm)
            if fpm is not None:
                tli.add_pass(fpm)

        tm = kws.get('tm')
        if tm:
            tm.add_analysis_passes(pm)
            if fpm is not None:
                tm.add_analysis_passes(fpm)

        pmb.populate(pm)
        if fpm is not None:
            pmb.populate(fpm)

        return namedtuple("pms", ['pm', 'fpm'])(pm=pm, fpm=fpm)
コード例 #9
0
ファイル: passes.py プロジェクト: yelite/llvmlite
def build_pass_managers(**kws):
    mod = kws.get('mod')
    if not mod:
        raise NameError("module must be provided")

    pm = llvm.create_module_pass_manager()

    if kws.get('fpm', True):
        assert isinstance(mod, llvm.ModuleRef)
        fpm = llvm.create_function_pass_manager(mod)
    else:
        fpm = None

    with llvm.create_pass_manager_builder() as pmb:
        pmb.opt_level = opt = kws.get('opt', 2)
        pmb.loop_vectorize = kws.get('loop_vectorize', False)
        pmb.inlining_threshold = _inline_threshold(optlevel=opt)

        if mod:
            dl = llvm.create_target_data(mod.data_layout)
            dl.add_pass(pm)
            if fpm is not None:
                dl.add_pass(fpm)

            tli = llvm.create_target_library_info(mod.triple)
            if kws.get('nobuiltins', False):
                # Disable all builtins (-fno-builtins)
                tli.disable_all()
            else:
                # Disable a list of builtins given
                for k in kws.get('disable_builtins', ()):
                    libf = tli.get_libfunc(k)
                    tli.set_unavailable(libf)

            tli.add_pass(pm)
            if fpm is not None:
                tli.add_pass(fpm)

        tm = kws.get('tm')
        if tm:
            tm.add_analysis_passes(pm)
            if fpm is not None:
                tm.add_analysis_passes(fpm)

        pmb.populate(pm)
        if fpm is not None:
            pmb.populate(fpm)

        return namedtuple("pms", ['pm', 'fpm'])(pm=pm, fpm=fpm)
コード例 #10
0
def execute(module, optimization):
    parsed_module = llvm.parse_assembly(str(module))

    if optimization:
        # initialize pass manager builder
        pmb = llvm.PassManagerBuilder()
        pmb.opt_level = 3

        # initialize function pass manager
        fpm = llvm.create_function_pass_manager(parsed_module)
        pmb.populate(fpm)

        # initialize module pass manager
        pm = llvm.ModulePassManager()
        pmb.populate(pm)

        # add optimization passes
        pm.add_constant_merge_pass()
        pm.add_dead_arg_elimination_pass()
        pm.add_function_attrs_pass()
        pm.add_function_inlining_pass(200)  # threshold = 200
        pm.add_global_dce_pass()
        pm.add_global_optimizer_pass()
        pm.add_ipsccp_pass()
        pm.add_dead_code_elimination_pass()
        pm.add_cfg_simplification_pass()
        pm.add_gvn_pass()
        pm.add_instruction_combining_pass()
        pm.add_licm_pass()
        pm.add_sccp_pass()
        pm.add_sroa_pass()
        pm.add_type_based_alias_analysis_pass()
        pm.add_basic_alias_analysis_pass()

        # run optimization passes on the module
        is_modified = pm.run(parsed_module)

        # check if the optimizations made any modification to the module
        print("Optimizations made modification to the module: ", is_modified)

    parsed_module.verify()
    target_machine = llvm.Target.from_default_triple().create_target_machine()
    engine = llvm.create_mcjit_compiler(parsed_module, target_machine)
    engine.finalize_object()
    entry = engine.get_function_address("run")
    cfunc = CFUNCTYPE(c_int)(entry)
    result = cfunc()
    print("\nexit: {}".format(result))
    return parsed_module
コード例 #11
0
 def pm(self, mod=None):
     mod = mod or self.module()
     return llvm.create_function_pass_manager(mod)
コード例 #12
0
ファイル: ekcc.py プロジェクト: nithinmanne/compilers
def main(input_args=None):
    parser = argparse.ArgumentParser(
        description='The Extended-Kaleidoscope Language Compiler',
        add_help=False,
        usage=
        '%(prog)s [-h|-?] [-v] [-O] [-emit-ast|-emit-llvm] [-jit] [-o <output-file>] <input-file> [args]',
        epilog='Authors: Naga Nithin Manne & Dipti Sengupta')

    parser.add_argument('-h',
                        '-?',
                        action='help',
                        default=argparse.SUPPRESS,
                        help='Show this help message and exit')

    parser.add_argument('-v', action='store_true', help='Enable Verbose mode')

    parser.add_argument('-O', action='store_true', help='Enable Optimization')

    emit_group = parser.add_mutually_exclusive_group()
    emit_group.add_argument('-emit-ast',
                            action='store_true',
                            help='Dump AST in YAML Format to the Output File')
    emit_group.add_argument('-emit-llvm',
                            action='store_true',
                            help='Dump LLVM IR to the Output File')

    parser.add_argument('-jit',
                        action='store_true',
                        help='Run the compiled Code')

    parser.add_argument('-o',
                        metavar='output-file',
                        help='Output File to emit AST or LLVM IR')

    parser.add_argument('input',
                        metavar='input-file',
                        help='Input .ek File to Compile')

    parser.add_argument(
        'args',
        metavar='args',
        nargs='*',
        default=None,
        help='Optional arguments when performing JIT and execution')

    args = parser.parse_args(args=input_args)

    with open(args.input) as input_file:
        ast = yacc_parser.parse(input_file.read())

    if args.jit:
        ast.walk_ast(builder=os.path.basename(args.input), jit=args.args)
    else:
        ast.walk_ast(builder=os.path.basename(args.input), jit=None)

    llvm.initialize()
    llvm.initialize_native_target()
    llvm.initialize_native_asmprinter()
    llvm_ir = str(ast.module)

    # Create a target machine representing the host
    target = llvm.Target.from_default_triple()
    target_machine = target.create_target_machine()
    # And an execution engine with an empty backing module
    backing_mod = llvm.parse_assembly("")
    engine = llvm.create_mcjit_compiler(backing_mod, target_machine)

    # Create a LLVM module object from the IR
    mod = llvm.parse_assembly(llvm_ir)
    mod.verify()

    #add optimization pipeline
    if args.O:
        pmb = llvm.create_pass_manager_builder()
        pmb.opt_level = 3

        fpm = llvm.create_function_pass_manager(mod)
        pmb.populate(fpm)

        pm = llvm.create_module_pass_manager()
        pmb.populate(pm)

        pm.run(mod)

    if args.emit_ast:
        yaml.representer.Representer.add_multi_representer(
            Node, yaml.representer.Representer.represent_dict)
        if args.o:
            with open(args.o, 'w') as ast_output_file:
                yaml.dump(ast,
                          ast_output_file,
                          indent=2,
                          sort_keys=False,
                          explicit_start=True,
                          explicit_end=True)
        else:
            print(
                yaml.dump(ast,
                          indent=2,
                          sort_keys=False,
                          explicit_start=True,
                          explicit_end=True))
    elif args.emit_llvm:
        if args.o:
            with open(args.o, 'w') as ast_output_file:
                print(mod, file=ast_output_file)
        else:
            print(mod)

    # Now add the module and make sure it is ready for execution
    engine.add_module(mod)
    engine.finalize_object()
    engine.run_static_constructors()

    if args.jit:
        func_ptr = engine.get_function_address('run')
        from ctypes import CFUNCTYPE, c_int32
        cfunc = CFUNCTYPE(c_int32)(func_ptr)
        res = cfunc()
        sys.exit(res)
    else:
        if args.o:
            object_file = args.o + '.exe'
        else:
            object_file = args.input + '.exe'
        proc = subprocess.Popen(['clang', '-o', object_file, '-x', 'ir', '-'],
                                stdin=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        proc.communicate(str(mod).encode())
コード例 #13
0
def bind(module, *args, optimize = False):
    module = inject_built_in(module)

    llvm_ir_parsed = llvm.parse_assembly(str(module))
    if False:
        #general way of optimizing
        # print("from optimize")
        pmb = llvm.create_pass_manager_builder()
        pmb.opt_level = 3

        fpm = llvm.create_function_pass_manager(llvm_ir_parsed)
        pmb.populate(fpm)

        pm = llvm.create_module_pass_manager()
        pmb.populate(pm)
        a = pm.run(llvm_ir_parsed)
        # print(f'something was optimized {a}')


    ####################################################################
    if optimize:
        #more specific way of optimizing 
        opt_manager = llvm.PassManagerBuilder()
        mod_manager = llvm.ModulePassManager()

        mod_manager.add_constant_merge_pass()
        mod_manager.add_dead_arg_elimination_pass()
        mod_manager.add_function_inlining_pass(225)
        mod_manager.add_global_dce_pass()
        mod_manager.add_global_optimizer_pass()
        mod_manager.add_ipsccp_pass()
        mod_manager.add_dead_code_elimination_pass()
        mod_manager.add_cfg_simplification_pass()   
        mod_manager.add_gvn_pass()
        mod_manager.add_instruction_combining_pass()
        mod_manager.add_licm_pass()
        mod_manager.add_sccp_pass()
        mod_manager.add_type_based_alias_analysis_pass()
        mod_manager.add_basic_alias_analysis_pass()

        mod_manager.run(llvm_ir_parsed)

    ####################################################################

    llvm_ir_parsed.verify()


    # JIT
    target_machine = llvm.Target.from_default_triple().create_target_machine()
    engine = llvm.create_mcjit_compiler(llvm_ir_parsed, target_machine)
    engine.finalize_object()

    entry = engine.get_function_address("run")

    # arg_types = []
    # for arg in args:
    #     if type(arg) == int:
    #         arg_types.append(c_int)
    #     elif type(arg) == float:
    #         arg_types.append(c_float)
    #
    cfunc = CFUNCTYPE(c_int)(entry)
    # if len(arg_types) != 0:
    #     cfunc = CFUNCTYPE(*arg_types)(entry)

    # arg_values = []
    # for arg in args:
    #     if type(arg) == int:
    #         arg_values.append(arg)
    #     elif type(arg) == float:
    #         arg_values.append(c_float(arg))

    # result = cfunc(*arg_values)
    result = cfunc()
    print()
    print("program returns: {}".format(result))
    return llvm_ir_parsed