Skip to content

[RISCV] Increase default tail duplication threshold to 6 at -O3 #98873

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
merged 5 commits into from
Aug 1, 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
6 changes: 6 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3763,6 +3763,12 @@ RISCVInstrInfo::getSerializableMachineMemOperandTargetFlags() const {
return ArrayRef(TargetFlags);
}

unsigned RISCVInstrInfo::getTailDuplicateSize(CodeGenOptLevel OptLevel) const {
return OptLevel >= CodeGenOptLevel::Aggressive
? STI.getTailDupAggressiveThreshold()
: 2;
}

// Returns true if this is the sext.w pattern, addiw rd, rs1, 0.
bool RISCV::isSEXT_W(const MachineInstr &MI) {
return MI.getOpcode() == RISCV::ADDIW && MI.getOperand(1).isReg() &&
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
getSerializableMachineMemOperandTargetFlags() const override;

unsigned getTailDuplicateSize(CodeGenOptLevel OptLevel) const override;

unsigned getUndefInitOpcode(unsigned RegClassID) const override {
switch (RegClassID) {
case RISCV::VRRegClassID:
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Target/RISCV/RISCVProcessors.td
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class RISCVTuneInfo {
bits<32> MaxPrefetchIterationsAhead = -1;

bits<32> MinimumJumpTableEntries = 5;

// Tail duplication threshold at -O3.
bits<32> TailDupAggressiveThreshold = 6;
}

def RISCVTuneInfoTable : GenericTable {
Expand All @@ -29,7 +32,7 @@ def RISCVTuneInfoTable : GenericTable {
let Fields = ["Name", "PrefFunctionAlignment", "PrefLoopAlignment",
"CacheLineSize", "PrefetchDistance",
"MinPrefetchStride", "MaxPrefetchIterationsAhead",
"MinimumJumpTableEntries"];
"MinimumJumpTableEntries", "TailDupAggressiveThreshold"];
}

def getRISCVTuneInfo : SearchIndex {
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/Target/RISCV/RISCVSubtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ struct RISCVTuneInfo {
unsigned MaxPrefetchIterationsAhead;

unsigned MinimumJumpTableEntries;

// Tail duplication threshold at -O3.
unsigned TailDupAggressiveThreshold;
};

#define GET_RISCVTuneInfoTable_DECL
Expand Down Expand Up @@ -300,6 +303,10 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {

unsigned getMinimumJumpTableEntries() const;

unsigned getTailDupAggressiveThreshold() const {
return TuneInfo->TailDupAggressiveThreshold;
}

bool supportsInitUndef() const override { return hasVInstructions(); }
};
} // End llvm namespace
Expand Down
79 changes: 79 additions & 0 deletions llvm/test/CodeGen/RISCV/riscv-tail-dup-size.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mtriple=riscv64 -mattr=+m -O2 < %s | FileCheck %s --check-prefix=CHECK-O2
; RUN: llc -mtriple=riscv64 -mattr=+m -O3 < %s | FileCheck %s --check-prefix=CHECK-O3

; RUN: llc -mtriple=riscv64 -mattr=+m -tail-dup-size=2 < %s | FileCheck %s --check-prefix=CHECK-O2
; RUN: llc -mtriple=riscv64 -mattr=+m -tail-dup-placement-threshold=2 < %s | FileCheck %s --check-prefix=CHECK-O2
; RUN: llc -mtriple=riscv64 -mattr=+m -tail-dup-placement-threshold=6 < %s | FileCheck %s --check-prefix=CHECK-O3

@a = external global i32
@b = external global i32
@c = external global i32

declare i32 @foo(i32)

define i32 @test(i32 %n) {
; CHECK-O2-LABEL: test:
; CHECK-O2: # %bb.0: # %entry
; CHECK-O2-NEXT: sext.w a1, a0
; CHECK-O2-NEXT: blez a1, .LBB0_2
; CHECK-O2-NEXT: # %bb.1: # %if.then
; CHECK-O2-NEXT: lui a1, %hi(a)
; CHECK-O2-NEXT: lw a1, %lo(a)(a1)
; CHECK-O2-NEXT: mul a0, a1, a0
; CHECK-O2-NEXT: j .LBB0_3
; CHECK-O2-NEXT: .LBB0_2: # %if.else
; CHECK-O2-NEXT: lui a1, %hi(b)
; CHECK-O2-NEXT: lw a1, %lo(b)(a1)
; CHECK-O2-NEXT: divw a0, a1, a0
; CHECK-O2-NEXT: .LBB0_3: # %if.end
; CHECK-O2-NEXT: lui a1, %hi(c)
; CHECK-O2-NEXT: lw a1, %lo(c)(a1)
; CHECK-O2-NEXT: addi a0, a0, -1
; CHECK-O2-NEXT: mulw a0, a0, a1
; CHECK-O2-NEXT: tail foo
;
; CHECK-O3-LABEL: test:
; CHECK-O3: # %bb.0: # %entry
; CHECK-O3-NEXT: sext.w a1, a0
; CHECK-O3-NEXT: blez a1, .LBB0_2
; CHECK-O3-NEXT: # %bb.1: # %if.then
; CHECK-O3-NEXT: lui a1, %hi(a)
; CHECK-O3-NEXT: lw a1, %lo(a)(a1)
; CHECK-O3-NEXT: mul a0, a1, a0
; CHECK-O3-NEXT: lui a1, %hi(c)
; CHECK-O3-NEXT: lw a1, %lo(c)(a1)
; CHECK-O3-NEXT: addi a0, a0, -1
; CHECK-O3-NEXT: mulw a0, a0, a1
; CHECK-O3-NEXT: tail foo
; CHECK-O3-NEXT: .LBB0_2: # %if.else
; CHECK-O3-NEXT: lui a1, %hi(b)
; CHECK-O3-NEXT: lw a1, %lo(b)(a1)
; CHECK-O3-NEXT: divw a0, a1, a0
; CHECK-O3-NEXT: lui a1, %hi(c)
; CHECK-O3-NEXT: lw a1, %lo(c)(a1)
; CHECK-O3-NEXT: addi a0, a0, -1
; CHECK-O3-NEXT: mulw a0, a0, a1
; CHECK-O3-NEXT: tail foo
entry:
%cmp = icmp sgt i32 %n, 0
br i1 %cmp, label %if.then, label %if.else

if.then:
%va = load i32, ptr @a
%mul = mul nsw i32 %va, %n
br label %if.end

if.else:
%vb = load i32, ptr @b
%div = sdiv i32 %vb, %n
br label %if.end

if.end:
%phi = phi i32 [ %mul, %if.then ], [ %div, %if.else ]
%vc = load i32, ptr @c
%add = add nsw i32 %phi, -1
%arg = mul i32 %add, %vc
%ret = tail call i32 @foo(i32 %arg)
ret i32 %ret
}
Loading