StanfordLegion / legion

The Legion Parallel Programming System
https://legion.stanford.edu
Apache License 2.0
675 stars 146 forks source link

Regent: potential bug in inliner #1189

Open mariodirenzo opened 2 years ago

mariodirenzo commented 2 years ago

The following script

import 'regent'

local C = regentlib.c
local HDF5 = terralib.includec('hdf5.h')

HDF5.H5F_ACC_RDONLY = 0
HDF5.H5P_DEFAULT = 0

-- The script compiles if this task is not inlined or if the call to
-- H5Aread is commented out
__demand(__inline)
task readIspaceFromHDF5(dirname : regentlib.string)
   -- Open file
   var filename = [&int8](C.malloc(256))
   C.snprintf(filename, 256, '%s/master.hdf', [&int8](dirname))
   var fid = HDF5.H5Fopen(filename, HDF5.H5F_ACC_RDONLY,
                          HDF5.H5P_DEFAULT)
   -- Register data types to describe index spaces
   var colorType = HDF5.H5Tcreate(HDF5.H5T_COMPOUND, 24)
   var x = HDF5.H5Tinsert(colorType, "x",  0, HDF5.H5T_STD_I64LE_g)
   var y = HDF5.H5Tinsert(colorType, "y",  8, HDF5.H5T_STD_I64LE_g)
   var z = HDF5.H5Tinsert(colorType, "z", 16, HDF5.H5T_STD_I64LE_g)
   -- Check number of tiles
   var res = HDF5.H5Aexists_by_name(fid, ".", "tilesNumber",
                                    HDF5.H5P_DEFAULT)
   var aid = HDF5.H5Aopen(fid, "tilesNumber", HDF5.H5P_DEFAULT)
   var out : int64[3]
   res = HDF5.H5Aread(aid, colorType, &out)
   -- Clean up
   HDF5.H5Aclose(aid)
   HDF5.H5Tclose(colorType)
   HDF5.H5Fclose(fid)
   C.free(filename)
   return int3d{out[0], out[1], out[2]}
end

task main()
   var dirname = "."
   var dim3 = readIspaceFromHDF5(dirname)
end

regentlib.saveobj(main, "test.o")

fails with the error

/Users/direnzo/Codes/legion/language/src/common/ast.t:520: wrong number of arguments to 'insert'
stack traceback:
    [C]: in function 'insert'
    /Users/direnzo/Codes/legion/language/src/common/ast.t:520: in function 'map_node_postorder'
    /Users/direnzo/Codes/legion/language/src/common/ast.t:511: in function 'expr'
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:348: in function <.../direnzo/Codes/legion/language/src/regent/inline_tasks.t:346>
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:371: in function 'stat'
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:375: in function 'fn'
    ...rs/direnzo/Codes/legion/language/terra/src/terralist.lua:150: in function 'map'
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:375: in function 'block'
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:441: in function 'expr_call'
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:518: in function 'expr'
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:538: in function <.../direnzo/Codes/legion/language/src/regent/inline_tasks.t:534>
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:588: in function 'stat'
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:593: in function 'fn'
    ...rs/direnzo/Codes/legion/language/terra/src/terralist.lua:150: in function 'map'
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:593: in function 'block'
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:598: in function 'top_task'
    .../direnzo/Codes/legion/language/src/regent/inline_tasks.t:608: in function <.../direnzo/Codes/legion/language/src/regent/inline_tasks.t:602>
    /Users/direnzo/Codes/legion/language/src/regent/passes.t:64: in function </Users/direnzo/Codes/legion/language/src/regent/passes.t:57>
    RegentInlinerBug.rg:39: in main chunk

if executed with the command INCLUDE_PATH=$HDF_ROOT/include $LEGION_DIR/language/regent.py RegentInlinerBug.rg. If the task readIspaceFromHDF5 is not inlined or if the call to H5Aread is commented out, the script is executed correctly.

elliottslaughter commented 2 years ago

@mariodirenzo What machine are you hitting this on?

@magnatelee has a workaround, but it seems to be a LuaJIT bug.

magnatelee commented 2 years ago

or a Terra bug, if Terra was messing with tokens from the lexer.

I pushed this workaround to master and control replication: https://gitlab.com/StanfordLegion/legion/-/commit/07a5d195e6d644f772fba6da4e443969d9479107. Let me know if this still doesn't fix your issue.

elliottslaughter commented 2 years ago

Here's a new reproducer. This is the shortest one I've been able to make, and (fortunately) does not require HDF5.

import 'regent'

terra f(x : &int64)
  return 123
end

__demand(__inline)
task g()
   var out : int64
   var res = f(&out)
end

task main()
   g()
end
main:compile()
elliottslaughter commented 2 years ago

I'm suspecting Wonchan may be right about the Terra parser, but just for posterity: I reproduced on Moonjit and two versions of LuaJIT (most recent v2.1, and a version about 2 years old that's currently checked in to Terra).

mariodirenzo commented 2 years ago

What machine are you hitting this on?

I discovered it on my mac with Moonjit.

Let me know if this still doesn't fix your issue.

The problem seems solved. Thanks!

Should we leave this issue open until you find out if this is a LuaJIT or Terra bug?

elliottslaughter commented 2 years ago

It's looking like this is a Terra issue, but I'm not sure how to move forward with debugging because—even with the minimal reproducer program—we're effectively debugging the Terra parser against the entire Regent compiler code base. Even if I add diagnostics to Terra to show the parser state, there will be far too much to look through if I run it past the entire compiler.

Anyway, let's leave this open for now. I'll deprioritize it in #1189.