Example #1
0
    def compile(self, module):
        """Compile the module to a relocatable object for this target."""

        if os.getenv("ARTIQ_DUMP_SIG"):
            print("====== MODULE_SIGNATURE DUMP ======", file=sys.stderr)
            print(module, file=sys.stderr)

        type_printer = types.TypePrinter()
        _dump(
            os.getenv("ARTIQ_DUMP_IR"), "ARTIQ IR", ".txt", lambda: "\n".join(
                fn.as_entity(type_printer) for fn in module.artiq_ir))

        llmod = module.build_llvm_ir(self)

        try:
            llparsedmod = llvm.parse_assembly(str(llmod))
            llparsedmod.verify()
        except RuntimeError:
            _dump("", "LLVM IR (broken)", ".ll", lambda: str(llmod))
            raise

        _dump(os.getenv("ARTIQ_DUMP_UNOPT_LLVM"), "LLVM IR (generated)",
              "_unopt.ll", lambda: str(llparsedmod))

        self.optimize(llparsedmod)

        _dump(os.getenv("ARTIQ_DUMP_LLVM"), "LLVM IR (optimized)", ".ll",
              lambda: str(llparsedmod))

        return llparsedmod
Example #2
0
File: jit.py Project: cr1901/artiq
def main():
    libartiq_support = os.getenv('LIBARTIQ_SUPPORT')
    if libartiq_support is not None:
        llvm.load_library_permanently(libartiq_support)

    def process_diagnostic(diag):
        print("\n".join(diag.render()))
        if diag.level in ("fatal", "error"):
            exit(1)

    engine = diagnostic.Engine()
    engine.process = process_diagnostic

    source = "".join(fileinput.input())
    source = source.replace("#ARTIQ#", "")
    mod = Module(Source.from_string(source.expandtabs(), engine=engine))

    target = NativeTarget()
    llmod = mod.build_llvm_ir(target)
    llparsedmod = llvm.parse_assembly(str(llmod))
    llparsedmod.verify()

    llmachine = llvm.Target.from_triple(target.triple).create_target_machine()
    lljit = llvm.create_mcjit_compiler(llparsedmod, llmachine)
    llmain = lljit.get_pointer_to_global(llparsedmod.get_function(llmod.name + ".__modinit__"))
    ctypes.CFUNCTYPE(None)(llmain)()
Example #3
0
    def compile(self, module):
        """Compile the module to a relocatable object for this target."""

        if os.getenv("ARTIQ_DUMP_SIG"):
            print("====== MODULE_SIGNATURE DUMP ======", file=sys.stderr)
            print(module, file=sys.stderr)

        if os.getenv("ARTIQ_IR_NO_LOC") is not None:
            ir.BasicBlock._dump_loc = False

        type_printer = types.TypePrinter()
        _dump(os.getenv("ARTIQ_DUMP_IR"), "ARTIQ IR", ".txt",
              lambda: "\n".join(fn.as_entity(type_printer) for fn in module.artiq_ir))

        llmod = module.build_llvm_ir(self)

        try:
            llparsedmod = llvm.parse_assembly(str(llmod))
            llparsedmod.verify()
        except RuntimeError:
            _dump("", "LLVM IR (broken)", ".ll", lambda: str(llmod))
            raise

        _dump(os.getenv("ARTIQ_DUMP_UNOPT_LLVM"), "LLVM IR (generated)", "_unopt.ll",
              lambda: str(llparsedmod))

        self.optimize(llparsedmod)

        _dump(os.getenv("ARTIQ_DUMP_LLVM"), "LLVM IR (optimized)", ".ll",
              lambda: str(llparsedmod))

        return llparsedmod
Example #4
0
 def finalize(self):
     self.llvm_module_ref = llvm.parse_assembly(str(self.llvm_module))
     pmb = llvm.create_pass_manager_builder()
     pmb.opt_level = 2
     pm = llvm.create_module_pass_manager()
     pmb.populate(pm)
     pm.run(self.llvm_module_ref)
Example #5
0
 def finalize(self):
     self.llvm_module_ref = llvm.parse_assembly(str(self.llvm_module))
     pmb = llvm.create_pass_manager_builder()
     pmb.opt_level = 2
     pm = llvm.create_module_pass_manager()
     pmb.populate(pm)
     pm.run(self.llvm_module_ref)
Example #6
0
def main():
    libartiq_support = os.getenv("LIBARTIQ_SUPPORT")
    if libartiq_support is not None:
        llvm.load_library_permanently(libartiq_support)

    def process_diagnostic(diag):
        print("\n".join(diag.render()))
        if diag.level in ("fatal", "error"):
            exit(1)

    engine = diagnostic.Engine()
    engine.process = process_diagnostic

    source = "".join(fileinput.input())
    source = source.replace("#ARTIQ#", "")
    mod = Module(Source.from_string(source.expandtabs(), engine=engine))

    target = NativeTarget()
    llmod = mod.build_llvm_ir(target)
    llparsedmod = llvm.parse_assembly(str(llmod))
    llparsedmod.verify()

    llmachine = llvm.Target.from_triple(target.triple).create_target_machine()
    lljit = llvm.create_mcjit_compiler(llparsedmod, llmachine)
    llmain = lljit.get_function_address(llmod.name + ".__modinit__")
    ctypes.CFUNCTYPE(None)(llmain)()
Example #7
0
    def compile(self, module):
        """Compile the module to a relocatable object for this target."""

        if os.getenv("ARTIQ_DUMP_SIG"):
            print("====== MODULE_SIGNATURE DUMP ======", file=sys.stderr)
            print(module, file=sys.stderr)

        if os.getenv("ARTIQ_DUMP_IR"):
            print("====== ARTIQ IR DUMP ======", file=sys.stderr)
            type_printer = types.TypePrinter()
            for function in module.artiq_ir:
                print(function.as_entity(type_printer), file=sys.stderr)

        llmod = module.build_llvm_ir(self)

        try:
            llparsedmod = llvm.parse_assembly(str(llmod))
            llparsedmod.verify()
        except RuntimeError:
            print("====== LLVM IR DUMP (PARSE FAILED) ======", file=sys.stderr)
            print(str(llmod), file=sys.stderr)
            raise

        if os.getenv("ARTIQ_DUMP_LLVM"):
            print("====== LLVM IR DUMP ======", file=sys.stderr)
            print(str(llparsedmod), file=sys.stderr)

        llpassmgrbuilder = llvm.create_pass_manager_builder()
        llpassmgrbuilder.opt_level  = 2 # -O2
        llpassmgrbuilder.size_level = 1 # -Os
        llpassmgrbuilder.inlining_threshold = 75 # -Os threshold

        llpassmgr = llvm.create_module_pass_manager()
        llpassmgrbuilder.populate(llpassmgr)
        llpassmgr.run(llparsedmod)

        if os.getenv("ARTIQ_DUMP_LLVM"):
            print("====== LLVM IR DUMP (OPTIMIZED) ======", file=sys.stderr)
            print(str(llparsedmod), file=sys.stderr)

        lltarget = llvm.Target.from_triple(self.triple)
        llmachine = lltarget.create_target_machine(
                        features=",".join(["+{}".format(f) for f in self.features]),
                        reloc="pic", codemodel="default")

        if os.getenv("ARTIQ_DUMP_ASSEMBLY"):
            print("====== ASSEMBLY DUMP ======", file=sys.stderr)
            print(llmachine.emit_assembly(llparsedmod), file=sys.stderr)

        return llmachine.emit_object(llparsedmod)
Example #8
0
    def compile(self, module):
        """Compile the module to a relocatable object for this target."""

        if os.getenv("ARTIQ_DUMP_SIG"):
            print("====== MODULE_SIGNATURE DUMP ======", file=sys.stderr)
            print(module, file=sys.stderr)

        type_printer = types.TypePrinter()
        _dump(os.getenv("ARTIQ_DUMP_IR"), "ARTIQ IR", ".txt",
              lambda: "\n".join(fn.as_entity(type_printer) for fn in module.artiq_ir))

        llmod = module.build_llvm_ir(self)

        try:
            llparsedmod = llvm.parse_assembly(str(llmod))
            llparsedmod.verify()
        except RuntimeError:
            _dump("", "LLVM IR (broken)", ".ll", lambda: str(llmod))
            raise

        _dump(os.getenv("ARTIQ_DUMP_UNOPT_LLVM"), "LLVM IR (generated)", "_unopt.ll",
              lambda: str(llparsedmod))

        llpassmgrbuilder = llvm.create_pass_manager_builder()
        llpassmgrbuilder.opt_level  = 2 # -O2
        llpassmgrbuilder.size_level = 1 # -Os
        llpassmgrbuilder.inlining_threshold = 75 # -Os threshold

        llpassmgr = llvm.create_module_pass_manager()
        llpassmgrbuilder.populate(llpassmgr)
        llpassmgr.run(llparsedmod)

        _dump(os.getenv("ARTIQ_DUMP_LLVM"), "LLVM IR (optimized)", ".ll",
              lambda: str(llparsedmod))

        return llparsedmod
Example #9
0
 def compile(self):
     with open(self.file, "r") as f:
         llmodule = llvm.parse_assembly(f.read())
     llmodule.verify()
     return self.target.link([self.target.assemble(llmodule)],
                             init_fn="__modinit__")
Example #10
0
 def compile(self):
     with open(self.file, "r") as f:
         llmodule = llvm.parse_assembly(f.read())
     llmodule.verify()
     return self.target.link([self.target.assemble(llmodule)],
                             init_fn="__modinit__")