[Mlir-commits] [mlir] [mlir][vector]: Extend convertIntegerAttr to handle float-to-integer conversion (PR #159627)
Yang Bai
llvmlistbot at llvm.org
Fri Sep 19 00:12:27 PDT 2025
================
@@ -396,15 +396,35 @@ std::optional<int64_t> vector::getConstantVscaleMultiplier(Value value) {
return {};
}
-/// Converts an IntegerAttr to have the specified type if needed.
-/// This handles cases where constant attributes have a different type than the
-/// target element type. If the input attribute is not an IntegerAttr or already
-/// has the correct type, returns it unchanged.
-static Attribute convertIntegerAttr(Attribute attr, Type expectedType) {
- if (auto intAttr = mlir::dyn_cast<IntegerAttr>(attr)) {
- if (intAttr.getType() != expectedType)
- return IntegerAttr::get(expectedType, intAttr.getInt());
+/// Converts numeric attributes to the expected type. Supports
+/// integer-to-integer and float-to-integer conversions. Returns the original
+/// attribute if no conversion is needed or supported.
+static Attribute convertNumericAttr(Attribute attr, Type expectedType) {
+ // Integer-to-integer conversion
+ if (auto intAttr = dyn_cast<IntegerAttr>(attr)) {
+ if (auto intType = dyn_cast<IntegerType>(expectedType)) {
+ if (intAttr.getType() != expectedType)
+ return IntegerAttr::get(expectedType, intAttr.getInt());
+ }
+ return attr;
}
+
+ // Float-to-integer conversion
+ if (auto floatAttr = dyn_cast<FloatAttr>(attr)) {
+ auto intType = dyn_cast<IntegerType>(expectedType);
+ if (!intType)
+ return attr;
+
+ APFloat floatVal = floatAttr.getValue();
+ APSInt intVal(intType.getWidth(), intType.isUnsigned());
+ bool isExact = false;
+ [[maybe_unused]] APFloat::opStatus status =
+ floatVal.convertToInteger(intVal, APFloat::rmTowardZero, &isExact);
----------------
yangtetris wrote:
> We want to keep the exact original bits but bitcast them to i8
+1. I think it should be something like `floatAttr.getValue().bitcastToAPInt()`.
https://github.com/llvm/llvm-project/pull/159627
More information about the Mlir-commits
mailing list