Reusable Cranelift-like SSA intermediate representation
///|
test "build an add function with IRBuilder" {
let builder = IRBuilder::new("add_i32")
let lhs = builder.add_param(I32)
let rhs = builder.add_param(I32)
builder.add_result(I32)
let entry = builder.create_block()
builder.switch_to_block(entry)
let sum = builder.iadd(lhs, rhs)
builder.return_([sum])
let func = builder.get_function()
inspect(func.verify(), content="()")
inspect(func.blocks.length(), content="1")
inspect(instruction_count(func), content="1")
}///|
test "fold constants in a MilkIR function" {
let builder = IRBuilder::new("const_add")
builder.add_result(I32)
let entry = builder.create_block()
builder.switch_to_block(entry)
let lhs = builder.iconst_i32(10)
let rhs = builder.iconst_i32(20)
let sum = builder.iadd(lhs, rhs)
builder.return_([sum])
let func = builder.get_function()
let before = instruction_count(func)
let result = fold_constants(func)
inspect(result.changed, content="true")
inspect(instruction_count(func) <= before, content="true")
inspect(func.verify(), content="()")
}type EGraphBuilderfn EGraphBuilder::new_with_limits_and_ruleset(saturation_limits : SaturationLimits, ruleset : IndexedRuleSet) -> EGraphBuilderpub struct EGraphOptimizeStats {
changed : Bool
total_classes : Int
total_nodes : Int
total_rule_applications : Int
}pub(all) struct ExtOp {
dialect : String
opcode : String
immediates : FixedArray[Int]
} derive(Debug)pub struct Function {
name : String
signature : Signature
params : Array[(Value, Type)]
results : Array[Type]
blocks : Array[Block]
stack_slots : Array[StackSlot]
external_symbols : Array[ExternalSymbol]
next_value_id : Int
next_inst_id : Int
next_block_id : Int
next_stack_slot_id : Int
} derive(Debug)type IRBuilderpub(all) enum Opcode {
Iconst(Int64)
Fconst(Double)
Iadd
Isub
Imul
Umulh
Smulh
Sdiv
Udiv
Srem
Urem
Band
Bor
Bxor
Bnot
Ishl
Sshr
Ushr
Rotl
Rotr
Clz
Ctz
Popcnt
Icmp(IntCC)
IcmpEq
Fadd
Fsub
Fmul
Fdiv
Fmin
Fmax
Fcmp(FloatCC)
Fneg
Fabs
Fsqrt
Fceil
Ffloor
Ftrunc
Fnearest
Ireduce
Sextend
Uextend
Fpromote
Fdemote
FcvtToSint
FcvtToUint
FcvtToSintSat
FcvtToUintSat
SintToFcvt
UintToFcvt
Bitcast
Sextend8
Sextend16
Sextend32
Select
Copy
Load
Store
StackAddr(StackSlot)
Call(ExternalSymbol)
CallIndirect(Signature)
LoadPtr(Type)
StorePtr(Type)
LoadPtrNarrow(Type, Int, Bool)
StorePtrNarrow(Int)
CallPtr(Int, Int)
Trap(String)
Custom(String)
Ext(ExtOp)
V128Const(Bytes)
V128Splat8
V128Splat16
V128Splat32
V128Splat64
V128SplatF32
V128SplatF64
V128ExtractLane8S(Int)
V128ExtractLane8U(Int)
V128ExtractLane16S(Int)
V128ExtractLane16U(Int)
V128ExtractLane32(Int)
V128ExtractLane64(Int)
V128ExtractLaneF32(Int)
V128ExtractLaneF64(Int)
V128ReplaceLane8(Int)
V128ReplaceLane16(Int)
V128ReplaceLane32(Int)
V128ReplaceLane64(Int)
V128ReplaceLaneF32(Int)
V128ReplaceLaneF64(Int)
V128Shuffle(FixedArray[Int])
V128Swizzle
V128Not
V128And
V128AndNot
V128Or
V128Xor
V128Bitselect
V128AnyTrue
V128AllTrue8
V128AllTrue16
V128AllTrue32
V128AllTrue64
V128Bitmask8
V128Bitmask16
V128Bitmask32
V128Bitmask64
V128Add8
V128Add16
V128Add32
V128Add64
V128Sub8
V128Sub16
V128Sub32
V128Sub64
V128Mul16
V128Mul32
V128Mul64
V128AddSat8S
V128AddSat8U
V128AddSat16S
V128AddSat16U
V128SubSat8S
V128SubSat8U
V128SubSat16S
V128SubSat16U
V128Min8S
V128Min8U
V128Min16S
V128Min16U
V128Min32S
V128Min32U
V128Max8S
V128Max8U
V128Max16S
V128Max16U
V128Max32S
V128Max32U
V128Avgr8U
V128Avgr16U
V128Abs8
V128Abs16
V128Abs32
V128Abs64
V128Neg8
V128Neg16
V128Neg32
V128Neg64
V128Popcnt8
V128Shl8
V128Shl16
V128Shl32
V128Shl64
V128Shr8S
V128Shr8U
V128Shr16S
V128Shr16U
V128Shr32S
V128Shr32U
V128Shr64S
V128Shr64U
V128Eq8
V128Eq16
V128Eq32
V128Eq64
V128Ne8
V128Ne16
V128Ne32
V128Ne64
V128Lt8S
V128Lt8U
V128Lt16S
V128Lt16U
V128Lt32S
V128Lt32U
V128Lt64S
V128Gt8S
V128Gt8U
V128Gt16S
V128Gt16U
V128Gt32S
V128Gt32U
V128Gt64S
V128Le8S
V128Le8U
V128Le16S
V128Le16U
V128Le32S
V128Le32U
V128Le64S
V128Ge8S
V128Ge8U
V128Ge16S
V128Ge16U
V128Ge32S
V128Ge32U
V128Ge64S
V128Narrow16to8S
V128Narrow16to8U
V128Narrow32to16S
V128Narrow32to16U
V128ExtendLow8to16S
V128ExtendHigh8to16S
V128ExtendLow8to16U
V128ExtendHigh8to16U
V128ExtendLow16to32S
V128ExtendHigh16to32S
V128ExtendLow16to32U
V128ExtendHigh16to32U
V128ExtendLow32to64S
V128ExtendHigh32to64S
V128ExtendLow32to64U
V128ExtendHigh32to64U
V128ExtMulLow8to16S
V128ExtMulHigh8to16S
V128ExtMulLow8to16U
V128ExtMulHigh8to16U
V128ExtMulLow16to32S
V128ExtMulHigh16to32S
V128ExtMulLow16to32U
V128ExtMulHigh16to32U
V128ExtMulLow32to64S
V128ExtMulHigh32to64S
V128ExtMulLow32to64U
V128ExtMulHigh32to64U
V128ExtAddPairwise8to16S
V128ExtAddPairwise8to16U
V128ExtAddPairwise16to32S
V128ExtAddPairwise16to32U
V128Dot16to32S
V128Q15MulrSat16S
V128AddF32
V128AddF64
V128SubF32
V128SubF64
V128MulF32
V128MulF64
V128DivF32
V128DivF64
V128MinF32
V128MinF64
V128MaxF32
V128MaxF64
V128PMinF32
V128PMinF64
V128PMaxF32
V128PMaxF64
V128AbsF32
V128AbsF64
V128NegF32
V128NegF64
V128SqrtF32
V128SqrtF64
V128CeilF32
V128CeilF64
V128FloorF32
V128FloorF64
V128TruncF32
V128TruncF64
V128NearestF32
V128NearestF64
V128EqF32
V128EqF64
V128NeF32
V128NeF64
V128LtF32
V128LtF64
V128GtF32
V128GtF64
V128LeF32
V128LeF64
V128GeF32
V128GeF64
V128TruncSatF32toI32S
V128TruncSatF32toI32U
V128TruncSatF64toI32SZero
V128TruncSatF64toI32UZero
V128ConvertI32toF32S
V128ConvertI32toF32U
V128ConvertLowI32toF64S
V128ConvertLowI32toF64U
V128DemoteF64toF32Zero
V128PromoteLowF32toF64
V128Load8x8S(Int, Int, Int64)
V128Load8x8U(Int, Int, Int64)
V128Load16x4S(Int, Int, Int64)
V128Load16x4U(Int, Int, Int64)
V128Load32x2S(Int, Int, Int64)
V128Load32x2U(Int, Int, Int64)
V128Load8Splat(Int, Int, Int64)
V128Load16Splat(Int, Int, Int64)
V128Load32Splat(Int, Int, Int64)
V128Load64Splat(Int, Int, Int64)
V128Load32Zero(Int, Int, Int64)
V128Load64Zero(Int, Int, Int64)
V128Load8Lane(Int, Int, Int64, Int)
V128Load16Lane(Int, Int, Int64, Int)
V128Load32Lane(Int, Int, Int64, Int)
V128Load64Lane(Int, Int, Int64, Int)
V128Store8Lane(Int, Int, Int64, Int)
V128Store16Lane(Int, Int, Int64, Int)
V128Store32Lane(Int, Int, Int64, Int)
V128Store64Lane(Int, Int, Int64, Int)
V128RelaxedSwizzle
V128RelaxedTruncF32toI32S
V128RelaxedTruncF32toI32U
V128RelaxedTruncF64toI32SZero
V128RelaxedTruncF64toI32UZero
V128RelaxedMaddF32
V128RelaxedNmaddF32
V128RelaxedMaddF64
V128RelaxedNmaddF64
V128RelaxedLaneselect8
V128RelaxedLaneselect16
V128RelaxedLaneselect32
V128RelaxedLaneselect64
V128RelaxedMinF32
V128RelaxedMaxF32
V128RelaxedMinF64
V128RelaxedMaxF64
V128RelaxedQ15MulrS
V128RelaxedDot8to16S
V128RelaxedDot8to32AddS
} derive(Eq, Hash, Debug)pub(all) enum OptLevel {
O0
O1
O2
O3
}fn optimize_function_with_stats_with_limits(func : Function, limits : SaturationLimits) -> EGraphOptimizeStatsfn optimize_function_with_stats_with_limits_and_ruleset(func : Function, limits : SaturationLimits, ruleset : IndexedRuleSet) -> EGraphOptimizeStatsReusable Cranelift-like SSA intermediate representation