Skip to content

Instantly share code, notes, and snippets.

@soraros
Last active February 10, 2026 07:04
Show Gist options
  • Select an option

  • Save soraros/44d56698cb20a6c5db3160f13ca81675 to your computer and use it in GitHub Desktop.

Select an option

Save soraros/44d56698cb20a6c5db3160f13ca81675 to your computer and use it in GitHub Desktop.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from compile import compile_info, get_linkage_name
from os import path
from reflection.location import call_location
@export
fn f(x: Int) -> Int:
# print_val(x)
return x + 1
fn main():
dump_ir[f, "out"]()
@fieldwise_init
struct Setting(ImplicitlyCopyable):
var kind: StaticString
var ext: StaticString
comptime settings: List[Setting] = [
{"llvm", "ll"},
{"llvm-opt", "opt.ll"},
{"asm", "s"},
]
@no_inline
fn dump_ir[Fn: __TypeOfAllTypes, //, f: Fn, name: StaticString = "out"]():
"""Dump IR for a function to a file."""
print(get_linkage_name[f]())
var dir = path.dirname(call_location[inline_count=0]().file_name)
@parameter
for s in settings:
var ir = compile_info[f, emission_kind = s.kind]()
print("-", full_path := path.join(dir, String(name, ".", s.ext)))
try:
with open(full_path, "w") as f:
f.write(ir)
except e:
print("error:", e)
@no_inline
fn print_val[T: Writable, //](x: T):
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment