Skip to content

[IR][TRE] Support associative intrinsics #74226

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 1 commit into from
Dec 5, 2023
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
23 changes: 15 additions & 8 deletions llvm/include/llvm/IR/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/OperandTraits.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
Expand Down Expand Up @@ -1095,18 +1096,24 @@ class ConstantExpr : public Constant {
static Constant *getExactLogBase2(Constant *C);

/// Return the identity constant for a binary opcode.
/// The identity constant C is defined as X op C = X and C op X = X for every
/// X when the binary operation is commutative. If the binop is not
/// commutative, callers can acquire the operand 1 identity constant by
/// setting AllowRHSConstant to true. For example, any shift has a zero
/// identity constant for operand 1: X shift 0 = X.
/// If this is a fadd/fsub operation and we don't care about signed zeros,
/// then setting NSZ to true returns the identity +0.0 instead of -0.0.
/// Return nullptr if the operator does not have an identity constant.
/// If the binop is not commutative, callers can acquire the operand 1
/// identity constant by setting AllowRHSConstant to true. For example, any
/// shift has a zero identity constant for operand 1: X shift 0 = X. If this
/// is a fadd/fsub operation and we don't care about signed zeros, then
/// setting NSZ to true returns the identity +0.0 instead of -0.0. Return
/// nullptr if the operator does not have an identity constant.
static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty,
bool AllowRHSConstant = false,
bool NSZ = false);

static Constant *getIntrinsicIdentity(Intrinsic::ID, Type *Ty);

/// Return the identity constant for a binary or intrinsic Instruction.
/// The identity constant C is defined as X op C = X and C op X = X where C
/// and X are the first two operands, and the operation is commutative.
static Constant *getIdentity(Instruction *I, Type *Ty,
bool AllowRHSConstant = false, bool NSZ = false);

/// Return the absorbing element for the given binary
/// operation, i.e. a constant C such that X op C = C and C op X = C for
/// every X. For example, this returns zero for integer multiplication.
Expand Down
12 changes: 12 additions & 0 deletions llvm/include/llvm/IR/IntrinsicInst.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ class IntrinsicInst : public CallInst {
return getCalledFunction()->getIntrinsicID();
}

bool isAssociative() const {
switch (getIntrinsicID()) {
case Intrinsic::smax:
case Intrinsic::smin:
case Intrinsic::umax:
case Intrinsic::umin:
return true;
default:
return false;
}
}

/// Return true if swapping the first two arguments to the intrinsic produces
/// the same result.
bool isCommutative() const {
Expand Down
26 changes: 26 additions & 0 deletions llvm/lib/IR/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2556,6 +2556,32 @@ Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty,
}
}

Constant *ConstantExpr::getIntrinsicIdentity(Intrinsic::ID ID, Type *Ty) {
switch (ID) {
case Intrinsic::umax:
return Constant::getNullValue(Ty);
case Intrinsic::umin:
return Constant::getAllOnesValue(Ty);
case Intrinsic::smax:
return Constant::getIntegerValue(
Ty, APInt::getSignedMinValue(Ty->getIntegerBitWidth()));
case Intrinsic::smin:
return Constant::getIntegerValue(
Ty, APInt::getSignedMaxValue(Ty->getIntegerBitWidth()));
default:
return nullptr;
}
}

Constant *ConstantExpr::getIdentity(Instruction *I, Type *Ty,
bool AllowRHSConstant, bool NSZ) {
if (I->isBinaryOp())
return getBinOpIdentity(I->getOpcode(), Ty, AllowRHSConstant, NSZ);
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
return getIntrinsicIdentity(II->getIntrinsicID(), Ty);
return nullptr;
}

Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
switch (Opcode) {
default:
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/IR/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,8 @@ const DebugLoc &Instruction::getStableDebugLoc() const {
}

bool Instruction::isAssociative() const {
if (auto *II = dyn_cast<IntrinsicInst>(this))
return II->isAssociative();
unsigned Opcode = getOpcode();
if (isAssociative(Opcode))
return true;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/Reassociate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2554,7 +2554,7 @@ ReassociatePass::BuildPairMap(ReversePostOrderTraversal<Function *> &RPOT) {
// Make a "pairmap" of how often each operand pair occurs.
for (BasicBlock *BI : RPOT) {
for (Instruction &I : *BI) {
if (!I.isAssociative())
if (!I.isAssociative() || !I.isBinaryOp())
continue;

// Ignore nodes that aren't at the root of trees.
Expand Down
14 changes: 10 additions & 4 deletions llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,14 @@ static bool canTransformAccumulatorRecursion(Instruction *I, CallInst *CI) {
if (!I->isAssociative() || !I->isCommutative())
return false;

assert(I->getNumOperands() == 2 &&
"Associative/commutative operations should have 2 args!");
assert(I->getNumOperands() >= 2 &&
"Associative/commutative operations should have at least 2 args!");

if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
// Accumulators must have an identity.
if (!ConstantExpr::getIntrinsicIdentity(II->getIntrinsicID(), I->getType()))
return false;
}

// Exactly one operand should be the result of the call instruction.
if ((I->getOperand(0) == CI && I->getOperand(1) == CI) ||
Expand Down Expand Up @@ -569,8 +575,8 @@ void TailRecursionEliminator::insertAccumulator(Instruction *AccRecInstr) {
for (pred_iterator PI = PB; PI != PE; ++PI) {
BasicBlock *P = *PI;
if (P == &F.getEntryBlock()) {
Constant *Identity = ConstantExpr::getBinOpIdentity(
AccRecInstr->getOpcode(), AccRecInstr->getType());
Constant *Identity =
ConstantExpr::getIdentity(AccRecInstr, AccRecInstr->getType());
AccPN->addIncoming(Identity, P);
} else {
AccPN->addIncoming(AccPN, P);
Expand Down
40 changes: 39 additions & 1 deletion llvm/test/Transforms/TailCallElim/accum_recursion.ll
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ define i64 @test3_fib(i64 %n) nounwind readnone {
; CHECK-NEXT: ]
; CHECK: bb1:
; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[N_TR]], -1
; CHECK-NEXT: [[RECURSE1:%.*]] = tail call i64 @test3_fib(i64 [[TMP0]]) #[[ATTR1:[0-9]+]]
; CHECK-NEXT: [[RECURSE1:%.*]] = tail call i64 @test3_fib(i64 [[TMP0]]) #[[ATTR2:[0-9]+]]
; CHECK-NEXT: [[TMP1]] = add i64 [[N_TR]], -2
; CHECK-NEXT: [[ACCUMULATE]] = add nsw i64 [[ACCUMULATOR_TR]], [[RECURSE1]]
; CHECK-NEXT: br label [[TAILRECURSE]]
Expand Down Expand Up @@ -290,3 +290,41 @@ return:
%retval.0 = phi i32 [ %accumulate1, %if.then2 ], [ %accumulate2, %if.end3 ], [ 0, %entry ]
ret i32 %retval.0
}

%struct.ListNode = type { i32, ptr }

; We cannot TRE commutative, non-associative intrinsics
define i32 @test_non_associative_sadd_sat(ptr %a) local_unnamed_addr {
; CHECK-LABEL: define i32 @test_non_associative_sadd_sat(
; CHECK-SAME: ptr [[A:%.*]]) local_unnamed_addr {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq ptr [[A]], null
; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label [[COMMON_RET6:%.*]], label [[IF_END:%.*]]
; CHECK: common.ret6:
; CHECK-NEXT: ret i32 -1
; CHECK: if.end:
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[A]], align 4
; CHECK-NEXT: [[NEXT:%.*]] = getelementptr inbounds [[STRUCT_LISTNODE:%.*]], ptr [[A]], i64 0, i32 1
; CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr [[NEXT]], align 8
; CHECK-NEXT: [[CALL:%.*]] = tail call i32 @test_non_associative_sadd_sat(ptr [[TMP1]])
; CHECK-NEXT: [[DOTSROA_SPECULATED:%.*]] = tail call i32 @llvm.sadd.sat.i32(i32 [[TMP0]], i32 [[CALL]])
; CHECK-NEXT: ret i32 [[DOTSROA_SPECULATED]]
;
entry:
%tobool.not = icmp eq ptr %a, null
br i1 %tobool.not, label %common.ret6, label %if.end

common.ret6: ; preds = %entry, %if.end
%common.ret6.op = phi i32 [ %.sroa.speculated, %if.end ], [ -1, %entry ]
ret i32 %common.ret6.op

if.end: ; preds = %entry
%0 = load i32, ptr %a
%next = getelementptr inbounds %struct.ListNode, ptr %a, i64 0, i32 1
%1 = load ptr, ptr %next
%call = tail call i32 @test_non_associative_sadd_sat(ptr %1)
%.sroa.speculated = tail call i32 @llvm.sadd.sat.i32(i32 %0, i32 %call)
br label %common.ret6
}

declare i32 @llvm.sadd.sat.i32(i32, i32)
Loading