递归程序执行期间松露语言运行时错误

时间:2018-07-25 09:31:19

标签: java jvm dsl truffle graalvm

我目前正在编写自己的简单语言(https://github.com/graalvm/simplelanguage/)实现,以学习如何使用松露。

我写了一些程序来测试我的实现,例如以下斐波那契算法的实现。

function fib(num) { 
  if (num <= 1) {return 1;}
  else {return fib(num-1) + fib(num-2); }
}

function fib(num) {
  if (num < 1) {return 0;}
  n1 = 0;
  n2 = 1;
  i = 1;
  while (i < num) {
    next = n2 + n1;
    n1 = n2;
    n2 = next;
    i = i + 1;
  }
  return n2;
}

虽然可以使用任何大参数值(例如10000)执行非递归版本,但递归版本会迅速引发运行时错误。

例如,我开发了一个简单的测试,该测试以增量值执行递归fib函数,并获得以下结果:

fib(1) = 1(61)
fib(2) = 2(14)
fib(3) = 3(1)
fib(4) = 5(2)
fib(5) = 8(5)
fib(6) = 13(13)
fib(7) = 21(28)
fib(8) = 34(24)
fib(9) = 55(40)
fib(10) = 89(30)
fib(11) = 144(25)
fib(12) = 233(93)
fib(13) = 377(127)
fib(14) = 610(86)
fib(15) = 987(118)
fib(16) = 1597(248)
fib(17) = 2584(314)
fib(18) = 4181(402)
fib(19) = 6765(524)
fib(20) = 10946(372)
fib(21) = 17711(646)
[truffle] opt fail         Root@62804e23                                               |Reason org.graalvm.compiler.code.SourceStackTraceBailoutException$1: Object of type Lorg/graalvm/compiler/truffle/runtime/FrameWithoutBoxing; should not be materialized (must not let virtual object escape at node 10349|EndNode): 
org.graalvm.compiler.code.SourceStackTraceBailoutException$1: Object of type Lorg/graalvm/compiler/truffle/runtime/FrameWithoutBoxing; should not be materialized (must not let virtual object escape at node 10349|EndNode):
    at fr.mleduc.simplelanguage.revisitor.revisitors.ExecSLRevisitor$6.eval(ExecSLRevisitor.java:527)
    at fr.mleduc.simplelanguage.revisitor.model.objects.Root.execute(Root.java:34)
    at org.graalvm.compiler.truffle.runtime.OptimizedCallTarget.callProxy(OptimizedCallTarget.java:262)
    at org.graalvm.compiler.truffle.runtime.OptimizedCallTarget.callRoot(OptimizedCallTarget.java:251)
Caused by: org.graalvm.compiler.graph.VerificationError: Object of type Lorg/graalvm/compiler/truffle/runtime/FrameWithoutBoxing; should not be materialized (must not let virtual object escape at node 10349|EndNode):
    at org.graalvm.compiler.nodes.virtual.EnsureVirtualizedNode.ensureVirtualFailure(EnsureVirtualizedNode.java:98)
    at org.graalvm.compiler.nodes.virtual.CommitAllocationNode.lower(CommitAllocationNode.java:117)
    at org.graalvm.compiler.phases.common.LoweringPhase$Round.process(LoweringPhase.java:464)
    at org.graalvm.compiler.phases.common.LoweringPhase$Round.access$200(LoweringPhase.java:340)
    at org.graalvm.compiler.phases.common.LoweringPhase$Round$ProcessFrame.preprocess(LoweringPhase.java:404)
    at org.graalvm.compiler.phases.common.LoweringPhase.processBlock(LoweringPhase.java:563)
    at org.graalvm.compiler.phases.common.LoweringPhase$Round.run(LoweringPhase.java:389)
    at org.graalvm.compiler.phases.Phase.run(Phase.java:49)
    at org.graalvm.compiler.phases.BasePhase.apply(BasePhase.java:197)
    at org.graalvm.compiler.phases.BasePhase.apply(BasePhase.java:139)
    at org.graalvm.compiler.phases.PhaseSuite.run(PhaseSuite.java:212)
    at org.graalvm.compiler.phases.common.IncrementalCanonicalizerPhase.run(IncrementalCanonicalizerPhase.java:56)
    at org.graalvm.compiler.phases.common.IncrementalCanonicalizerPhase.run(IncrementalCanonicalizerPhase.java:38)
    at org.graalvm.compiler.phases.BasePhase.apply(BasePhase.java:197)
    at org.graalvm.compiler.phases.BasePhase.apply(BasePhase.java:139)
    at org.graalvm.compiler.phases.common.LoweringPhase.lower(LoweringPhase.java:260)
    at org.graalvm.compiler.phases.common.LoweringPhase.run(LoweringPhase.java:253)
    at org.graalvm.compiler.phases.common.LoweringPhase.run(LoweringPhase.java:92)
    at org.graalvm.compiler.phases.BasePhase.apply(BasePhase.java:197)
    at org.graalvm.compiler.phases.BasePhase.apply(BasePhase.java:139)
    at org.graalvm.compiler.phases.PhaseSuite.run(PhaseSuite.java:212)
    at org.graalvm.compiler.phases.BasePhase.apply(BasePhase.java:197)
    at org.graalvm.compiler.phases.BasePhase.apply(BasePhase.java:139)
    at org.graalvm.compiler.core.GraalCompiler.emitFrontEnd(GraalCompiler.java:256)
    at org.graalvm.compiler.core.GraalCompiler.compile(GraalCompiler.java:180)
    at org.graalvm.compiler.core.GraalCompiler.compileGraph(GraalCompiler.java:165)
    at org.graalvm.compiler.truffle.compiler.TruffleCompilerImpl.compilePEGraph(TruffleCompilerImpl.java:447)
    at org.graalvm.compiler.truffle.compiler.TruffleCompilerImpl.compileAST(TruffleCompilerImpl.java:393)
    at org.graalvm.compiler.truffle.compiler.TruffleCompilerImpl$TruffleCompilationWrapper.performCompilation(TruffleCompilerImpl.java:546)
    at org.graalvm.compiler.truffle.compiler.TruffleCompilerImpl$TruffleCompilationWrapper.performCompilation(TruffleCompilerImpl.java:495)
    at org.graalvm.compiler.core.CompilationWrapper.run(CompilationWrapper.java:169)
    at org.graalvm.compiler.truffle.compiler.TruffleCompilerImpl.doCompile(TruffleCompilerImpl.java:224)
    at org.graalvm.compiler.truffle.runtime.GraalTruffleRuntime.doCompile(GraalTruffleRuntime.java:710)
    at org.graalvm.compiler.truffle.runtime.GraalTruffleRuntime$1.run(GraalTruffleRuntime.java:776)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
    at org.graalvm.compiler.core.CompilerThread.run(CompilerThread.java:42)

fib(22) = 28657(570)
fib(23) = 46368(735)
fib(24) = 75025(1235)

我试图将自己的实现与官方的简单语言实现进行比较,而没有找到确定问题根源的方法。

所以我的问题是,如何找到问题的根本原因?

PS:到目前为止,我没有足够的信息来确定要包含在此问题中的相关代码段,但是如果需要,可以随时询问更多详细信息。谢谢:)

1 个答案:

答案 0 :(得分:1)

这里的错误是,您让VirtualFrame对象脱离了部分评估的范围。实际上,堆栈跟踪的第一部分是您的解释器在部分评估下的堆栈跟踪。特别是:

at fr.mleduc.simplelanguage.revisitor.revisitors.ExecSLRevisitor$6.eval(ExecSLRevisitor.java:527)
at fr.mleduc.simplelanguage.revisitor.model.objects.Root.execute(Root.java:34)

ExecSLRevisitor.java:527周围的东西让VirtualFrame逃脱了,但是如果不看代码就很难分辨。

有2种解决方案:

  • 别让框架逃脱
  • 此时将框架材料化(请参见VirtualFrame的javadoc)