Author: Arseniy Obolenskiy
Date: 2026-10-13T14:04:56+02:00
New Revision: 4b7c1006cde9fdd9e1937d32998cfc1e0f421c38

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

LOG: [CIR] Fix cir.select fold crash on a non-bool constant condition (#231428)

The fold cast any constant condition to BoolAttr and asserted on
`#cir.undef`

Fold only bool constants

Added: 
    

Modified: 
    clang/lib/CIR/Dialect/IR/CIRDialect.cpp
    clang/test/CIR/Transforms/select.cir

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index f02288dd92f69e..cd7adbf2b63aa8 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -3400,11 +3400,9 @@ void cir::TernaryOp::build(
 
//===----------------------------------------------------------------------===//
 
 OpFoldResult cir::SelectOp::fold(FoldAdaptor adaptor) {
-  mlir::Attribute condition = adaptor.getCondition();
-  if (condition) {
-    bool conditionValue = mlir::cast<cir::BoolAttr>(condition).getValue();
-    return conditionValue ? getTrueValue() : getFalseValue();
-  }
+  if (auto condition =
+          mlir::dyn_cast_if_present<cir::BoolAttr>(adaptor.getCondition()))
+    return condition.getValue() ? getTrueValue() : getFalseValue();
 
   // cir.select if %0 then x else x -> x
   mlir::Attribute trueValue = adaptor.getTrueValue();

diff  --git a/clang/test/CIR/Transforms/select.cir 
b/clang/test/CIR/Transforms/select.cir
index 413b6cc668a7c8..a2473b05457b38 100644
--- a/clang/test/CIR/Transforms/select.cir
+++ b/clang/test/CIR/Transforms/select.cir
@@ -24,6 +24,18 @@ module {
   // CHECK-NEXT:   cir.return %[[ARG1]] : !s32i
   // CHECK-NEXT: }
 
+  cir.func @no_fold_undef(%arg0 : !s32i, %arg1 : !s32i) -> !s32i {
+    %0 = cir.const #cir.undef : !cir.bool
+    %1 = cir.select if %0 then %arg0 else %arg1 : (!cir.bool, !s32i, !s32i) -> 
!s32i
+    cir.return %1 : !s32i
+  }
+
+  //      CHECK: cir.func{{.*}} @no_fold_undef(%[[ARG0:.+]]: !s32i, 
%[[ARG1:.+]]: !s32i) -> !s32i {
+  // CHECK-NEXT:   %[[#C:]] = cir.const #cir.undef : !cir.bool
+  // CHECK-NEXT:   %[[#R:]] = cir.select if %[[#C]] then %[[ARG0]] else 
%[[ARG1]]
+  // CHECK-NEXT:   cir.return %[[#R]] : !s32i
+  // CHECK-NEXT: }
+
   cir.func @fold_to_const(%arg0 : !cir.bool) -> !s32i {
     %0 = cir.const #cir.int<42> : !s32i
     %1 = cir.select if %arg0 then %0 else %0 : (!cir.bool, !s32i, !s32i) -> 
!s32i


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to