Skip to content

Teach the backend to make references to swift_async_extendedFramePointerFlags weak if it emits it #3695

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

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
5 changes: 5 additions & 0 deletions llvm/include/llvm/CodeGen/AsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,11 @@ class AsmPrinter : public MachineFunctionPass {

/// This method decides whether the specified basic block requires a label.
bool shouldEmitLabelForBasicBlock(const MachineBasicBlock &MBB) const;

protected:
virtual bool shouldEmitWeakSwiftAsyncExtendedFramePointerFlags() const {
return false;
}
};

} // end namespace llvm
Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,17 @@ bool AsmPrinter::doFinalization(Module &M) {
continue;
OutStreamer->emitSymbolAttribute(getSymbol(&GO), MCSA_WeakReference);
}
if (shouldEmitWeakSwiftAsyncExtendedFramePointerFlags()) {
auto SymbolName = "swift_async_extendedFramePointerFlags";
auto Global = M.getGlobalVariable(SymbolName);
if (!Global) {
auto Int8PtrTy = Type::getInt8PtrTy(M.getContext());
Global = new GlobalVariable(M, Int8PtrTy, false,
GlobalValue::ExternalWeakLinkage, nullptr,
SymbolName);
OutStreamer->emitSymbolAttribute(getSymbol(Global), MCSA_WeakReference);
}
}
}

// Print aliases in topological order, that is, for each alias a = b,
Expand Down
14 changes: 14 additions & 0 deletions llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class AArch64AsmPrinter : public AsmPrinter {
StackMaps SM;
FaultMaps FM;
const AArch64Subtarget *STI;
bool ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags = false;

public:
AArch64AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
Expand Down Expand Up @@ -202,6 +203,10 @@ class AArch64AsmPrinter : public AsmPrinter {
using MInstToMCSymbol = std::map<const MachineInstr *, MCSymbol *>;

MInstToMCSymbol LOHInstToLabel;

bool shouldEmitWeakSwiftAsyncExtendedFramePointerFlags() const override {
return ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags;
}
};

} // end anonymous namespace
Expand Down Expand Up @@ -1225,6 +1230,15 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
if (emitPseudoExpansionLowering(*OutStreamer, MI))
return;

if (MI->getOpcode() == AArch64::ADRP) {
for (auto &Opd : MI->operands()) {
if (Opd.isSymbol() && StringRef(Opd.getSymbolName()) ==
"swift_async_extendedFramePointerFlags") {
ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags = true;
}
}
}

if (AArch64FI->getLOHRelated().count(MI)) {
// Generate a label for LOH related instruction
MCSymbol *LOHLabel = createTempSymbol("loh");
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/X86/X86AsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
FaultMaps FM;
std::unique_ptr<MCCodeEmitter> CodeEmitter;
bool EmitFPOData = false;
bool ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags = false;
bool NeedsRetpoline = false;

// This utility class tracks the length of a stackmap instruction's 'shadow'.
Expand Down Expand Up @@ -148,6 +149,10 @@ class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
bool runOnMachineFunction(MachineFunction &MF) override;
void emitFunctionBodyStart() override;
void emitFunctionBodyEnd() override;

bool shouldEmitWeakSwiftAsyncExtendedFramePointerFlags() const override {
return ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags;
}
};

} // end namespace llvm
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/Target/X86/X86MCInstLower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2371,6 +2371,15 @@ void X86AsmPrinter::emitInstruction(const MachineInstr *MI) {
const X86RegisterInfo *RI =
MF->getSubtarget<X86Subtarget>().getRegisterInfo();

if (MI->getOpcode() == X86::OR64rm) {
for (auto &Opd : MI->operands()) {
if (Opd.isSymbol() && StringRef(Opd.getSymbolName()) ==
"swift_async_extendedFramePointerFlags") {
ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags = true;
}
}
}

// Add a comment about EVEX-2-VEX compression for AVX-512 instrs that
// are compressed from EVEX encoding to VEX encoding.
if (TM.Options.MCOptions.ShowMCEncoding) {
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/AArch64/swift-dynamic-async-frame.ll
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
; CHECK-DYNAMIC: adrp x16, _swift_async_extendedFramePointerFlags@GOTPAGE
; CHECK-DYNAMIC: ldr x16, [x16, _swift_async_extendedFramePointerFlags@GOTPAGEOFF]
; CHECK-DYNAMIC: orr x29, x29, x16
; CHECK-DYNAMIC: .weak_reference _swift_async_extendedFramePointerFlags

; CHECK-DYNAMIC-32-LABEL: foo:
; CHECK-DYNAMIC-32: adrp x16, _swift_async_extendedFramePointerFlags@GOTPAGE
; CHECK-DYNAMIC-32: ldr w16, [x16, _swift_async_extendedFramePointerFlags@GOTPAGEOFF]
; CHECK-DYNAMIC-32: orr x29, x29, x16, lsl #32
; CHECK-DYNAMIC-32: .weak_reference _swift_async_extendedFramePointerFlags

define void @foo(i8* swiftasync) "frame-pointer"="all" {
ret void
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/X86/swift-dynamic-async-frame.ll
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

; CHECK-DYNAMIC-LABEL: foo:
; CHECK-DYNAMIC: orq _swift_async_extendedFramePointerFlags@GOTPCREL(%rip), %rbp
; CHECK-DYNAMIC: .weak_reference _swift_async_extendedFramePointerFlags

; CHECK-NEVER-LABEL: foo:
; CHECK-NEVER-NOT: btsq $60, %rbp
Expand Down