https://github.com/Men-cotton updated https://github.com/llvm/llvm-project/pull/234348
>From d6c3aad0303fe5d259c0cbe02c259d0ca3774167 Mon Sep 17 00:00:00 2001 From: mencotton <[email protected]> Date: Tue, 20 Oct 2026 16:58:28 -0500 Subject: [PATCH] [CIR] Diagnose failed initializer type adjustments Report the CIR initializer type whose conversion fails during recursive layout adjustment, including active union members nested in structs and arrays. Carry that type to the lowering operation so diagnostics identify the unsupported initializer type even when the enclosing storage type converts successfully. Extend the initializer and language address space tests to check the type-specific diagnostics while retaining coverage for inactive unsupported members. Assisted-by: Codex / GPT-6 --- clang/include/clang/CIR/LoweringHelpers.h | 9 ++-- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 28 +++++++---- clang/lib/CIR/Lowering/LoweringHelpers.cpp | 47 ++++++++++++------- .../CIR/Lowering/unlowered-address-spaces.cir | 4 ++ .../unsupported-initializer-types.cir | 6 ++- 5 files changed, 63 insertions(+), 31 deletions(-) diff --git a/clang/include/clang/CIR/LoweringHelpers.h b/clang/include/clang/CIR/LoweringHelpers.h index c67153b28a512a..2870567c3aeaaa 100644 --- a/clang/include/clang/CIR/LoweringHelpers.h +++ b/clang/include/clang/CIR/LoweringHelpers.h @@ -71,10 +71,12 @@ mlir::Attribute spellOutSplitTrailingZeros(mlir::Attribute attr); /// source of truth for the shape of a lowered record constant; the /// value-producing paths (the insertvalue visitor and lowerConstRecordAttr) /// conform to it. Returns null if the input type or an initializer type cannot -/// be lowered. +/// be lowered. If provided, \p failedType receives the CIR initializer type +/// whose conversion failed, including in nested aggregates. mlir::Type adjustGlobalTypeForInit(mlir::Type llvmType, mlir::Attribute init, const mlir::TypeConverter &converter, - const mlir::DataLayout &dataLayout); + const mlir::DataLayout &dataLayout, + mlir::Type *failedType = nullptr); // A version of adjustGlobalTypeForInit which records where additional padding // was added in the middle, so we can properly adjust field indexes. @@ -82,7 +84,8 @@ mlir::Type adjustGlobalTypeForInit(mlir::Type llvmType, mlir::Attribute init, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, - llvm::SmallVectorImpl<unsigned> &paddingAddedIndexes); + llvm::SmallVectorImpl<unsigned> &paddingAddedIndexes, + mlir::Type *failedType = nullptr); mlir::Value getConstAPInt(mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ, const llvm::APInt &val); diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 5744afee3d56e2..d2e797937f0d4f 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -937,10 +937,13 @@ mlir::Value CIRAttrToValue::visitCirAttr(cir::BlockAddrDiffAttr blockAddrDiff) { mlir::Value CIRAttrToValue::visitCirAttr(cir::ConstArrayAttr attr) { mlir::Type convertedTy = converter->convertType(attr.getType()); mlir::DataLayout dataLayout(parentModule); - mlir::Type llvmTy = - adjustGlobalTypeForInit(convertedTy, attr, *converter, dataLayout); - if (!llvmTy) + mlir::Type failedType = attr.getType(); + mlir::Type llvmTy = adjustGlobalTypeForInit(convertedTy, attr, *converter, + dataLayout, &failedType); + if (!llvmTy) { + emitTypeConversionError(parentLoc, failedType); return {}; + } mlir::Value result; // When the array can be represented as a single constant attribute, emit one @@ -1013,10 +1016,13 @@ mlir::Value CIRAttrToValue::visitCirAttr(cir::ConstRecordAttr constRecord) { mlir::Type llvmTy = converter->convertType(constRecord.getType()); mlir::DataLayout dataLayout(parentModule); llvm::SmallVector<unsigned> paddingAddedIndexes; + mlir::Type failedType = constRecord.getType(); llvmTy = adjustGlobalTypeForInit(llvmTy, constRecord, *converter, dataLayout, - paddingAddedIndexes); - if (!llvmTy) + paddingAddedIndexes, &failedType); + if (!llvmTy) { + emitTypeConversionError(parentLoc, failedType); return {}; + } mlir::Value result = mlir::LLVM::UndefOp::create(rewriter, parentLoc, llvmTy); uint64_t insertIdx = 0; @@ -3463,11 +3469,14 @@ CIRToLLVMGlobalOpLowering::setupRegionInitializedLLVMGlobalOp( // Keep the global's type in sync with the value built by CIRAttrToValue: a // flexible array member initializer requires an oversized anonymous struct. + mlir::Type failedType = op.getSymType(); if (std::optional<mlir::Attribute> init = op.getInitialValue()) llvmType = adjustGlobalTypeForInit(llvmType, *init, *getTypeConverter(), - dataLayout); - if (!llvmType) + dataLayout, &failedType); + if (!llvmType) { + emitTypeConversionError(op, failedType); return {}; + } // FIXME: These default values are placeholders until the the equivalent // attributes are available on cir.global ops. This duplicates code @@ -3601,11 +3610,12 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite( // A flexible array member initializer makes the constant larger than the // record's declared type, so the global must use an oversized anonymous // struct instead. + mlir::Type failedType = cirSymType; if (init.has_value()) llvmType = adjustGlobalTypeForInit(llvmType, *init, *getTypeConverter(), - dataLayout); + dataLayout, &failedType); if (!llvmType) - return mlir::failure(); + return emitTypeConversionError(op, failedType); // FIXME: These default values are placeholders until the the equivalent // attributes are available on cir.global ops. diff --git a/clang/lib/CIR/Lowering/LoweringHelpers.cpp b/clang/lib/CIR/Lowering/LoweringHelpers.cpp index 6734b4e9a32e44..9895ddcf8f6119 100644 --- a/clang/lib/CIR/Lowering/LoweringHelpers.cpp +++ b/clang/lib/CIR/Lowering/LoweringHelpers.cpp @@ -674,7 +674,8 @@ static bool shouldPackFAMStruct(const mlir::DataLayout &dataLayout, static mlir::Type adjustGlobalStructTypeForInit( mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, - llvm::SmallVectorImpl<unsigned> &paddingAddedIndexes) { + llvm::SmallVectorImpl<unsigned> &paddingAddedIndexes, + mlir::Type *failedType) { assert(paddingAddedIndexes.empty() && "Not for accumulation, just single depth"); llvm::ArrayRef<mlir::Attribute> initMembers = @@ -692,8 +693,8 @@ static mlir::Type adjustGlobalStructTypeForInit( for (auto [idx, member] : llvm::enumerate(initMembers)) { if (idx >= origBody.size()) break; - mlir::Type adjusted = - adjustGlobalTypeForInit(origBody[idx], member, converter, dataLayout); + mlir::Type adjusted = adjustGlobalTypeForInit( + origBody[idx], member, converter, dataLayout, failedType); if (!adjusted) return {}; unsigned adjustedAlign = dataLayout.getTypeABIAlignment(adjusted); @@ -745,9 +746,9 @@ static mlir::Type adjustGlobalStructTypeForInit( mlir::Type lastInitType = mlir::cast<mlir::TypedAttr>(initMembers.back()).getType(); if (mlir::cast<cir::ArrayType>(lastInitType).getSize() != 0) { - newBody.back() = - adjustGlobalTypeForInit(converter.convertType(lastInitType), - initMembers.back(), converter, dataLayout); + newBody.back() = adjustGlobalTypeForInit( + converter.convertType(lastInitType), initMembers.back(), converter, + dataLayout, failedType); if (!newBody.back()) return {}; packed = packed || shouldPackFAMStruct(dataLayout, newBody); @@ -786,7 +787,8 @@ static mlir::Type adjustGlobalStructTypeForInit( // size, mirroring classic codegen. static mlir::Type adjustGlobalUnionTypeForInit( mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord, - const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout) { + const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, + mlir::Type *failedType) { auto unionTy = mlir::cast<cir::UnionType>(constRecord.getType()); @@ -798,7 +800,8 @@ static mlir::Type adjustGlobalUnionTypeForInit( // The active member may itself need adjusting (e.g. it is a nested union, or // a struct containing one), so recurse before using its type below. - memberTy = adjustGlobalTypeForInit(memberTy, member, converter, dataLayout); + memberTy = adjustGlobalTypeForInit(memberTy, member, converter, dataLayout, + failedType); if (!memberTy) return {}; @@ -836,7 +839,8 @@ static mlir::Type adjustGlobalUnionTypeForInit( // <{ [M x T], [N x T] }> static mlir::Type adjustGlobalArrayTypeForInit( mlir::LLVM::LLVMArrayType arrayTy, cir::ConstArrayAttr arrayInit, - const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout) { + const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, + mlir::Type *failedType) { auto elts = mlir::dyn_cast<mlir::ArrayAttr>(arrayInit.getElts()); if (!elts) @@ -848,8 +852,8 @@ static mlir::Type adjustGlobalArrayTypeForInit( bool changed = elts.size() > arrayTy.getNumElements(); for (mlir::Attribute elt : elts) { - mlir::Type adjusted = - adjustGlobalTypeForInit(origEltTy, elt, converter, dataLayout); + mlir::Type adjusted = adjustGlobalTypeForInit(origEltTy, elt, converter, + dataLayout, failedType); if (!adjusted) return {}; changed |= adjusted != origEltTy; @@ -893,15 +897,20 @@ mlir::Type adjustGlobalTypeForInit(mlir::Type llvmType, mlir::Attribute init, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, - llvm::SmallVectorImpl<unsigned> &paddingAddedIndexes) { - if (!llvmType) + llvm::SmallVectorImpl<unsigned> &paddingAddedIndexes, + mlir::Type *failedType) { + if (!llvmType) { + if (failedType) + if (auto typedInit = mlir::dyn_cast_if_present<mlir::TypedAttr>(init)) + *failedType = typedInit.getType(); return {}; + } if (auto arrayInit = mlir::dyn_cast_if_present<cir::ConstArrayAttr>(init)) { auto arrayTy = mlir::dyn_cast<mlir::LLVM::LLVMArrayType>(llvmType); if (!arrayTy) return llvmType; return adjustGlobalArrayTypeForInit(arrayTy, arrayInit, converter, - dataLayout); + dataLayout, failedType); } // Conversions for both only happen if we have a record init. @@ -918,19 +927,21 @@ adjustGlobalTypeForInit(mlir::Type llvmType, mlir::Attribute init, // Structs can have a flexible array member, adjust that. if (mlir::isa<cir::StructType>(constRecord.getType())) return adjustGlobalStructTypeForInit(structTy, constRecord, converter, - dataLayout, paddingAddedIndexes); + dataLayout, paddingAddedIndexes, + failedType); if (mlir::isa<cir::UnionType>(constRecord.getType())) return adjustGlobalUnionTypeForInit(structTy, constRecord, converter, - dataLayout); + dataLayout, failedType); return llvmType; } mlir::Type adjustGlobalTypeForInit(mlir::Type llvmType, mlir::Attribute init, const mlir::TypeConverter &converter, - const mlir::DataLayout &dataLayout) { + const mlir::DataLayout &dataLayout, + mlir::Type *failedType) { llvm::SmallVector<unsigned> ignoredAddedIndexes; return adjustGlobalTypeForInit(llvmType, init, converter, dataLayout, - ignoredAddedIndexes); + ignoredAddedIndexes, failedType); } std::optional<mlir::Attribute> diff --git a/clang/test/CIR/Lowering/unlowered-address-spaces.cir b/clang/test/CIR/Lowering/unlowered-address-spaces.cir index c58d1fe26c4cf1..d837129cb7d65f 100644 --- a/clang/test/CIR/Lowering/unlowered-address-spaces.cir +++ b/clang/test/CIR/Lowering/unlowered-address-spaces.cir @@ -38,6 +38,7 @@ module attributes {cir.triple = "spirv64-unknown-unknown"} { module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { // The union's integer storage converts, but its active initializer does not. + // expected-error @below {{ClangIR lowering NYI: cannot convert '!cir.ptr<!cir.void, lang_address_space(offload_global)>'}} // expected-error @below {{failed to legalize operation 'cir.global'}} cir.global external @union_active_initializer_language_address_space = #cir.const_record<{#cir.ptr<null> : !langptr}> : !rec_U } @@ -286,6 +287,7 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { cir.func @constant_union_active_initializer_language_address_space() { + // expected-error @below {{ClangIR lowering NYI: cannot convert '!cir.ptr<!cir.void, lang_address_space(offload_global)>'}} // expected-error @below {{failed to legalize operation 'cir.const'}} %0 = cir.const #cir.const_record<{#cir.ptr<null> : !langptr}> : !rec_U cir.return @@ -303,6 +305,7 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { // Propagate adjustment failure through both the enclosing union and struct. cir.func @constant_nested_union_initializer_language_address_space() { + // expected-error @below {{ClangIR lowering NYI: cannot convert '!cir.ptr<!cir.void, lang_address_space(offload_global)>'}} // expected-error @below {{failed to legalize operation 'cir.const'}} %0 = cir.const #cir.const_record<{#cir.const_record<{#cir.const_record<{#cir.ptr<null> : !langptr}> : !rec_U}> : !rec_Outer}> : !rec_S cir.return @@ -318,6 +321,7 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { cir.func @constant_union_array_initializer_language_address_space() { + // expected-error @below {{ClangIR lowering NYI: cannot convert '!cir.ptr<!cir.void, lang_address_space(offload_global)>'}} // expected-error @below {{failed to legalize operation 'cir.const'}} %0 = cir.const #cir.const_array<[#cir.const_record<{#cir.ptr<null> : !langptr}> : !rec_U]> : !array cir.return diff --git a/clang/test/CIR/Lowering/unsupported-initializer-types.cir b/clang/test/CIR/Lowering/unsupported-initializer-types.cir index 43b34f3994b934..850bb8c7b486ff 100644 --- a/clang/test/CIR/Lowering/unsupported-initializer-types.cir +++ b/clang/test/CIR/Lowering/unsupported-initializer-types.cir @@ -1,13 +1,14 @@ // RUN: cir-opt %s --split-input-file --cir-to-llvm -verify-diagnostics | FileCheck %s // Vector-of-_BitInt conversion is unsupported; propagate its failure to the -// enclosing operation instead of constructing invalid LLVM operations. +// enclosing operation and diagnose the active initializer's type. !vec = !cir.vector<4 x !cir.int<s, 7, bitint>> !union = !cir.union<"U" {data !cir.int<s, 128>, data !vec}> module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { // The union's integer storage converts, but its active initializer does not. + // expected-error @below {{ClangIR lowering NYI: cannot convert '!cir.vector<4 x !cir.int<s, 7, bitint>>'}} // expected-error @below {{failed to legalize operation 'cir.global'}} cir.global external @global_union = #cir.const_record<{#cir.zero : !vec}> : !union } @@ -19,6 +20,7 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { cir.func @constant_union() { + // expected-error @below {{ClangIR lowering NYI: cannot convert '!cir.vector<4 x !cir.int<s, 7, bitint>>'}} // expected-error @below {{failed to legalize operation 'cir.const'}} %0 = cir.const #cir.const_record<{#cir.zero : !vec}> : !union cir.return @@ -35,6 +37,7 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { // Propagate adjustment failure through both the enclosing union and struct. cir.func @nested_union() { + // expected-error @below {{ClangIR lowering NYI: cannot convert '!cir.vector<4 x !cir.int<s, 7, bitint>>'}} // expected-error @below {{failed to legalize operation 'cir.const'}} %0 = cir.const #cir.const_record<{#cir.const_record<{#cir.const_record<{#cir.zero : !vec}> : !union}> : !outer}> : !record cir.return @@ -48,6 +51,7 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { module attributes {cir.triple = "x86_64-unknown-linux-gnu"} { cir.func @array_union() { + // expected-error @below {{ClangIR lowering NYI: cannot convert '!cir.vector<4 x !cir.int<s, 7, bitint>>'}} // expected-error @below {{failed to legalize operation 'cir.const'}} %0 = cir.const #cir.const_array<[#cir.const_record<{#cir.zero : !vec}> : !union]> : !cir.array<!union x 1> cir.return _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
