Struct initializer in loop

  • From: Denis Golovan <denis.golovan@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 19 May 2016 13:10:28 +0300

Hi all

It looks like following code has troubles with hoisting constant
expressions out of loop. Is this a bug or some architectural
restriction?
Tested in latest (35b09e692ead67181755795352f1df12547fd4fa) git version.

=================================================
ffi = require("ffi")
ffi.cdef[[
typedef struct point_t { double x, y; } point_t;
]]

function set(a,l,v)
  local v2=ffi.new("struct point_t", v)
  for i=0,l-1 do
    a[i]=v2
  end
end

function set2(a,l,v)
  for i=0,l-1 do
    local v2=ffi.new("struct point_t", v)
    a[i]=v2
  end
end

require('jit').flush()
a=ffi.new("point_t[?]", 10000)
set(a,10000, { x=1,y=2 })     --produces nice tight loop
set2(a,10000, { x=1,y=2 })    -- gets "JIT compilation disabled for function"
=================================================

In case if it matters, I attached jit dump.

BR,
Denis
---- TRACE flush

---- TRACE 1 start 0x00238f80:14
0005  GGET     7   0      ; "ffi"
0006  TGETS    7   7   1  ; "new"
0007  KSTR     8   2      ; "struct point_t"
0008  MOV      9   2
0009  CALL     7   2   3
0000  . FUNCC               ; ffi.istype
---- TRACE 1 IR
0001 >  int SLOAD  #5    CRI
0002 >  int LE     0001  +2147483646
0003    int SLOAD  #4    CI
0004    fun SLOAD  #0    R
0005    tab FLOAD  0004  func.env
0006    int FLOAD  0005  tab.hmask
0007 >  int EQ     0006  +511
0008    p32 FLOAD  0005  tab.node
0009 >  p32 HREFK  0008  "ffi" @72
0010 >  tab HLOAD  0009
0011    int FLOAD  0010  tab.hmask
0012 >  int EQ     0011  +31 
0013    p32 FLOAD  0010  tab.node
0014 >  p32 HREFK  0013  "new" @3
0015 >  fun HLOAD  0014
0016 >  tab SLOAD  #3    T
0017 >  fun EQ     0015  ffi.istype
0018 >  cdt CNEW   +1345
0019    p64 ADD    0018  +8  
---- TRACE 1 abort 0x00238f80:15 -- JIT compilation disabled for function

---- TRACE 1 start 0x00238f80:14
0005  GGET     7   0      ; "ffi"
0006  TGETS    7   7   1  ; "new"
0007  KSTR     8   2      ; "struct point_t"
0008  MOV      9   2
0009  CALL     7   2   3
0000  . FUNCC               ; ffi.istype
---- TRACE 1 IR
0001 >  int SLOAD  #5    CRI
0002 >  int LE     0001  +2147483646
0003    int SLOAD  #4    CI
0004    fun SLOAD  #0    R
0005    tab FLOAD  0004  func.env
0006    int FLOAD  0005  tab.hmask
0007 >  int EQ     0006  +511
0008    p32 FLOAD  0005  tab.node
0009 >  p32 HREFK  0008  "ffi" @72
0010 >  tab HLOAD  0009
0011    int FLOAD  0010  tab.hmask
0012 >  int EQ     0011  +31 
0013    p32 FLOAD  0010  tab.node
0014 >  p32 HREFK  0013  "new" @3
0015 >  fun HLOAD  0014
0016 >  tab SLOAD  #3    T
0017 >  fun EQ     0015  ffi.istype
0018 >  cdt CNEW   +1345
0019    p64 ADD    0018  +8  
---- TRACE 1 abort 0x00238f80:15 -- JIT compilation disabled for function

---- TRACE 1 start 0x00238f80:14
0005  GGET     7   0      ; "ffi"
0006  TGETS    7   7   1  ; "new"
0007  KSTR     8   2      ; "struct point_t"
0008  MOV      9   2
0009  CALL     7   2   3
0000  . FUNCC               ; ffi.istype
---- TRACE 1 IR
0001 >  int SLOAD  #5    CRI
0002 >  int LE     0001  +2147483646
0003    int SLOAD  #4    CI
0004    fun SLOAD  #0    R
0005    tab FLOAD  0004  func.env
0006    int FLOAD  0005  tab.hmask
0007 >  int EQ     0006  +511
0008    p32 FLOAD  0005  tab.node
0009 >  p32 HREFK  0008  "ffi" @72
0010 >  tab HLOAD  0009
0011    int FLOAD  0010  tab.hmask
0012 >  int EQ     0011  +31 
0013    p32 FLOAD  0010  tab.node
0014 >  p32 HREFK  0013  "new" @3
0015 >  fun HLOAD  0014
0016 >  tab SLOAD  #3    T
0017 >  fun EQ     0015  ffi.istype
0018 >  cdt CNEW   +1345
0019    p64 ADD    0018  +8  
---- TRACE 1 abort 0x00238f80:15 -- JIT compilation disabled for function

---- TRACE 1 start 0x00238f80:14
0005  GGET     7   0      ; "ffi"
0006  TGETS    7   7   1  ; "new"
0007  KSTR     8   2      ; "struct point_t"
0008  MOV      9   2
0009  CALL     7   2   3
0000  . FUNCC               ; ffi.istype
---- TRACE 1 IR
0001 >  int SLOAD  #5    CRI
0002 >  int LE     0001  +2147483646
0003    int SLOAD  #4    CI
0004    fun SLOAD  #0    R
0005    tab FLOAD  0004  func.env
0006    int FLOAD  0005  tab.hmask
0007 >  int EQ     0006  +511
0008    p32 FLOAD  0005  tab.node
0009 >  p32 HREFK  0008  "ffi" @72
0010 >  tab HLOAD  0009
0011    int FLOAD  0010  tab.hmask
0012 >  int EQ     0011  +31 
0013    p32 FLOAD  0010  tab.node
0014 >  p32 HREFK  0013  "new" @3
0015 >  fun HLOAD  0014
0016 >  tab SLOAD  #3    T
0017 >  fun EQ     0015  ffi.istype
0018 >  cdt CNEW   +1345
0019    p64 ADD    0018  +8  
---- TRACE 1 abort 0x00238f80:15 -- JIT compilation disabled for function

---- TRACE 1 start 0x00238f80:14
0005  GGET     7   0      ; "ffi"
0006  TGETS    7   7   1  ; "new"
0007  KSTR     8   2      ; "struct point_t"
0008  MOV      9   2
0009  CALL     7   2   3
0000  . FUNCC               ; ffi.istype
---- TRACE 1 IR
0001 >  int SLOAD  #5    CRI
0002 >  int LE     0001  +2147483646
0003    int SLOAD  #4    CI
0004    fun SLOAD  #0    R
0005    tab FLOAD  0004  func.env
0006    int FLOAD  0005  tab.hmask
0007 >  int EQ     0006  +511
0008    p32 FLOAD  0005  tab.node
0009 >  p32 HREFK  0008  "ffi" @72
0010 >  tab HLOAD  0009
0011    int FLOAD  0010  tab.hmask
0012 >  int EQ     0011  +31 
0013    p32 FLOAD  0010  tab.node
0014 >  p32 HREFK  0013  "new" @3
0015 >  fun HLOAD  0014
0016 >  tab SLOAD  #3    T
0017 >  fun EQ     0015  ffi.istype
0018 >  cdt CNEW   +1345
0019    p64 ADD    0018  +8  
---- TRACE 1 abort 0x00238f80:15 -- JIT compilation disabled for function

---- TRACE 1 start 0x00238f80:14
0005  GGET     7   0      ; "ffi"
0006  TGETS    7   7   1  ; "new"
0007  KSTR     8   2      ; "struct point_t"
0008  MOV      9   2
0009  CALL     7   2   3
0000  . FUNCC               ; ffi.istype
---- TRACE 1 IR
0001 >  int SLOAD  #5    CRI
0002 >  int LE     0001  +2147483646
0003    int SLOAD  #4    CI
0004    fun SLOAD  #0    R
0005    tab FLOAD  0004  func.env
0006    int FLOAD  0005  tab.hmask
0007 >  int EQ     0006  +511
0008    p32 FLOAD  0005  tab.node
0009 >  p32 HREFK  0008  "ffi" @72
0010 >  tab HLOAD  0009
0011    int FLOAD  0010  tab.hmask
0012 >  int EQ     0011  +31 
0013    p32 FLOAD  0010  tab.node
0014 >  p32 HREFK  0013  "new" @3
0015 >  fun HLOAD  0014
0016 >  tab SLOAD  #3    T
0017 >  fun EQ     0015  ffi.istype
0018 >  cdt CNEW   +1345
0019    p64 ADD    0018  +8  
---- TRACE 1 abort 0x00238f80:15 -- JIT compilation disabled for function

---- TRACE 1 start 0x00238f80:14
0005  GGET     7   0      ; "ffi"
0006  TGETS    7   7   1  ; "new"
0007  KSTR     8   2      ; "struct point_t"
0008  MOV      9   2
0009  CALL     7   2   3
0000  . FUNCC               ; ffi.istype
---- TRACE 1 IR
0001 >  int SLOAD  #5    CRI
0002 >  int LE     0001  +2147483646
0003    int SLOAD  #4    CI
0004    fun SLOAD  #0    R
0005    tab FLOAD  0004  func.env
0006    int FLOAD  0005  tab.hmask
0007 >  int EQ     0006  +511
0008    p32 FLOAD  0005  tab.node
0009 >  p32 HREFK  0008  "ffi" @72
0010 >  tab HLOAD  0009
0011    int FLOAD  0010  tab.hmask
0012 >  int EQ     0011  +31 
0013    p32 FLOAD  0010  tab.node
0014 >  p32 HREFK  0013  "new" @3
0015 >  fun HLOAD  0014
0016 >  tab SLOAD  #3    T
0017 >  fun EQ     0015  ffi.istype
0018 >  cdt CNEW   +1345
0019    p64 ADD    0018  +8  
---- TRACE 1 abort 0x00238f80:15 -- JIT compilation disabled for function

---- TRACE 1 start 0x00238f80:14
0005  GGET     7   0      ; "ffi"
0006  TGETS    7   7   1  ; "new"
0007  KSTR     8   2      ; "struct point_t"
0008  MOV      9   2
0009  CALL     7   2   3
0000  . FUNCC               ; ffi.istype
---- TRACE 1 IR
0001 >  int SLOAD  #5    CRI
0002 >  int LE     0001  +2147483646
0003    int SLOAD  #4    CI
0004    fun SLOAD  #0    R
0005    tab FLOAD  0004  func.env
0006    int FLOAD  0005  tab.hmask
0007 >  int EQ     0006  +511
0008    p32 FLOAD  0005  tab.node
0009 >  p32 HREFK  0008  "ffi" @72
0010 >  tab HLOAD  0009
0011    int FLOAD  0010  tab.hmask
0012 >  int EQ     0011  +31 
0013    p32 FLOAD  0010  tab.node
0014 >  p32 HREFK  0013  "new" @3
0015 >  fun HLOAD  0014
0016 >  tab SLOAD  #3    T
0017 >  fun EQ     0015  ffi.istype
0018 >  cdt CNEW   +1345
0019    p64 ADD    0018  +8  
---- TRACE 1 abort 0x00238f80:15 -- JIT compilation disabled for function

---- TRACE 1 start 0x00238f80:14
0005  GGET     7   0      ; "ffi"
0006  TGETS    7   7   1  ; "new"
0007  KSTR     8   2      ; "struct point_t"
0008  MOV      9   2
0009  CALL     7   2   3
0000  . FUNCC               ; ffi.istype
---- TRACE 1 IR
0001 >  int SLOAD  #5    CRI
0002 >  int LE     0001  +2147483646
0003    int SLOAD  #4    CI
0004    fun SLOAD  #0    R
0005    tab FLOAD  0004  func.env
0006    int FLOAD  0005  tab.hmask
0007 >  int EQ     0006  +511
0008    p32 FLOAD  0005  tab.node
0009 >  p32 HREFK  0008  "ffi" @72
0010 >  tab HLOAD  0009
0011    int FLOAD  0010  tab.hmask
0012 >  int EQ     0011  +31 
0013    p32 FLOAD  0010  tab.node
0014 >  p32 HREFK  0013  "new" @3
0015 >  fun HLOAD  0014
0016 >  tab SLOAD  #3    T
0017 >  fun EQ     0015  ffi.istype
0018 >  cdt CNEW   +1345
0019    p64 ADD    0018  +8  
---- TRACE 1 abort 0x00238f80:15 -- JIT compilation disabled for function

Other related posts: