cucapra/dahlia

Issue in C++ backend when passing local array to function

Closed this issue · 0 comments

Calling fuse run one.fuse -o one.test.cpp results in a C++ compilation error

def fn(xs: ubit<32>[16]) = {
  for (let i=0..16) {
    xs[i] += 1;
  }
}

let arr: ubit<32>[16];

fn(arr);

Error message

(base) m.nimalan@nimalans-MacBook-Pro ~/D/o/p/d/scratch> fuse run one.fuse -o one.test.cpp                                                               master!?
Failed to generate the executable one.test.cpp.o.
one.test.cpp:14:3: error: no matching function for call to 'fn'
  fn(arr);
  ^~
one.test.cpp:3:6: note: candidate function not viable: no known conversion from 'unsigned int[16]' to 'vector<unsigned int> &' for 1st argument
void fn(vector<unsigned int> &xs) {
     ^
1 error generated.

Code in one.test.cpp. Arrays created with let statements are created as normal arrays, while function arguments are vectors.

void fn(vector<unsigned int> &xs) {
  
  for(int i = 0; i < 16; i++) {
    xs[i] += 1;
  }
}
/***************** Parse helpers  ******************/
/***************************************************/
void kernel() {
  
  unsigned int arr[16];
  fn(arr);
  json_t __;
  std::cout << __.dump(2) << std::endl;
}

Modifying the emitLet functions of the C++ backend should fix the problem

override def emitLet(l: CLet) = l match {
case CLet(id, Some(TArray(typ, dims, _)), init) => {
emitType(typ) <+> id <> hsep(dims.map({
case (size, _) => brackets(text(size.toString))
})) <> init.map(i => space <> emitExpr(i)).getOrElse(emptyDoc) <> semi
}
case _ => super.emitLet(l)

I'll raise a PR for this