{"id":14995749,"url":"https://github.com/spcl/pymlir","last_synced_at":"2025-05-15T15:07:08.943Z","repository":{"id":36949847,"uuid":"233456565","full_name":"spcl/pymlir","owner":"spcl","description":"Python interface for MLIR - the Multi-Level Intermediate Representation","archived":false,"fork":false,"pushed_at":"2024-11-28T08:36:41.000Z","size":131,"stargazers_count":255,"open_issues_count":7,"forks_count":42,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-05-08T00:52:34.135Z","etag":null,"topics":["compilers","llvm","mlir","python"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spcl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-12T20:49:35.000Z","updated_at":"2025-05-05T05:55:27.000Z","dependencies_parsed_at":"2024-05-28T20:23:55.198Z","dependency_job_id":"b553e0b7-8b75-430f-8473-866da33f6206","html_url":"https://github.com/spcl/pymlir","commit_stats":{"total_commits":71,"total_committers":10,"mean_commits":7.1,"dds":"0.47887323943661975","last_synced_commit":"4f0986a67a9f44dae9a4daa1704c300b6df58334"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spcl%2Fpymlir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spcl%2Fpymlir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spcl%2Fpymlir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spcl%2Fpymlir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spcl","download_url":"https://codeload.github.com/spcl/pymlir/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254364270,"owners_count":22058878,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compilers","llvm","mlir","python"],"created_at":"2024-09-24T16:19:47.467Z","updated_at":"2025-05-15T15:07:03.929Z","avatar_url":"https://github.com/spcl.png","language":"Python","readme":"[![codecov](https://codecov.io/gh/spcl/pymlir/branch/master/graph/badge.svg)](https://codecov.io/gh/spcl/pymlir)\n\n\n# pyMLIR: Python Interface for the Multi-Level Intermediate Representation\n\npyMLIR is a full Python interface to parse, process, and output [MLIR](https://mlir.llvm.org/) files according to the\nsyntax described in the [MLIR documentation](https://github.com/llvm/llvm-project/tree/master/mlir/docs). pyMLIR \nsupports the basic dialects and can be extended with other dialects. It uses [Lark](https://github.com/lark-parser/lark)\nto parse the MLIR syntax, and mirrors the classes into Python classes. Custom dialects can also be implemented with a\nPython string-format-like syntax, or via direct parsing.\n\nNote that the tool *does not depend on LLVM or MLIR*. It can be installed and invoked directly from Python. \n\n## Instructions \n\n**How to install:** `pip install pymlir`\n\n**Requirements:** Python 3.6 or newer, and the requirements in `setup.py` or `requirements.txt`. To manually install the\nrequirements, use `pip install -r requirements.txt`\n\n**Problem parsing MLIR files?** Run the file through LLVM's `mlir-opt --mlir-print-op-generic` to get the generic form\nof the IR (instructions on how to build/install MLIR can be found [here](https://mlir.llvm.org/getting_started/)):\n```\n$ mlir-opt file.mlir --mlir-print-op-generic \u003e output.mlir\n```\n\n**Found other problems parsing files?** Not all dialects and modes are supported. Feel free to send us an issue or\ncreate a pull request! This is a community project and we welcome any contribution.\n\n## Usage examples\n\n### Parsing MLIR files into Python\n\n```python\nimport mlir\n\n# Read a file path, file handle (stream), or a string\nast1 = mlir.parse_path('/path/to/file.mlir')\nast2 = mlir.parse_file(open('/path/to/file.mlir', 'r'))\nast3 = mlir.parse_string('''\nmodule {\n  func.func @toy_func(%tensor: tensor\u003c2x3xf64\u003e) -\u003e tensor\u003c3x2xf64\u003e {\n    %t_tensor = \"toy.transpose\"(%tensor) { inplace = true } : (tensor\u003c2x3xf64\u003e) -\u003e tensor\u003c3x2xf64\u003e\n    return %t_tensor : tensor\u003c3x2xf64\u003e\n  }\n}\n''')\n```\n\n### Inspecting MLIR files in Python\n\nMLIR files can be inspected by dumping their contents (which will print standard MLIR code), or by using the same tools\nas you would with Python's [ast](https://docs.python.org/3/library/ast.html) module.\n\n```python\nimport mlir\n\n# Dump valid MLIR files\nm = mlir.parse_path('/path/to/file.mlir')\nprint(m.dump())\n\nprint('---')\n\n# Dump the AST directly\nprint(m.dump_ast())\n\nprint('---')\n\n# Or visit each node type by implementing visitor functions\nclass MyVisitor(mlir.NodeVisitor):\n    def visit_Function(self, node: mlir.astnodes.Function):\n        print('Function detected:', node.name.value)\n        \nMyVisitor().visit(m)\n```\n\n### Transforming MLIR files\n\nMLIR files can also be transformed with a Python-like \n[NodeTransformer](https://docs.python.org/3/library/ast.html#ast.NodeTransformer) object.\n\n```python\nimport mlir\n\nm = mlir.parse_path('/path/to/file.mlir')\n\n# Simple node transformer that removes all operations with a result\nclass RemoveAllResultOps(mlir.NodeTransformer):\n    def visit_Operation(self, node: mlir.astnodes.Operation):\n        # There are one or more outputs, return None to remove from AST\n        if len(node.result_list) \u003e 0:\n            return None\n            \n        # No outputs, no need to do anything\n        return self.generic_visit(node)\n        \nm = RemoveAllResultOps().visit(m)\n\n# Write back to file\nwith open('output.mlir', 'w') as fp:\n    fp.write(m.dump())\n```\n\n### Using custom dialects\n\nCustom dialects can be written and loaded as part of the pyMLIR parser. [See full tutorial here](doc/custom_dialect.rst).\n\n```python\nimport mlir\nfrom lark import UnexpectedCharacters\nfrom .mydialect import dialect\n\n# Try to parse as-is\ntry:\n    m = mlir.parse_path('/path/to/matrixfile.mlir')\nexcept UnexpectedCharacters:  # MyMatrix dialect not recognized\n    pass\n    \n# Add dialect to the parser\nm = mlir.parse_path('/path/to/matrixfile.mlir', \n                    dialects=[dialect])\n\n# Print output back\nprint(m.dump_ast())\n```\n\n### MLIR from scratch with the builder API\n\npyMLIR has a Builder API that can create MLIR ASTs on the fly within Python code.\n\n```python\nimport mlir.builder\n\nbuilder = mlir.builder.IRBuilder()\nmlirfile = builder.make_mlir_file()\nmodule = mlirfile.default_module\n\nwith builder.goto_block(builder.make_block(module.region)):\n    hello = builder.function(\"hello_world\")\n    block = builder.make_block(hello.region)\n    builder.position_at_entry(block)\n\n    x, y = builder.add_function_args(hello, [builder.F64, builder.F64], ['a', 'b'])\n\n    adder = builder.addf(x, y, builder.F64)\n    builder.func.ret([adder], [builder.F64])\n\nprint(mlirfile.dump())\n```\nprints:\n```mlir\nmodule {\n  func.func @hello_world(%a: f64, %b: f64) {\n    %_pymlir_ssa = addf %a , %b : f64\n    return %_pymlir_ssa : f64\n  }\n}\n```\n\nSee also [saxpy](tests/test_builder.py) for a full example that registers and uses a dialect in the builder.\n\n### Built-in dialect implementations and more examples\n\nAll dialect implementations can be found in the [dialects](mlir/dialects) subfolder. Additional uses\nof the library, including a custom dialect implementation, can be found in the [tests](tests)\nsubfolder.\n","funding_links":[],"categories":["Python","Frameworks","FFI Bindings"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspcl%2Fpymlir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspcl%2Fpymlir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspcl%2Fpymlir/lists"}