Skip to content

Commit

Permalink
[Substrait] Add folder for project op with empty expressions.
Browse files Browse the repository at this point in the history
Signed-off-by: Ingo Müller <[email protected]>
  • Loading branch information
ingomueller-net committed May 31, 2024
1 parent 54029b4 commit 51e3db6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/structured/Dialect/Substrait/IR/SubstraitOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ def Substrait_ProjectOp : Substrait_RelOp<"project", [
$input attr-dict `:` type($input) `->` type($result) $expressions
}];
let hasRegionVerifier = 1;
let hasFolder = 1;
let extraClassDefinition = [{
/// Implement OpAsmOpInterface.
::llvm::StringRef $cppClass::getDefaultDialect() {
Expand Down
11 changes: 11 additions & 0 deletions lib/Dialect/Substrait/IR/Substrait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,17 @@ LogicalResult PlanRelOp::verifyRegions() {
return verifyNamedStruct(getOperation(), fieldNames, tupleType);
}

OpFoldResult ProjectOp::fold(FoldAdaptor adaptor) {
Operation *terminator = adaptor.getExpressions().front().getTerminator();

// If the region does not yield any values, the the `project` has no effect.
if (terminator->getNumOperands() == 0) {
return getInput();
}

return {};
}

LogicalResult ProjectOp::verifyRegions() {
// Verify that the expression block has a matching argument type.
auto inputTupleType = llvm::cast<TupleType>(getInput().getType());
Expand Down
19 changes: 19 additions & 0 deletions test/Dialect/Substrait/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,22 @@ substrait.plan version 0 : 42 : 1 {
yield %5 : tuple<si1, si32>
}
}

// -----

// Check that empty `project` folded.

// CHECK-LABEL: substrait.plan
// CHECK-NEXT: relation
// CHECK-NEXT: %[[V0:.*]] = named_table
// CHECK-NEXT: yield %[[V0]]

substrait.plan version 0 : 42 : 1 {
relation {
%0 = named_table @t1 as ["a"] : tuple<si32>
%1 = project %0 : tuple<si32> -> tuple<si32> {
^bb0(%arg0: tuple<si32>):
}
yield %1 : tuple<si32>
}
}

0 comments on commit 51e3db6

Please sign in to comment.