-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[mlir] Add loop bounds normalization pass #93781
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
jtuyls
wants to merge
1
commit into
llvm:main
Choose a base branch
from
jtuyls:normalize-loop-bounds
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//===- LoopUtils.h - Helpers related to loop operations ---------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This header file defines utilities for loop operations. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/IR/PatternMatch.h" | ||
|
||
namespace mlir { | ||
|
||
// This structure is to pass and return sets of loop parameters without | ||
// confusing the order. | ||
struct LoopParams { | ||
Value lowerBound; | ||
Value upperBound; | ||
Value step; | ||
}; | ||
|
||
/// Calculate the normalized loop upper bounds with lower bound equal to zero | ||
/// and step equal to one. | ||
LoopParams emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc, | ||
Value lb, Value ub, Value step); | ||
|
||
} // namespace mlir |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -375,6 +375,132 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> { | |
}]; | ||
} | ||
|
||
def LoopLikeWithInductionVarsOpInterface | ||
: OpInterface<"LoopLikeWithInductionVarsOpInterface", [LoopLikeOpInterface]> { | ||
let description = [{ | ||
Interface for loop-like operations with one or more induction variables. | ||
This interface contains helper functions for retrieving and updating the | ||
lower bound, upper bound and step size for each induction variable and | ||
provides a utility function to check whether the loop is normalized., i.e. | ||
all lower bounds are equal to zero and steps are equal to one. | ||
}]; | ||
let cppNamespace = "::mlir"; | ||
|
||
let methods = [ | ||
InterfaceMethod<[{ | ||
Return the induction variables if they exist, otherwise return | ||
std::nullopt. | ||
}], | ||
/*retTy=*/"::mlir::ValueRange", | ||
/*methodName=*/"getInductionVars" | ||
>, | ||
InterfaceMethod<[{ | ||
Return the lower bound values or attributes as OpFoldResult. | ||
}], | ||
/*retTy=*/"SmallVector<::mlir::OpFoldResult>", | ||
/*methodName=*/"getMixedLowerBound" | ||
>, | ||
InterfaceMethod<[{ | ||
Return the step values or attributes if they exist as OpFoldResult. | ||
}], | ||
/*retTy=*/"SmallVector<::mlir::OpFoldResult>", | ||
/*methodName=*/"getMixedStep" | ||
>, | ||
InterfaceMethod<[{ | ||
Return the upper bound values or attributes as OpFoldResult. | ||
}], | ||
/*retTy=*/"SmallVector<::mlir::OpFoldResult>", | ||
/*methodName=*/"getMixedUpperBound" | ||
>, | ||
InterfaceMethod<[{ | ||
Return the lower bounds as values. | ||
}], | ||
/*retTy=*/"SmallVector<Value>", | ||
/*methodName=*/"getLowerBound", | ||
/*args=*/(ins "OpBuilder &":$b) | ||
>, | ||
InterfaceMethod<[{ | ||
Return the steps as values. | ||
}], | ||
/*retTy=*/"SmallVector<Value>", | ||
/*methodName=*/"getStep", | ||
/*args=*/(ins "OpBuilder &":$b) | ||
>, | ||
InterfaceMethod<[{ | ||
Return the upper bounds as values. | ||
}], | ||
/*retTy=*/"SmallVector<Value>", | ||
/*methodName=*/"getUpperBound", | ||
/*args=*/(ins "OpBuilder &":$b) | ||
>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto, also the set part |
||
InterfaceMethod<[{ | ||
Set the lower bounds from an array of `OpFoldResult`. | ||
}], | ||
/*retTy=*/"void", | ||
/*methodName=*/"setMixedLowerBounds", | ||
/*args=*/(ins "OpBuilder &":$b, "ArrayRef<OpFoldResult>":$lbs) | ||
>, | ||
InterfaceMethod<[{ | ||
Set the steps from an array of `OpFoldResult`. | ||
}], | ||
/*retTy=*/"void", | ||
/*methodName=*/"setMixedSteps", | ||
/*args=*/(ins "OpBuilder &":$b, "ArrayRef<OpFoldResult>":$lbs) | ||
>, | ||
InterfaceMethod<[{ | ||
Set the upper bounds from an array of `OpFoldResult`. | ||
}], | ||
/*retTy=*/"void", | ||
/*methodName=*/"setMixedUpperBounds", | ||
/*args=*/(ins "OpBuilder &":$b, "ArrayRef<OpFoldResult>":$lbs) | ||
>, | ||
InterfaceMethod<[{ | ||
Set the lower bounds from an array of values. | ||
}], | ||
/*retTy=*/"void", | ||
/*methodName=*/"setLowerBounds", | ||
/*args=*/(ins "ArrayRef<Value>":$lbs) | ||
>, | ||
InterfaceMethod<[{ | ||
Set the steps from an array of values. | ||
}], | ||
/*retTy=*/"void", | ||
/*methodName=*/"setSteps", | ||
/*args=*/(ins "ArrayRef<Value>":$lbs) | ||
>, | ||
InterfaceMethod<[{ | ||
Set the upper bounds from an array of values. | ||
}], | ||
/*retTy=*/"void", | ||
/*methodName=*/"setUpperBounds", | ||
/*args=*/(ins "ArrayRef<Value>":$lbs) | ||
>, | ||
InterfaceMethod<[{ | ||
Checks if the lower bounds are zeros and steps are ones. | ||
}], | ||
/*retTy=*/"bool", | ||
/*methodName=*/"isNormalized", | ||
/*args=*/(ins), | ||
/*methodBody=*/"", | ||
/*defaultImplementation=*/[{ | ||
auto allEqual = [](ArrayRef<OpFoldResult> results, int64_t val) { | ||
return llvm::all_of(results, [&](OpFoldResult ofr) { | ||
auto intValue = getConstantIntValue(ofr); | ||
return intValue.has_value() && intValue == val; | ||
}); | ||
}; | ||
SmallVector<::mlir::OpFoldResult> lbs = $_op.getMixedLowerBound(); | ||
SmallVector<::mlir::OpFoldResult> steps = $_op.getMixedStep(); | ||
return allEqual(lbs, 0) && allEqual(steps, 1); | ||
}] | ||
> | ||
]; | ||
|
||
let verify = [{ | ||
return detail::verifyLoopLikeWithInductionVarsOpInterface($_op); | ||
}]; | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Traits | ||
//===----------------------------------------------------------------------===// | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should this function designed to be an interface method? extraClassDeclaration should be more suitable for your scenario IMO, because the return value of this function can be computed by getMixedLowerBound.