Skip to content

[DAG] Handle truncated splat in isBoolConstant #145473

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions llvm/include/llvm/CodeGen/SelectionDAG.h
Original file line number Diff line number Diff line change
Expand Up @@ -2479,8 +2479,7 @@ class SelectionDAG {

/// Check if a value \op N is a constant using the target's BooleanContent for
/// its type.
LLVM_ABI std::optional<bool>
isBoolConstant(SDValue N, bool AllowTruncation = false) const;
LLVM_ABI std::optional<bool> isBoolConstant(SDValue N) const;

/// Set CallSiteInfo to be associated with Node.
void addCallSiteInfo(const SDNode *Node, CallSiteInfo &&CallInfo) {
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/CodeGen/TargetLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -4375,6 +4375,11 @@ class LLVM_ABI TargetLowering : public TargetLoweringBase {
Op.getOpcode() == ISD::SPLAT_VECTOR_PARTS;
}

/// Return true if the given select/vselect should be considered canonical and
/// not be transformed. Currently only used for "vselect (not Cond), N1, N2 ->
/// vselect Cond, N2, N1".
virtual bool isTargetCanonicalSelect(SDNode *N) const { return false; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(style) add a description of what a canonical select means

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a very specific hack, can you do this another way

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could try moving the AVX512 fold to X86DAGToDAGISel / isel patterns? @phoebewang WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X86DAGToDAGISel sounds good to me. We have hundreds of AVX512 instructions, adding patterns for them is too verbose.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phoebewang I've created #145724 to investigate this - I'm not yet sure how much yak shaving will be necessary to address the remaining regressions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me like there are only broadcasting missing except for a scalar case?


struct DAGCombinerInfo {
void *DC; // The DAG Combiner object.
CombineLevel Level;
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12967,8 +12967,9 @@ SDValue DAGCombiner::visitVSELECT(SDNode *N) {
return V;

// vselect (not Cond), N1, N2 -> vselect Cond, N2, N1
if (SDValue F = extractBooleanFlip(N0, DAG, TLI, false))
return DAG.getSelect(DL, VT, F, N2, N1);
if (!TLI.isTargetCanonicalSelect(N))
if (SDValue F = extractBooleanFlip(N0, DAG, TLI, false))
return DAG.getSelect(DL, VT, F, N2, N1);

// select (sext m), (add X, C), X --> (add X, (and C, (sext m))))
if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N2 && N1->hasOneUse() &&
Expand Down
11 changes: 6 additions & 5 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10349,7 +10349,7 @@ SDValue SelectionDAG::simplifySelect(SDValue Cond, SDValue T, SDValue F) {

// select true, T, F --> T
// select false, T, F --> F
if (auto C = isBoolConstant(Cond, /*AllowTruncation=*/true))
if (auto C = isBoolConstant(Cond))
return *C ? T : F;

// select ?, T, T --> T
Expand Down Expand Up @@ -13562,13 +13562,14 @@ bool SelectionDAG::isConstantFPBuildVectorOrConstantFP(SDValue N) const {
return false;
}

std::optional<bool> SelectionDAG::isBoolConstant(SDValue N,
bool AllowTruncation) const {
ConstantSDNode *Const = isConstOrConstSplat(N, false, AllowTruncation);
std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
ConstantSDNode *Const =
isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
if (!Const)
return std::nullopt;

const APInt &CVal = Const->getAPIntValue();
EVT VT = N->getValueType(0);
const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
switch (TLI->getBooleanContents(N.getValueType())) {
case TargetLowering::ZeroOrOneBooleanContent:
if (CVal.isOne())
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4975,6 +4975,16 @@ X86TargetLowering::getTargetConstantFromLoad(LoadSDNode *LD) const {
return getTargetConstantFromNode(LD);
}

bool X86TargetLowering::isTargetCanonicalSelect(SDNode *N) const {
// Do not fold (vselect not(C), X, 0s) to (vselect C, Os, X)
SDValue Cond = N->getOperand(0);
SDValue RHS = N->getOperand(2);
EVT CondVT = Cond.getValueType();
return N->getOpcode() == ISD::VSELECT && Subtarget.hasAVX512() &&
CondVT.getVectorElementType() == MVT::i1 &&
ISD::isBuildVectorAllZeros(RHS.getNode());
}

// Extract raw constant bits from constant pools.
static bool getTargetConstantBitsFromNode(SDValue Op, unsigned EltSizeInBits,
APInt &UndefElts,
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/X86/X86ISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,8 @@ namespace llvm {
TargetLowering::isTargetCanonicalConstantNode(Op);
}

bool isTargetCanonicalSelect(SDNode *N) const override;

const Constant *getTargetConstantFromLoad(LoadSDNode *LD) const override;

SDValue unwrapAddress(SDValue N) const override;
Expand Down
Loading
Loading