Skip to content

Commit 4d052a7

Browse files
authored
[Intrinsics][PreISelIntrinsicLowering] llvm.memset.inline length no longer needs to be constant (#95397)
As requested in https://discourse.llvm.org/t/rfc-introducing-an-llvm-memset-pattern-inline-intrinsic/79496 this patch removes the requirement that the length of llvm.memset.inline is a constant, and adjusts PreISelIntrinsicLowering so it supports expanding such the intrinsic in the case it has a non-constant length.
1 parent 1205371 commit 4d052a7

File tree

6 files changed

+47
-13
lines changed

6 files changed

+47
-13
lines changed

llvm/docs/LangRef.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15220,7 +15220,6 @@ at the destination location. If the argument is known to be
1522015220
aligned to some boundary, this can be specified as an attribute on
1522115221
the argument.
1522215222

15223-
``len`` must be a constant expression.
1522415223
If ``<len>`` is 0, it is no-op modulo the behavior of attributes attached to
1522515224
the arguments.
1522615225
If ``<len>`` is not a well-defined value, the behavior is undefined.

llvm/include/llvm/IR/IntrinsicInst.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,9 +1240,6 @@ class MemSetInst : public MemSetBase<MemIntrinsic> {
12401240
/// This class wraps the llvm.memset.inline intrinsic.
12411241
class MemSetInlineInst : public MemSetInst {
12421242
public:
1243-
ConstantInt *getLength() const {
1244-
return cast<ConstantInt>(MemSetInst::getLength());
1245-
}
12461243
// Methods for support type inquiry through isa, cast, and dyn_cast:
12471244
static bool classof(const IntrinsicInst *I) {
12481245
return I->getIntrinsicID() == Intrinsic::memset_inline;

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ def int_memset_inline
10011001
[llvm_anyptr_ty, llvm_i8_ty, llvm_anyint_ty, llvm_i1_ty],
10021002
[IntrWriteMem, IntrArgMemOnly, IntrWillReturn, IntrNoFree, IntrNoCallback,
10031003
NoCapture<ArgIndex<0>>, WriteOnly<ArgIndex<0>>,
1004-
ImmArg<ArgIndex<2>>, ImmArg<ArgIndex<3>>]>;
1004+
ImmArg<ArgIndex<3>>]>;
10051005

10061006
// FIXME: Add version of these floating point intrinsics which allow non-default
10071007
// rounding modes and FP exception handling.

llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,19 @@ bool PreISelIntrinsicLowering::expandMemIntrinsicUses(Function &F) const {
263263

264264
break;
265265
}
266+
case Intrinsic::memset_inline: {
267+
// Only expand llvm.memset.inline with non-constant length in this
268+
// codepath, leaving the current SelectionDAG expansion for constant
269+
// length memset intrinsics undisturbed.
270+
auto *Memset = cast<MemSetInlineInst>(Inst);
271+
if (isa<ConstantInt>(Memset->getLength()))
272+
break;
273+
274+
expandMemSetAsLoop(Memset);
275+
Changed = true;
276+
Memset->eraseFromParent();
277+
break;
278+
}
266279
default:
267280
llvm_unreachable("unhandled intrinsic");
268281
}
@@ -280,6 +293,7 @@ bool PreISelIntrinsicLowering::lowerIntrinsics(Module &M) const {
280293
case Intrinsic::memcpy:
281294
case Intrinsic::memmove:
282295
case Intrinsic::memset:
296+
case Intrinsic::memset_inline:
283297
Changed |= expandMemIntrinsicUses(F);
284298
break;
285299
case Intrinsic::load_relative:
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -mtriple=x86_64-pc-linux-gnu -passes=pre-isel-intrinsic-lowering -S -o - %s | FileCheck %s
3+
4+
; Constant length memset.inline should be left unmodified.
5+
define void @memset_32(ptr %a, i8 %value) nounwind {
6+
; CHECK-LABEL: define void @memset_32(
7+
; CHECK-SAME: ptr [[A:%.*]], i8 [[VALUE:%.*]]) #[[ATTR0:[0-9]+]] {
8+
; CHECK-NEXT: tail call void @llvm.memset.inline.p0.i64(ptr [[A]], i8 [[VALUE]], i64 32, i1 false)
9+
; CHECK-NEXT: ret void
10+
;
11+
tail call void @llvm.memset.inline.p0.i64(ptr %a, i8 %value, i64 32, i1 0)
12+
ret void
13+
}
14+
15+
define void @memset_x(ptr %a, i8 %value, i64 %x) nounwind {
16+
; CHECK-LABEL: define void @memset_x(
17+
; CHECK-SAME: ptr [[A:%.*]], i8 [[VALUE:%.*]], i64 [[X:%.*]]) #[[ATTR0]] {
18+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i64 0, [[X]]
19+
; CHECK-NEXT: br i1 [[TMP1]], label %[[SPLIT:.*]], label %[[LOADSTORELOOP:.*]]
20+
; CHECK: [[LOADSTORELOOP]]:
21+
; CHECK-NEXT: [[TMP2:%.*]] = phi i64 [ 0, [[TMP0:%.*]] ], [ [[TMP4:%.*]], %[[LOADSTORELOOP]] ]
22+
; CHECK-NEXT: [[TMP3:%.*]] = getelementptr inbounds i8, ptr [[A]], i64 [[TMP2]]
23+
; CHECK-NEXT: store i8 [[VALUE]], ptr [[TMP3]], align 1
24+
; CHECK-NEXT: [[TMP4]] = add i64 [[TMP2]], 1
25+
; CHECK-NEXT: [[TMP5:%.*]] = icmp ult i64 [[TMP4]], [[X]]
26+
; CHECK-NEXT: br i1 [[TMP5]], label %[[LOADSTORELOOP]], label %[[SPLIT]]
27+
; CHECK: [[SPLIT]]:
28+
; CHECK-NEXT: ret void
29+
;
30+
tail call void @llvm.memset.inline.p0.i64(ptr %a, i8 %value, i64 %x, i1 0)
31+
ret void
32+
}

llvm/test/Verifier/intrinsic-immarg.ll

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@ define void @memset_inline_is_volatile(ptr %dest, i8 %value, i1 %is.volatile) {
7171
ret void
7272
}
7373

74-
define void @memset_inline_variable_size(ptr %dest, i8 %value, i32 %size) {
75-
; CHECK: immarg operand has non-immediate parameter
76-
; CHECK-NEXT: i32 %size
77-
; CHECK-NEXT: call void @llvm.memset.inline.p0.i32(ptr %dest, i8 %value, i32 %size, i1 true)
78-
call void @llvm.memset.inline.p0.i32(ptr %dest, i8 %value, i32 %size, i1 true)
79-
ret void
80-
}
81-
8274

8375
declare i64 @llvm.objectsize.i64.p0(ptr, i1, i1, i1)
8476
define void @objectsize(ptr %ptr, i1 %a, i1 %b, i1 %c) {

0 commit comments

Comments
 (0)