<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/118576>118576</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            OpenCL SPIRV Unresolved external reference to _Z13get_global_idj
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Calandracas606
      </td>
    </tr>
</table>

<pre>
    clang is generating spirv with function calls which should be treated as builtins.

This is only occurring when using the C++ for OpenCL mode

kernel.cl:
```C
// this sample doesn't use any C++ for OpenCL features
__kernel void sample(write_only image2d_t img) {

 const int col = get_global_id(0);
  const int row = get_global_id(1);
 int2 coord;
  coord.x = col;
  coord.y = row;

  float4 px = {0, 0, 0, 1};

  write_imagef(img, coord, px);
}
```

When compiling with `-cl-std=clc++`
```
$ clang -c -target spirv64 -cl-std=clc++ -o sample_cpp.spv kernel.cl
$ spirv-link sample_cpp.spv 
error: 0: Unresolved external reference to "_Z13get_global_idj".
```

When I examine `sample_cpp.spv` with `spirv-dis` I see:
- `OpFunctionCall %ulong %_Z13get_global_idj %uint_0`
- `OpFunctionCall %void %_Z12write_imagef14ocl_image2d_woDv2_iDv4_f %19 %29 %32`

This appears to be incorrect.

When i repeat the same process but without the `-cl-std=clc++` flag:
```
$ clang -c -target spirv64 -o sample.spv kernel.cl
$ spirv-link sample.spv 
```
I get no errors.

Examining the spirv output, I see the expected output in the spirv:
- `%21 = OpLoad %v3ulong %__spirv_BuiltInGlobalInvocationId Aligned 32`
- `OpImageWrite %19 %28 %18`

I have tried this with multiple versions of clang, and it happens with clang>=19. It does not occur with clang18

clang version 20.0.0git 14a259f85b6cbe6827677d94990c8803e31c847d

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVVuP46gS_jXkpRQL4_tDHtLJ9FGkkebonN0daV8sjMs2MwQswEn3v1-B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