https://github.com/koparasy created https://github.com/llvm/llvm-project/pull/244068
During the `-emit-cir=pristine` exploration I found some parsing errors we have: - **`cir.switch.flat`**: An empty case list (`[ ]`) was parsed without setting the required `caseValues` attribute, so it failed with requires attribute `caseValues`. - **`cir.switch.flat`**: Case values were printed as signed and parsed as `int64_t`. For an unsigned condition this breaks: a `u1` case `1` printed as `-1` and asserted in `APInt` on parse, and `u64` values above `INT64_MAX` could not be read. - **`cir.dyn_cast`**: Printed its optional `#cir.dyn_cast_info` attribute last, right before the op's location. MLIR's `parseOptionalAttribute` accepts `loc(...)` as an attribute, so for casts without the info attribute the parser consumed the trailing location instead (`undefined symbol alias id` for aliased locations). The attribute is now printed after the cast kind. >From 222509028f9e550f8ce2951786fc73db59257df0 Mon Sep 17 00:00:00 2001 From: Konstantinos Parasyris <[email protected]> Date: Wed, 18 Nov 2026 16:51:16 -0800 Subject: [PATCH 1/2] [CIR] Fix cir.switch.flat print/parse round-trip An empty case list parsed without the required caseValues attribute, and case values were printed signed and parsed as int64_t, so a u1 case 1 printed as -1 and asserted on parse. Print with the condition type's signedness and parse into an APInt checked against its width. --- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 24 ++++++++++---- clang/test/CIR/IR/invalid-switch-flat.cir | 38 ++++++++++++++++++++++ clang/test/CIR/IR/switch-flat.cir | 39 +++++++++++++++++++++++ 3 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 clang/test/CIR/IR/invalid-switch-flat.cir diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 2a496863cd971f..9b1a7829da8378 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -2534,16 +2534,25 @@ ParseResult cir::parseSwitchFlatOpCases( SmallVectorImpl<llvm::SmallVector<Type>> &caseOperandTypes) { if (failed(parser.parseLSquare())) return failure(); - if (succeeded(parser.parseOptionalRSquare())) + if (succeeded(parser.parseOptionalRSquare())) { + caseValues = ArrayAttr::get(flagType.getContext(), {}); return success(); + } llvm::SmallVector<mlir::Attribute> values; + auto intType = mlir::cast<cir::IntType>(flagType); - auto parseCase = [&]() { - int64_t value = 0; + auto parseCase = [&]() -> ParseResult { + llvm::APInt value; + llvm::SMLoc loc = parser.getCurrentLocation(); if (failed(parser.parseInteger(value))) return failure(); - - values.push_back(cir::IntAttr::get(flagType, value)); + if (intType.isSigned() ? !value.isSignedIntN(intType.getWidth()) + : (value.isNegative() || + value.getActiveBits() > intType.getWidth())) + return parser.emitError(loc, "case value out of range for ") << flagType; + values.push_back(cir::IntAttr::get( + flagType, intType.isSigned() ? value.sextOrTrunc(intType.getWidth()) + : value.zextOrTrunc(intType.getWidth()))); Block *destination; llvm::SmallVector<OpAsmParser::UnresolvedOperand> operands; @@ -2576,7 +2585,7 @@ void cir::printSwitchFlatOpCases(OpAsmPrinter &p, cir::SwitchFlatOp op, const TypeRangeRange &caseOperandTypes) { p << '['; p.printNewline(); - if (!caseValues) { + if (!caseValues || caseValues.empty()) { p << ']'; return; } @@ -2587,7 +2596,8 @@ void cir::printSwitchFlatOpCases(OpAsmPrinter &p, cir::SwitchFlatOp op, [&](auto i) { p << " "; mlir::Attribute a = std::get<0>(i); - p << mlir::cast<cir::IntAttr>(a).getValue(); + mlir::cast<cir::IntAttr>(a).getValue().print( + p.getStream(), mlir::cast<cir::IntType>(flagType).isSigned()); p << ": "; p.printSuccessorAndUseList(std::get<1>(i), caseOperands[index++]); }, diff --git a/clang/test/CIR/IR/invalid-switch-flat.cir b/clang/test/CIR/IR/invalid-switch-flat.cir new file mode 100644 index 00000000000000..5fcdf4d5db1888 --- /dev/null +++ b/clang/test/CIR/IR/invalid-switch-flat.cir @@ -0,0 +1,38 @@ +// RUN: cir-opt %s -verify-diagnostics -split-input-file + +!u1i = !cir.int<u, 1> + +cir.func @u1_out_of_range(%arg0: !u1i) { + cir.switch.flat %arg0 : !u1i, ^bb1 [ + // expected-error@+1 {{case value out of range for '!cir.int<u, 1>'}} + 2: ^bb1 + ] + ^bb1: + cir.return +} + +// ----- + +!s8i = !cir.int<s, 8> + +cir.func @s8_out_of_range(%arg0: !s8i) { + cir.switch.flat %arg0 : !s8i, ^bb1 [ + // expected-error@+1 {{case value out of range for '!cir.int<s, 8>'}} + 128: ^bb1 + ] + ^bb1: + cir.return +} + +// ----- + +!u32i = !cir.int<u, 32> + +cir.func @unsigned_negative(%arg0: !u32i) { + cir.switch.flat %arg0 : !u32i, ^bb1 [ + // expected-error@+1 {{case value out of range for '!cir.int<u, 32>'}} + -1: ^bb1 + ] + ^bb1: + cir.return +} diff --git a/clang/test/CIR/IR/switch-flat.cir b/clang/test/CIR/IR/switch-flat.cir index 61a9c91c4cea23..47f6b8e56c5fda 100644 --- a/clang/test/CIR/IR/switch-flat.cir +++ b/clang/test/CIR/IR/switch-flat.cir @@ -66,3 +66,42 @@ cir.func @switchWithOperands(%arg0: !s32i, %arg1: !s32i, %arg2: !s32i) { // CHECK-NEXT: cir.br ^bb3 // CHECK-NEXT: ^bb3: // CHECK-NEXT: cir.return + +cir.func @FlatSwitchNoCases(%arg0: !s32i) { + cir.switch.flat %arg0 : !s32i, ^bb1 [ + ] + ^bb1: + cir.return +} + +// CHECK: cir.func @FlatSwitchNoCases +// CHECK: cir.switch.flat %arg0 : !s32i, ^bb1 [ +// CHECK-NEXT: ] + +!u1i = !cir.int<u, 1> +!s8i = !cir.int<s, 8> +!u64i = !cir.int<u, 64> + +cir.func @FlatSwitchCaseValues(%a: !u1i, %b: !s8i, %c: !u64i) { + cir.switch.flat %a : !u1i, ^bb1 [ + 1: ^bb1 + ] + ^bb1: + cir.switch.flat %b : !s8i, ^bb2 [ + -128: ^bb2 + ] + ^bb2: + cir.switch.flat %c : !u64i, ^bb3 [ + 18446744073709551615: ^bb3 + ] + ^bb3: + cir.return +} + +// CHECK: cir.func @FlatSwitchCaseValues +// CHECK: cir.switch.flat %arg0 : {{.*}}, ^bb1 [ +// CHECK-NEXT: 1: ^bb1 +// CHECK: cir.switch.flat %arg1 : !s8i, ^bb2 [ +// CHECK-NEXT: -128: ^bb2 +// CHECK: cir.switch.flat %arg2 : !u64i, ^bb3 [ +// CHECK-NEXT: 18446744073709551615: ^bb3 >From 29a1b8f41a1c65f2d2e8196a58cf820991fb33af Mon Sep 17 00:00:00 2001 From: Konstantinos Parasyris <[email protected]> Date: Wed, 18 Nov 2026 16:54:54 -0800 Subject: [PATCH 2/2] [CIR] Fix cir.dyn_cast parsing when followed by a location The optional #cir.dyn_cast_info attribute was printed after the result type, so for casts without it the parser consumed the op's trailing loc(...) as the attribute. Print it after the cast kind instead. --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 10 ++++------ clang/test/CIR/Bytecode/global-view.cir | 4 ++-- clang/test/CIR/CodeGen/dynamic-cast.cpp | 8 ++++---- clang/test/CIR/IR/dynamic-cast-loc.cir | 17 +++++++++++++++++ clang/test/CIR/IR/dynamic-cast.cir | 8 ++++---- clang/test/CIR/IR/invalid-dyn-cast.cir | 4 ++-- 6 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 clang/test/CIR/IR/dynamic-cast-loc.cir diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 2a1600faaece8e..8c6419aad46cad 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -509,14 +509,13 @@ def CIR_DynamicCastOp : CIR_Op<"dyn_cast"> { ``` %0 = cir.dyn_cast ptr %p : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> - %1 = cir.dyn_cast ref %r : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> - #cir.dyn_cast_info< + %1 = cir.dyn_cast ref #cir.dyn_cast_info< srcRtti = #cir.global_view<@_ZTI4Base> : !cir.ptr<!u8i>, destRtti = #cir.global_view<@_ZTI7Derived> : !cir.ptr<!u8i>, runtimeFunc = @__dynamic_cast, badCastFunc = @__cxa_bad_cast, offsetHint = #cir.int<0> : !s64i - > + > %r : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> ``` }]; @@ -531,9 +530,8 @@ def CIR_DynamicCastOp : CIR_Op<"dyn_cast"> { ); let assemblyFormat = [{ - enum($kind) $src - `:` qualified(type($src)) `->` qualified(type($result)) - (qualified($info)^)? attr-dict + enum($kind) (qualified($info)^)? $src + `:` qualified(type($src)) `->` qualified(type($result)) attr-dict }]; let extraClassDeclaration = [{ diff --git a/clang/test/CIR/Bytecode/global-view.cir b/clang/test/CIR/Bytecode/global-view.cir index a7575847d68374..030a0111c35c76 100644 --- a/clang/test/CIR/Bytecode/global-view.cir +++ b/clang/test/CIR/Bytecode/global-view.cir @@ -50,7 +50,7 @@ module { cir.func private @__cxa_bad_cast() cir.func @cast(%arg0: !cir.ptr<!rec_Base>) -> !cir.ptr<!rec_Derived> { - %0 = cir.dyn_cast ptr %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> #info + %0 = cir.dyn_cast ptr #info %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> cir.return %0 : !cir.ptr<!rec_Derived> } } @@ -70,4 +70,4 @@ module { // CHECK: cir.global external @m_virtual = #cir.method<vtable_index = 1> : !cir.method<!cir.func<(!s32i)> in !rec_Other> // CHECK: cir.global external @m_virtual0 = #cir.method<vtable_index = 0> : !cir.method<!cir.func<(!s32i)> in !rec_Other> // CHECK: cir.global external @m_bigo = #cir.method<vtable_index = 18446744073709551615> : !cir.method<!cir.func<(!s32i)> in !rec_Other> -// CHECK: %0 = cir.dyn_cast ptr %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> #dyn_cast_info__ZTI4Base__ZTI7Derived +// CHECK: %0 = cir.dyn_cast ptr #dyn_cast_info__ZTI4Base__ZTI7Derived %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> diff --git a/clang/test/CIR/CodeGen/dynamic-cast.cpp b/clang/test/CIR/CodeGen/dynamic-cast.cpp index baae72a1f6c737..aeb70eea1639cc 100644 --- a/clang/test/CIR/CodeGen/dynamic-cast.cpp +++ b/clang/test/CIR/CodeGen/dynamic-cast.cpp @@ -22,7 +22,7 @@ Derived *ptr_cast(Base *b) { } // CIR-BEFORE: cir.func {{.*}} @_Z8ptr_castP4Base -// CIR-BEFORE: %{{.+}} = cir.dyn_cast ptr %{{.+}} : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> #dyn_cast_info__ZTI4Base__ZTI7Derived +// CIR-BEFORE: %{{.+}} = cir.dyn_cast ptr #dyn_cast_info__ZTI4Base__ZTI7Derived %{{.+}} : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> // CIR-BEFORE: } // CIR-AFTER: cir.func {{.*}} @_Z8ptr_castP4Base @@ -70,7 +70,7 @@ Derived &ref_cast(Base &b) { } // CIR-BEFORE: cir.func {{.*}} @_Z8ref_castR4Base -// CIR-BEFORE: %{{.+}} = cir.dyn_cast ref %{{.+}} : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> #dyn_cast_info__ZTI4Base__ZTI7Derived +// CIR-BEFORE: %{{.+}} = cir.dyn_cast ref #dyn_cast_info__ZTI4Base__ZTI7Derived %{{.+}} : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> // CIR-BEFORE: } // CIR-AFTER: cir.func {{.*}} @_Z8ref_castR4Base @@ -166,7 +166,7 @@ const Derived *const_ptr_cast(const Base *b) { } // CIR-BEFORE: cir.func {{.*}} @_Z14const_ptr_castPK4Base -// CIR-BEFORE: %{{.+}} = cir.dyn_cast ptr %{{.+}} : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> #dyn_cast_info__ZTI4Base__ZTI7Derived +// CIR-BEFORE: %{{.+}} = cir.dyn_cast ptr #dyn_cast_info__ZTI4Base__ZTI7Derived %{{.+}} : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> // CIR-BEFORE: } // LLVM: define {{.*}} @_Z14const_ptr_castPK4Base @@ -182,7 +182,7 @@ const Derived &const_ref_cast(const Base &b) { } // CIR-BEFORE: cir.func {{.*}} @_Z14const_ref_castRK4Base -// CIR-BEFORE: %{{.+}} = cir.dyn_cast ref %{{.+}} : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> #dyn_cast_info__ZTI4Base__ZTI7Derived +// CIR-BEFORE: %{{.+}} = cir.dyn_cast ref #dyn_cast_info__ZTI4Base__ZTI7Derived %{{.+}} : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> // CIR-BEFORE: } // LLVM: define {{.*}} ptr @_Z14const_ref_castRK4Base diff --git a/clang/test/CIR/IR/dynamic-cast-loc.cir b/clang/test/CIR/IR/dynamic-cast-loc.cir new file mode 100644 index 00000000000000..f2ac4584bd0afa --- /dev/null +++ b/clang/test/CIR/IR/dynamic-cast-loc.cir @@ -0,0 +1,17 @@ +// A cir.dyn_cast without #cir.dyn_cast_info followed by a location round-trips. + +// RUN: cir-opt --verify-roundtrip --mlir-print-debuginfo %s | FileCheck %s + +!rec_Base = !cir.struct<"Base" {data !cir.vptr}> +!void = !cir.void + +module { + cir.func @cast_to_void(%arg0: !cir.ptr<!rec_Base>) -> !cir.ptr<!void> { + %0 = cir.dyn_cast ptr %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!void> loc(#loc1) + cir.return %0 : !cir.ptr<!void> + } +} +#loc1 = loc("t.cpp":3:4) + +// CHECK: cir.dyn_cast ptr %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!void> loc(#[[LOC:loc[0-9]*]]) +// CHECK: #[[LOC]] = loc("t.cpp":3:4) diff --git a/clang/test/CIR/IR/dynamic-cast.cir b/clang/test/CIR/IR/dynamic-cast.cir index 731972f25c48de..672ab69879de53 100644 --- a/clang/test/CIR/IR/dynamic-cast.cir +++ b/clang/test/CIR/IR/dynamic-cast.cir @@ -18,22 +18,22 @@ module { cir.func private @__cxa_bad_cast() cir.func @test_ptr_cast(%arg0: !cir.ptr<!rec_Base>) -> !cir.ptr<!rec_Derived> { - %0 = cir.dyn_cast ptr %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> #dyn_cast_info__ZTI4Base__ZTI7Derived + %0 = cir.dyn_cast ptr #dyn_cast_info__ZTI4Base__ZTI7Derived %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> cir.return %0 : !cir.ptr<!rec_Derived> } // CHECK: cir.func @test_ptr_cast(%arg0: !cir.ptr<!rec_Base>) -> !cir.ptr<!rec_Derived> { - // CHECK: %0 = cir.dyn_cast ptr %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> #dyn_cast_info__ZTI4Base__ZTI7Derived + // CHECK: %0 = cir.dyn_cast ptr #dyn_cast_info__ZTI4Base__ZTI7Derived %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> // CHECK: cir.return %0 : !cir.ptr<!rec_Derived> // CHECK: } cir.func @test_ref_cast(%arg0: !cir.ptr<!rec_Base>) -> !cir.ptr<!rec_Derived> { - %0 = cir.dyn_cast ref %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> #dyn_cast_info__ZTI4Base__ZTI7Derived + %0 = cir.dyn_cast ref #dyn_cast_info__ZTI4Base__ZTI7Derived %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> cir.return %0 : !cir.ptr<!rec_Derived> } // CHECK: cir.func @test_ref_cast(%arg0: !cir.ptr<!rec_Base>) -> !cir.ptr<!rec_Derived> { - // CHECK: %0 = cir.dyn_cast ref %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> #dyn_cast_info__ZTI4Base__ZTI7Derived + // CHECK: %0 = cir.dyn_cast ref #dyn_cast_info__ZTI4Base__ZTI7Derived %arg0 : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived> // CHECK: cir.return %0 : !cir.ptr<!rec_Derived> // CHECK: } diff --git a/clang/test/CIR/IR/invalid-dyn-cast.cir b/clang/test/CIR/IR/invalid-dyn-cast.cir index c5c7df784d4fc7..4deb5410212092 100644 --- a/clang/test/CIR/IR/invalid-dyn-cast.cir +++ b/clang/test/CIR/IR/invalid-dyn-cast.cir @@ -16,7 +16,7 @@ module { cir.func private @__cxa_bad_cast() cir.func @test(%arg0 : !cir.ptr<!Base>) { // expected-error@+1 {{srcRtti must be an RTTI pointer}} - %0 = cir.dyn_cast ptr %arg0 : !cir.ptr<!Base> -> !cir.ptr<!Derived> #cir.dyn_cast_info<src_rtti = #cir.global_view<@_ZTI4Base> : !cir.ptr<!u32i>, dest_rtti = #cir.global_view<@_ZTI7Derived> : !cir.ptr<!u8i>, runtime_func = @__dynamic_cast, bad_cast_func = @__cxa_bad_cast, offset_hint = #cir.int<0> : !s64i> + %0 = cir.dyn_cast ptr #cir.dyn_cast_info<src_rtti = #cir.global_view<@_ZTI4Base> : !cir.ptr<!u32i>, dest_rtti = #cir.global_view<@_ZTI7Derived> : !cir.ptr<!u8i>, runtime_func = @__dynamic_cast, bad_cast_func = @__cxa_bad_cast, offset_hint = #cir.int<0> : !s64i> %arg0 : !cir.ptr<!Base> -> !cir.ptr<!Derived> } } @@ -38,6 +38,6 @@ module { cir.func private @__cxa_bad_cast() cir.func @test(%arg0 : !cir.ptr<!Base>) { // expected-error@+1 {{destRtti must be an RTTI pointer}} - %0 = cir.dyn_cast ptr %arg0 : !cir.ptr<!Base> -> !cir.ptr<!Derived> #cir.dyn_cast_info<src_rtti = #cir.global_view<@_ZTI4Base> : !cir.ptr<!u8i>, dest_rtti = #cir.global_view<@_ZTI7Derived> : !cir.ptr<!u32i>, runtime_func = @__dynamic_cast, bad_cast_func = @__cxa_bad_cast, offset_hint = #cir.int<0> : !s64i> + %0 = cir.dyn_cast ptr #cir.dyn_cast_info<src_rtti = #cir.global_view<@_ZTI4Base> : !cir.ptr<!u8i>, dest_rtti = #cir.global_view<@_ZTI7Derived> : !cir.ptr<!u32i>, runtime_func = @__dynamic_cast, bad_cast_func = @__cxa_bad_cast, offset_hint = #cir.int<0> : !s64i> %arg0 : !cir.ptr<!Base> -> !cir.ptr<!Derived> } } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
