A possible JIT bug in a nested for loop

  • From: Grandma Caillou <potheadgrandma@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 16 Dec 2014 20:11:50 +0200

Today I encountered a problem which I was unable to solve by myself. I
created a fairly simple function to output n-grams generated from the given
input.

local function ngrams(arr, n)
    local result = {}
    local ia = 1

    for i = 1, #arr - n + 1 do
        local inner = {}
        local ib = 1

        -- print(i + n - 1)

        for j = i, i + n - 1 do
            inner[ib] = arr[j]
            ib = ib + 1
        end

        result[ia] = inner
        ia = ia + 1
    end

    return result
end

The function is used as:

local arr = {1,2,3,4,5,6,7...}
local a  = ngrams(array, 2) -- expected output: {{1,2}, {2,3}, {3,4}...}
local b  = ngrams(array, 3) -- expected output: {{1,2,3}, {2,3,4},
{3,4,5}...}

However, this function does not work as intended. Examples:

Assuming that arr is a table with more than 58 consecutive items in it.

1) ngrams(arr, 1) -- output: {...{57}, {58,59}, {59, 60} ... { 69, 70 }, {
70 }} | expected: {...{57}, {58}, {59}, ... {69}, {70}}
2) ngrams(arr, 2) -- output: {...{56,57}, {57,58}, {58}} | expected:
{...{56,57}, {57,58}, {58, 59}}

For some reason smaller tables work just fine.

The function produces the correct output when I uncomment this line: --
print(i + n - 1). Using io.write instead of print does not work. However
using a plain while loop does work. To me it looks like there is something
fishy going on with the inner for loop.

This bug seems to only affect jitted code. Running "luajit -joff test.lua"
works correctly.
I am running the latest version of LuaJIT 2.0.3 on OS X Yosemite 10.10
(iMac mid-2011).

Is this a known bug and/or is there something I am doing wrong?

Anna

Other related posts: