Skip to content

[mlir] Guard sccp pass from crashing with different source type #120656

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 4 commits into from
Dec 25, 2024
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
12 changes: 10 additions & 2 deletions mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2523,8 +2523,16 @@ OpFoldResult BroadcastOp::fold(FoldAdaptor adaptor) {
if (!adaptor.getSource())
return {};
auto vectorType = getResultVectorType();
if (llvm::isa<IntegerAttr, FloatAttr>(adaptor.getSource()))
return DenseElementsAttr::get(vectorType, adaptor.getSource());
if (auto attr = llvm::dyn_cast<IntegerAttr>(adaptor.getSource())) {
if (vectorType.getElementType() != attr.getType())
return {};
return DenseElementsAttr::get(vectorType, attr);
}
if (auto attr = llvm::dyn_cast<FloatAttr>(adaptor.getSource())) {
if (vectorType.getElementType() != attr.getType())
return {};
return DenseElementsAttr::get(vectorType, attr);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for separating the handling of int and float types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dyn_cast cannot take multiple arguments to be casted.

  if (auto attr = (llvm::dyn_cast<IntegerAttr>(adaptor.getSource()) || llvm::dyn_cast<FloatAttr>(adaptor.getSource()))) {

Even if I unioned the result of the cast like this, it causes the following error.

llvm-project/mlir/lib/Dialect/Vector/IR/VectorOps.cpp:2527:44: error: member reference base type 'bool' is not a structure or union
    if (vectorType.getElementType() != attr.getType())
                     

So we can only separate the logic to handle int and float separetely.

if (auto attr = llvm::dyn_cast<SplatElementsAttr>(adaptor.getSource()))
return DenseElementsAttr::get(vectorType, attr.getSplatValue<Attribute>());
return {};
Expand Down
9 changes: 9 additions & 0 deletions mlir/test/Transforms/sccp.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,12 @@ func.func @op_with_region() -> (i32) {
^b:
return %1 : i32
}

// CHECK-LABEL: no_crash_with_different_source_type
func.func @no_crash_with_different_source_type() {
// CHECK: llvm.mlir.constant(0 : index) : i64
%0 = llvm.mlir.constant(0 : index) : i64
// CHECK: vector.broadcast %[[CST:.*]] : i64 to vector<128xi64>
%1 = vector.broadcast %0 : i64 to vector<128xi64>
llvm.return
}
Loading