Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixing problem with ceq at end of block due to block split after inst…
…ruction--resulted in stack size problem. So, I removed all the optimization code.
  • Loading branch information
kaby76 committed Mar 27, 2018
1 parent e02fb13 commit 75b990d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 81 deletions.
61 changes: 33 additions & 28 deletions Campy.Compiler/inst.cs
Expand Up @@ -1761,40 +1761,45 @@ public override INST Convert(JITER converter, STATE state)
if (t1.isIntegerTy() && t2.isIntegerTy())
{
IntPredicate op;
if (IsSigned) op = _int_pred[(int)Predicate];
else op = _uint_pred[(int)Predicate];
if (IsSigned) op = _int_pred[(int) Predicate];
else op = _uint_pred[(int) Predicate];

cmp = LLVM.BuildICmp(Builder, op, v1.V, v2.V, "i" + instruction_id++);
if (Next == null) return null;
var t = Next.GetType();
if (t == typeof(i_brfalse))
{
// Push, Pop, branch -> combine
}
else if (t == typeof(i_brfalse_s))
{
// Push, Pop, branch -> combine
}
else if (t == typeof(i_brtrue))
if (Next != null)
{
// Push, Pop, branch -> combine
}
else if (t == typeof(i_brtrue_s))
{
// Push, Pop, branch -> combine
//var t = Next.GetType();
//if (t == typeof(i_brfalse))
//{
// // Push, Pop, branch -> combine
// return Next;
//}
//else if (t == typeof(i_brfalse_s))
//{
// // Push, Pop, branch -> combine
// return Next;
//}
//else if (t == typeof(i_brtrue))
//{
// // Push, Pop, branch -> combine
// return Next;
//}
//else if (t == typeof(i_brtrue_s))
//{
// // Push, Pop, branch -> combine
// return Next;
//}
}
else
{
// Set up for push of 0/1.
var return_type = new TYPE(typeof(bool));
var ret_llvm = LLVM.BuildZExt(Builder, cmp, return_type.IntermediateType, "");
var ret = new VALUE(ret_llvm, return_type);
if (Campy.Utils.Options.IsOn("jit_trace"))
System.Console.WriteLine(ret);

state._stack.Push(ret);
}
// Set up for push of 0/1.
var return_type = new TYPE(typeof(bool));
var ret_llvm = LLVM.BuildZExt(Builder, cmp, return_type.IntermediateType, "");
var ret = new VALUE(ret_llvm, return_type);
if (Campy.Utils.Options.IsOn("jit_trace"))
System.Console.WriteLine(ret);

state._stack.Push(ret);
}

return Next;
}
}
Expand Down
20 changes: 14 additions & 6 deletions Campy.Compiler/jiter.cs
Expand Up @@ -1478,8 +1478,8 @@ private List<CFG.Vertex> RemoveBasicBlocksAlreadyCompiled(List<CFG.Vertex> basic

if (Campy.Utils.Options.IsOn("state_computation_trace"))
{
System.Console.WriteLine("state in output");
state_in.OutputTrace();
System.Console.WriteLine("state in");
state_in.OutputTrace(new String(' ', 4));
}

bb.StateIn = state_in;
Expand All @@ -1488,7 +1488,7 @@ private List<CFG.Vertex> RemoveBasicBlocksAlreadyCompiled(List<CFG.Vertex> basic
if (Campy.Utils.Options.IsOn("state_computation_trace"))
{
bb.OutputEntireNode();
state_in.OutputTrace();
state_in.OutputTrace(new String(' ', 4));
}

INST last_inst = null;
Expand All @@ -1500,7 +1500,7 @@ private List<CFG.Vertex> RemoveBasicBlocksAlreadyCompiled(List<CFG.Vertex> basic
last_inst = inst;
inst = inst.Convert(this, bb.StateOut);
if (Campy.Utils.Options.IsOn("state_computation_trace"))
bb.StateOut.OutputTrace();
bb.StateOut.OutputTrace(new String(' ', 4));
}
if (last_inst != null
&& (
Expand All @@ -1519,20 +1519,28 @@ private List<CFG.Vertex> RemoveBasicBlocksAlreadyCompiled(List<CFG.Vertex> basic
// Finally, update phi functions with "incoming" information from predecessors.
foreach (var ob in order)
{
if (Campy.Utils.Options.IsOn("state_computation_trace"))
System.Console.WriteLine("Working on phis for node " + ob.Name);
CFG.Vertex node = ob;
CFG.Vertex llvm_node = node;
int size = llvm_node.StateIn._stack.Count;
for (int i = 0; i < size; ++i)
{
var count = llvm_node._graph.Predecessors(llvm_node).Count();
if (count < 2) continue;
if (Campy.Utils.Options.IsOn("state_computation_trace"))
System.Console.WriteLine("phi nodes need for "
+ ob.Name + " for stack depth " + i);
ValueRef res;
res = llvm_node.StateIn._stack[i].V;
if (!llvm_node.StateIn._phi.Contains(res)) continue;
ValueRef[] phi_vals = new ValueRef[count];
for (int c = 0; c < count; ++c)
{
var p = llvm_node._graph.PredecessorEdges(llvm_node).ToList()[c].From;
if (Campy.Utils.Options.IsOn("state_computation_trace"))
System.Console.WriteLine("Adding in phi for pred state "
+ p.Name);
var plm = p;
var vr = plm.StateOut._stack[i];
phi_vals[c] = vr.V;
Expand Down Expand Up @@ -1564,8 +1572,8 @@ private List<CFG.Vertex> RemoveBasicBlocksAlreadyCompiled(List<CFG.Vertex> basic
CFG.Vertex llvm_node = node;

node.OutputEntireNode();
llvm_node.StateIn.OutputTrace();
llvm_node.StateOut.OutputTrace();
llvm_node.StateIn.OutputTrace(new String(' ', 4));
llvm_node.StateOut.OutputTrace(new String(' ', 4));
}
}
}
Expand Down
50 changes: 20 additions & 30 deletions Campy.Compiler/state.cs
Expand Up @@ -277,55 +277,45 @@ public STATE(STATE other)
_locals = _stack.Section(other._locals.Base, other._locals.Len);
}

public void OutputTrace()
public void OutputTrace(string indent)
{
int args = _arguments.Len;
int locs = _locals.Len;
System.Console.WriteLine("This size = " + _this.Len);
System.Console.WriteLine("Args size = " + _arguments.Len);
System.Console.WriteLine("Locals size = " + _locals.Len);
System.Console.WriteLine("Stack size = " + _stack.Count);
System.Console.WriteLine(indent + "This size = " + _this.Len);
System.Console.WriteLine(indent + "Args size = " + _arguments.Len);
System.Console.WriteLine(indent + "Locals size = " + _locals.Len);
System.Console.WriteLine(indent + "Stack size = " + _stack.Count);
if (_this.Len > 0)
{
System.Console.WriteLine("[this (base " + _this.Base + ")");
System.Console.WriteLine(_this[0]);
System.Console.WriteLine();
System.Console.WriteLine("]");
System.Console.WriteLine();
System.Console.WriteLine(indent + "[this (base " + _this.Base + ")");
System.Console.WriteLine(indent + _this[0]);
System.Console.WriteLine(indent + "]");
}
System.Console.WriteLine("[args (base " + _arguments.Base + ")");
System.Console.WriteLine(indent + "[args (base " + _arguments.Base + ")");
for (int i = 0; i < args; ++i)
{
System.Console.WriteLine(" " + _arguments[i]);
System.Console.WriteLine();
System.Console.WriteLine(indent + _arguments[i]);
}
System.Console.WriteLine("]");
System.Console.WriteLine();
System.Console.WriteLine("[locs (base " + _locals.Base + ")");
System.Console.WriteLine(indent + "]");
System.Console.WriteLine(indent + "[locs (base " + _locals.Base + ")");
for (int i = 0; i < locs; ++i)
{
System.Console.WriteLine(" " + _locals[i]);
System.Console.WriteLine();
System.Console.WriteLine(indent + _locals[i]);
}
System.Console.WriteLine("]");
System.Console.WriteLine();
System.Console.WriteLine("[rest of stack (base " + (args + locs) + ")");
System.Console.WriteLine(indent + "]");
System.Console.WriteLine(indent + "[rest of stack (base " + (args + locs) + ")");
// NB. Args includes "this" pointer.
for (int i = args + locs; i < _stack.Size(); ++i)
{
System.Console.WriteLine(" " + _stack[i]);
System.Console.WriteLine();
System.Console.WriteLine(indent + _stack[i]);
}
System.Console.WriteLine("]");
System.Console.WriteLine();
System.Console.WriteLine("[complete stack (base " + 0 + ")");
System.Console.WriteLine(indent + "]");
System.Console.WriteLine(indent + "[complete stack (base " + 0 + ")");
for (int i = 0; i < _stack.Size(); ++i)
{
System.Console.WriteLine(" " + _stack[i]);
System.Console.WriteLine();
System.Console.WriteLine(indent + _stack[i]);
}
System.Console.WriteLine("]");
System.Console.WriteLine();
System.Console.WriteLine(indent + "]");
}
}
}
72 changes: 55 additions & 17 deletions ConsoleApp4/Program.cs
Expand Up @@ -9,6 +9,54 @@

namespace ConsoleApp4
{
// https://stackoverflow.com/questions/463105/in-place-radix-sort
// https://arxiv.org/abs/0706.4107
// https://dl.acm.org/citation.cfm?id=1778601
// https://www.geeksforgeeks.org/radix-sort/
// http://www.geekviewpoint.com/java/sorting/radixsort
// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.404.8825&rep=rep1&type=pdf
// https://www.cs.princeton.edu/~rs/AlgsDS07/18RadixSort.pdf
// https://rosettacode.org/wiki/Sorting_algorithms/Radix_sort#C.23

class RadixSort
{
public static void Sort(int[] e)
{
int i, j;
int[] tmp = new int[e.Length];
for (int shift = 31; shift > -1; --shift)
{
j = 0;
for (i = 0; i < e.Length; ++i)
{
bool move = (e[i] << shift) >= 0;
if (shift == 0 ? !move : move) // shift the 0's to old's head
e[i - j] = e[i];
else // move the 1's to tmp
tmp[j++] = e[i];
}
Array.Copy(tmp, 0, e, e.Length - j, j);
}
}

public static void SortP(int[] e)
{
int[] tmp = new int[e.Length];
for (int shift = 31; shift > -1; --shift)
{
int j = 0;
Campy.Parallel.For(e.Length, i =>
{
bool move = (e[i] << shift) >= 0;
if (shift == 0 ? !move : move) // shift the 0's to old's head
e[i - j] = e[i];
else // move the 1's to tmp
tmp[j++] = e[i]; // THIS IS UNSAFE CODE DUE TO J BEING INCREMENTED UNCONTROLLED AMONG VARIOUS THREADS.
});
Array.Copy(tmp, 0, e, e.Length - j, j);
}
}
}

class Program
{
Expand All @@ -28,27 +76,17 @@ static void StartDebugging()
Campy.Utils.Options.Set("runtime_trace");
}

class A
{
public int X { get; set; }

public int Score(A b)
{
return X + b.X;
}
}

static void Main(string[] args)
{
StartDebugging();

A[] array = new A[10];
for (int i = 0; i < 10; ++i) array[i] = new A();

Campy.Parallel.For(10, i =>
{
array[i].X = i;
});
Random rnd = new Random();
int N = 8;
int[] a = Enumerable.Range(0, N).ToArray().OrderBy(x => rnd.Next()).ToArray();
RadixSort.SortP(a);
for (int i = 0; i < N; ++i)
if (a[i] != i)
throw new Exception();
}
}
}

0 comments on commit 75b990d

Please sign in to comment.