Author: Akimasa Watanuki
Date: 2026-11-18T09:52:33-06:00
New Revision: b84124be5ffb2a5cca3d8bd29d5cdb314a99a500

URL: 
https://github.com/llvm/llvm-project/commit/b84124be5ffb2a5cca3d8bd29d5cdb314a99a500
DIFF: 
https://github.com/llvm/llvm-project/commit/b84124be5ffb2a5cca3d8bd29d5cdb314a99a500.diff

LOG: [CIR] Diagnose failed initializer type adjustments (#234348)

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

Added: 
    

Modified: 
    clang/include/clang/CIR/LoweringHelpers.h
    clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
    clang/lib/CIR/Lowering/LoweringHelpers.cpp
    clang/test/CIR/Lowering/unlowered-address-spaces.cir
    clang/test/CIR/Lowering/unsupported-initializer-types.cir

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/CIR/LoweringHelpers.h 
b/clang/include/clang/CIR/LoweringHelpers.h
index 6451c72671de4..458f224243bd4 100644
--- a/clang/include/clang/CIR/LoweringHelpers.h
+++ b/clang/include/clang/CIR/LoweringHelpers.h
@@ -64,10 +64,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.
@@ -75,7 +77,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 51a904e8dea08..e5dac7a4452a8 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -970,10 +970,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
@@ -1046,10 +1049,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;
@@ -3470,11 +3476,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
@@ -3608,11 +3617,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 b647ce475d7a7..249f1b84bb696 100644
--- a/clang/lib/CIR/Lowering/LoweringHelpers.cpp
+++ b/clang/lib/CIR/Lowering/LoweringHelpers.cpp
@@ -686,7 +686,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 =
@@ -704,8 +705,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);
@@ -757,9 +758,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);
@@ -798,7 +799,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());
 
@@ -810,7 +812,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 {};
 
@@ -848,7 +851,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)
@@ -860,8 +864,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;
@@ -905,15 +909,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.
@@ -930,19 +939,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 ded4b5ca6b20e..8a94b7b0457a4 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 43b34f3994b93..850bb8c7b486f 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

Reply via email to