-
-
Notifications
You must be signed in to change notification settings - Fork 15k
MIR passes do not take into account if an operation is convergent #137086
Copy link
Copy link
Closed
rust-lang/stdarch
#1805Labels
A-mir-optArea: MIR optimizationsArea: MIR optimizationsC-bugCategory: This is a bug.Category: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeIssue: Correct Rust code lowers to incorrect machine codeI-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessO-NVPTXTarget: the NVPTX LLVM backend for running rust on GPUs, https://llvm.org/docs/NVPTXUsage.htmlTarget: the NVPTX LLVM backend for running rust on GPUs, https://llvm.org/docs/NVPTXUsage.htmlO-amdgcnTarget: the Radeon 9001XT and suchTarget: the Radeon 9001XT and suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.WG-mir-optWorking group: MIR optimizationsWorking group: MIR optimizations
Metadata
Metadata
Assignees
Labels
A-mir-optArea: MIR optimizationsArea: MIR optimizationsC-bugCategory: This is a bug.Category: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeIssue: Correct Rust code lowers to incorrect machine codeI-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessO-NVPTXTarget: the NVPTX LLVM backend for running rust on GPUs, https://llvm.org/docs/NVPTXUsage.htmlTarget: the NVPTX LLVM backend for running rust on GPUs, https://llvm.org/docs/NVPTXUsage.htmlO-amdgcnTarget: the Radeon 9001XT and suchTarget: the Radeon 9001XT and suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.WG-mir-optWorking group: MIR optimizationsWorking group: MIR optimizations
Type
Fields
Give feedbackNo fields configured for issues without a type.
Issue
I encountered an issue when using the
core::arch::nvptx::_syncthreads()while the MIR pass JumpThreading is enabled. The issue can be reproduced with a simple kernel when executed with the following parameters:block_dim = BlockDim{x : 512, y : 1, z : 1};grid_dim = GridDim{x : 2, y : 1, z : 1};n = 1000;compute-sanitizer --tool synccheckcomplains about barrier errors. The resulting.ptxshows the reason for that. The code is transformed to the following:I could track down this transformation to the MIR pass JumpThreading. However,
_syncthreads()is a convergent operation. This property must be considered when doing code transformations (see this LLVM issue for reference).Therefore turning off the MIR pass JumpThreading completely prevents this transformation from happening and the resulting code is correct (
compute-sanitizerdoes also not complain any longer).PTX with JumpThreading
PTX without JumpThreading
In the above example
_syncthreads()does nothing useful and can be ommited. However I encountered this issue in a more complex stencil kernel where these transformations lead to side effects and race conditions.Compiler arguments
With JumpThreading:
Without JumpThreading:
Background
Targets like nvptx64-nvidia-cuda, amdgpu and probably also spir-v (rust-gpu) make use of so called convergent operations (like
_syncthreads(). LLVM provides a detailed explanation for this type of operations. Special care must be taken when code that involves convergent operations is transformed.To my knowledge rustc does not know if an operation is convergent so passes do not handle these operations correctly.
Zulip-Stream