{"id":18016890,"url":"https://github.com/fitzgen/souper-ir","last_synced_at":"2025-03-26T18:32:30.511Z","repository":{"id":57667990,"uuid":"280538844","full_name":"fitzgen/souper-ir","owner":"fitzgen","description":"A library for manipulating Souper IR","archived":false,"fork":false,"pushed_at":"2020-10-16T18:13:51.000Z","size":58,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-17T11:59:45.425Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fitzgen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-17T22:46:05.000Z","updated_at":"2023-09-26T01:18:33.000Z","dependencies_parsed_at":"2022-09-07T15:41:47.154Z","dependency_job_id":null,"html_url":"https://github.com/fitzgen/souper-ir","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fsouper-ir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fsouper-ir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fsouper-ir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fsouper-ir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fitzgen","download_url":"https://codeload.github.com/fitzgen/souper-ir/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245713133,"owners_count":20660348,"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":[],"created_at":"2024-10-30T04:19:37.314Z","updated_at":"2025-03-26T18:32:30.255Z","avatar_url":"https://github.com/fitzgen.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `souper-ir`\n\nA library for manipulating [Souper] IR.\n\n[![](https://docs.rs/souper-ir/badge.svg)](https://docs.rs/souper-ir/)\n[![](https://img.shields.io/crates/v/souper-ir.svg)](https://crates.io/crates/souper-ir)\n[![](https://img.shields.io/crates/d/souper-ir.svg)](https://crates.io/crates/souper-ir)\n![CI](https://github.com/fitzgen/souper-ir/workflows/CI/badge.svg)\n\nThis crate provides AST types for parsing or generating Souper IR. It is a\nsuitable building block either for writing a custom LHS extractor, or for\ntranslating learned optimizations into your own peephole optimizations pass.\n\n### AST\n\nThe AST type definitions and builders live in the `souper_ir::ast` module.\n\n### Parsing Souper IR\n\nWhen the `parse` Cargo feature is enabled, the `souper_ir::parse` module\ncontains functions for parsing Souper IR from a file or an in-memory string.\n\n```rust\nuse std::path::Path;\n\n// We provide a filename to get better error messages.\nlet filename = Path::new(\"example.souper\");\n\nlet replacements = souper_ir::parse::parse_replacements_str(\"\n    ;; x + x --\u003e 2 * x\n    %0 = var\n    %1 = add %0, %0\n    %2 = mul %0, 2\n    cand %1, %2\n\n    ;; x \u0026 x --\u003e x\n    %0 = var\n    %1 = and %0, %0\n    cand %1, %0\n\", Some(filename))?;\n```\n\n### Emitting Souper IR's Text Format\n\nWhen the `stringify` Cargo feature is enabled, then the\n`souper_ir::ast::Replacement`, `souper_ir::ast::LeftHandSide`, and\n`souper_ir::ast::RightHandSide` types all implement `std::fmt::Display`. The\n`Display` implementation writes the AST type out as Souper's text format.\n\n```rust\nuse souper_ir::ast;\n\n// Build this Souper left-hand side:\n//\n//     %x:i32 = var\n//     %y = mul %x, 2\n//     infer %y\n//\n// We expect that Souper would be able to synthesize a right-hand side that\n// does a left shift by one instead of a multiplication.\n\nlet mut lhs = ast::LeftHandSideBuilder::default();\n\nlet x = lhs.assignment(\n    Some(\"x\".into()),\n    Some(ast::Type { width: 32 }),\n    ast::AssignmentRhs::Var,\n    vec![],\n);\n\nlet y = lhs.assignment(\n    Some(\"y\".into()),\n    None,\n    ast::Instruction::Mul {\n        a: x.into(),\n        b: ast::Constant { value: 2, r#type: None }.into(),\n    },\n    vec![],\n);\n\nlet lhs = lhs.finish(y, vec![]);\n\n// Now we can stringify the LHS (and then, presumably, give it to Souper)\n// with `std::fmt::Display`:\n\nuse std::io::Write;\n\nlet mut file = std::fs::File::create(\"my-lhs.souper\")?;\nwrite!(\u0026mut file, \"{}\", lhs)?;\n```\n[Souper]: https://github.com/google/souper\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffitzgen%2Fsouper-ir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffitzgen%2Fsouper-ir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffitzgen%2Fsouper-ir/lists"}