Skip to content

[RISCV] Implement RISCVTTIImpl::shouldConsiderAddressTypePromotion for RISCV #102560

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
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
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,7 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,

// Disable strict node mutation.
IsStrictFPEnabled = true;
EnableExtLdPromotion = true;

// Let the subtarget decide if a predictable select is more expensive than the
// corresponding branch. This information is used in CGP/SelectOpt to decide
Expand Down
32 changes: 32 additions & 0 deletions llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1989,3 +1989,35 @@ bool RISCVTTIImpl::areInlineCompatible(const Function *Caller,
// target-features.
return (CallerBits & CalleeBits) == CalleeBits;
}

/// See if \p I should be considered for address type promotion. We check if \p
/// I is a sext with right type and used in memory accesses. If it used in a
/// "complex" getelementptr, we allow it to be promoted without finding other
/// sext instructions that sign extended the same initial value. A getelementptr
/// is considered as "complex" if it has more than 2 operands.
bool RISCVTTIImpl::shouldConsiderAddressTypePromotion(
const Instruction &I, bool &AllowPromotionWithoutCommonHeader) {
bool Considerable = false;
AllowPromotionWithoutCommonHeader = false;
if (!isa<SExtInst>(&I))
return false;
Type *ConsideredSExtType =
Type::getInt64Ty(I.getParent()->getParent()->getContext());
if (I.getType() != ConsideredSExtType)
return false;
// See if the sext is the one with the right type and used in at least one
// GetElementPtrInst.
for (const User *U : I.users()) {
if (const GetElementPtrInst *GEPInst = dyn_cast<GetElementPtrInst>(U)) {
Considerable = true;
// A getelementptr is considered as "complex" if it has more than 2
// operands. We will promote a SExt used in such complex GEP as we
// expect some computation to be merged if they are done on 64 bits.
if (GEPInst->getNumOperands() > 2) {
AllowPromotionWithoutCommonHeader = true;
break;
}
}
}
return Considerable;
}
4 changes: 3 additions & 1 deletion llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
bool shouldFoldTerminatingConditionAfterLSR() const {
return true;
}

bool
shouldConsiderAddressTypePromotion(const Instruction &I,
bool &AllowPromotionWithoutCommonHeader);
std::optional<unsigned> getMinPageSize() const { return 4096; }
};

Expand Down
95 changes: 95 additions & 0 deletions llvm/test/CodeGen/RISCV/riscv-codegen-prepare-atp.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt -passes='require<profile-summary>,function(codegenprepare)' < %s -S | FileCheck %s

target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "riscv64"

%struct.match_state = type { i64, i64 }

; %add is also promoted by forking an extra sext.
define void @promoteTwoOne(i32 %i, i32 %j, ptr %P1, ptr %P2 ) {
; CHECK-LABEL: define void @promoteTwoOne(
; CHECK-SAME: i32 [[I:%.*]], i32 [[J:%.*]], ptr [[P1:%.*]], ptr [[P2:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[S2:%.*]] = sext i32 [[I]] to i64
; CHECK-NEXT: [[PROMOTED2:%.*]] = sext i32 [[J]] to i64
; CHECK-NEXT: [[S:%.*]] = add nsw i64 [[S2]], [[PROMOTED2]]
; CHECK-NEXT: [[ADDR1:%.*]] = getelementptr inbounds i64, ptr [[P1]], i64 [[S]]
; CHECK-NEXT: store i64 [[S]], ptr [[ADDR1]], align 8
; CHECK-NEXT: [[ADDR2:%.*]] = getelementptr inbounds i64, ptr [[P2]], i64 [[S2]]
; CHECK-NEXT: store i64 [[S2]], ptr [[ADDR2]], align 8
; CHECK-NEXT: ret void
;
entry:
%add = add nsw i32 %i, %j
%s = sext i32 %add to i64
%addr1 = getelementptr inbounds i64, ptr %P1, i64 %s
store i64 %s, ptr %addr1
%s2 = sext i32 %i to i64
%addr2 = getelementptr inbounds i64, ptr %P2, i64 %s2
store i64 %s2, ptr %addr2
ret void
}

; Both %add1 and %add2 are promoted by forking extra sexts.
define void @promoteTwoTwo(i32 %i, i32 %j, i32 %k, ptr %P1, ptr %P2) {
; CHECK-LABEL: define void @promoteTwoTwo(
; CHECK-SAME: i32 [[I:%.*]], i32 [[J:%.*]], i32 [[K:%.*]], ptr [[P1:%.*]], ptr [[P2:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[PROMOTED3:%.*]] = sext i32 [[J]] to i64
; CHECK-NEXT: [[PROMOTED4:%.*]] = sext i32 [[I]] to i64
; CHECK-NEXT: [[S:%.*]] = add nsw i64 [[PROMOTED3]], [[PROMOTED4]]
; CHECK-NEXT: [[ADDR1:%.*]] = getelementptr inbounds i64, ptr [[P1]], i64 [[S]]
; CHECK-NEXT: store i64 [[S]], ptr [[ADDR1]], align 8
; CHECK-NEXT: [[PROMOTED2:%.*]] = sext i32 [[K]] to i64
; CHECK-NEXT: [[S2:%.*]] = add nsw i64 [[PROMOTED3]], [[PROMOTED2]]
; CHECK-NEXT: [[ADDR2:%.*]] = getelementptr inbounds i64, ptr [[P2]], i64 [[S2]]
; CHECK-NEXT: store i64 [[S2]], ptr [[ADDR2]], align 8
; CHECK-NEXT: ret void
;
entry:
%add1 = add nsw i32 %j, %i
%s = sext i32 %add1 to i64
%addr1 = getelementptr inbounds i64, ptr %P1, i64 %s
store i64 %s, ptr %addr1
%add2 = add nsw i32 %j, %k
%s2 = sext i32 %add2 to i64
%addr2 = getelementptr inbounds i64, ptr %P2, i64 %s2
store i64 %s2, ptr %addr2
ret void
}

define i64 @promoteGEPSunk(i1 %cond, ptr %base, i32 %i) {
; CHECK-LABEL: define i64 @promoteGEPSunk(
; CHECK-SAME: i1 [[COND:%.*]], ptr [[BASE:%.*]], i32 [[I:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[PROMOTED1:%.*]] = sext i32 [[I]] to i64
; CHECK-NEXT: [[S:%.*]] = add nsw i64 [[PROMOTED1]], 1
; CHECK-NEXT: [[ADDR:%.*]] = getelementptr inbounds i64, ptr [[BASE]], i64 [[S]]
; CHECK-NEXT: [[S2:%.*]] = add nsw i64 [[PROMOTED1]], 2
; CHECK-NEXT: [[ADDR2:%.*]] = getelementptr inbounds i64, ptr [[BASE]], i64 [[S2]]
; CHECK-NEXT: br i1 [[COND]], label [[IF_THEN:%.*]], label [[IF_THEN2:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[V:%.*]] = load i64, ptr [[ADDR]], align 8
; CHECK-NEXT: [[V2:%.*]] = load i64, ptr [[ADDR2]], align 8
; CHECK-NEXT: [[R:%.*]] = add i64 [[V]], [[V2]]
; CHECK-NEXT: ret i64 [[R]]
; CHECK: if.then2:
; CHECK-NEXT: ret i64 0
;
entry:
%add = add nsw i32 %i, 1
%s = sext i32 %add to i64
%addr = getelementptr inbounds i64, ptr %base, i64 %s
%add2 = add nsw i32 %i, 2
%s2 = sext i32 %add2 to i64
%addr2 = getelementptr inbounds i64, ptr %base, i64 %s2
br i1 %cond, label %if.then, label %if.then2
if.then:
%v = load i64, ptr %addr
%v2 = load i64, ptr %addr2
%r = add i64 %v, %v2
ret i64 %r
if.then2:
ret i64 0;
}
Loading