PMunch/futhark

Question about pass by ref and by value call with procedure

dinau opened this issue · 2 comments

dinau commented

I'm trying to compile imguin of my test project with Futhark.
I've had curious problem below,
(this program is simplified the issue)

const FutharkEnable = true
static:
  writefile("csource.h", """
      typedef struct ImVec2 ImVec2;
      struct ImVec2 { float x, y; };
      void test( ImVec2 pos);
  """)
  writefile("csource.c", """
      #include <stdio.h>
      #include "csource.h"
      void test( ImVec2 pos){
        printf("\nPos(%.1f,%.1f)",pos.x,pos.y);
      }
  """)
when FutharkEnable:
  import futhark
  importc:
    path "./"
    "csource.h"
else:
  type
    ImVec2 {.importc: "ImVec2", header: "csource.h".} = object
      x, y: cfloat
  proc test(pos: ImVec2) {.importc, header: "csource.h".}

{.compile: "csource.c".}
proc main() =
  var pos = ImVec2(x: 1.0, y: 2.0)
  test(pos)

main()

If set const FutharkEnable = true,the result is,

Pos(0.0,0.0)

above is unexpected result for me.

If set const FutharkEnable = false the result is,

Pos(1.0,2.0)

this is correct result.

How do I resolve this difference ?

PMunch commented

Hmm, Futhark should indeed add bycopy to all the objects. The reason why this hasn't been done so far is that C libraries typically pass things by pointers, and I wanted to give Nim the opportunity to optimize passing internally. But in this case it does indeed create incorrect code. I've created a new release 0.9.1 which adds the bycopy pragma.

dinau commented

Thank you very much.
It works well!.