Skip to content

Instantly share code, notes, and snippets.

View aviatesk's full-sized avatar
✈️
JET it!

Shuhei Kadowaki aviatesk

✈️
JET it!
View GitHub Profile
@aviatesk
aviatesk / codegen-lookup.jl
Last active August 30, 2023 14:27
[codegen] custom cache lookup
using InteractiveUtils, Test
using Core: MethodInstance, CodeInstance
using Base: CodegenParams
const CC = Core.Compiler
include("test/compiler/newinterp.jl")
@newinterp ConstInvokeInterp
function CC.concrete_eval_eligible(interp::ConstInvokeInterp,
@nospecialize(f), result::CC.MethodCallResult, arginfo::CC.ArgInfo, sv::CC.AbsIntState)
@aviatesk
aviatesk / monty-hall-prob.jl
Last active April 11, 2023 13:40
Simulate (rather, just "write down") the Monty Hall problem.
function onegame()
doors = (1,2,3)
# drop(i::Int) = i == 1 ? (2,3) : i == 2 ? (1,3) : (1,2)
true_ans = rand(doors)
player1_ans = first_ans = rand(doors)
if first_ans == 1
if true_ans == 1
@aviatesk
aviatesk / ocperformance.jl
Created July 27, 2022 19:47
Check opaque closure invocation performance
import Core: MethodMatch
import Core.Compiler: _methods_by_ftype, InferenceParams, get_world_counter, MethodInstance,
specialize_method, InferenceResult, typeinf, InferenceState, NativeInterpreter,
code_cache
function custominvoke(f, args...)
@nospecialize f args
interp = NativeInterpreter()
tt = Base.signature_type(f, Tuple{map(Core.Typeof, args)...})
mm = get_single_method_match(tt, InferenceParams(interp).MAX_METHODS, get_world_counter(interp))
@aviatesk
aviatesk / custominvoke.jl
Created July 27, 2022 18:22
V and S scratched something
import Core: MethodMatch
import Core.Compiler: _methods_by_ftype, InferenceParams, get_world_counter, MethodInstance,
specialize_method, InferenceResult, typeinf, InferenceState, NativeInterpreter,
code_cache
function custominvoke(f, args...)
@nospecialize f args
interp = NativeInterpreter(get_world_counter())
oc = invoke_in_absint(f, interp, args...)
oc(args...)
@aviatesk
aviatesk / CCProfiler.jl
Last active December 5, 2022 05:45
Core.Compiler performance tracking
const CC = Core.Compiler
import .CC: MethodInstance, CodeInstance, InferenceResult, WorldRange, WorldView
struct CCProfilerCache
dict::IdDict{MethodInstance,CodeInstance}
end
struct CCProfiler <: CC.AbstractInterpreter
world::UInt
inf_cache::Vector{InferenceResult}
code_cache::CCProfilerCache
@aviatesk
aviatesk / devmacro.jl
Last active September 24, 2021 06:27
# write the macro
macro return_const_case(x, d)
@assert isa(d, Symbol)
repr = string(d)
return quote
if $(esc(x)) == $(esc(d))
return $repr
end
end
@aviatesk
aviatesk / memeff.ts
Created August 4, 2021 09:59
memory effect on TypeScript's type safety
interface Foo {
bar: undefined | Bar
}
interface Bar {
baz: string
}
export function getbaz(foo: Foo) {
if (foo.bar) {
# adapted from https://github.com/JuliaCompilerPlugins/Mixtape.jl/blob/75fc4aa25e918f92bfd0ba706236f61a466efc11/examples/dynamic_overlay.jl
module DynamicOverlay
using Mixtape
import Mixtape: CompilationContext, transform, allow
using MacroTools
using CodeInfoTools
using BenchmarkTools
julia> using JET, JET.JETInterfaces
julia> get_module(sv::Core.Compiler.InferenceState) = sv.mod
get_module (generic function with 2 methods)
julia> function get_module(linfo::Core.MethodInstance)
def = linfo.def
return isa(def, Module) ? def : def.module
end
get_module (generic function with 2 methods)
julia> ] add Revise
julia> using Revise
julia> Revise.track(Core.Compiler)
# typeinf_local(interp::AbstractInterpreter, frame::InferenceState)の1行目に以下の変更を加えてsave
# println(frame.slottypes)
julia> code_typed(use_tvar_info, (Union{Int,Nothing},Int); optimize=false)