-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][Vector] Teach how to materialize UB constant to Vector #125596
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
Conversation
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-vector Author: Diego Caballero (dcaballe) ChangesThis PR adds support for UB constant materialization (i.e., generating Full diff: https://github.com/llvm/llvm-project/pull/125596.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
index 93f89eda2da5a6..190b643f1c7ae5 100644
--- a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
+++ b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
@@ -437,6 +437,9 @@ void VectorDialect::initialize() {
Operation *VectorDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
+ if (auto poisonAttr = dyn_cast<ub::PoisonAttrInterface>(value))
+ return builder.create<ub::PoisonOp>(loc, type, poisonAttr);
+
return arith::ConstantOp::materialize(builder, value, type, loc);
}
@@ -2273,20 +2276,6 @@ LogicalResult foldExtractFromFromElements(ExtractOp extractOp,
return success();
}
-/// Fold an insert or extract operation into an poison value when a poison index
-/// is found at any dimension of the static position.
-template <typename OpTy>
-LogicalResult
-canonicalizePoisonIndexInsertExtractOp(OpTy op, PatternRewriter &rewriter) {
- if (auto poisonAttr = foldPoisonIndexInsertExtractOp(
- op.getContext(), op.getStaticPosition(), OpTy::kPoisonIndex)) {
- rewriter.replaceOpWithNewOp<ub::PoisonOp>(op, op.getType(), poisonAttr);
- return success();
- }
-
- return failure();
-}
-
} // namespace
void ExtractOp::getCanonicalizationPatterns(RewritePatternSet &results,
@@ -2295,7 +2284,6 @@ void ExtractOp::getCanonicalizationPatterns(RewritePatternSet &results,
ExtractOpFromBroadcast, ExtractOpFromCreateMask>(context);
results.add(foldExtractFromShapeCastToShapeCast);
results.add(foldExtractFromFromElements);
- results.add(canonicalizePoisonIndexInsertExtractOp<ExtractOp>);
}
static void populateFromInt64AttrArray(ArrayAttr arrayAttr,
@@ -3068,7 +3056,6 @@ void InsertOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<InsertToBroadcast, BroadcastFolder, InsertSplatToSplat,
InsertOpConstantFolder>(context);
- results.add(canonicalizePoisonIndexInsertExtractOp<InsertOp>);
}
OpFoldResult vector::InsertOp::fold(FoldAdaptor adaptor) {
diff --git a/mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir b/mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
index 7df6defc0f202f..722ab0499d858e 100644
--- a/mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
+++ b/mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
@@ -1255,8 +1255,8 @@ func.func @extract_poison_idx(%arg0: vector<16xf32>) -> f32 {
return %0 : f32
}
// CHECK-LABEL: @extract_poison_idx
-// CHECK: %[[IDX:.*]] = llvm.mlir.constant(-1 : i64) : i64
-// CHECK: llvm.extractelement {{.*}}[%[[IDX]] : i64] : vector<16xf32>
+// CHECK: %[[UB:.*]] = ub.poison : f32
+// CHECK: return %[[UB]] : f32
// -----
|
// CHECK: %[[UB:.*]] = ub.poison : f32 | ||
// CHECK: return %[[UB]] : f32 |
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.
Could you use this opportunity and rename:
@extract_poison_idx
, as@extract_scalar_from_vec_1d_f32_poison_idx
This way we would maintain consistent naming.
Also, is there a test with poison idx when extracting form something rank > 1?
This PR adds support for UB constant materialization (i.e., generating `ub::PoisonOp` to `VectorDialect::materializeConstant`. This was the reason why the vector folders generating poison didn't work.
86d32a6
to
ea3a0f2
Compare
…25596) This PR adds support for UB constant materialization (i.e., generating `ub::PoisonOp` to `VectorDialect::materializeConstant`. This was the reason why the vector folders generating poison didn't work.
This PR adds support for UB constant materialization (i.e., generating
ub::PoisonOp
toVectorDialect::materializeConstant
. This was the reason why the vector folders generating poison didn't work.