[VarDecl] Why the LocalDeclMap doesn' t hold VarDecl of Template function

  • class CodeGenFunction has a member variable LocalDeclMap, who keeps track for local C decls.

llvm-project/clang/lib/CodeGen/CodeGenFunction.h at 48ae61470104e9d7a8be5beb8739c24f52cc33c0 · llvm/llvm-project · GitHub

  • In my local downstream, I add a new pragma noprefetch to Indicate relate VarDecl should not be prefecthed, it works fine with nomal function, but I find it doesn’t hold VarDecl of Template function, such as testcase: Compiler Explorer
template <class flt_t>
void foo (flt_t dest[N], unsigned int index[N], flt_t src[Length], int n)
{
    #pragma clang loop noprefetch(index)
    for (int j = 0; j < n; j++) {
        dest[j] = src[index[j]];
    }
}

void foo_float (float dest[], unsigned int index[], float src[], int n) {
	return foo<float>(dest, index, src, n);
}

I tried to some test code in function CodeGenFunction::EmitForStmt, then
It can be see we’ll not reach line 1183 for the above Template function, and it can reach the line 1183 if it is a normal function Compiler Explorer.

image

  • The above experiment showed the member variable LocalDeclMap of class CodeGenFunction only hold the VarDecl of normal function ?
  • Base on a little debug, I find the VarDecl index of Template function will also be pushed into LocalDeclMap, but there are 2 different point values.

    a) ParmVarDecl 0xaaaabe017388 it pushed into LocalDeclMap


    b) Use ParmVarDecl 0xaaaabe016060 to find the VarDecl index, so it failed to reach line 1183 on above test .

  • It seems the ParmVarDecl 0xaaaabe016060 is pointer to the template function, and ParmVarDecl 0xaaaabe017388 ponter to the instantiation function with float type base on the template function, so the issue is the ForAttrs record the VarDecl of template function itself ?

    .i.e: LocalDeclMap record the VarDecl of instantiation function with setAddrOfLocalVar, while EmitForStmt deals with the template function itself because the ForAttrs record the VarDecl of template function itself.