[clang] [Clang][X86] Lower __stosb to llvm.memset.inline (PR #247215)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 30 07:47:43 PST 2026
https://github.com/ebraminio created https://github.com/llvm/llvm-project/pull/247215
### Problem
`__stosb` is currently lowered to a volatile `llvm.memset`. That works for
hosted targets, but in a freestanding/CRT-free Windows build it leaves an
external `memset` reference and fails to link:
```text
error LNK2019: unresolved external symbol memset referenced in function repro_fill
```
This was found while investigating CRT-free Windows builds and LLVM issue
#33522. `__movsb` does not have the same problem because it is implemented in
`intrin.h` with inline asm, so the two intrinsics behave inconsistently.
### Change
Lower `__stosb` to `llvm.memset.inline` instead of `llvm.memset`. The inline
intrinsic is guaranteed not to introduce a libcall, while retaining standard
memory-operation semantics for the optimizer and sanitizers. The existing IR
test is updated from `llvm.memset` to `llvm.memset.inline` for i386/x86_64.
`LoopIdiomRecognize` already refuses to convert `llvm.memset.inline` into
`llvm.memset` via its `isForceInlined()` checks, so the equivalent of the
guard proposed in #227650 is already present on current `main`. This patch
does not depend on #227650 being applied separately here.
### Validation
The touched `clang/test/CodeGen/ms-intrinsics.c` passes for i386, Thumb,
x86_64, and AArch64 with the rebuilt compiler.
A minimal freestanding reproducer:
- baseline clang: undefined `memset`, no-CRT DLL link fails with LNK2019
- patched clang: no undefined `memset`, no-CRT DLL link succeeds
### Scope
This fixes the libcall/CRT-free part of #33522. It does **not** guarantee a
literal `rep stosb`; `llvm.memset.inline` still lets the backend choose the
inline expansion. If exact `rep stosb` selection is required, the separate
inline-asm approach should be discussed independently.
>From d9680a658a17c4e2d7880a242959fd4e3c9e3905 Mon Sep 17 00:00:00 2001
From: Ebrahim <ebrahim at gnu.org>
Date: Mon, 30 Nov 2026 19:16:16 +0330
Subject: [PATCH] [Clang][X86] Lower __stosb to llvm.memset.inline
Use llvm.memset.inline instead of a volatile memset so freestanding
builds do not get an external memset reference.
---
clang/lib/CodeGen/TargetBuiltins/X86.cpp | 7 ++++---
clang/test/CodeGen/ms-intrinsics.c | 4 ++--
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/clang/lib/CodeGen/TargetBuiltins/X86.cpp b/clang/lib/CodeGen/TargetBuiltins/X86.cpp
index dfcef74e3b2ec2..4409540174825d 100644
--- a/clang/lib/CodeGen/TargetBuiltins/X86.cpp
+++ b/clang/lib/CodeGen/TargetBuiltins/X86.cpp
@@ -2986,9 +2986,10 @@ Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
return Builder.CreateCall(F);
}
case X86::BI__stosb: {
- // We treat __stosb as a volatile memset - it may not generate "rep stosb"
- // instruction, but it will create a memset that won't be optimized away.
- return Builder.CreateMemSet(Ops[0], Ops[1], Ops[2], Align(1), true);
+ // Use memset.inline so the backend must expand the store without
+ // introducing a libcall. A regular memset can become an undefined
+ // memset reference in freestanding / CRT-free builds.
+ return Builder.CreateMemSetInline(Ops[0], Align(1), Ops[1], Ops[2], true);
}
case X86::BI__ud2:
// llvm.trap makes a ud2a instruction on x86.
diff --git a/clang/test/CodeGen/ms-intrinsics.c b/clang/test/CodeGen/ms-intrinsics.c
index d3c71786d9e567..58ba090898c8dd 100644
--- a/clang/test/CodeGen/ms-intrinsics.c
+++ b/clang/test/CodeGen/ms-intrinsics.c
@@ -23,12 +23,12 @@ void test__stosb(unsigned char *Dest, unsigned char Data, size_t Count) {
}
// CHECK-I386: define{{.*}}void @test__stosb
-// CHECK-I386: tail call void @llvm.memset.p0.i32(ptr align 1 %Dest, i8 %Data, i32 %Count, i1 true)
+// CHECK-I386: tail call void @llvm.memset.inline.p0.i32(ptr align 1 %Dest, i8 %Data, i32 %Count, i1 true)
// CHECK-I386: ret void
// CHECK-I386: }
// CHECK-X64: define{{.*}}void @test__stosb
-// CHECK-X64: tail call void @llvm.memset.p0.i64(ptr align 1 %Dest, i8 %Data, i64 %Count, i1 true)
+// CHECK-X64: tail call void @llvm.memset.inline.p0.i64(ptr align 1 %Dest, i8 %Data, i64 %Count, i1 true)
// CHECK-X64: ret void
// CHECK-X64: }
More information about the cfe-commits
mailing list