This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 6653620075 [ROCm] Fix some ROCm codegen bugs (#15518)
6653620075 is described below

commit 665362007552268547526b78d20e2b304e9ef278
Author: llpan <[email protected]>
AuthorDate: Fri Feb 27 22:38:10 2026 +0800

    [ROCm] Fix some ROCm codegen bugs (#15518)
    
    ## 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));
    ```
---
 src/target/llvm/codegen_llvm.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/target/llvm/codegen_llvm.cc b/src/target/llvm/codegen_llvm.cc
index ccb9173cd1..93f0282015 100644
--- a/src/target/llvm/codegen_llvm.cc
+++ b/src/target/llvm/codegen_llvm.cc
@@ -757,7 +757,7 @@ llvm::GlobalVariable* 
CodeGenLLVM::AllocateSharedMemory(DataType dtype, size_t s
       new llvm::GlobalVariable(*module_, type, false, linkage, 
llvm::UndefValue::get(type), "shmem",
                                nullptr, llvm::GlobalValue::NotThreadLocal, 
shared_address_space);
 #if TVM_LLVM_VERSION >= 100
-  global->setAlignment(llvm::Align(alignment));
+  global->setAlignment(llvm::MaybeAlign(alignment));
 #else
   global->setAlignment(alignment);
 #endif

Reply via email to