https://github.com/erichkeane created https://github.com/llvm/llvm-project/pull/224379
A ptr-to-int conversion can't be converted to an APInt/APFloat, so this ends up not being able to produce a valid dense element attr. This patch makes us 'fall back' to the insert-value version of any potential dense-elements lowering if there is a global view as one of the values. >From 8389fb37c6e3cd6a26b87849ba36ccbeec598232 Mon Sep 17 00:00:00 2001 From: erichkeane <[email protected]> Date: Thu, 17 Sep 2026 10:16:28 -0700 Subject: [PATCH] [CIR] Give up when we hit a global-view in dense-element-attr lowering A ptr-to-int conversion can't be converted to an APInt/APFloat, so this ends up not being able to produce a valid dense element attr. This patch makes us 'fall back' to the insert-value version of any potential dense-elements lowering if there is a global view as one of the values. --- clang/lib/CIR/Lowering/LoweringHelpers.cpp | 20 ++++++++++++++----- .../test/CIR/CodeGen/global-address-to-int.c | 6 ++++++ .../const-array-bulk-lowering-fallbacks.cir | 10 ++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/clang/lib/CIR/Lowering/LoweringHelpers.cpp b/clang/lib/CIR/Lowering/LoweringHelpers.cpp index 1dad8beaa4bce..0b64a37cb6bf4 100644 --- a/clang/lib/CIR/Lowering/LoweringHelpers.cpp +++ b/clang/lib/CIR/Lowering/LoweringHelpers.cpp @@ -124,7 +124,7 @@ template <> mlir::APFloat getZeroInitFromType(mlir::Type ty) { /// \param dimIndex the current dimension we're processing /// \param currentIndex the current index in the values array template <typename AttrTy, typename StorageTy> -void convertToDenseElementsAttrImpl( +bool convertToDenseElementsAttrImpl( cir::ConstArrayAttr attr, llvm::SmallVectorImpl<StorageTy> &values, const llvm::SmallVectorImpl<int64_t> ¤tDims, int64_t dimIndex, int64_t currentIndex) { @@ -136,7 +136,7 @@ void convertToDenseElementsAttrImpl( } // Remaining slots are trailing zeros; values was zero-initialized. currentIndex += attr.getTrailingZerosNum(); - return; + return true; } } @@ -171,12 +171,18 @@ void convertToDenseElementsAttrImpl( continue; } + // A global view can be the result of pointer conversions, so we can't + // represent them as an APInt/APFloat. So give up if we see one. + if (auto global = mlir::dyn_cast<cir::GlobalViewAttr>(eltAttr)) + return false; + llvm_unreachable("unknown element in ConstArrayAttr"); } + return true; } template <typename AttrTy, typename StorageTy> -mlir::DenseElementsAttr convertToDenseElementsAttr( +std::optional<mlir::DenseElementsAttr> convertToDenseElementsAttr( cir::ConstArrayAttr attr, const llvm::SmallVectorImpl<int64_t> &dims, mlir::Type elementType, mlir::Type convertedElementType) { unsigned vectorSize = 1; @@ -184,8 +190,12 @@ mlir::DenseElementsAttr convertToDenseElementsAttr( vectorSize *= dim; auto values = llvm::SmallVector<StorageTy, 8>( vectorSize, getZeroInitFromType<StorageTy>(elementType)); - convertToDenseElementsAttrImpl<AttrTy>(attr, values, dims, /*currentDim=*/0, - /*initialIndex=*/0); + + if (!convertToDenseElementsAttrImpl<AttrTy>(attr, values, dims, + /*currentDim=*/0, + /*initialIndex=*/0)) + return std::nullopt; + return mlir::DenseElementsAttr::get( mlir::RankedTensorType::get(dims, convertedElementType), llvm::ArrayRef(values)); diff --git a/clang/test/CIR/CodeGen/global-address-to-int.c b/clang/test/CIR/CodeGen/global-address-to-int.c index 3b24941d4c9c4..d6e02a95d5001 100644 --- a/clang/test/CIR/CodeGen/global-address-to-int.c +++ b/clang/test/CIR/CodeGen/global-address-to-int.c @@ -20,3 +20,9 @@ unsigned long garr2 = (unsigned long)&arr[2]; unsigned long gf = (unsigned long)&f; // CIR: cir.global external @gf = #cir.global_view<@f> : !u64i // LLVM: @gf = global i64 ptrtoint (ptr @f to i64), align 8 + +long arr_with_addr[] = { 1L, (long)&x }; +// CIR: cir.global external @arr_with_addr = +// CIR-SAME: #cir.const_array<[#cir.int<1> : !s64i, #cir.global_view<@x> : !s64i]> +// CIR-SAME: : !cir.array<!s64i x 2> +// LLVM: @arr_with_addr = global [2 x i64] [i64 1, i64 ptrtoint (ptr @x to i64)] 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 0b4a09d179d44..d321ab39e1c3b 100644 --- a/clang/test/CIR/Lowering/const-array-bulk-lowering-fallbacks.cir +++ b/clang/test/CIR/Lowering/const-array-bulk-lowering-fallbacks.cir @@ -1,6 +1,7 @@ // RUN: cir-opt %s --cir-to-llvm -o - | FileCheck %s !s32i = !cir.int<s, 32> +!s64i = !cir.int<s, 64> !s8i = !cir.int<s, 8> !u16i = !cir.int<u, 16> !bi33 = !cir.int<s, 33, bitint> @@ -21,6 +22,11 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { #cir.const_array<[#cir.global_view<@as1> : !cir.ptr<!s32i>]> : !cir.array<!cir.ptr<!s32i> x 1> + cir.global external @int_arr_with_globalview = #cir.const_array<[ + #cir.int<1> : !s64i, + #cir.global_view<@as1> : !s64i + ]> : !cir.array<!s64i x 2> + cir.global external @matrix = #cir.const_array<[ #cir.const_array<[#cir.int<1> : !s32i, #cir.int<2> : !s32i]> : !cir.array<!s32i x 2>, #cir.const_array<[#cir.int<3> : !s32i, #cir.int<4> : !s32i]> : !cir.array<!s32i x 2> @@ -73,6 +79,10 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { // CHECK-LABEL: llvm.mlir.global external @as_mismatch // CHECK: llvm.insertvalue +// CHECK-LABEL: llvm.mlir.global external @int_arr_with_globalview +// CHECK: llvm.ptrtoint +// CHECK: llvm.insertvalue + // CHECK-LABEL: llvm.mlir.global external @matrix( // CHECK-SAME: dense< _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
