TuringLang / IRTracker.jl

Dynamically track IR as a graph, using source transformations
31 stars 5 forks source link

Fix recording "too many" returns #10

Closed phipsgabler closed 5 years ago

phipsgabler commented 5 years ago

Currently, every return statement is always recorded, even if there actually happend a jump before it; see both @7s here:

@1: [Argument §1:%1] = Main.DynamicComputationGraphs.weird
@2: [Argument §1:%2] = 3
@3: [§1:%3] Main.DynamicComputationGraphs.rand() = 0.666241398790441
  @1: [Argument §1:%1] = rand
  @2: [§1:%2] (@1)(Random.GLOBAL_RNG, Random.Float64) = 0.666241398790441
  @3: [§1:1] return @2 = 0.666241398790441
@4: [§1:%4] @2 + 1 = 4
  @1: [Argument §1:%1] = +
  @2: [Argument §1:%2] = 3
  @3: [Argument §1:%3] = 1
  @4: [§1:%4] Base.add_int(@2, @3) = 4
  @5: [§1:1] return @4 = 4
@5: [§1:%5] 1 / @4 = 0.25
  @1: [Argument §1:%1] = /
  @2: [Argument §1:%2] = 1
  @3: [Argument §1:%3] = 4
  @4: [§1:%4] Base.float(@2) = 1.0
  @5: [§1:%5] Base.float(@3) = 4.0
  @6: [§1:%6] @4 / @5 = 0.25
  @7: [§1:1] return @6 = 0.25
@6: [§1:%6] @3 < @5 = false
  @1: [Argument §1:%1] = <
  @2: [Argument §1:%2] = 0.666241398790441
  @3: [Argument §1:%3] = 0.25
  @4: [§1:%4] Base.lt_float(@2, @3) = false
  @5: [§1:1] return @4 = false
@7: [§1:2] return @2 = 3
@8: [§1:1] goto §2 since @6
@9: [§2:%7] @2 + 1 = 4
  @1: [Argument §1:%1] = +
  @2: [Argument §1:%2] = 3
  @3: [Argument §1:%3] = 1
  @4: [§1:%4] Base.add_int(@2, @3) = 4
  @5: [§1:1] return @4 = 4
@10: [§2:%8] Main.DynamicComputationGraphs.weird(@9) = 12
  @1: [Argument §1:%1] = Main.DynamicComputationGraphs.weird
  @2: [Argument §1:%2] = 4
  @3: [§1:%3] Main.DynamicComputationGraphs.rand() = 0.2498905338845061
  @4: [§1:%4] @2 + 1 = 5
  @5: [§1:%5] 1 / @4 = 0.2
  @6: [§1:%6] @3 < @5 = false
  @7: [§1:2] return @2 = 4
  @8: [§1:1] goto §2 since @6
  @9: [§2:%7] @2 + 1 = 5
  @10: [§2:%8] Main.DynamicComputationGraphs.weird(@9) = 12
  @11: [§2:1] return @10 = 12
@11: [§2:1] return @10 = 12% 

This was something I didn't think of before. Solution: replace the return-recording by a branch to a new, last block, which does the recording and takes the Return as an argument, just as with Branches.

(This is not an "error", it just does unnecessary things and shifts the tapes indices).

phipsgabler commented 5 years ago

Fixed by 5152878.