Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions src/Apps/Kernel/RexlKernel/Builtins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@

using Microsoft.Rexl.Bind;
using Microsoft.Rexl.Code;
using Microsoft.Rexl.Onnx;
using Microsoft.Rexl.Private;
using Microsoft.Rexl.Solve;

namespace Microsoft.Rexl.Kernel;

internal sealed class KernelBuiltins : OperationRegistry
{
public KernelBuiltins(Func<bool, bool> setShowIL = null)
: base(BuiltinFunctions.Instance, BuiltinProcedures.Instance, FlowProcs.Instance)
: base(BuiltinFunctions.Instance, BuiltinProcedures.Instance, FlowProcs.Instance,
SolverFunctions.Instance, ModelFunctions.Instance)
{
#if WITH_SOLVE
AddParent(Solve.SolverFunctions.Instance);
#endif
#if WITH_ONNX
AddParent(Onnx.ModelFunctions.Instance);
#endif
if (setShowIL != null)
AddOne(new ShowILFunc(setShowIL));
}
Expand All @@ -29,14 +26,8 @@ public KernelBuiltins(Func<bool, bool> setShowIL = null)
internal sealed class KernelGenerators : GeneratorRegistry
{
public KernelGenerators()
: base(BuiltinGenerators.Instance)
: base(BuiltinGenerators.Instance, SolverGenerators.Instance, ModelFuncGenerators.Instance)
{
#if WITH_SOLVE
AddParent(Solve.SolverGenerators.Instance);
#endif
#if WITH_ONNX
AddParent(Onnx.ModelFuncGenerators.Instance);
#endif
Add(ShowILFunc.MakeGen());
}
}
Expand Down
12 changes: 0 additions & 12 deletions src/Apps/Kernel/RexlKernel/Exec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,6 @@ public async Task ExecAsync(ExecuteMessage msg, int min, int lim)
}
}

#if WITH_SOLVE
protected override bool TryOptimizeMip(bool isMax, RuntimeModule modSrc, int imsr, DName solver,
out double score, out List<(DName name, object value)> symValues)
{
Validation.AssertValue(modSrc);
Validation.AssertIndex(imsr, modSrc.Bnd.Symbols.Length);
Validation.Assert(modSrc.Bnd.Symbols[imsr].IsMeasureSym);

return Solve.MipSolver.TryOptimize(Sink, _codeGen, isMax, modSrc, imsr, solver, out score, out symValues);
}
#endif

private void PublishText(ExecuteMessage msg, string text)
{
Validation.AssertValue(msg);
Expand Down
1 change: 0 additions & 1 deletion src/Apps/Kernel/RexlKernel/RexlKernel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>RexlKernel</AssemblyName>
<DefineConstants>$(DefineConstants);WITH_ONNX;WITH_SOLVE</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
19 changes: 5 additions & 14 deletions src/Apps/RexlBench/Exec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
using Microsoft.Rexl;
using Microsoft.Rexl.Bind;
using Microsoft.Rexl.Code;
using Microsoft.Rexl.Onnx;
using Microsoft.Rexl.Parse;
using Microsoft.Rexl.Private;
using Microsoft.Rexl.Sink;
using Microsoft.Rexl.Solve;

namespace Microsoft.Rexl.RexlBench;

Expand Down Expand Up @@ -65,29 +67,18 @@ protected override void WriteValueCore(DType type, object? value, int max)
sealed class Operations : OperationRegistry
{
public Operations()
: base(BuiltinFunctions.Instance, BuiltinProcedures.Instance, FlowProcs.Instance)
: base(BuiltinFunctions.Instance, BuiltinProcedures.Instance, FlowProcs.Instance,
SolverFunctions.Instance, ModelFunctions.Instance)
{
#if WITH_SOLVE
AddParent(Solve.SolverFunctions.Instance);
#endif
#if WITH_ONNX
AddParent(Onnx.ModelFunctions.Instance);
#endif
AddOne(WrapFunc.Instance);
}
}

sealed class Generators : GeneratorRegistry
{
public Generators()
: base(BuiltinGenerators.Instance)
: base(BuiltinGenerators.Instance, SolverGenerators.Instance, ModelFuncGenerators.Instance)
{
#if WITH_SOLVE
AddParent(Solve.SolverGenerators.Instance);
#endif
#if WITH_ONNX
AddParent(Onnx.ModelFuncGenerators.Instance);
#endif
Add(WrapFunc.MakeGenerator());
}
}
Expand Down
12 changes: 0 additions & 12 deletions src/Apps/RexlBench/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -782,16 +782,4 @@ public SimpleHarness(IHarnessConfig config, OperationRegistry opers, CodeGenerat
: base(config, opers, codeGen, storage)
{
}

#if WITH_SOLVE
protected override bool TryOptimizeMip(bool isMax, RuntimeModule modSrc, int imsr, DName solver,
out double score, out List<(DName name, object value)> symValues)
{
Validation.AssertValue(modSrc);
Validation.AssertIndex(imsr, modSrc.Bnd.Symbols.Length);
Validation.Assert(modSrc.Bnd.Symbols[imsr].IsMeasureSym);

return Solve.MipSolver.TryOptimize(Sink, _codeGen, isMax, modSrc, imsr, solver, out score, out symValues);
}
#endif
}
1 change: 0 additions & 1 deletion src/Apps/RexlBench/RexlBench.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<OutputType>WinExe</OutputType>
<UseWindowsForms>true</UseWindowsForms>
<RootNamespace>Microsoft.Rexl.RexlBench</RootNamespace>
<DefineConstants>$(DefineConstants);WITH_ONNX;WITH_SOLVE</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
19 changes: 5 additions & 14 deletions src/Apps/RexlRun/Builtins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License.

using Microsoft.Rexl.Code;
using Microsoft.Rexl.Onnx;
using Microsoft.Rexl.Solve;

namespace Microsoft.Rexl.RexlRun;

Expand All @@ -11,27 +13,16 @@ namespace Microsoft.Rexl.RexlRun;
internal sealed class RexlRunOperations : OperationRegistry
{
public RexlRunOperations()
: base(BuiltinFunctions.Instance, BuiltinProcedures.Instance)
: base(BuiltinFunctions.Instance, BuiltinProcedures.Instance,
SolverFunctions.Instance, ModelFunctions.Instance)
{
#if WITH_SOLVE
AddParent(Solve.SolverFunctions.Instance);
#endif
#if WITH_ONNX
AddParent(Onnx.ModelFunctions.Instance);
#endif
}
}

internal sealed class RexlRunGenerators : GeneratorRegistry
{
public RexlRunGenerators()
: base(BuiltinGenerators.Instance)
: base(BuiltinGenerators.Instance, SolverGenerators.Instance, ModelFuncGenerators.Instance)
{
#if WITH_SOLVE
AddParent(Solve.SolverGenerators.Instance);
#endif
#if WITH_ONNX
AddParent(Onnx.ModelFuncGenerators.Instance);
#endif
}
}
12 changes: 0 additions & 12 deletions src/Apps/RexlRun/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,4 @@ public SimpleHarness(IHarnessConfig config, OperationRegistry opers, CodeGenerat
: base(config, opers, codeGen, storage)
{
}

#if WITH_SOLVE
protected override bool TryOptimizeMip(bool isMax, RuntimeModule modSrc, int imsr, DName solver,
out double score, out List<(DName name, object value)> symValues)
{
Validation.AssertValue(modSrc);
Validation.AssertIndex(imsr, modSrc.Bnd.Symbols.Length);
Validation.Assert(modSrc.Bnd.Symbols[imsr].IsMeasureSym);

return Solve.MipSolver.TryOptimize(Sink, _codeGen, isMax, modSrc, imsr, solver, out score, out symValues);
}
#endif
}
1 change: 0 additions & 1 deletion src/Apps/RexlRun/RexlRun.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<DefineConstants>$(DefineConstants);WITH_ONNX;WITH_SOLVE</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
79 changes: 76 additions & 3 deletions src/Core/Rexl.Code/Code/CodeGenerator.Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected override bool PreVisitModule(BndModuleNode bmod, int idx)
// For item ifma:
// * If ifma is the value for symbol isym and the symbol is "settable" (parameter or free variable)
// and flags[citem + isym] is true, then the value comes from rec.
// * Otherwise, if flags[csym + ifma] is true, then the item slot is not set (assumed to be valid).
// * Otherwise, if flags[ifma] is true, then the item slot is not set (assumed to be valid).
// * Otherwise, the value is computed from its formula.
int isym = 0;
for (int ifma = 0; ifma < citem; ifma++)
Expand Down Expand Up @@ -291,6 +291,77 @@ protected override bool PreVisitModule(BndModuleNode bmod, int idx)
}
PopType(stMakeRec);

// Get the set_module_value_item function. The function takes an items tuple (being built),
// item index (for the value item of a parameter or free variable), and the value to be
// set (as an object).
frame = StartFunctionDisjoint("set_module_value_item", stItems, stItems, typeof(int), typeof(object));
Type stSetValueItem = frame.DelegateType;

{
var laiItems = LocArgInfo.FromArg(1, stItems);

// Do this first so the default label has the smallest index in IL dumps.
var labDef = IL.DefineLabel();

var labels = new Label[bmod.Items.Length];
for (int isym = 0; isym < bmod.Symbols.Length; isym++)
{
var sym = bmod.Symbols[isym];
switch (sym.SymKind)
{
case ModSymKind.Parameter:
case ModSymKind.FreeVariable:
labels[sym.IfmaValue] = IL.DefineLabel();
break;
}
}

for (int ifma = 0; ifma < labels.Length; ifma++)
{
if (labels[ifma] == default)
labels[ifma] = labDef;
}

IL
.Ldarg(2)
.Switch(labels);

for (int isym = 0; isym < bmod.Symbols.Length; isym++)
{
var sym = bmod.Symbols[isym];
switch (sym.SymKind)
{
default:
continue;
case ModSymKind.Parameter:
case ModSymKind.FreeVariable:
break;
}

// REVIEW: TypeManager should support emitting the proper instructions
// to set the field. This code shouldn't know about the field layout of
// the tuple system type.
int ifma = sym.IfmaValue;
Validation.Assert(labels[ifma] != labDef);
var fld = stItems.GetField("_F" + ifma).VerifyValue();

IL
.Br_Non(labDef)
.MarkLabel(labels[ifma])
.Ldarg(1)
.Ldarg(3)
.Unbox_Any(fld.FieldType)
.Stfld(fld);
}

IL
.MarkLabel(labDef)
.Ldarg(1);
PushType(stItems);
EndFunctionCore();
}
PopType(stSetValueItem);

GenLoadConst(bmod);

// Instantiate the runtime module.
Expand All @@ -299,15 +370,17 @@ protected override bool PreVisitModule(BndModuleNode bmod, int idx)
{
IL.Ldloc(locExt);
Type stModImpl = typeof(RuntimeModule<,,>).MakeGenericType(stRec, stItems, stExt);
ctor = stModImpl.GetConstructor(new Type[] { stSetItems, stItems, stMakeRec, typeof(BndModuleNode), stExt });
ctor = stModImpl.GetConstructor(
new Type[] { stSetItems, stItems, stMakeRec, stSetValueItem, typeof(BndModuleNode), stExt });
if (ctor is null)
throw Unexpected("Ctor for runtime module with externals");
}
else
{
// Construct the module instance.
Type stModImpl = typeof(RuntimeModule<,>).MakeGenericType(stRec, stItems);
ctor = stModImpl.GetConstructor(new Type[] { stSetItems, stItems, stMakeRec, typeof(BndModuleNode) });
ctor = stModImpl.GetConstructor(
new Type[] { stSetItems, stItems, stMakeRec, stSetValueItem, typeof(BndModuleNode) });
if (ctor is null)
throw Unexpected("Ctor for runtime module");
}
Expand Down
16 changes: 14 additions & 2 deletions src/Core/Rexl.Code/Code/ILWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,20 @@ private void Log(OpCode op, Label arg)
[Conditional("LOG_IL")]
private void Log(OpCode op, Label[] arg)
{
// REVIEW: Should do something better, if we ever use switch.
Log(op, (object)arg);
#if LOG_IL
if (_lines != null)
{
var sink = StartLine().TWrite("{0,5}) {1} [", LogPrefix(), op);
var pre = "";
foreach (var lab in arg)
{
sink.TWrite(pre).TWrite(lab.GetHashCode() - 1);
pre = ",";
}
sink.Write("]");
AddLine(sink);
}
#endif
}

private void Emit(OpCode op, Type arg)
Expand Down
Loading