KhronosGroup / KhronosGroup/SPIRV-LLVM-Translator
Emit token ids in determinate order
- Dominant language
- LLVM
- Stars
- 625
- Forks
- 279
- Avg merge
- 3d 5h
- Merged PRs (30d)
- 34
Description
Currently the translator emits token ids in indeterminate order depending on the platform, compiler, compiler options and so on.
Here is an example of such behavior with the same kernel complied using arm and x86 versions of the translator:
```
--- x86.spirv.txt
+++ arm.spirv.txt
@@ -118,7 +118,7 @@
%45 = OpTypeInt 16 0
%164 = OpConstant %2 0
%167 = OpConstant %2 64
- %169 = OpConstant %2 3
+ %170 = OpConstant %2 3
%173 = OpConstant %2 7
%176 = OpConstant %2 9
%184 = OpConstant %2 16
@@ -158,7 +158,7 @@
%116 = OpTypePointer Function %2
%120 = OpTypePointer Function %26
%145 = OpTypePointer Function %10
- %170 = OpTypeBool
+ %169 = OpTypeBool
%208 = OpTypeFunction %2
%217 = OpTypeFunction %19
%577 = OpTypePointer Function %52
```
Resulted spirv files are semantically identical, but have different token ids.
This happens because of calls in source code with unspecified order of invocation. [For example:](https://github.com/KhronosGroup/SPIRV-LLVM-Translator/blob/a6ca74547993aec032a12577d1d0e0e9f44f6b9f/lib/SPIRV/SPIRVWriter.cpp#L981)
```c++
SPIRVInstruction *BI =
BM->addCmpInst(transBoolOpCode(TOp0, CmpMap::map(Cmp->getPredicate())),
transType(Cmp->getType()), TOp0, TOp1, BB);
```
Depending on which `trans*` call is invoked first we can get different ids order for tokens.
I suppose it would be nice to have an option to emit ids in determinate order.
Contributor guide
Research direction
Start in lib/SPIRV/SPIRVWriter.cpp at the cited comparison-instruction translation and trace the trans* calls that allocate token ids. Determine how an opt-in deterministic emission mode should order ids across platforms and compiler options; done means equivalent translations produce the same token-id ordering.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100