-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[mlir][vector]: Extend convertIntegerAttr to handle float-to-integer conversion #159627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mlir][vector]: Extend convertIntegerAttr to handle float-to-integer conversion #159627
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-vector Author: Jhalak Patel (jhalakpatel) ChangesIssue: #159613 Full diff: https://github.com/llvm/llvm-project/pull/159627.diff 3 Files Affected:
diff --git a/float_attr.md b/float_attr.md
new file mode 100644
index 0000000000000..13b80fc25f1bc
--- /dev/null
+++ b/float_attr.md
@@ -0,0 +1,27 @@
+
+
+Looks like integer to float is not possible for i8 -> float8
+
+# | /home/jhalakp/dev/llvm-project/mlir/test/Dialect/Vector/float_attr.mlir:5:10: error: 'llvm.mlir.constant' op result #0 must be LLVM dialect-compatible type, but got 'f8E4M3FN'
+# | %cst = llvm.mlir.constant(42 : i8) : f8E4M3FN
+# | ^
+# | /home/jhalakp/dev/llvm-project/mlir/test/Dialect/Vector/float_attr.mlir:5:10: note: see current operation: %0 = "llvm.mlir.constant"() <{value = 42 : i8}> : () -> f8E4M3FN
+
+
+Even i8 -> f32 is not supported
+
+# .---command stderr------------
+# | /home/jhalakp/dev/llvm-project/mlir/test/Dialect/Vector/float_attr.mlir:5:10: error: 'llvm.mlir.constant' op expected integer type
+# | %cst = llvm.mlir.constant(42 : i8) : f32
+# | ^
+# | /home/jhalakp/dev/llvm-project/mlir/test/Dialect/Vector/float_attr.mlir:5:10: note: see current operation: %0 = "llvm.mlir.constant"() <{value = 42 : i8}> : () -> f32
+# `-----------------------------
+
+
+Even f16 -> bf16 is not supported
+# | /home/jhalakp/dev/llvm-project/mlir/test/Dialect/Vector/float_attr.mlir:5:10: error: 'llvm.mlir.constant' op attribute and type have different float semantics
+# | %cst = llvm.mlir.constant(1.5 : f16) : bf16
+# | ^
+# | /home/jhalakp/dev/llvm-project/mlir/test/Dialect/Vector/float_attr.mlir:5:10: note: see current operation: %0 = "llvm.mlir.constant"() <{value = 1.500000e+00 : f16}> : () -> bf16
+
+
diff --git a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
index 8d6e263934fb4..a9aebff75243f 100644
--- a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
+++ b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
@@ -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);
+ assert(status == APFloat::opOK && "float-to-integer conversion failed");
+ assert(isExact && "float-to-integer conversion must be exact");
+ return IntegerAttr::get(expectedType, intVal);
}
+
return attr;
}
@@ -2475,7 +2495,7 @@ static OpFoldResult foldFromElementsToConstant(FromElementsOp fromElementsOp,
// Constant attributes might have a different type than the return type.
// Convert them before creating the dense elements attribute.
auto convertedElements = llvm::map_to_vector(elements, [&](Attribute attr) {
- return convertIntegerAttr(attr, destEltType);
+ return convertNumericAttr(attr, destEltType);
});
return DenseElementsAttr::get(destVecType, convertedElements);
@@ -3497,13 +3517,13 @@ foldDenseElementsAttrDestInsertOp(InsertOp insertOp, Attribute srcAttr,
SmallVector<Attribute> insertedValues;
Type destEltType = destTy.getElementType();
- /// Converts the expected type to an IntegerAttr if there's
+ /// Converts the expected type to a numeric attribute if there's
/// a mismatch.
if (auto denseSource = llvm::dyn_cast<DenseElementsAttr>(srcAttr)) {
for (auto value : denseSource.getValues<Attribute>())
- insertedValues.push_back(convertIntegerAttr(value, destEltType));
+ insertedValues.push_back(convertNumericAttr(value, destEltType));
} else {
- insertedValues.push_back(convertIntegerAttr(srcAttr, destEltType));
+ insertedValues.push_back(convertNumericAttr(srcAttr, destEltType));
}
auto allValues = llvm::to_vector(denseDst.getValues<Attribute>());
diff --git a/mlir/test/Dialect/Vector/canonicalize.mlir b/mlir/test/Dialect/Vector/canonicalize.mlir
index 05c88b8abfbb0..dcab7088c631a 100644
--- a/mlir/test/Dialect/Vector/canonicalize.mlir
+++ b/mlir/test/Dialect/Vector/canonicalize.mlir
@@ -3375,6 +3375,28 @@ func.func @negative_from_elements_to_constant() -> vector<1x!llvm.ptr> {
return %b : vector<1x!llvm.ptr>
}
+// -----
+
+// CHECK-LABEL: func @from_elements_float8_to_i8_conversion(
+// CHECK-NEXT: %[[CST:.*]] = arith.constant dense<0> : vector<1xi8>
+// CHECK-NEXT: return %[[CST]] : vector<1xi8>
+func.func @from_elements_float8_to_i8_conversion() -> vector<1xi8> {
+ %cst = llvm.mlir.constant(0.0 : f8E4M3FN) : i8
+ %v = vector.from_elements %cst : vector<1xi8>
+ return %v : vector<1xi8>
+}
+
+// -----
+
+// CHECK-LABEL: func @from_elements_i1_to_i8_conversion(
+// CHECK-NEXT: %[[CST:.*]] = arith.constant dense<0> : vector<1xi8>
+// CHECK-NEXT: return %[[CST]] : vector<1xi8>
+func.func @from_elements_i1_to_i8_conversion() -> vector<1xi8> {
+ %cst = llvm.mlir.constant(0: i1) : i8
+ %v = vector.from_elements %cst : vector<1xi8>
+ return %v : vector<1xi8>
+}
+
// +---------------------------------------------------------------------------
// End of Tests for foldFromElementsToConstant
// +---------------------------------------------------------------------------
|
f12ed01
to
4c23fa6
Compare
4c23fa6
to
c3b3918
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Just a minor comment. Thanks for fixing it!
c3b3918
to
7c6e603
Compare
@jhalakpatel Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/116/builds/18846 Here is the relevant piece of the build log for the reference
|
…conversion (llvm#159627) Fixes llvm#159613 `vector.from_elements` crashes when processing float attributes with integer result types (e.g., `llvm.mlir.constant(0.0 : f8E4M3FN) : i8` from arith-to-llvm lowering): ``` Assertion `newType.getElementType() == curType.getElementType() && "expected the same element type"' failed. ``` ## Implementation - Rename `convertIntegerAttr` → `convertNumericAttr` - Add float-to-integer conversion using `APFloat::convertToInteger()` with exactness assertion - Preserve existing integer-to-integer conversion behavior Only implements conversions that pass LLVM verification. Other patterns (int→float, float→float, bool→int) are rejected by LLVM verifier before reaching this code, as documented in the attached verification failures.
Fixes #159613
vector.from_elements
crashes when processing float attributes with integer result types (e.g.,llvm.mlir.constant(0.0 : f8E4M3FN) : i8
from arith-to-llvm lowering):Implementation
convertIntegerAttr
→convertNumericAttr
APFloat::convertToInteger()
with exactness assertionOnly implements conversions that pass LLVM verification. Other patterns (int→float, float→float, bool→int) are rejected by LLVM verifier before reaching this code, as documented in the attached verification failures.