Skip to content

[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

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ void VectorDialect::initialize() {
Operation *VectorDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
if (isa<ub::PoisonAttrInterface>(value))
return value.getDialect().materializeConstant(builder, value, type, loc);

return arith::ConstantOp::materialize(builder, value, type, loc);
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 14 additions & 4 deletions mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1250,13 +1250,13 @@ func.func @extract_scalar_from_vec_1d_f32(%arg0: vector<16xf32>) -> f32 {

// -----

func.func @extract_poison_idx(%arg0: vector<16xf32>) -> f32 {
func.func @extract_scalar_from_vec_1d_f32_poison_idx(%arg0: vector<16xf32>) -> f32 {
%0 = vector.extract %arg0[-1]: f32 from vector<16xf32>
return %0 : f32
}
// CHECK-LABEL: @extract_poison_idx
// CHECK: %[[IDX:.*]] = llvm.mlir.constant(-1 : i64) : i64
// CHECK: llvm.extractelement {{.*}}[%[[IDX]] : i64] : vector<16xf32>
// CHECK-LABEL: @extract_scalar_from_vec_1d_f32_poison_idx
// CHECK: %[[UB:.*]] = ub.poison : f32
// CHECK: return %[[UB]] : f32
Comment on lines +1258 to +1259
Copy link
Contributor

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?


// -----

Expand Down Expand Up @@ -1335,6 +1335,16 @@ func.func @extract_vec_2d_from_vec_3d_f32(%arg0: vector<4x3x16xf32>) -> vector<3

// -----

func.func @extract_vec_2d_from_vec_3d_f32_poison_idx(%arg0: vector<4x3x16xf32>) -> vector<3x16xf32> {
%0 = vector.extract %arg0[-1]: vector<3x16xf32> from vector<4x3x16xf32>
return %0 : vector<3x16xf32>
}
// CHECK-LABEL: @extract_vec_2d_from_vec_3d_f32_poison_idx
// CHECK: %[[UB:.*]] = ub.poison : vector<3x16xf32>
// CHECK: return %[[UB]] : vector<3x16xf32>

// -----

func.func @extract_vec_2d_from_vec_3d_f32_scalable(%arg0: vector<4x3x[16]xf32>) -> vector<3x[16]xf32> {
%0 = vector.extract %arg0[0]: vector<3x[16]xf32> from vector<4x3x[16]xf32>
return %0 : vector<3x[16]xf32>
Expand Down