Skip to content

Commit 698c31b

Browse files
author
Justin Lebar
committed
[NVPTX] Upgrade NVVM intrinsics in InstCombineCalls.
Summary: There are many NVVM intrinsics that we can't entirely get rid of, but that nonetheless often correspond to target-generic LLVM intrinsics. For example, if flush denormals to zero (ftz) is enabled, we can convert @llvm.nvvm.ceil.ftz.f to @llvm.ceil.f32. On the other hand, if ftz is disabled, we can't do this, because @llvm.ceil.f32 will be lowered to a non-ftz PTX instruction. In this case, we can, however, simplify the non-ftz nvvm ceil intrinsic, @llvm.nvvm.ceil.f, to @llvm.ceil.f32. These transformations are particularly useful because they let us constant fold instructions that appear in libdevice, the bitcode library that ships with CUDA and essentially functions as its libm. Reviewers: tra Subscribers: hfinkel, majnemer, llvm-commits Differential Revision: https://reviews.llvm.org/D28794 llvm-svn: 293244
1 parent 322c127 commit 698c31b

File tree

2 files changed

+721
-0
lines changed

2 files changed

+721
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,253 @@ static bool removeTriviallyEmptyRange(IntrinsicInst &I, unsigned StartID,
14981498
return false;
14991499
}
15001500

1501+
// Convert NVVM intrinsics to target-generic LLVM code where possible.
1502+
static Instruction *SimplifyNVVMIntrinsic(IntrinsicInst *II, InstCombiner &IC) {
1503+
// Each NVVM intrinsic we can simplify can be replaced with one of:
1504+
//
1505+
// * an LLVM intrinsic,
1506+
// * an LLVM cast operation,
1507+
// * an LLVM binary operation, or
1508+
// * ad-hoc LLVM IR for the particular operation.
1509+
1510+
// Some transformations are only valid when the module's
1511+
// flush-denormals-to-zero (ftz) setting is true/false, whereas other
1512+
// transformations are valid regardless of the module's ftz setting.
1513+
enum FtzRequirementTy {
1514+
FTZ_Any, // Any ftz setting is ok.
1515+
FTZ_MustBeOn, // Transformation is valid only if ftz is on.
1516+
FTZ_MustBeOff, // Transformation is valid only if ftz is off.
1517+
};
1518+
// Classes of NVVM intrinsics that can't be replaced one-to-one with a
1519+
// target-generic intrinsic, cast op, or binary op but that we can nonetheless
1520+
// simplify.
1521+
enum SpecialCase {
1522+
SPC_Reciprocal,
1523+
};
1524+
1525+
// SimplifyAction is a poor-man's variant (plus an additional flag) that
1526+
// represents how to replace an NVVM intrinsic with target-generic LLVM IR.
1527+
struct SimplifyAction {
1528+
// Invariant: At most one of these Optionals has a value.
1529+
Optional<Intrinsic::ID> IID;
1530+
Optional<Instruction::CastOps> CastOp;
1531+
Optional<Instruction::BinaryOps> BinaryOp;
1532+
Optional<SpecialCase> Special;
1533+
1534+
FtzRequirementTy FtzRequirement = FTZ_Any;
1535+
1536+
SimplifyAction() = default;
1537+
1538+
SimplifyAction(Intrinsic::ID IID, FtzRequirementTy FtzReq)
1539+
: IID(IID), FtzRequirement(FtzReq) {}
1540+
1541+
// Cast operations don't have anything to do with FTZ, so we skip that
1542+
// argument.
1543+
SimplifyAction(Instruction::CastOps CastOp) : CastOp(CastOp) {}
1544+
1545+
SimplifyAction(Instruction::BinaryOps BinaryOp, FtzRequirementTy FtzReq)
1546+
: BinaryOp(BinaryOp), FtzRequirement(FtzReq) {}
1547+
1548+
SimplifyAction(SpecialCase Special, FtzRequirementTy FtzReq)
1549+
: Special(Special), FtzRequirement(FtzReq) {}
1550+
};
1551+
1552+
// Try to generate a SimplifyAction describing how to replace our
1553+
// IntrinsicInstr with target-generic LLVM IR.
1554+
const SimplifyAction Action = [II]() -> SimplifyAction {
1555+
switch (II->getIntrinsicID()) {
1556+
1557+
// NVVM intrinsics that map directly to LLVM intrinsics.
1558+
case Intrinsic::nvvm_ceil_d:
1559+
return {Intrinsic::ceil, FTZ_Any};
1560+
case Intrinsic::nvvm_ceil_f:
1561+
return {Intrinsic::ceil, FTZ_MustBeOff};
1562+
case Intrinsic::nvvm_ceil_ftz_f:
1563+
return {Intrinsic::ceil, FTZ_MustBeOn};
1564+
case Intrinsic::nvvm_fabs_d:
1565+
return {Intrinsic::fabs, FTZ_Any};
1566+
case Intrinsic::nvvm_fabs_f:
1567+
return {Intrinsic::fabs, FTZ_MustBeOff};
1568+
case Intrinsic::nvvm_fabs_ftz_f:
1569+
return {Intrinsic::fabs, FTZ_MustBeOn};
1570+
case Intrinsic::nvvm_floor_d:
1571+
return {Intrinsic::floor, FTZ_Any};
1572+
case Intrinsic::nvvm_floor_f:
1573+
return {Intrinsic::floor, FTZ_MustBeOff};
1574+
case Intrinsic::nvvm_floor_ftz_f:
1575+
return {Intrinsic::floor, FTZ_MustBeOn};
1576+
case Intrinsic::nvvm_fma_rn_d:
1577+
return {Intrinsic::fma, FTZ_Any};
1578+
case Intrinsic::nvvm_fma_rn_f:
1579+
return {Intrinsic::fma, FTZ_MustBeOff};
1580+
case Intrinsic::nvvm_fma_rn_ftz_f:
1581+
return {Intrinsic::fma, FTZ_MustBeOn};
1582+
case Intrinsic::nvvm_fmax_d:
1583+
return {Intrinsic::maxnum, FTZ_Any};
1584+
case Intrinsic::nvvm_fmax_f:
1585+
return {Intrinsic::maxnum, FTZ_MustBeOff};
1586+
case Intrinsic::nvvm_fmax_ftz_f:
1587+
return {Intrinsic::maxnum, FTZ_MustBeOn};
1588+
case Intrinsic::nvvm_fmin_d:
1589+
return {Intrinsic::minnum, FTZ_Any};
1590+
case Intrinsic::nvvm_fmin_f:
1591+
return {Intrinsic::minnum, FTZ_MustBeOff};
1592+
case Intrinsic::nvvm_fmin_ftz_f:
1593+
return {Intrinsic::minnum, FTZ_MustBeOn};
1594+
case Intrinsic::nvvm_round_d:
1595+
return {Intrinsic::round, FTZ_Any};
1596+
case Intrinsic::nvvm_round_f:
1597+
return {Intrinsic::round, FTZ_MustBeOff};
1598+
case Intrinsic::nvvm_round_ftz_f:
1599+
return {Intrinsic::round, FTZ_MustBeOn};
1600+
case Intrinsic::nvvm_sqrt_rn_d:
1601+
return {Intrinsic::sqrt, FTZ_Any};
1602+
case Intrinsic::nvvm_sqrt_f:
1603+
// nvvm_sqrt_f is a special case. For most intrinsics, foo_ftz_f is the
1604+
// ftz version, and foo_f is the non-ftz version. But nvvm_sqrt_f adopts
1605+
// the ftz-ness of the surrounding code. sqrt_rn_f and sqrt_rn_ftz_f are
1606+
// the versions with explicit ftz-ness.
1607+
return {Intrinsic::sqrt, FTZ_Any};
1608+
case Intrinsic::nvvm_sqrt_rn_f:
1609+
return {Intrinsic::sqrt, FTZ_MustBeOff};
1610+
case Intrinsic::nvvm_sqrt_rn_ftz_f:
1611+
return {Intrinsic::sqrt, FTZ_MustBeOn};
1612+
case Intrinsic::nvvm_trunc_d:
1613+
return {Intrinsic::trunc, FTZ_Any};
1614+
case Intrinsic::nvvm_trunc_f:
1615+
return {Intrinsic::trunc, FTZ_MustBeOff};
1616+
case Intrinsic::nvvm_trunc_ftz_f:
1617+
return {Intrinsic::trunc, FTZ_MustBeOn};
1618+
1619+
// NVVM intrinsics that map to LLVM cast operations.
1620+
//
1621+
// Note that llvm's target-generic conversion operators correspond to the rz
1622+
// (round to zero) versions of the nvvm conversion intrinsics, even though
1623+
// most everything else here uses the rn (round to nearest even) nvvm ops.
1624+
case Intrinsic::nvvm_d2i_rz:
1625+
case Intrinsic::nvvm_f2i_rz:
1626+
case Intrinsic::nvvm_d2ll_rz:
1627+
case Intrinsic::nvvm_f2ll_rz:
1628+
return {Instruction::FPToSI};
1629+
case Intrinsic::nvvm_d2ui_rz:
1630+
case Intrinsic::nvvm_f2ui_rz:
1631+
case Intrinsic::nvvm_d2ull_rz:
1632+
case Intrinsic::nvvm_f2ull_rz:
1633+
return {Instruction::FPToUI};
1634+
case Intrinsic::nvvm_i2d_rz:
1635+
case Intrinsic::nvvm_i2f_rz:
1636+
case Intrinsic::nvvm_ll2d_rz:
1637+
case Intrinsic::nvvm_ll2f_rz:
1638+
return {Instruction::SIToFP};
1639+
case Intrinsic::nvvm_ui2d_rz:
1640+
case Intrinsic::nvvm_ui2f_rz:
1641+
case Intrinsic::nvvm_ull2d_rz:
1642+
case Intrinsic::nvvm_ull2f_rz:
1643+
return {Instruction::UIToFP};
1644+
1645+
// NVVM intrinsics that map to LLVM binary ops.
1646+
case Intrinsic::nvvm_add_rn_d:
1647+
return {Instruction::FAdd, FTZ_Any};
1648+
case Intrinsic::nvvm_add_rn_f:
1649+
return {Instruction::FAdd, FTZ_MustBeOff};
1650+
case Intrinsic::nvvm_add_rn_ftz_f:
1651+
return {Instruction::FAdd, FTZ_MustBeOn};
1652+
case Intrinsic::nvvm_mul_rn_d:
1653+
return {Instruction::FMul, FTZ_Any};
1654+
case Intrinsic::nvvm_mul_rn_f:
1655+
return {Instruction::FMul, FTZ_MustBeOff};
1656+
case Intrinsic::nvvm_mul_rn_ftz_f:
1657+
return {Instruction::FMul, FTZ_MustBeOn};
1658+
case Intrinsic::nvvm_div_rn_d:
1659+
return {Instruction::FDiv, FTZ_Any};
1660+
case Intrinsic::nvvm_div_rn_f:
1661+
return {Instruction::FDiv, FTZ_MustBeOff};
1662+
case Intrinsic::nvvm_div_rn_ftz_f:
1663+
return {Instruction::FDiv, FTZ_MustBeOn};
1664+
1665+
// The remainder of cases are NVVM intrinsics that map to LLVM idioms, but
1666+
// need special handling.
1667+
//
1668+
// We seem to be mising intrinsics for rcp.approx.{ftz.}f32, which is just
1669+
// as well.
1670+
case Intrinsic::nvvm_rcp_rn_d:
1671+
return {SPC_Reciprocal, FTZ_Any};
1672+
case Intrinsic::nvvm_rcp_rn_f:
1673+
return {SPC_Reciprocal, FTZ_MustBeOff};
1674+
case Intrinsic::nvvm_rcp_rn_ftz_f:
1675+
return {SPC_Reciprocal, FTZ_MustBeOn};
1676+
1677+
// We do not currently simplify intrinsics that give an approximate answer.
1678+
// These include:
1679+
//
1680+
// - nvvm_cos_approx_{f,ftz_f}
1681+
// - nvvm_ex2_approx_{d,f,ftz_f}
1682+
// - nvvm_lg2_approx_{d,f,ftz_f}
1683+
// - nvvm_sin_approx_{f,ftz_f}
1684+
// - nvvm_sqrt_approx_{f,ftz_f}
1685+
// - nvvm_rsqrt_approx_{d,f,ftz_f}
1686+
// - nvvm_div_approx_{ftz_d,ftz_f,f}
1687+
// - nvvm_rcp_approx_ftz_d
1688+
//
1689+
// Ideally we'd encode them as e.g. "fast call @llvm.cos", where "fast"
1690+
// means that fastmath is enabled in the intrinsic. Unfortunately only
1691+
// binary operators (currently) have a fastmath bit in SelectionDAG, so this
1692+
// information gets lost and we can't select on it.
1693+
//
1694+
// TODO: div and rcp are lowered to a binary op, so these we could in theory
1695+
// lower them to "fast fdiv".
1696+
1697+
default:
1698+
return {};
1699+
}
1700+
}();
1701+
1702+
// If Action.FtzRequirementTy is not satisfied by the module's ftz state, we
1703+
// can bail out now. (Notice that in the case that IID is not an NVVM
1704+
// intrinsic, we don't have to look up any module metadata, as
1705+
// FtzRequirementTy will be FTZ_Any.)
1706+
if (Action.FtzRequirement != FTZ_Any) {
1707+
bool FtzEnabled =
1708+
II->getFunction()->getFnAttribute("nvptx-f32ftz").getValueAsString() ==
1709+
"true";
1710+
1711+
if (FtzEnabled != (Action.FtzRequirement == FTZ_MustBeOn))
1712+
return nullptr;
1713+
}
1714+
1715+
// Simplify to target-generic intrinsic.
1716+
if (Action.IID) {
1717+
SmallVector<Value *, 4> Args(II->arg_operands());
1718+
// All the target-generic intrinsics currently of interest to us have one
1719+
// type argument, equal to that of the nvvm intrinsic's argument.
1720+
ArrayRef<Type *> Tys = {II->getArgOperand(0)->getType()};
1721+
return CallInst::Create(
1722+
Intrinsic::getDeclaration(II->getModule(), *Action.IID, Tys), Args);
1723+
}
1724+
1725+
// Simplify to target-generic binary op.
1726+
if (Action.BinaryOp)
1727+
return BinaryOperator::Create(*Action.BinaryOp, II->getArgOperand(0),
1728+
II->getArgOperand(1), II->getName());
1729+
1730+
// Simplify to target-generic cast op.
1731+
if (Action.CastOp)
1732+
return CastInst::Create(*Action.CastOp, II->getArgOperand(0), II->getType(),
1733+
II->getName());
1734+
1735+
// All that's left are the special cases.
1736+
if (!Action.Special)
1737+
return nullptr;
1738+
1739+
switch (*Action.Special) {
1740+
case SPC_Reciprocal:
1741+
// Simplify reciprocal.
1742+
return BinaryOperator::Create(
1743+
Instruction::FDiv, ConstantFP::get(II->getArgOperand(0)->getType(), 1),
1744+
II->getArgOperand(0), II->getName());
1745+
}
1746+
}
1747+
15011748
Instruction *InstCombiner::visitVAStartInst(VAStartInst &I) {
15021749
removeTriviallyEmptyRange(I, Intrinsic::vastart, Intrinsic::vaend, *this);
15031750
return nullptr;
@@ -1587,6 +1834,9 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
15871834
if (Changed) return II;
15881835
}
15891836

1837+
if (Instruction *I = SimplifyNVVMIntrinsic(II, *this))
1838+
return I;
1839+
15901840
auto SimplifyDemandedVectorEltsLow = [this](Value *Op, unsigned Width,
15911841
unsigned DemandedWidth) {
15921842
APInt UndefElts(Width, 0);

0 commit comments

Comments
 (0)