Skip to content
10 changes: 9 additions & 1 deletion EqSat/src/simple_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct AstIdx(pub u32);
pub struct Arena {
pub elements: Vec<(SimpleAst, AstData)>,
ast_to_idx: AHashMap<SimpleAst, AstIdx>,
isle_cache: AHashMap<AstIdx, AstIdx>,

// Map a name to it's corresponds symbol index.
symbol_ids: Vec<(String, AstIdx)>,
Expand All @@ -37,13 +38,15 @@ impl Arena {
pub fn new() -> Self {
let elements = Vec::with_capacity(65536);
let ast_to_idx = AHashMap::with_capacity(65536);
let isle_cache = AHashMap::with_capacity(65536);

let symbol_ids = Vec::with_capacity(255);
let name_to_symbol = AHashMap::with_capacity(255);

Arena {
elements: elements,
ast_to_idx: ast_to_idx,
isle_cache: isle_cache,

symbol_ids: symbol_ids,
name_to_symbol: name_to_symbol,
Expand Down Expand Up @@ -813,6 +816,9 @@ pub fn eval_ast(ctx: &Context, idx: AstIdx, value_mapping: &HashMap<AstIdx, u64>

// Recursively apply ISLE over an AST.
pub fn recursive_simplify(ctx: &mut Context, idx: AstIdx) -> AstIdx {
if ctx.arena.isle_cache.get(&idx).is_some() {
return *ctx.arena.isle_cache.get(&idx).unwrap();
}
let mut ast = ctx.arena.get_node(idx).clone();

match ast {
Expand Down Expand Up @@ -862,7 +868,9 @@ pub fn recursive_simplify(ctx: &mut Context, idx: AstIdx) -> AstIdx {
ast = result.unwrap();
}

return ctx.arena.ast_to_idx[&ast];
let result = ctx.arena.ast_to_idx[&ast];
ctx.arena.isle_cache.insert(idx, result);
result
}

// Evaluate the current AST for all possible combinations of zeroes and ones as inputs.
Expand Down
1 change: 1 addition & 0 deletions Mba.Simplifier/Bindings/AstCtx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public unsafe AstCtx()

// Constructors
public unsafe AstIdx Add(AstIdx a, AstIdx b) => Api.ContextAdd(this, a, b);
public unsafe AstIdx Sub(AstIdx a, AstIdx b) => Add(a, Mul(Constant(ulong.MaxValue, GetWidth(b)), b));
public unsafe AstIdx Mul(AstIdx a, AstIdx b) => Api.ContextMul(this, a, b);
public unsafe AstIdx Pow(AstIdx a, AstIdx b) => Api.ContextPow(this, a, b);
public unsafe AstIdx And(AstIdx a, AstIdx b) => Api.ContextAnd(this, a, b);
Expand Down
5 changes: 5 additions & 0 deletions Mba.Simplifier/Bindings/AstIdx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public override string ToString()
return ctx.GetAstString(Idx);
}

public override int GetHashCode()
{
return Idx.GetHashCode();
}

public unsafe static implicit operator uint(AstIdx reg) => reg.Idx;

public unsafe static implicit operator AstIdx(uint reg) => new AstIdx(reg);
Expand Down
21 changes: 16 additions & 5 deletions Mba.Simplifier/Jit/Amd64AssemblerDifferentialTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public unsafe Amd64AssemblerDifferentialTester(byte* buffer)
icedAssembler = new IcedAmd64Assembler(new Assembler(64));
}

public static void Test()
{
var buffer = new byte[64 * 4096];
fixed(byte* p = buffer)
{
new Amd64AssemblerDifferentialTester(p).Run();
}
}

public void Run()
{
for (int i = 0; i < registers.Length; i++)
Expand All @@ -56,13 +65,15 @@ private void DiffRegInsts(Register reg1)
for (int _ = 0; _ < 100; _++)
{
var c = (ulong)rand.NextInt64();
c |= rand.Next(0, 2) == 0 ? 0 : (1ul << 63);

Diff(nameof(IAmd64Assembler.MovabsRegImm64), reg1, c);
Diff(nameof(IAmd64Assembler.AddRegImm32), reg1, c);
Diff(nameof(IAmd64Assembler.SubRegImm32), reg1, c);
Diff(nameof(IAmd64Assembler.AndRegImm32), reg1, c);
Diff(nameof(IAmd64Assembler.ShrRegImm8), reg1, c);
Diff(nameof(IAmd64Assembler.AddRegImm32), reg1, (uint)c);
Diff(nameof(IAmd64Assembler.SubRegImm32), reg1, (uint)c);
Diff(nameof(IAmd64Assembler.AndRegImm32), reg1, (uint)c);
Diff(nameof(IAmd64Assembler.ShrRegImm8), reg1, (byte)c);
if (reg1 != rsp)
Diff(nameof(IAmd64Assembler.PushMem64), reg1, c);
Diff(nameof(IAmd64Assembler.PushMem64), reg1, (int)c);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Mba.Simplifier/Minimization/BooleanMinimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private static AstIdx MinimizeAnf(AstCtx ctx, IReadOnlyList<AstIdx> variables, T
}

var r = ctx.MinimizeAnf(TableDatabase.Instance.db, truthTable, tempVars, MultibitSiMBA.JitPage.Value);
var backSubst = GeneralSimplifier.ApplyBackSubstitution(ctx, r, invSubstMapping);
var backSubst = GeneralSimplifier.BackSubstitute(ctx, r, invSubstMapping);
return backSubst;
}
}
Expand Down
Loading