<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/78278>78278</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SPIRV] Global unnamed variables cause SPIR-V Backend code generation to crash
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
VyacheslavLevytskyy
</td>
</tr>
</table>
<pre>
Unnamed global variables (represented as an unsigned numeric value with @ prefix, e.g., @0) cause code generation to crash (assert in Debug, undefined behaviour in Release builds). An example of such a behaviour presents the following ll code:
```
@0 = addrspace(1) global i8 123
; Function Attrs: nounwind
define spir_kernel void @foo() #0 {
ret void
}
attributes #0 = { nounwind }
```
Further investigation and debug show the culprit as Instruction Select pass (after legalizer). The reason for the crash is that `GlobalValue`'s `getGlobalIdentifier()` would fail for unnamed globals when trying to access the first character of the name.
There are two possible approaches for a local fix of the issue:
- add a special case of empty name in global variables (pass empty string, check for it when generating linkage decoration, etc.),
- generate a name of a global variable, as in case of other Target implementation (see: lib/IR/Mangler.cpp:131 and code generation for `llc -o` generates `.globl __unnamed_1` and `__unnamed_1:` for `@0`).
Complexity of two fixes is similar, both are local fixes, and the latter seems a better solution due to the intricacy of expected linkage attributes for unnamed global variables (see, for example, https://discourse.llvm.org/t/naming-and-linkage-for-llvm-ir-anonymous-globals/61595). It's expected that by default a global variable is an external linkage symbol, and both left its name empty and don't generate external linkage attribute would cause to a disputable resolution. Probably, a better fix is to generate a name for an unnamed global in a style of lib/IR/Mangler.cpp:131
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8Vklv4zoS_jX0pWBDprzp4EMWeBBgBmh09-QalKiSxBeaFLjY8fv1D0XJztZ4QGBHcrGWbykJQ9CdJdqL9b1YP84wxd75_fMFVU_B4Om_dLrE8Hq5zGrXXPb_txaP1EBnXI0GTug11oYCCLnzNHgKZCM1gAHQQrI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