https://github.com/martin-luecke updated https://github.com/llvm/llvm-project/pull/236082
From e37a46aaaae6935e42690616a5dccbad0e1d2d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Paul=20L=C3=BCcke?= <[email protected]> Date: Mon, 26 Oct 2026 13:56:03 +0000 Subject: [PATCH 1/4] [CIR] Use memory type for bool aggregate constants CIR bool values lower to i1, while aggregate storage uses the type selected by convertTypeForMemory. Build dense bool payloads and record members with that storage type so LLVM aggregate attributes agree with their container types. --- clang/lib/CIR/Lowering/LoweringHelpers.cpp | 38 +++++++---- .../const-array-bulk-lowering-fallbacks.cir | 63 +++++++++++++++++++ 2 files changed, 90 insertions(+), 11 deletions(-) diff --git a/clang/lib/CIR/Lowering/LoweringHelpers.cpp b/clang/lib/CIR/Lowering/LoweringHelpers.cpp index 6734b4e9a32e44..5a770419f729aa 100644 --- a/clang/lib/CIR/Lowering/LoweringHelpers.cpp +++ b/clang/lib/CIR/Lowering/LoweringHelpers.cpp @@ -198,8 +198,8 @@ template <> mlir::APFloat getZeroInitFromType(mlir::Type ty) { template <typename AttrTy, typename StorageTy> bool convertToDenseElementsAttrImpl( cir::ConstArrayAttr attr, llvm::SmallVectorImpl<StorageTy> &values, - const llvm::SmallVectorImpl<int64_t> ¤tDims, int64_t dimIndex, - int64_t currentIndex) { + const llvm::SmallVectorImpl<int64_t> ¤tDims, + mlir::Type convertedElementType, int64_t dimIndex, int64_t currentIndex) { if (auto stringAttr = mlir::dyn_cast<mlir::StringAttr>(attr.getElts())) { if (auto arrayType = mlir::dyn_cast<cir::ArrayType>(attr.getType())) { for (auto element : stringAttr) { @@ -221,8 +221,10 @@ bool convertToDenseElementsAttrImpl( for (auto eltAttr : arrayAttr) { if constexpr (std::is_same_v<StorageTy, mlir::APInt>) { if (auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(eltAttr)) { + unsigned bitWidth = + mlir::cast<mlir::IntegerType>(convertedElementType).getWidth(); values[currentIndex++] = - llvm::APInt(1, static_cast<uint64_t>(boolAttr.getValue())); + llvm::APInt(bitWidth, static_cast<uint64_t>(boolAttr.getValue())); continue; } } @@ -233,7 +235,8 @@ bool convertToDenseElementsAttrImpl( if (auto subArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(eltAttr)) { convertToDenseElementsAttrImpl<AttrTy>(subArrayAttr, values, currentDims, - dimIndex, currentIndex); + convertedElementType, dimIndex, + currentIndex); currentIndex += elementsSizeInCurrentDim; continue; } @@ -260,10 +263,18 @@ std::optional<mlir::DenseElementsAttr> convertToDenseElementsAttr( unsigned vectorSize = 1; for (auto dim : dims) vectorSize *= dim; - auto values = llvm::SmallVector<StorageTy, 8>( - vectorSize, getZeroInitFromType<StorageTy>(elementType)); + StorageTy zero = getZeroInitFromType<StorageTy>(elementType); + if constexpr (std::is_same_v<StorageTy, mlir::APInt>) { + if (mlir::isa<cir::BoolType>(elementType)) { + unsigned bitWidth = + mlir::cast<mlir::IntegerType>(convertedElementType).getWidth(); + zero = mlir::APInt::getZero(bitWidth); + } + } + llvm::SmallVector<StorageTy> values(vectorSize, zero); if (!convertToDenseElementsAttrImpl<AttrTy>(attr, values, dims, + convertedElementType, /*currentDim=*/0, /*initialIndex=*/0)) return std::nullopt; @@ -521,9 +532,12 @@ lowerConstArrayAttr(cir::ConstArrayAttr constArr, constArr, dims, type, converter->convertType(type)); } - if (mlir::isa<cir::BoolType>(type)) + if (mlir::isa<cir::BoolType>(type)) { + mlir::Type storageType = + convertTypeForMemory(*converter, mlir::DataLayout(moduleOp), type); return convertToDenseElementsAttr<cir::IntAttr, mlir::APInt>( - constArr, dims, type, converter->convertType(type)); + constArr, dims, type, storageType); + } if (mlir::isa<cir::FPTypeInterface>(type)) return convertToDenseElementsAttr<cir::FPAttr, mlir::APFloat>( @@ -616,9 +630,11 @@ lowerConstRecordMemberAttr(mlir::Attribute attr, intAttr.getValue()); } - if (auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(attr)) - return mlir::IntegerAttr::get(converter->convertType(boolAttr.getType()), - boolAttr.getValue() ? 1 : 0); + if (auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(attr)) { + mlir::Type storageType = convertTypeForMemory( + *converter, mlir::DataLayout(moduleOp), boolAttr.getType()); + return mlir::IntegerAttr::get(storageType, boolAttr.getValue() ? 1 : 0); + } if (auto fpAttr = mlir::dyn_cast<cir::FPAttr>(attr)) return mlir::FloatAttr::get(converter->convertType(fpAttr.getType()), diff --git a/clang/test/CIR/Lowering/const-array-bulk-lowering-fallbacks.cir b/clang/test/CIR/Lowering/const-array-bulk-lowering-fallbacks.cir index d321ab39e1c3b4..dc12053d9f66b1 100644 --- a/clang/test/CIR/Lowering/const-array-bulk-lowering-fallbacks.cir +++ b/clang/test/CIR/Lowering/const-array-bulk-lowering-fallbacks.cir @@ -10,7 +10,11 @@ !rec_entry = !cir.struct<{data !u16i, data !s32i}> !rec_iptr = !cir.struct<{data !cir.ptr<!s8i>}> !rec_bitint = !cir.struct<{data !bi33, data !s32i}> +!rec_bool = !cir.struct<{data !cir.bool}> +!rec_bool_array = !cir.struct<{data !cir.array<!cir.bool x 3>}> !rec_poison = !cir.struct<{data !s32i}> +#false = #cir.bool<false> : !cir.bool +#true = #cir.bool<true> : !cir.bool module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { cir.global external @tz_arr = @@ -71,6 +75,41 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { #cir.int<69> : !s8i, #cir.int<78> : !s8i]> : !cir.array<!s8i x 4>, #cir.zero : !cir.array<!s8i x 2> }> : !rec_data + + cir.global external @bool_matrix = #cir.const_array<[ + #cir.const_array<[#true, #false]> : !cir.array<!cir.bool x 2>, + #cir.const_array<[#false, #true]> : !cir.array<!cir.bool x 2> + ]> : !cir.array<!cir.array<!cir.bool x 2> x 2> + + cir.global external @bool_record_array = #cir.const_array<[ + #cir.const_record<{#true}> : !rec_bool, + #cir.const_record<{#false}> : !rec_bool + ]> : !cir.array<!rec_bool x 2> + + cir.func @bool_array(%arg0: !cir.ptr<!cir.array<!cir.bool x 3>>) { + %0 = cir.const #cir.const_array<[#true, #false, #true]> : + !cir.array<!cir.bool x 3> + cir.store %0, %arg0 : !cir.array<!cir.bool x 3>, + !cir.ptr<!cir.array<!cir.bool x 3>> + cir.return + } + + cir.func @bool_array_trailing_zeros( + %arg0: !cir.ptr<!cir.array<!cir.bool x 3>>) { + %0 = cir.const #cir.const_array<[#true], trailing_zeros> : + !cir.array<!cir.bool x 3> + cir.store %0, %arg0 : !cir.array<!cir.bool x 3>, + !cir.ptr<!cir.array<!cir.bool x 3>> + cir.return + } + + cir.func @record_with_bool_array(%arg0: !cir.ptr<!rec_bool_array>) { + %0 = cir.const #cir.const_record<{ + #cir.const_array<[#true, #true, #true]> : !cir.array<!cir.bool x 3> + }> : !rec_bool_array + cir.store %0, %arg0 : !rec_bool_array, !cir.ptr<!rec_bool_array> + cir.return + } } // CHECK-LABEL: llvm.mlir.global external @tz_arr( @@ -116,3 +155,27 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { // CHECK-SAME: : !llvm.struct<(array<4 x i8>, array<2 x i8>)> // CHECK-NOT: llvm.insertvalue // CHECK-NOT: llvm.return + +// CHECK-LABEL: llvm.mlir.global external @bool_matrix( +// CHECK-SAME: dense<{{\[\[}}1, 0], [0, 1]]> : tensor<2x2xi8>) +// CHECK-SAME: : !llvm.array<2 x array<2 x i8>> + +// CHECK-LABEL: llvm.mlir.global external @bool_record_array( +// CHECK-SAME: {{\[\[}}1 : i8], [0 : i8]]) +// CHECK-SAME: : !llvm.array<2 x struct<(i8)>> + +// CHECK-LABEL: llvm.func @bool_array +// CHECK: %[[INIT:.+]] = llvm.mlir.constant( +// CHECK-SAME: dense<[1, 0, 1]> : tensor<3xi8>) : !llvm.array<3 x i8> +// CHECK: llvm.store %[[INIT]], %{{.+}} : !llvm.array<3 x i8>, !llvm.ptr + +// CHECK-LABEL: llvm.func @bool_array_trailing_zeros +// CHECK: %[[INIT:.+]] = llvm.mlir.constant( +// CHECK-SAME: dense<[1, 0, 0]> : tensor<3xi8>) : !llvm.array<3 x i8> +// CHECK: llvm.store %[[INIT]], %{{.+}} : !llvm.array<3 x i8>, !llvm.ptr + +// CHECK-LABEL: llvm.func @record_with_bool_array +// CHECK: %[[INIT:.+]] = llvm.mlir.constant( +// CHECK-SAME: dense<1> : tensor<3xi8>) : !llvm.array<3 x i8> +// CHECK: llvm.insertvalue %[[INIT]], %{{.+}}[0] +// CHECK-SAME: : !llvm.struct<(array<3 x i8>)> From 809d92711e3b471d7a10d130cbf727f5bd61bfb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Paul=20L=C3=BCcke?= <[email protected]> Date: Mon, 9 Nov 2026 09:16:38 +0000 Subject: [PATCH 2/4] [CIR] Require module context for aggregate constant lowering Pass the captured module alongside CIRPreConversionTypeMap when lowering array constants, and require the module at aggregate helper call sites so memory types use the module's data layout. --- clang/include/clang/CIR/LoweringHelpers.h | 22 ++++++++----------- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 2 +- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/clang/include/clang/CIR/LoweringHelpers.h b/clang/include/clang/CIR/LoweringHelpers.h index c67153b28a512a..ef5934c26e07f3 100644 --- a/clang/include/clang/CIR/LoweringHelpers.h +++ b/clang/include/clang/CIR/LoweringHelpers.h @@ -43,19 +43,15 @@ convertToDenseElementsAttr(cir::ConstArrayAttr attr, const llvm::SmallVectorImpl<int64_t> &dims, mlir::Type type); -std::optional<mlir::Attribute> -lowerConstArrayAttr(cir::ConstArrayAttr constArr, - mlir::SymbolTableCollection &symbolTables, - const mlir::TypeConverter *converter, - const CIRPreConversionTypeMap &preConversionTypes, - mlir::ModuleOp moduleOp = {}); - -std::optional<mlir::Attribute> -lowerConstRecordAttr(cir::ConstRecordAttr constRecord, - mlir::SymbolTableCollection &symbolTables, - const mlir::TypeConverter *converter, - const CIRPreConversionTypeMap &preConversionTypes, - mlir::ModuleOp moduleOp = {}); +std::optional<mlir::Attribute> lowerConstArrayAttr( + cir::ConstArrayAttr constArr, mlir::SymbolTableCollection &symbolTables, + const mlir::TypeConverter *converter, + const CIRPreConversionTypeMap &preConversionTypes, mlir::ModuleOp moduleOp); + +std::optional<mlir::Attribute> lowerConstRecordAttr( + cir::ConstRecordAttr constRecord, mlir::SymbolTableCollection &symbolTables, + const mlir::TypeConverter *converter, + const CIRPreConversionTypeMap &preConversionTypes, mlir::ModuleOp moduleOp); /// Return \p attr with the trailing zeros of any array that /// adjustGlobalTypeForInit would split off written out as explicit zero diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 3360f3dd2b1f32..50262f199f7097 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -2944,7 +2944,7 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite( std::optional<mlir::Attribute> denseAttr; if (constArr && (denseAttr = lowerConstArrayAttr(constArr, symbolTables, typeConverter, - preConversionTypes))) { + preConversionTypes, moduleOp))) { attr = denseAttr.value(); } else { const mlir::Value initVal = From 767873cc5d096918823f7288de18f32b37765a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Paul=20L=C3=BCcke?= <[email protected]> Date: Mon, 9 Nov 2026 09:18:28 +0000 Subject: [PATCH 3/4] [CIR] Append the converted element type in dense array lowering Keep the existing recursive helper arguments in their original order and pass the converted element type last. --- clang/lib/CIR/Lowering/LoweringHelpers.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/clang/lib/CIR/Lowering/LoweringHelpers.cpp b/clang/lib/CIR/Lowering/LoweringHelpers.cpp index 5a770419f729aa..27cc52a6da38f5 100644 --- a/clang/lib/CIR/Lowering/LoweringHelpers.cpp +++ b/clang/lib/CIR/Lowering/LoweringHelpers.cpp @@ -195,11 +195,12 @@ template <> mlir::APFloat getZeroInitFromType(mlir::Type ty) { /// \param currentDims the shpae of tensor we're going to convert to /// \param dimIndex the current dimension we're processing /// \param currentIndex the current index in the values array +/// \param convertedElementType the lowered storage type of each element template <typename AttrTy, typename StorageTy> bool convertToDenseElementsAttrImpl( cir::ConstArrayAttr attr, llvm::SmallVectorImpl<StorageTy> &values, - const llvm::SmallVectorImpl<int64_t> ¤tDims, - mlir::Type convertedElementType, int64_t dimIndex, int64_t currentIndex) { + const llvm::SmallVectorImpl<int64_t> ¤tDims, int64_t dimIndex, + int64_t currentIndex, mlir::Type convertedElementType) { if (auto stringAttr = mlir::dyn_cast<mlir::StringAttr>(attr.getElts())) { if (auto arrayType = mlir::dyn_cast<cir::ArrayType>(attr.getType())) { for (auto element : stringAttr) { @@ -235,8 +236,8 @@ bool convertToDenseElementsAttrImpl( if (auto subArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(eltAttr)) { convertToDenseElementsAttrImpl<AttrTy>(subArrayAttr, values, currentDims, - convertedElementType, dimIndex, - currentIndex); + dimIndex, currentIndex, + convertedElementType); currentIndex += elementsSizeInCurrentDim; continue; } @@ -274,9 +275,9 @@ std::optional<mlir::DenseElementsAttr> convertToDenseElementsAttr( llvm::SmallVector<StorageTy> values(vectorSize, zero); if (!convertToDenseElementsAttrImpl<AttrTy>(attr, values, dims, - convertedElementType, - /*currentDim=*/0, - /*initialIndex=*/0)) + /*dimIndex=*/0, + /*currentIndex=*/0, + convertedElementType)) return std::nullopt; return mlir::DenseElementsAttr::get( From 4e6dc8318e7a769416aed95527f6c8216a8ea982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Paul=20L=C3=BCcke?= <[email protected]> Date: Mon, 9 Nov 2026 09:19:07 +0000 Subject: [PATCH 4/4] [CIR] Test bool aggregate constant storage from C++ Add an AMDGPU C++ test for a local record containing a bool array and a partially initialized bool array. Check their byte-sized storage and preserve the i1 representation of scalar bool results. Both unoptimized and optimized compilation exercise the constant-lowering failure. --- .../CIR/CodeGen/bool-aggregate-storage.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 clang/test/CIR/CodeGen/bool-aggregate-storage.cpp diff --git a/clang/test/CIR/CodeGen/bool-aggregate-storage.cpp b/clang/test/CIR/CodeGen/bool-aggregate-storage.cpp new file mode 100644 index 00000000000000..a9fbe8225e7fae --- /dev/null +++ b/clang/test/CIR/CodeGen/bool-aggregate-storage.cpp @@ -0,0 +1,31 @@ +// RUN: %clang_cc1 -std=c++17 -triple amdgpu-amd-amdhsa -fclangir \ +// RUN: -emit-llvm %s -o %t.ll +// RUN: FileCheck %s --input-file=%t.ll +// RUN: %clang_cc1 -std=c++17 -triple amdgpu-amd-amdhsa -fclangir \ +// RUN: -O3 -emit-llvm %s -o /dev/null + +struct BoolArray { + bool values[3]; +}; + +// CHECK-LABEL: define {{.+}}i1 @record() +// CHECK: store %struct.BoolArray { [3 x i8] c"\01\00\01" } +// CHECK: ret i1 +extern "C" bool record() { + BoolArray value{true, false, true}; + return value.values[0]; +} + +// CHECK-LABEL: define {{.+}}i1 @partial_array() +// CHECK: store [3 x i8] c"\01\00\00" +// CHECK: ret i1 +extern "C" bool partial_array() { + bool values[3] = {true}; + return values[2]; +} + +// CHECK-LABEL: define {{.+}}i1 @scalar_bool() +// CHECK: ret i1 +extern "C" bool scalar_bool() { + return true; +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
