-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[IR] Initial introduction of memset_pattern #97583
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 7 commits
627f1ef
3a0d10a
d710a1f
4bcc00e
7d1347c
60ba68b
be558f9
dfc0564
8ac8b69
6d16c82
55ee84a
1e60edd
88b5af3
ea429b4
e9c98c8
30d59b9
d83fdfb
64bc6af
c19adc1
03e07d5
a7373b7
a68aa8d
9580ab0
4ebc985
78bad3b
71dd9b5
ad7585c
4d6d9ab
0b0e81e
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 |
---|---|---|
|
@@ -15230,6 +15230,62 @@ The behavior of '``llvm.memset.inline.*``' is equivalent to the behavior of | |
'``llvm.memset.*``', but the generated code is guaranteed not to call any | ||
external functions. | ||
|
||
.. _int_memset_pattern: | ||
|
||
'``llvm.memset.pattern``' Intrinsic | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Syntax: | ||
""""""" | ||
|
||
This is an overloaded intrinsic. You can use ``llvm.memset_pattern`` on | ||
asb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
any integer bit width that is an integral number of bytes and for different | ||
asb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
address spaces. Not all targets support all bit widths however. | ||
|
||
:: | ||
|
||
declare void @llvm.memset.pattern.p0.i64.i128(ptr <dest>, i128 <val>, | ||
nikic marked this conversation as resolved.
Show resolved
Hide resolved
asb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
i64 <len>, i1 <isvolatile>) | ||
|
||
Overview: | ||
""""""""" | ||
|
||
The '``llvm.memset.pattern.*``' intrinsics fill a block of memory with | ||
asb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
a particular value. This may be expanded to an inline loop, a sequence of | ||
stores, or a libcall depending on what is available for the target and the | ||
expected performance and code size impact. | ||
|
||
Arguments: | ||
"""""""""" | ||
|
||
The first argument is a pointer to the destination to fill, the second | ||
is the value with which to fill it, the third argument is an integer | ||
argument specifying the number of times to fill the value, and the fourth is a | ||
boolean indicating a volatile access. | ||
|
||
The :ref:`align <attr_align>` parameter attribute can be provided | ||
for the first argument. | ||
|
||
If the ``isvolatile`` parameter is ``true``, the | ||
``llvm.memset.pattern`` call is a :ref:`volatile operation <volatile>`. The | ||
detailed access behavior is not very cleanly specified and it is unwise to | ||
depend on it. | ||
asb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Semantics: | ||
"""""""""" | ||
|
||
The '``llvm.memset.pattern*``' intrinsic fills "len" bytes of memory | ||
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. Here it says that 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. Thanks, that had indeed been missed and has now been addressed. |
||
starting at the destination location. If the argument is known to be aligned | ||
to some boundary, this can be specified as an attribute on the argument. | ||
|
||
If ``<len>`` is not an integer multiple of the pattern width in bytes, then any | ||
remainder bytes will be copied from ``<val>``. | ||
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. Could be worth describing how this works with respect to endianess. 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've added what aims to be a minimal description. Also fixed the outdated references to byte counts from an earlier iteration of the intrinsic. |
||
If ``<len>`` is 0, it is no-op modulo the behavior of attributes attached to | ||
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. Another thing that could be clarified here is that we increment by the alloc size of the type. (Only relevant for over-aligned types.) 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've added text that intends to convey this. |
||
the arguments. | ||
If ``<len>`` is not a well-defined value, the behavior is undefined. | ||
If ``<len>`` is not zero, ``<dest>`` should be well-defined, otherwise the | ||
behavior is undefined. | ||
|
||
.. _int_sqrt: | ||
|
||
'``llvm.sqrt.*``' Intrinsic | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1208,6 +1208,7 @@ class MemIntrinsic : public MemIntrinsicBase<MemIntrinsic> { | |
case Intrinsic::memmove: | ||
case Intrinsic::memset: | ||
case Intrinsic::memset_inline: | ||
case Intrinsic::memset_pattern: | ||
case Intrinsic::memcpy_inline: | ||
return true; | ||
default: | ||
|
@@ -1219,14 +1220,16 @@ class MemIntrinsic : public MemIntrinsicBase<MemIntrinsic> { | |
} | ||
}; | ||
|
||
/// This class wraps the llvm.memset and llvm.memset.inline intrinsics. | ||
/// This class wraps the llvm.memset, llvm.memset.inline, and | ||
/// llvm.memset.pattern intrinsics. | ||
class MemSetInst : public MemSetBase<MemIntrinsic> { | ||
public: | ||
// Methods for support type inquiry through isa, cast, and dyn_cast: | ||
static bool classof(const IntrinsicInst *I) { | ||
switch (I->getIntrinsicID()) { | ||
case Intrinsic::memset: | ||
case Intrinsic::memset_inline: | ||
case Intrinsic::memset_pattern: | ||
asb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true; | ||
default: | ||
return false; | ||
|
@@ -1249,6 +1252,18 @@ class MemSetInlineInst : public MemSetInst { | |
} | ||
}; | ||
|
||
/// This class wraps the llvm.memset.pattern intrinsic. | ||
class MemSetPatternInst : public MemSetInst { | ||
public: | ||
// Methods for support type inquiry through isa, cast, and dyn_cast: | ||
static bool classof(const IntrinsicInst *I) { | ||
return I->getIntrinsicID() == Intrinsic::memset_pattern; | ||
} | ||
static bool classof(const Value *V) { | ||
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); | ||
} | ||
}; | ||
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. Why do we need both MemSetPatternIntrinsic and MemSetPatternInst? 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'm mirroring the pattern followed by other mem intrinsics, and although it's not super pretty, having both classes like this (as for the standard MemSet intrinsics) seems the way that reduces copy and paste of accessor code. 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. Hm okay, I thought this might only be needed for cases where we have both atomic and non-atomic variants. |
||
|
||
/// This class wraps the llvm.memcpy/memmove intrinsics. | ||
class MemTransferInst : public MemTransferBase<MemIntrinsic> { | ||
public: | ||
|
@@ -1328,6 +1343,7 @@ class AnyMemIntrinsic : public MemIntrinsicBase<AnyMemIntrinsic> { | |
case Intrinsic::memmove: | ||
case Intrinsic::memset: | ||
case Intrinsic::memset_inline: | ||
case Intrinsic::memset_pattern: | ||
case Intrinsic::memcpy_element_unordered_atomic: | ||
case Intrinsic::memmove_element_unordered_atomic: | ||
case Intrinsic::memset_element_unordered_atomic: | ||
|
@@ -1350,6 +1366,7 @@ class AnyMemSetInst : public MemSetBase<AnyMemIntrinsic> { | |
switch (I->getIntrinsicID()) { | ||
case Intrinsic::memset: | ||
case Intrinsic::memset_inline: | ||
case Intrinsic::memset_pattern: | ||
case Intrinsic::memset_element_unordered_atomic: | ||
return true; | ||
default: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1003,6 +1003,14 @@ def int_memset_inline | |
NoCapture<ArgIndex<0>>, WriteOnly<ArgIndex<0>>, | ||
ImmArg<ArgIndex<3>>]>; | ||
|
||
// Memset variant that writes a given pattern. | ||
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. Comment what the operands are 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. Added with inline comments (there's not a totally consistent pattern for describing args - many intrinsics have no description at all, but I see some other examples in the file using inline comments for the args) 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. Really we ought to have tablegen mandatory doc strings |
||
def int_memset_pattern | ||
: Intrinsic<[], | ||
[llvm_anyptr_ty, llvm_anyint_ty, llvm_anyint_ty, llvm_i1_ty], | ||
[IntrWriteMem, IntrArgMemOnly, IntrWillReturn, IntrNoFree, IntrNoCallback, | ||
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. DefaultAttrIntrinsic would hide most of these 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 dug through this and unfortunately I can't make use of |
||
NoCapture<ArgIndex<0>>, WriteOnly<ArgIndex<0>>, | ||
ImmArg<ArgIndex<3>>]>; | ||
|
||
// FIXME: Add version of these floating point intrinsics which allow non-default | ||
// rounding modes and FP exception handling. | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -456,6 +456,56 @@ static void createMemMoveLoop(Instruction *InsertBefore, Value *SrcAddr, | |||||
ElseTerm->eraseFromParent(); | ||||||
} | ||||||
|
||||||
static void createMemSetPatternLoop(Instruction *InsertBefore, Value *DstAddr, | ||||||
asb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
Value *Count, Value *SetValue, | ||||||
Align DstAlign, bool IsVolatile) { | ||||||
BasicBlock *OrigBB = InsertBefore->getParent(); | ||||||
Function *F = OrigBB->getParent(); | ||||||
const DataLayout &DL = F->getDataLayout(); | ||||||
|
||||||
if (DL.isBigEndian()) | ||||||
report_fatal_error("memset.pattern expansion not currently " | ||||||
"implemented for big-endian targets", | ||||||
false); | ||||||
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. Why this check? Your implementation is endian-ness independent. The endian-specific handling is inside the store.
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. Now removed - this was a holdover from an earlier implementation. The PPC test now serves as a simple smoke test for the expansion proceeding as expected in big-endian targets. |
||||||
|
||||||
if (!isPowerOf2_32(SetValue->getType()->getScalarSizeInBits())) | ||||||
report_fatal_error("Pattern width for memset_pattern must be a power of 2", | ||||||
false); | ||||||
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 limitation would have to be checked by the IR verifier. I don't think it's needed though, your code should work fine without it. 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've removed this limitation and added test coverage. |
||||||
|
||||||
Type *TypeOfCount = Count->getType(); | ||||||
|
||||||
BasicBlock *NewBB = OrigBB->splitBasicBlock(InsertBefore, "split"); | ||||||
BasicBlock *LoopBB = | ||||||
BasicBlock::Create(F->getContext(), "storeloop", F, NewBB); | ||||||
IRBuilder<> Builder(OrigBB->getTerminator()); | ||||||
|
||||||
Builder.CreateCondBr( | ||||||
Builder.CreateICmpEQ(ConstantInt::get(TypeOfCount, 0), Count), NewBB, | ||||||
LoopBB); | ||||||
OrigBB->getTerminator()->eraseFromParent(); | ||||||
|
||||||
IRBuilder<> LoopBuilder(LoopBB); | ||||||
PHINode *CurrentDst = LoopBuilder.CreatePHI(DstAddr->getType(), 0); | ||||||
CurrentDst->addIncoming(DstAddr, OrigBB); | ||||||
PHINode *LoopCount = LoopBuilder.CreatePHI(TypeOfCount, 0); | ||||||
LoopCount->addIncoming(Count, OrigBB); | ||||||
|
||||||
// Create the store instruction for the pattern | ||||||
LoopBuilder.CreateAlignedStore(SetValue, CurrentDst, DstAlign, IsVolatile); | ||||||
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. Doesn't this also need the "PartAlign" logic? It's DstAlign on the first iteration, but afterwards its the common alignment of DstAlign and the stride. 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. Good catch, thanks. Logic fixed and test coverage for expected alignment added. |
||||||
|
||||||
Value *NextDst = LoopBuilder.CreateInBoundsGEP( | ||||||
SetValue->getType(), CurrentDst, ConstantInt::get(TypeOfCount, 1)); | ||||||
CurrentDst->addIncoming(NextDst, LoopBB); | ||||||
|
||||||
Value *NewLoopCount = | ||||||
LoopBuilder.CreateSub(LoopCount, ConstantInt::get(TypeOfCount, 1)); | ||||||
LoopCount->addIncoming(NewLoopCount, LoopBB); | ||||||
|
||||||
LoopBuilder.CreateCondBr( | ||||||
LoopBuilder.CreateICmpNE(NewLoopCount, ConstantInt::get(TypeOfCount, 0)), | ||||||
LoopBB, NewBB); | ||||||
} | ||||||
|
||||||
static void createMemSetLoop(Instruction *InsertBefore, Value *DstAddr, | ||||||
Value *CopyLen, Value *SetValue, Align DstAlign, | ||||||
bool IsVolatile) { | ||||||
|
@@ -591,6 +641,16 @@ bool llvm::expandMemMoveAsLoop(MemMoveInst *Memmove, | |||||
} | ||||||
|
||||||
void llvm::expandMemSetAsLoop(MemSetInst *Memset) { | ||||||
if (isa<MemSetPatternInst>(Memset)) { | ||||||
return createMemSetPatternLoop( | ||||||
/* InsertBefore */ Memset, | ||||||
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.
Suggested change
|
||||||
/* DstAddr */ Memset->getRawDest(), | ||||||
/* Count */ Memset->getLength(), | ||||||
/* SetValue */ Memset->getValue(), | ||||||
/* Alignment */ Memset->getDestAlign().valueOrOne(), | ||||||
Memset->isVolatile()); | ||||||
} | ||||||
|
||||||
createMemSetLoop(/* InsertBefore */ Memset, | ||||||
/* DstAddr */ Memset->getRawDest(), | ||||||
/* CopyLen */ Memset->getLength(), | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.