https://github.com/mmha created https://github.com/llvm/llvm-project/pull/245031
Implement `__builtin_alloca_with_align` and `__builtin_alloca_with_align_uninitialized` in ClangIR. Emit them like `__builtin_alloca`, with one difference: the dynamic alloca gets the alignment that the second argument gives. That argument is an integer constant expression in bits. Classic codegen does the same. Assisted-By: Claude Opus 5.5 and GPT-6.2 Sol as reviewer >From 1e30d6776bf3a0721e037a73b75fe54ee0379f33 Mon Sep 17 00:00:00 2001 From: Morris Hafner <[email protected]> Date: Wed, 18 Nov 2026 01:08:27 +0000 Subject: [PATCH] [CIR] Implement __builtin_alloca_with_align ClangIR could not compile __builtin_alloca_with_align or __builtin_alloca_with_align_uninitialized. Every translation unit that used them stopped with an NYI error. Emit them like __builtin_alloca, with one difference: the dynamic alloca gets the alignment that the second argument gives. That argument is an integer constant expression in bits. Classic codegen does the same. Found by compiling the Eigen 5.0.1 tests with -fclangir. Eigen's stack-allocation helper uses __builtin_alloca_with_align on Linux, so 587 of its 1,188 test files failed to compile. --- clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 24 ++++++++------ .../CIR/CodeGen/builtin-alloca-with-align.c | 32 +++++++++++++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 clang/test/CIR/CodeGen/builtin-alloca-with-align.c diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 2fd24edd626a69..8f83e37f9d636d 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -671,17 +671,22 @@ static RValue errorBuiltinNYI(CIRGenFunction &cgf, const CallExpr *e, static RValue emitBuiltinAlloca(CIRGenFunction &cgf, const CallExpr *e, unsigned builtinID) { - assert(builtinID == Builtin::BI__builtin_alloca || - builtinID == Builtin::BI__builtin_alloca_uninitialized || - builtinID == Builtin::BIalloca || builtinID == Builtin::BI_alloca); + const bool withAlign = + builtinID == Builtin::BI__builtin_alloca_with_align || + builtinID == Builtin::BI__builtin_alloca_with_align_uninitialized; + assert((withAlign || builtinID == Builtin::BI__builtin_alloca || + builtinID == Builtin::BI__builtin_alloca_uninitialized || + builtinID == Builtin::BIalloca || builtinID == Builtin::BI_alloca) && + "not an alloca builtin"); // Get alloca size input mlir::Value size = cgf.emitScalarExpr(e->getArg(0)); - // The alignment of the alloca should correspond to __BIGGEST_ALIGNMENT__. const TargetInfo &ti = cgf.getContext().getTargetInfo(); - const CharUnits suitableAlignmentInBytes = - cgf.getContext().toCharUnitsFromBits(ti.getSuitableAlign()); + const CharUnits alignmentInBytes = cgf.getContext().toCharUnitsFromBits( + withAlign + ? e->getArg(1)->EvaluateKnownConstInt(cgf.getContext()).getZExtValue() + : ti.getSuitableAlign()); // Emit the alloca op with type `u8 *` to match the semantics of // `llvm.alloca`. We later bitcast the type to `void *` to match the @@ -692,10 +697,11 @@ static RValue emitBuiltinAlloca(CIRGenFunction &cgf, const CallExpr *e, CIRGenBuilderTy &builder = cgf.getBuilder(); mlir::Value allocaAddr = builder.createAlloca( cgf.getLoc(e->getSourceRange()), builder.getUInt8PtrTy(), - builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size); + builder.getUInt8Ty(), "bi_alloca", alignmentInBytes, size); // Initialize the allocated buffer if required. - if (builtinID != Builtin::BI__builtin_alloca_uninitialized) { + if (builtinID != Builtin::BI__builtin_alloca_uninitialized && + builtinID != Builtin::BI__builtin_alloca_with_align_uninitialized) { // Initialize the alloca with the given size and alignment according to // the lang opts. Only the trivial non-initialization is supported for // now. @@ -2423,9 +2429,9 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, case Builtin::BI_alloca: case Builtin::BI__builtin_alloca_uninitialized: case Builtin::BI__builtin_alloca: - return emitBuiltinAlloca(*this, e, builtinID); case Builtin::BI__builtin_alloca_with_align_uninitialized: case Builtin::BI__builtin_alloca_with_align: + return emitBuiltinAlloca(*this, e, builtinID); case Builtin::BI__builtin_infer_alloc_token: return errorBuiltinNYI(*this, e, builtinID); case Builtin::BIbzero: diff --git a/clang/test/CIR/CodeGen/builtin-alloca-with-align.c b/clang/test/CIR/CodeGen/builtin-alloca-with-align.c new file mode 100644 index 00000000000000..00f77dae10ab16 --- /dev/null +++ b/clang/test/CIR/CodeGen/builtin-alloca-with-align.c @@ -0,0 +1,32 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG + +void *with_align(unsigned long n) { + return __builtin_alloca_with_align(n, 256); +} + +// CIR-LABEL: cir.func {{.*}} @with_align( +// CIR: %{{.+}} = cir.alloca "bi_alloca" align(32) size(%{{.+}}) : !cir.ptr<!u8i> + +// LLVM-LABEL: define {{.*}} ptr @with_align( +// LLVM: alloca i8, i64 %{{.+}}, align 32 + +// OGCG-LABEL: define {{.*}} ptr @with_align( +// OGCG: alloca i8, i64 %{{.+}}, align 32 + +void *with_align_uninitialized(unsigned long n) { + return __builtin_alloca_with_align_uninitialized(n, 8); +} + +// CIR-LABEL: cir.func {{.*}} @with_align_uninitialized( +// CIR: %{{.+}} = cir.alloca "bi_alloca" align(1) size(%{{.+}}) : !cir.ptr<!u8i> + +// LLVM-LABEL: define {{.*}} ptr @with_align_uninitialized( +// LLVM: alloca i8, i64 %{{.+}}, align 1 + +// OGCG-LABEL: define {{.*}} ptr @with_align_uninitialized( +// OGCG: alloca i8, i64 %{{.+}}, align 1 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
