Skip to content

[MLIR][Interfaces] Remove negative branch weight verifier #148234

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 0 additions & 9 deletions flang/test/Fir/invalid.fir
Original file line number Diff line number Diff line change
Expand Up @@ -1426,12 +1426,3 @@ func.func @wrong_weights_number_in_if_then_else(%cond: i1) {
}
return
}

// -----

func.func @negative_weight_in_if_then(%cond: i1) {
// expected-error @below {{weight #0 must be non-negative}}
fir.if %cond weights([-1, 101]) {
}
return
}
28 changes: 16 additions & 12 deletions mlir/include/mlir/Interfaces/ControlFlowInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,15 @@ def WeightedBranchOpInterface : OpInterface<"WeightedBranchOpInterface"> {
This interface provides weight information for branching terminator
operations, i.e. terminator operations with successors.

This interface provides methods for getting/setting integer non-negative
weight of each branch. The probability of executing a branch
is computed as the ratio between the branch's weight and the total
sum of the weights (which cannot be zero).
The weights are optional. If they are provided, then their number
This interface provides methods for getting/setting integer weights of each
branch. The probability of executing a branch is computed as the ratio
between the branch's weight and the total sum of the weights (which cannot
be zero). The weights are optional. If they are provided, then their number
must match the number of successors of the operation.

Note that the branch weight use an i32 representation but they are to be
interpreted as unsigned integers.

The default implementations of the methods expect the operation
to have an attribute of type DenseI32ArrayAttr named branch_weights.
}];
Expand Down Expand Up @@ -440,19 +442,21 @@ def WeightedRegionBranchOpInterface
This interface provides weight information for region operations
that exhibit branching behavior between held regions.

This interface provides methods for getting/setting integer non-negative
weight of each branch. The probability of executing a region is computed
as the ratio between the region branch's weight and the total sum
of the weights (which cannot be zero).
The weights are optional. If they are provided, then their number
must match the number of regions held by the operation
(including empty regions).
This interface provides methods for getting/setting integer weights of each
branch. The probability of executing a region is computed as the ratio
between the region branch's weight and the total sum of the weights (which
cannot be zero). The weights are optional. If they are provided, then their
number must match the number of regions held by the operation (including
empty regions).

The weights specify the probability of branching to a particular
region when first executing the operation.
For example, for loop-like operations with a single region
the weight specifies the probability of entering the loop.

Note that the branch weight use an i32 representation but they are to be
interpreted as unsigned integers.

The default implementations of the methods expect the operation
to have an attribute of type DenseI32ArrayAttr named branch_weights.
}];
Expand Down
4 changes: 0 additions & 4 deletions mlir/lib/Interfaces/ControlFlowInterfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ static LogicalResult verifyWeights(Operation *op,
<< ": " << weights.size() << " vs "
<< expectedWeightsNum;

for (auto [index, weight] : llvm::enumerate(weights))
if (weight < 0)
return op->emitError() << "weight #" << index << " must be non-negative";

if (llvm::all_of(weights, [](int32_t value) { return value == 0; }))
return op->emitError() << "branch weights cannot all be zero";

Expand Down
12 changes: 0 additions & 12 deletions mlir/test/Dialect/ControlFlow/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,6 @@ func.func @wrong_weights_number(%cond: i1) {

// -----

// CHECK-LABEL: func @negative_weight
func.func @wrong_total_weight(%cond: i1) {
// expected-error@+1 {{weight #0 must be non-negative}}
cf.cond_br %cond weights([-1, 101]), ^bb1, ^bb2
^bb1:
return
^bb2:
return
}

// -----

// CHECK-LABEL: func @zero_weights
func.func @wrong_total_weight(%cond: i1) {
// expected-error@+1 {{branch weights cannot all be zero}}
Expand Down