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

    <tr>
        <th>Summary</th>
        <td>
            [SPIRV] Use of token type results to undefined behavior of SPIR-V Backend code generator
        </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>
    Use of token type in LLVM intrinsic causes undefined behavior of SPIR-V Backend code generator when assertions are disabled.

For example, when running the following example in a Release build of the current version of SPIR-V Backend

```
declare token @llvm.myfun()
define dso_local spir_func void @func() {
entry:
 %tok = call token @llvm.myfun()
  ret void
}
```
code generator never produces SPIR-V output, looping (expectedly forever) in an arbitrary piece of LLVM code.

In a build with assertions enabled we are able to see the reason for such a behaviour: execution reaches `lib/IR/Function.cpp:985` that calls `llvm_unreachable(...)`, because `getMangledTypeStr()` has no support for token type. `llvm_unreachable` in its turn in non-asserts builds causes undefined behavior of SPIR-V Backend code generator, without a reasonable diagnostics/error message.

The same behaviour can be observed not only for user-defined intrinsic, but with other rare cases of generally legitimate usage of token type, like the following:

```
; Example of token usage is from https://llvm.org/docs/LangRef.html (Preallocated Operand Bundles)
%foo = type { i64, i32 }
define dso_local spir_func void @test() {
entry:
 %tok = call token @llvm.call.preallocated.setup(i32 1)
  %a = call ptr @llvm.call.preallocated.arg(token %tok, i32 0) preallocated(%foo)
  ret void
}
declare token @llvm.call.preallocated.setup(i32 %num_args)
declare ptr @llvm.call.preallocated.arg(token %setup_token, i32 %arg_index)
```

Analysis of possible fix options shows at least two alternative paths:
1) to enhance support for token type in LLVM library and SPIR-V Backend itself, starting probably from simple addition of token type in mangling

```
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -994,6 +994,7 @@ static std::string getMangledTypeStr(Type *Ty, bool &HasUnnamedType) {
     case Type::PPC_FP128TyID: Result += "ppcf128";  break;
     case Type::X86_MMXTyID:   Result += "x86mmx";   break;
     case Type::X86_AMXTyID:   Result += "x86amx";   break;
+    case Type::TokenTyID:     Result += "Token";   break;
     case Type::IntegerTyID:
       Result += "i" + utostr(cast<IntegerType>(Ty)->getBitWidth());
       break;
```
2) to generate comprehensive error message when such a usage of token type was detected that would lead to undefined behavior of SPIR-V Backend code generator.

The second options looks like an adequate solution for now, because SPIR-V doesn't support token type anyway without additional extensions.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykV11v4rrW_jXmZgkUDAS44KJMB72VZvRW3d1z9l3lxIvEp46dY69A-fdHy0kpTDsfZ-8RYnBjP-vzeZajYjSVQ9yIxVYsbkeqo9qHzbeTKmuMVh2-4OFE8fl0GhVenzZ_RgS_B_LP6IBOLYJx8OXLt69gHAXjoimhVF3ECJ3TuDcONRRYq4PxgU_-cX_3MP4GW1U-o9NQeo1QocOgyAc41uhAxYiBjHcRVEDQJqrCop6I7FZkN_33zgfAF9W0FoX81J8LnXPGVUA1wt5b64-8Gnaxnwoe0KKKCEVnrE6B1AhlFwI6ggOGaLx77-WlYZFnwyctNZaWnewTIuaZtYdm0pz2nRNyJeT6dRtnAnT0T9aXykJsTXjad66EgzeaD_KiPwJiue2PoaNwErPBNAi5IP8MYnYLpbL2V0YBAlLCH1xf3n4Yw3c1cHjAAG3wuisxvqbCd9R2xLm23recWCFX-NJiSajtCfY-8Dl2nzPtQIXCUFDhBK3BMnVN6hO2dlXKOy5MX5Cjofqy_OhS5eGIqRN4AeQhIqbCBVTROzYNsStrhuk7rQtidgP4gmXHQLyR2xlEnllTCLm7exByt-tcyY8nZduK2c16tRB5BlQrSuntt9tD89S5BMDmhVxNJhNOcZ5xMgpM3c5bK6SvylUW9eOpxT8oDLXIM6hVBOchdm3rAyWH3xg0-dBOnnEeDUWgLjj-7bwb97mJfbriP2BaIo2h2ncEakhkSq82qnI-kimjkD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