Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Frontend] Replace IR with bytecode #1311

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
Frontend no longer uses pybind11 to connect to the compiler. Instead, it uses subprocess instead.
[(#1285)](https://github.com/PennyLaneAI/catalyst/pull/1285)

As a result of this, we now pass the MLIR module as MLIR bytecode instead of the textual form.
[(#1311)](https://github.com/PennyLaneAI/catalyst/pull/1311)

* Replace pybind11 with nanobind for C++/Python bindings in the frontend.
[(#1173)](https://github.com/PennyLaneAI/catalyst/pull/1173)

Expand Down
11 changes: 6 additions & 5 deletions frontend/catalyst/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,69 +428,72 @@
return cmd

@debug_logger
def run_from_ir(self, ir: str, module_name: str, workspace: Directory):
"""Compile a shared object from a textual IR (MLIR or LLVM).

Args:
ir (str): Textual MLIR to be compiled
module_name (str): Module name to use for naming
workspace (Directory): directory that holds output files and/or debug dumps.

Returns:
output_filename (str): Output file name. For the default pipeline this would be the
shared object library path.
out_IR (str): Output IR in textual form. For the default pipeline this would be the
LLVM IR.
"""
assert isinstance(
workspace, Directory
), f"Compiler expects a Directory type, got {type(workspace)}."
assert workspace.is_dir(), f"Compiler expects an existing directory, got {workspace}."

lower_to_llvm = self.options.lower_to_llvm or False
output_ir_ext = ".ll" if lower_to_llvm else ".mlir"

if self.options.verbose:
print(f"[LIB] Running compiler driver in {workspace}", file=self.options.logfile)

data = ir if type(ir) != str else ir.encode("utf-8")
suffix = ".bc" if type(ir) != str else ".mlir"

with tempfile.NamedTemporaryFile(
mode="w", suffix=".mlir", dir=str(workspace), delete=False
mode="w+b", suffix=suffix, dir=str(workspace), delete=False
dime10 marked this conversation as resolved.
Show resolved Hide resolved
) as tmp_infile:
tmp_infile_name = tmp_infile.name
tmp_infile.write(ir)
tmp_infile.write(data)

output_object_name = os.path.join(str(workspace), f"{module_name}.o")
output_ir_name = os.path.join(str(workspace), f"{module_name}{output_ir_ext}")

cmd = self.get_cli_command(tmp_infile_name, output_ir_name, module_name, workspace)
try:
if self.options.verbose:
print(f"[SYSTEM] {' '.join(cmd)}", file=self.options.logfile)
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
if self.options.verbose or os.getenv("ENABLE_DIAGNOSTICS"):
if result.stdout:
print(result.stdout.strip(), file=self.options.logfile)
if result.stderr:
print(result.stderr.strip(), file=self.options.logfile)
except subprocess.CalledProcessError as e: # pragma: nocover
raise CompileError(
f"catalyst-cli failed with error code {e.returncode}: {e.stderr}"
) from e

with open(output_ir_name, "r", encoding="utf-8") as f:
out_IR = f.read()

if lower_to_llvm:
output = LinkerDriver.run(output_object_name, options=self.options)
output_object_name = str(pathlib.Path(output).absolute())

# Clean up temporary files
if os.path.exists(tmp_infile_name):
os.remove(tmp_infile_name)
if os.path.exists(output_ir_name):
os.remove(output_ir_name)

return output_object_name, out_IR

Check notice on line 496 in frontend/catalyst/compiler.py

View check run for this annotation

codefactor.io / CodeFactor

frontend/catalyst/compiler.py#L431-L496

Complex Method

@debug_logger
def run(self, mlir_module, *args, **kwargs):
Expand All @@ -509,9 +512,7 @@
"""

return self.run_from_ir(
mlir_module.operation.get_asm(
binary=False, print_generic_op_form=False, assume_verified=True
),
mlir_module.operation.get_asm(binary=True, assume_verified=True),
str(mlir_module.operation.attributes["sym_name"]).replace('"', ""),
*args,
**kwargs,
Expand Down
Loading