[clang] [CIR] Fold constant int/bool casts and narrow a constant ternary's re… (PR #241645)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 06:48:39 PST 2026
================
@@ -1362,6 +1362,14 @@ OpFoldResult cir::CastOp::fold(FoldAdaptor adaptor) {
: srcValue.zextOrTrunc(dstIntTy.getWidth());
return cir::IntAttr::get(dstIntTy, newVal);
}
+ case cir::CastKind::int_to_bool:
+ if (auto intAttr = mlir::dyn_cast<cir::IntAttr>(src))
+ return cir::BoolAttr::get(getContext(), !intAttr.getValue().isZero());
+ break;
+ case cir::CastKind::bool_to_int:
+ if (auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(src))
+ return cir::IntAttr::get(getType(), boolAttr.getValue() ? 1 : 0);
----------------
xlauko wrote:
This asserts when the result type is !cir.int<s, 1>: the int64_t builder makes a signed APInt(1, 1), which is out of range. Building the bit unsigned avoids it and matches zext i1:
```
return cir::IntAttr::get(
getType(), llvm::APInt(mlir::cast<cir::IntType>(getType()).getWidth(),
boolAttr.getValue()));
```
Could you add a canonicalize.cir case casting #true to !cir.int<s, 1>?
https://github.com/llvm/llvm-project/pull/241645
More information about the cfe-commits
mailing list