compiler: setting ARTIQ_DUMP_* dumps to a file instead of stderr.

This commit is contained in:
whitequark 2016-02-24 17:13:17 +00:00
parent 6b28b8e36e
commit 442673076f
1 changed files with 23 additions and 22 deletions

View File

@ -43,6 +43,17 @@ class RunTool:
for f in self.files: for f in self.files:
f.close() f.close()
def _dump(target, kind, suffix, content):
if target is not None:
print("====== {} DUMP ======".format(kind.upper()), file=sys.stderr)
if target == "":
file = tempfile.NamedTemporaryFile(suffix, delete=False)
else:
file = open(target + suffix, "wb")
file.write(content())
file.close()
print("{} dumped as {}".format(kind, file.name), file=sys.stderr)
class Target: class Target:
""" """
A description of the target environment where the binaries A description of the target environment where the binaries
@ -73,11 +84,9 @@ class Target:
print("====== MODULE_SIGNATURE DUMP ======", file=sys.stderr) print("====== MODULE_SIGNATURE DUMP ======", file=sys.stderr)
print(module, file=sys.stderr) print(module, file=sys.stderr)
if os.getenv("ARTIQ_DUMP_IR"): type_printer = types.TypePrinter()
print("====== ARTIQ IR DUMP ======", file=sys.stderr) _dump(os.getenv("ARTIQ_DUMP_IR"), "ARTIQ IR", ".txt",
type_printer = types.TypePrinter() lambda: "\n".join(fn.as_entity(type_printer) for fn in module.artiq_ir))
for function in module.artiq_ir:
print(function.as_entity(type_printer), file=sys.stderr)
llmod = module.build_llvm_ir(self) llmod = module.build_llvm_ir(self)
@ -85,13 +94,11 @@ class Target:
llparsedmod = llvm.parse_assembly(str(llmod)) llparsedmod = llvm.parse_assembly(str(llmod))
llparsedmod.verify() llparsedmod.verify()
except RuntimeError: except RuntimeError:
print("====== LLVM IR DUMP (PARSE FAILED) ======", file=sys.stderr) _dump("", "LLVM IR (broken)", ".ll", lambda: str(llmod))
print(str(llmod), file=sys.stderr)
raise raise
if os.getenv("ARTIQ_DUMP_LLVM"): _dump(os.getenv("ARTIQ_DUMP_UNOPT_LLVM"), "LLVM IR (generated)", "_unopt.ll",
print("====== LLVM IR DUMP ======", file=sys.stderr) lambda: str(llparsedmod))
print(str(llparsedmod), file=sys.stderr)
llpassmgrbuilder = llvm.create_pass_manager_builder() llpassmgrbuilder = llvm.create_pass_manager_builder()
llpassmgrbuilder.opt_level = 2 # -O2 llpassmgrbuilder.opt_level = 2 # -O2
@ -102,18 +109,16 @@ class Target:
llpassmgrbuilder.populate(llpassmgr) llpassmgrbuilder.populate(llpassmgr)
llpassmgr.run(llparsedmod) llpassmgr.run(llparsedmod)
if os.getenv("ARTIQ_DUMP_LLVM"): _dump(os.getenv("ARTIQ_DUMP_LLVM"), "LLVM IR (optimized)", ".ll",
print("====== LLVM IR DUMP (OPTIMIZED) ======", file=sys.stderr) lambda: str(llparsedmod))
print(str(llparsedmod), file=sys.stderr)
lltarget = llvm.Target.from_triple(self.triple) lltarget = llvm.Target.from_triple(self.triple)
llmachine = lltarget.create_target_machine( llmachine = lltarget.create_target_machine(
features=",".join(["+{}".format(f) for f in self.features]), features=",".join(["+{}".format(f) for f in self.features]),
reloc="pic", codemodel="default") reloc="pic", codemodel="default")
if os.getenv("ARTIQ_DUMP_ASSEMBLY"): _dump(os.getenv("ARTIQ_DUMP_ASM"), "Assembly", ".s",
print("====== ASSEMBLY DUMP ======", file=sys.stderr) lambda: llmachine.emit_assembly(llparsedmod))
print(llmachine.emit_assembly(llparsedmod), file=sys.stderr)
return llmachine.emit_object(llparsedmod) return llmachine.emit_object(llparsedmod)
@ -127,12 +132,8 @@ class Target:
as results: as results:
library = results["output"].read() library = results["output"].read()
if os.getenv("ARTIQ_DUMP_ELF"): _dump(os.getenv("ARTIQ_DUMP_ELF"), "Shared library", ".so",
shlib_temp = tempfile.NamedTemporaryFile(suffix=".so", delete=False) lambda: library)
shlib_temp.write(library)
shlib_temp.close()
print("====== SHARED LIBRARY DUMP ======", file=sys.stderr)
print("Shared library dumped as {}".format(shlib_temp.name), file=sys.stderr)
return library return library