reactorlabs / rir

GNU General Public License v2.0
62 stars 18 forks source link

Wrong back-edge OSR heuristics #1251

Open fikovnik opened 12 months ago

fikovnik commented 12 months ago

The loop OSRing is implemented in interp.cpp using

static size_t loopCounter = 0;

as a consequence, it may OSR the wrong function:

g <- function() {
  x <- 0
  for (i in 1:5000) {
    x <- x + i
  }
  print("G done 1")
}

f <- function() {
  g()
  x <- 0
  for (i in 1:4) {
    x <- x + i
  }
  print("F Done 1")

}

rir.compile(f)
rir.compile(g)
f()
$ PIR_DEBUG=PrintPirAfterOpt,PrintToStdout ./bin/R -q -f ../../../rir/build/debug/a.r
...
> f()
[1] "G done 1"

╞═════════════════════════╡  Compiling continuation  ╞═════════════════════════╡

┌──────────────────────────────────────────────────────────────────────────────┐
│ osr[0x562ebeaca600]                                                          │
│ Context: CorrOrd,!TMany                                                      │
│ Properties:  Eager                                                           │
├────── PIR Version After Optimizations
osr[0x562ebeaca600]
BB0
  goto BB1
BB1   <- [0]
  env             e1.0  = LdFunctionEnv
  int$#-          %1.1  = LdArg                    0
  int$#-          %1.2  = LdArg                    1
  int$#-          %1.3  = LdArg                    2
  goto BB2
BB2   <- [1, 5]
  int$-           %2.0  = Phi                      %1.3:BB1, %5.0:BB5
  lgl$-           %2.1  = Neq                vd    %1.2, %2.0, elided
  lgl$#-          %2.2  = Identical                %2.1, false
  void                    Branch                   %2.2 -> BB3 (if true) | BB4 (if false)
BB3   <- [2]
  (cls|spec|blt)  %3.0  = LdFun              !     print, e1.0
  val?            %3.1  = Call               !     %3.0("F Done 1") e1.0   <val?_>
  void                    Return             l     %3.1
BB4   <- [2]
  void                    StVar              lW    i, %2.0, e1.0
  cp              %4.1  = Checkpoint                -> BB5 (default) | BB6 (if assume failed)
BB5   <- [4]
  int$-           %5.0  = Add                d     %2.0, %1.1, elided
  val?^ | miss    %5.1  = LdVar              eR    x, e1.0
  lgl$#-          %5.2  = IsType                   %5.1 isA real$-
  void                    Assume             D     %5.2, %4.1 (Typecheck@0x562ebda92315)
  real$-          %5.4  = CastType           d     dn %5.1
  void                    Visible            v
  real$-          %5.6  = Add                vd    %5.4, %2.0, elided   <real$->
  void                    StVar              lW    x, %5.6, e1.0
  goto BB2
BB6   <- [4]
  fs              %6.0  = FrameState         R     0x562ebda92190+209: [%1.1, %1.2, %2.0], env=e1.0
  void                    Deopt              !v    %6.0   !

│ osr[0x562ebeaca600]                                                          │
└──────────────────────────────────────────────────────────────────────────────┘
[1] "F Done 1"
>