-
Notifications
You must be signed in to change notification settings - Fork 75
[intel] improve pitch and width constexpr folding #5489
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,42 @@ static int __builtin_ctz(unsigned x) { | |
|
|
||
| namespace { | ||
|
|
||
| static Value skipCasts(Value v) { | ||
| Operation *def = v.getDefiningOp(); | ||
| if (def && | ||
| isa<LLVM::TruncOp, LLVM::SExtOp, LLVM::ZExtOp, LLVM::BitcastOp>(def)) | ||
| return def->getOperand(0); | ||
| return v; | ||
| } | ||
|
|
||
| static Value tryFoldOp(Value v) { | ||
| if (Operation *def = v.getDefiningOp()) { | ||
| SmallVector<OpFoldResult> results; | ||
| if (succeeded(def->fold(results)) && results.size() == 1) { | ||
| if (auto val = dyn_cast_or_null<Value>(results[0])) | ||
| return val; | ||
| } | ||
| } | ||
| return v; | ||
| } | ||
|
|
||
januszjah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| static std::optional<int64_t> tryConstEval(Value v, int depth = 16) { | ||
|
Contributor
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. can we port these changes to Utility.cpp and improve mlir::triton::intel::getFoldedConstantValue??
Contributor
Author
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. I think it will increase work done while compilation because getFoldedConstantValue is used in isConstant in many places and my version does multiple skips and folds instead of single fold, however IDK if it's a problem and how obsessive we are about cycles at this step. |
||
| for (int i = 0; i < depth; ++i) { | ||
| if (auto res = getConstantIntValue(v)) | ||
| return res; | ||
|
|
||
| Value newV = skipCasts(v); | ||
| newV = tryFoldOp(newV); | ||
|
|
||
| if (newV == v) | ||
| break; | ||
|
|
||
| v = newV; | ||
| } | ||
|
|
||
| return std::nullopt; | ||
| } | ||
|
|
||
| Value maybeAnd(RewriterBase &rewriter, Location loc, Value a, Value b) { | ||
| auto tb = TritonLLVMOpBuilder(loc, rewriter); | ||
| if (a && b) { | ||
|
|
@@ -1590,23 +1626,19 @@ struct LoadOpToBlockIOConversion | |
| std::swap(baseWidth, baseHeight); | ||
| } | ||
| // HW requires the pitch to be at least 64 bytes. | ||
| std::function<Value(Value)> skipTrunc = [&](Value v) { | ||
| if (dyn_cast_or_null<LLVM::TruncOp>(v.getDefiningOp())) | ||
| return skipTrunc(v.getDefiningOp()->getOperand(0)); | ||
| return v; | ||
| }; | ||
| if (Operation *op = skipTrunc(pitch).getDefiningOp()) { | ||
| std::optional<int64_t> pitchConst = | ||
| mlir::triton::intel::getFoldedConstantValue(op); | ||
| if (pitchConst.has_value()) { | ||
| if ((*pitchConst * elemSizeInBits / 8) < 64) | ||
| return failure(); | ||
| } | ||
| if (auto pitchConst = tryConstEval(pitch)) { | ||
| if ((*pitchConst * elemSizeInBits / 8) < 64) | ||
| return failure(); | ||
| } | ||
|
|
||
| baseWidth = b.trunc(i32_ty, baseWidth); | ||
| baseHeight = b.trunc(i32_ty, baseHeight); | ||
|
|
||
| if (auto widthConst = tryConstEval(baseWidth)) { | ||
| if ((*widthConst * elemSizeInBits / 8) < 64) | ||
| return failure(); | ||
| } | ||
januszjah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const unsigned originalElemBits = elemSizeInBits; | ||
| if (isTransposeRequired) { | ||
| // adjust the block io parameter to align HW's limitations on | ||
|
|
||
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.
The fold result could be an Attribute containing a constant value, not just a Value. If results[0] is an Attribute, it should be converted to a constant Value rather than being ignored. This would cause tryConstEval to miss foldable constant attributes.