-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][ptr] Add ptr.ptr_diff
operation
#157354
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
Changes from 3 commits
607a3e0
5ea8571
c25dd35
f4a8eb2
3fca67d
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
#include "mlir/IR/Matchers.h" | ||
#include "mlir/Interfaces/DataLayoutInterfaces.h" | ||
#include "mlir/Transforms/InliningUtils.h" | ||
#include "llvm/ADT/StringExtras.h" | ||
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. This looks like an accidental include. |
||
#include "llvm/ADT/TypeSwitch.h" | ||
|
||
using namespace mlir; | ||
|
@@ -391,6 +392,39 @@ LogicalResult PtrAddOp::inferReturnTypes( | |
return success(); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// PtrDiffOp | ||
//===----------------------------------------------------------------------===// | ||
|
||
LogicalResult PtrDiffOp::verify() { | ||
// If the operands are not shaped early exit. | ||
if (!isa<ShapedType>(getLhs().getType())) | ||
return success(); | ||
|
||
// Just check the container type matches, `SameOperandsAndResultShape` handles | ||
// the actual shape. | ||
if (getResult().getType().getTypeID() != getLhs().getType().getTypeID()) { | ||
return emitError() << "expected the result to have the same container " | ||
"type as the operands when operands are shaped"; | ||
} | ||
|
||
return success(); | ||
} | ||
|
||
ptr::PtrType PtrDiffOp::getPtrType() { | ||
Type lhsType = getLhs().getType(); | ||
if (auto shapedType = dyn_cast<ShapedType>(lhsType)) | ||
return cast<ptr::PtrType>(shapedType.getElementType()); | ||
return cast<ptr::PtrType>(lhsType); | ||
} | ||
|
||
Type PtrDiffOp::getIntType() { | ||
Type resultType = getResult().getType(); | ||
if (auto shapedType = dyn_cast<ShapedType>(resultType)) | ||
return shapedType.getElementType(); | ||
return resultType; | ||
} | ||
|
||
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 get a folder for the subtraction? 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. Sure, in general I need to improve the folders and canonicalizers of the entire dialect, so how about a new PR for that? |
||
//===----------------------------------------------------------------------===// | ||
// ToPtrOp | ||
//===----------------------------------------------------------------------===// | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
What does it mean? Can you provide an example of "signed" vs "unsigned" arithmetic here?
Can a pointer itself be negative?
Uh oh!
There was an error while loading. Please reload this page.
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.
Negative pointers no, but take: (ptr + 16) - (ptr + 32), the result will be negative.
I took as rationale the existence of
ptrdiff_t
in C https://en.cppreference.com/w/c/types/ptrdiff_t.htmlI'll clarify the comment, because it's not clear.
Uh oh!
There was an error while loading. Please reload this page.
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.
Be mindful that C is a language with signed type, while we're operating in a signless IR here.
The fact that the result can be negative is an obvious aspect of the operation being a subtraction, it has not much to do with "signed arithmetic".
I'd look at arith.subi for inspiration.
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.
Removed the comment because I already have the phrase:
in the description.