Skip to content

[mlir][spirv] Fix some issues related to converting ub.poison to SPIR-V #125905

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 1 commit into from
Feb 6, 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
5 changes: 0 additions & 5 deletions mlir/lib/Conversion/UBToSPIRV/UBToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ struct PoisonOpLowering final : OpConversionPattern<ub::PoisonOp> {
matchAndRewrite(ub::PoisonOp op, OpAdaptor,
ConversionPatternRewriter &rewriter) const override {
Type origType = op.getType();
if (!origType.isIntOrIndexOrFloat())
return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
diag << "unsupported type " << origType;
});

Type resType = getTypeConverter()->convertType(origType);
if (!resType)
return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Conversion/VectorToSPIRV/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ add_mlir_conversion_library(MLIRVectorToSPIRV
MLIRSPIRVConversion
MLIRVectorDialect
MLIRTransforms
MLIRUBToSPIRV
)
34 changes: 13 additions & 21 deletions mlir/lib/Conversion/VectorToSPIRV/VectorToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,13 @@ struct VectorExtractOpConvert final

if (std::optional<int64_t> id =
getConstantIntValue(extractOp.getMixedPosition()[0])) {
// TODO: ExtractOp::fold() already can fold a static poison index to
// ub.poison; remove this once ub.poison can be converted to SPIR-V.
if (id == vector::ExtractOp::kPoisonIndex) {
// Arbitrary choice of poison result, intended to stick out.
Value zero =
spirv::ConstantOp::getZero(dstType, extractOp.getLoc(), rewriter);
rewriter.replaceOp(extractOp, zero);
} else
rewriter.replaceOpWithNewOp<spirv::CompositeExtractOp>(
extractOp, dstType, adaptor.getVector(),
rewriter.getI32ArrayAttr(id.value()));
if (id == vector::ExtractOp::kPoisonIndex)
return rewriter.notifyMatchFailure(
extractOp,
"Static use of poison index handled elsewhere (folded to poison)");
rewriter.replaceOpWithNewOp<spirv::CompositeExtractOp>(
extractOp, dstType, adaptor.getVector(),
rewriter.getI32ArrayAttr(id.value()));
} else {
Value sanitizedIndex = sanitizeDynamicIndex(
rewriter, extractOp.getLoc(), adaptor.getDynamicPosition()[0],
Expand Down Expand Up @@ -306,16 +302,12 @@ struct VectorInsertOpConvert final

if (std::optional<int64_t> id =
getConstantIntValue(insertOp.getMixedPosition()[0])) {
// TODO: ExtractOp::fold() already can fold a static poison index to
// ub.poison; remove this once ub.poison can be converted to SPIR-V.
if (id == vector::InsertOp::kPoisonIndex) {
// Arbitrary choice of poison result, intended to stick out.
Value zero = spirv::ConstantOp::getZero(insertOp.getDestVectorType(),
insertOp.getLoc(), rewriter);
rewriter.replaceOp(insertOp, zero);
} else
rewriter.replaceOpWithNewOp<spirv::CompositeInsertOp>(
insertOp, adaptor.getSource(), adaptor.getDest(), id.value());
if (id == vector::InsertOp::kPoisonIndex)
return rewriter.notifyMatchFailure(
insertOp,
"Static use of poison index handled elsewhere (folded to poison)");
rewriter.replaceOpWithNewOp<spirv::CompositeInsertOp>(
insertOp, adaptor.getSource(), adaptor.getDest(), id.value());
} else {
Value sanitizedIndex = sanitizeDynamicIndex(
rewriter, insertOp.getLoc(), adaptor.getDynamicPosition()[0],
Expand Down
3 changes: 3 additions & 0 deletions mlir/lib/Conversion/VectorToSPIRV/VectorToSPIRVPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "mlir/Conversion/VectorToSPIRV/VectorToSPIRVPass.h"

#include "mlir/Conversion/UBToSPIRV/UBToSPIRV.h"
#include "mlir/Conversion/VectorToSPIRV/VectorToSPIRV.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
Expand Down Expand Up @@ -49,6 +50,8 @@ void ConvertVectorToSPIRVPass::runOnOperation() {

RewritePatternSet patterns(context);
populateVectorToSPIRVPatterns(typeConverter, patterns);
// Used for folds, e.g. vector.extract[-1] -> ub.poison -> spirv.Undef.
ub::populateUBToSPIRVConversionPatterns(typeConverter, patterns);

if (failed(applyPartialConversion(op, *target, std::move(patterns))))
return signalPassFailure();
Expand Down
3 changes: 1 addition & 2 deletions mlir/test/Conversion/UBToSPIRV/ub-to-spirv.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ func.func @check_poison() {
%1 = ub.poison : i16
// CHECK: {{.*}} = spirv.Undef : f64
%2 = ub.poison : f64
// TODO: vector is not covered yet
// CHECK: {{.*}} = ub.poison : vector<4xf32>
// CHECK: {{.*}} = spirv.Undef : vector<4xf32>
%3 = ub.poison : vector<4xf32>
return
}
Expand Down
9 changes: 5 additions & 4 deletions mlir/test/Conversion/VectorToSPIRV/vector-to-spirv.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ func.func @extract(%arg0 : vector<2xf32>) -> (vector<1xf32>, f32) {

// -----

// CHECK-LABEL: @extract_poison_idx
// CHECK: %[[R:.+]] = spirv.Undef : f32
// CHECK: return %[[R]]
func.func @extract_poison_idx(%arg0 : vector<4xf32>) -> f32 {
// CHECK: %[[ZERO:.+]] = spirv.Constant 0.000000e+00
// CHECK: return %[[ZERO]]
%0 = vector.extract %arg0[-1] : f32 from vector<4xf32>
return %0: f32
}
Expand Down Expand Up @@ -285,8 +286,8 @@ func.func @insert(%arg0 : vector<4xf32>, %arg1: f32) -> vector<4xf32> {
// -----

// CHECK-LABEL: @insert_poison_idx
// CHECK: %[[ZERO:.+]] = spirv.Constant dense<0.000000e+00>
// CHECK: return %[[ZERO]]
// CHECK: %[[R:.+]] = spirv.Undef : vector<4xf32>
// CHECK: return %[[R]]
func.func @insert_poison_idx(%arg0 : vector<4xf32>, %arg1: f32) -> vector<4xf32> {
%1 = vector.insert %arg1, %arg0[-1] : f32 into vector<4xf32>
return %1: vector<4xf32>
Expand Down