PTX backend not thread safe

Hi All,

We have a C like language and we’re using the PTX backend to generate code JIT for the GPU. This works great single threaded however with all the code we’re JIT’ing the compilation times can get out of hand. Therefore, we’re trying to compile this code in parallel as this speeds things up greatly! Unfortunately, it seems that calling llvm::legacy::PassManager::run() in parallel to generate the PTX ends up producing garbage PTX producing errors when compiling it similar to the following.

error: Failed to parse input PTX string

(api input string), line 26694; error : Unknown symbol ‘prototype_708’

(api input string), line 26694; error : Arguments mismatch for instruction ‘call’

(api input string), line 26699; error : Unknown symbol ‘prototype_708’

(api input string), line 27288; error : Unknown symbol ‘prototype_724’

(api input string), line 27288; error : Arguments mismatch for instruction ‘call’

(api input string), line 27293; error : Unknown symbol ‘prototype_724’

(api input string), line 34108; error : Unknown symbol ‘prototype_1118’

(api input string), line 34108; error : Arguments mismatch for instruction ‘call’

(api input string), line 34113; error : Unknown symbol ‘prototype_1118’

Cannot parse input PTX string

)

We looked into LLVM to try to figure out what the issues and it seems that the NVPTXISelLowering class has some static counter that it uses to create the unique value when generating the prototype symbol. We tried to fix this by naively just making this counter an atomic. You can see the patch we used for this in https://github.com/imageworks/OpenShadingLanguage/issues/1181 where we have originally been discussing the issue. Initially this seemed to fix the problem but after more rigorous testing is seems that this doesn’t catch all the cases. Can anybody help solve the missing piece of the puzzle here to get this working?

Cheers,
Dec