f0rki / mapping-high-level-constructs-to-llvm-ir

A guide that explains how high level programming language constructs are mapped to the LLVM intermediate language.
https://mapping-high-level-constructs-to-llvm-ir.readthedocs.io
Other
599 stars 62 forks source link

how pass all implicit as explicit arguments as arguments. #48

Closed pengxu-peter closed 1 year ago

pengxu-peter commented 1 year ago

file "https://github.com/f0rki/mapping-high-level-constructs-to-llvm-ir/blob/master/advanced-constructs/lambda-functions.rst" show:

int foo(int a)
{
  auto function = [a](int x) { return x + a; };
  return function(10);
}

can pass all implicit as explicit arguments as arguments like:

define internal i32 @lambda(i32 %a, i32 %x) {
    %1 = add i32 %a, %x
    ret i32 %1
}

define i32 @foo(i32 %a) {
    %1 = call i32 @lambda(i32 %a, i32 10)
    ret i32 %1
}

but when i run using command

clang++ -S -emit-llvm -std=c++14 -O1 -o - <cpp_file>

the result is implicit arguments using %class.anon*
%class.anon = type { i32 }

f0rki commented 1 year ago

Both is valid way to achieve the same thing. Either you pass all variables as implicit args, like it is described currently in this document, or you generate a new struct that packs all variables like it is done in current clang. Passing a single struct pointer instead of many implicit args is probably easier when capturing a lot of variables.

I'll add a note about this.