https://github.com/Men-cotton created 
https://github.com/llvm/llvm-project/pull/172663

Fixed a crash in `CIRToLLVMAllocaOpLowering` where `cir.alloca` operations 
without an explicit alignment attribute failed. The alignment now defaults to 0 
if the attribute is missing.

Added a regression test in `clang/test/CIR/Lowering/alloca.cir`.

>From 326addd57e92ad32094d4c81b412e279ee2ad130 Mon Sep 17 00:00:00 2001
From: mencotton <[email protected]>
Date: Wed, 17 Dec 2025 21:28:59 +0900
Subject: [PATCH] [CIR] Handle optional alignment in alloca lowering

---
 clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp |  7 +++++--
 clang/test/CIR/Lowering/alloca.cir                  | 13 +++++++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CIR/Lowering/alloca.cir

diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp 
b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 7d854997848aa..82e556f6e65db 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -1515,8 +1515,11 @@ mlir::LogicalResult 
CIRToLLVMAllocaOpLowering::matchAndRewrite(
   assert(!cir::MissingFeatures::addressSpace());
   assert(!cir::MissingFeatures::opAllocaAnnotations());
 
-  rewriter.replaceOpWithNewOp<mlir::LLVM::AllocaOp>(
-      op, resultTy, elementTy, size, op.getAlignmentAttr().getInt());
+  unsigned alignment = 0;
+  if (auto alignAttr = op.getAlignmentAttr())
+    alignment = alignAttr.getInt();
+  rewriter.replaceOpWithNewOp<mlir::LLVM::AllocaOp>(op, resultTy, elementTy,
+                                                    size, alignment);
 
   return mlir::success();
 }
diff --git a/clang/test/CIR/Lowering/alloca.cir 
b/clang/test/CIR/Lowering/alloca.cir
new file mode 100644
index 0000000000000..51980d393eb6f
--- /dev/null
+++ b/clang/test/CIR/Lowering/alloca.cir
@@ -0,0 +1,13 @@
+// RUN: cir-opt %s --cir-to-llvm | FileCheck %s
+
+!s32i = !cir.int<s, 32>
+
+module {
+  // CHECK-LABEL: llvm.func @alloca_no_align
+  // CHECK:         %[[SIZE:.*]] = llvm.mlir.constant(1 : i64) : i64
+  // CHECK-NEXT:    %{{.*}} = llvm.alloca %[[SIZE]] x i32 : (i64) -> !llvm.ptr
+  cir.func @alloca_no_align() {
+    %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["var_name"]
+    cir.return
+  }
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to