Skip to content
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

Backport https://github.com/llvm/llvm-project/commit/5c3beb7b1e26d38b… #25

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions llvm/lib/MC/WinCOFFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1190,12 +1190,14 @@ void WinCOFFObjectWriter::reset() {
bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
bool InSet, bool IsPCRel) const {
// MS LINK expects to be able to replace all references to a function with a
// thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
// away any relocations to functions.
// Don't drop relocations between functions, even if they are in the same text
// section. Multiple Visual C++ linker features depend on having the
// relocations present. The /INCREMENTAL flag will cause these relocations to
// point to thunks, and the /GUARD:CF flag assumes that it can use relocations
// to approximate the set of all address taken functions. LLD's implementation
// of /GUARD:CF also relies on the existance of these relocations.
uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
if (Asm.isIncrementalLinkerCompatible() &&
(Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
return false;
return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
InSet, IsPCRel);
Expand Down
11 changes: 8 additions & 3 deletions llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,13 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
eraseInstruction(M);
return true;
}
// If the size is zero, remove the memcpy. This also prevents infinite loops
// in processMemSetMemCpyDependence, which is a no-op for zero-length memcpys.

MemoryUseOrDef *MA = MSSA->getMemoryAccess(M);
if (!MA)
// Degenerate case: memcpy marked as not accessing memory.
return false;

// If copying from a constant, try to turn the memcpy into a memset.
if (auto *GV = dyn_cast<GlobalVariable>(M->getSource()))
Expand All @@ -1436,8 +1443,7 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
IRBuilder<> Builder(M);
Instruction *NewM = Builder.CreateMemSet(
M->getRawDest(), ByteVal, M->getLength(), M->getDestAlign(), false);
auto *LastDef =
cast<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(M));
auto *LastDef = cast<MemoryDef>(MA);
auto *NewAccess =
MSSAU->createMemoryAccessAfter(NewM, LastDef, LastDef);
MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
Expand All @@ -1448,7 +1454,6 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
}

BatchAAResults BAA(*AA);
MemoryUseOrDef *MA = MSSA->getMemoryAccess(M);
// FIXME: Not using getClobberingMemoryAccess() here due to PR54682.
MemoryAccess *AnyClobber = MA->getDefiningAccess();
MemoryLocation DestLoc = MemoryLocation::getForDest(M);
Expand Down
25 changes: 17 additions & 8 deletions llvm/test/MC/COFF/diff.s
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
// RUN: llvm-mc -filetype=obj -triple i686-pc-mingw32 %s | llvm-readobj -S --sr --sd - | FileCheck %s

// COFF resolves differences between labels in the same section, unless that
// label is declared with function type.

.section baz, "xr"
.def X
.scl 2;
.type 32;
.endef
.globl X
X:
mov Y-X+42, %eax
retl

.def Y
.scl 2;
.type 32;
.endef
.globl Y
Y:
retl
Expand All @@ -30,6 +25,11 @@ _foobar: # @foobar
# %bb.0:
ret

.globl _baz
_baz:
calll _foobar
retl

.data
.globl _rust_crate # @rust_crate
.align 4
Expand All @@ -39,6 +39,15 @@ _rust_crate:
.long _foobar-_rust_crate
.long _foobar-_rust_crate

// Even though _baz and _foobar are in the same .text section, we keep the
// relocation for compatibility with the VC linker's /guard:cf and /incremental
// flags, even on mingw.

// CHECK: Name: .text
// CHECK: Relocations [
// CHECK-NEXT: 0x12 IMAGE_REL_I386_REL32 _foobar
// CHECK-NEXT: ]

// CHECK: Name: .data
// CHECK: Relocations [
// CHECK-NEXT: 0x4 IMAGE_REL_I386_DIR32 _foobar
Expand Down
9 changes: 9 additions & 0 deletions llvm/test/Transforms/MemCpyOpt/memcpy.ll
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,15 @@ define void @byval_param_noalias_metadata(ptr align 4 byval(i32) %ptr) {
ret void
}

define void @memcpy_memory_none(ptr %p, ptr %p2, i64 %size) {
; CHECK-LABEL: @memcpy_memory_none(
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[P:%.*]], ptr [[P2:%.*]], i64 [[SIZE:%.*]], i1 false) #[[ATTR6:[0-9]+]]
; CHECK-NEXT: ret void
;
call void @llvm.memcpy.p0.p0.i64(ptr %p, ptr %p2, i64 %size, i1 false) memory(none)
ret void
}

!0 = !{!0}
!1 = !{!1, !0}
!2 = !{!1}