diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h --- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h +++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h @@ -2072,6 +2072,10 @@ return false; } + /// Return true if the function is a viable candidate for machine function + /// splitting. The criteria for if a function can be split may vary by target. + virtual bool isFunctionSafeToSplit(const MachineFunction &MF) const; + /// Produce the expression describing the \p MI loading a value into /// the physical register \p Reg. This hook should only be used with /// \p MIs belonging to VReg-less functions. diff --git a/llvm/lib/CodeGen/MachineFunctionSplitter.cpp b/llvm/lib/CodeGen/MachineFunctionSplitter.cpp --- a/llvm/lib/CodeGen/MachineFunctionSplitter.cpp +++ b/llvm/lib/CodeGen/MachineFunctionSplitter.cpp @@ -35,6 +35,7 @@ #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/CodeGen/Passes.h" +#include "llvm/CodeGen/TargetInstrInfo.h" #include "llvm/IR/Function.h" #include "llvm/InitializePasses.h" #include "llvm/Support/CommandLine.h" @@ -135,22 +136,10 @@ if (!UseProfileData && !SplitAllEHCode) return false; - // TODO: We don't split functions where a section attribute has been set - // since the split part may not be placed in a contiguous region. It may also - // be more beneficial to augment the linker to ensure contiguous layout of - // split functions within the same section as specified by the attribute. - if (MF.getFunction().hasSection() || - MF.getFunction().hasFnAttribute("implicit-section-name")) + const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); + if (!TII.isFunctionSafeToSplit(MF)) return false; - // We don't want to proceed further for cold functions - // or functions of unknown hotness. Lukewarm functions have no prefix. - std::optional SectionPrefix = MF.getFunction().getSectionPrefix(); - if (SectionPrefix && - (*SectionPrefix == "unlikely" || *SectionPrefix == "unknown")) { - return false; - } - // Renumbering blocks here preserves the order of the blocks as // sortBasicBlocksAndUpdateBranches uses the numeric identifier to sort // blocks. Preserving the order of blocks is essential to retaining decisions diff --git a/llvm/lib/CodeGen/TargetInstrInfo.cpp b/llvm/lib/CodeGen/TargetInstrInfo.cpp --- a/llvm/lib/CodeGen/TargetInstrInfo.cpp +++ b/llvm/lib/CodeGen/TargetInstrInfo.cpp @@ -1374,6 +1374,26 @@ return (DefCycle != -1 && DefCycle <= 1); } +bool TargetInstrInfo::isFunctionSafeToSplit(const MachineFunction &MF) const { + // TODO: We don't split functions where a section attribute has been set + // since the split part may not be placed in a contiguous region. It may also + // be more beneficial to augment the linker to ensure contiguous layout of + // split functions within the same section as specified by the attribute. + if (MF.getFunction().hasSection() || + MF.getFunction().hasFnAttribute("implicit-section-name")) + return false; + + // We don't want to proceed further for cold functions + // or functions of unknown hotness. Lukewarm functions have no prefix. + std::optional SectionPrefix = MF.getFunction().getSectionPrefix(); + if (SectionPrefix && + (*SectionPrefix == "unlikely" || *SectionPrefix == "unknown")) { + return false; + } + + return true; +} + std::optional TargetInstrInfo::describeLoadedValue(const MachineInstr &MI, Register Reg) const { diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.h b/llvm/lib/Target/AArch64/AArch64InstrInfo.h --- a/llvm/lib/Target/AArch64/AArch64InstrInfo.h +++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.h @@ -325,6 +325,8 @@ std::optional isAddImmediate(const MachineInstr &MI, Register Reg) const override; + bool isFunctionSafeToSplit(const MachineFunction &MF) const override; + std::optional describeLoadedValue(const MachineInstr &MI, Register Reg) const override; diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp --- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp +++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp @@ -8368,6 +8368,17 @@ return std::nullopt; } +bool AArch64InstrInfo::isFunctionSafeToSplit(const MachineFunction &MF) const { + // Functions cannot be split to different sections on AArch64 if they have + // a red zone. This is because relaxing a cross-section branch may require + // incrementing the stack pointer to spill a register, which would overwrite + // the red zone. + if (MF.getInfo()->hasRedZone().value_or(true)) + return false; + + return TargetInstrInfo::isFunctionSafeToSplit(MF); +} + std::optional AArch64InstrInfo::describeLoadedValue(const MachineInstr &MI, Register Reg) const { diff --git a/llvm/test/CodeGen/AArch64/machine-function-splitter.ll b/llvm/test/CodeGen/AArch64/machine-function-splitter.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/AArch64/machine-function-splitter.ll @@ -0,0 +1,54 @@ +; RUN: llc < %s -mtriple=aarch64-unknown-linux-gnu -enable-split-machine-functions -aarch64-redzone | FileCheck %s -check-prefix=MFS-REDZONE + +define i32 @nosplit_redzone(i1 zeroext %0, i32 %a, i32 %b) nounwind !prof !14 !section_prefix !15 { +;; Check that cold blocks in functions with red zones aren't split. +; MFS-REDZONE-LABEL: nosplit_redzone +; MFS-REDZONE-NOT: nosplit_redzone.cold: + %a.addr = alloca i32, align 4 + %b.addr = alloca i32, align 4 + %x = alloca i32, align 4 + + br i1 %0, label %2, label %3, !prof !16 + +2: ; preds = %1 + store i32 %a, ptr %a.addr, align 4 + store i32 %b, ptr %b.addr, align 4 + br label %4 + +3: ; preds = %1 + store i32 %a, ptr %b.addr, align 4 + store i32 %b, ptr %a.addr, align 4 + br label %4 + +4: ; preds = %3, %2 + %tmp = load i32, ptr %a.addr, align 4 + %tmp1 = load i32, ptr %b.addr, align 4 + %add = add nsw i32 %tmp, %tmp1 + store i32 %add, ptr %x, align 4 + %tmp2 = load i32, ptr %x, align 4 + ret i32 %tmp2 +} + +declare i32 @bar() +declare i32 @baz() +declare i32 @bam() +declare i32 @qux() + +!llvm.module.flags = !{!0} +!0 = !{i32 1, !"ProfileSummary", !1} +!1 = !{!2, !3, !4, !5, !6, !7, !8, !9} +!2 = !{!"ProfileFormat", !"InstrProf"} +!3 = !{!"TotalCount", i64 10000} +!4 = !{!"MaxCount", i64 10} +!5 = !{!"MaxInternalCount", i64 1} +!6 = !{!"MaxFunctionCount", i64 1000} +!7 = !{!"NumCounts", i64 3} +!8 = !{!"NumFunctions", i64 5} +!9 = !{!"DetailedSummary", !10} +!10 = !{!11, !12, !13} +!11 = !{i32 10000, i64 100, i32 1} +!12 = !{i32 999900, i64 100, i32 1} +!13 = !{i32 999999, i64 1, i32 2} +!14 = !{!"function_entry_count", i64 9000} +!15 = !{!"function_section_prefix", !"hot"} +!16 = !{!"branch_weights", i32 7000, i32 0}