[ROCm] Fix some ROCm codegen bugs#15518
Merged
Merged
Conversation
Collaborator
|
Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.
Generated by tvm-bot |
Member
|
Could you please add a regression test and also fill the PR body :) |
## Problem
`CodeGenLLVM::AllocateSharedMemory` calls `llvm::Align(alignment)` (for LLVM >= 10), but `llvm::Align` requires a non-zero, power-of-2 value and will trigger an assertion failure if given `0`.
This crash can be triggered on ROCm when allocating dynamic shared memory. In `codegen_amdgpu.cc`, the dynamic shared memory path calls:
```cpp
buf = AllocateSharedMemory(op->dtype, 0, 3, std::min(info.alignment, 16),
llvm::GlobalValue::ExternalLinkage);
```
`info.alignment` is looked up from `alloc_storage_info_`, which defaults to `0` if no alignment annotation has been set on the buffer. So `std::min(0, 16) = 0` is passed as the alignment argument, leading to:
```cpp
global->setAlignment(llvm::Align(0)); // assertion failure: alignment must be non-zero
```
## Fix
Replace `llvm::Align(alignment)` with `llvm::MaybeAlign(alignment)`. `llvm::MaybeAlign` treats `0` as "no explicit alignment specified" (equivalent to `std::nullopt`), which is safe and correct — LLVM will use the type's ABI alignment instead.
```cpp
// Before
global->setAlignment(llvm::Align(alignment));
// After
global->setAlignment(llvm::MaybeAlign(alignment));
```
Member
|
@tvm-bot rerun |
Contributor
|
Failed to re-run CI in https://github.com/apache/tvm/actions/runs/22485554870 Detailswith response |
tqchen
approved these changes
Feb 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CodeGenLLVM::AllocateSharedMemorycallsllvm::Align(alignment)(for LLVM >= 10), butllvm::Alignrequires a non-zero, power-of-2 value and will trigger an assertion failure if given0.This crash can be triggered on ROCm when allocating dynamic shared memory. In
codegen_amdgpu.cc, the dynamic shared memory path calls:info.alignmentis looked up fromalloc_storage_info_, which defaults to0if no alignment annotation has been set on the buffer. Sostd::min(0, 16) = 0is passed as the alignment argument, leading to:Fix
Replace
llvm::Align(alignment)withllvm::MaybeAlign(alignment).llvm::MaybeAligntreats0as "no explicit alignment specified" (equivalent tostd::nullopt), which is safe and correct — LLVM will use the type's ABI alignment instead.