Why is this assertion here?

I’m trying to understand why this assertion is here.

LegalizeAction

getCondCodeAction(ISD::CondCode CC, EVT VT) const {

assert((unsigned)CC < array_lengthof(CondCodeActions) &&

(unsigned)VT.getSimpleVT().SimpleTy < sizeof(CondCodeActions[0])*4 &&

“Table isn’t big enough!”);

LegalizeAction Action = (LegalizeAction)

((CondCodeActions[CC] >> (2*VT.getSimpleVT().SimpleTy)) & 3);

assert(Action != Promote && “Can’t promote condition code!”);

return Action;

}

The first part of the assertion I can understand, but why is there an assertion

that there are only 32 types? in TOT LLVM if this code is called with v8f32,v2f64

or v4f64, this assert is triggered.

Shouldn’t the assert be:

(unsigned)VT.getSimpleVT().SimpleTy < MVT::MAX_ALLOWED_VALUETYPE &&

or

(unsigned)VT.getSimpleVT().SimpleTy < MVT::LAST_VECTOR_VALUETYPE &&

?

Thanks,

Micah

Well, I found out the reason why this assert is here, and this is problematic.

CondCodeActions only supports up to 32 different value types. Since we are past 32, what LLVM has is broken.

Currently the 4 different Legalize states are stored in successive bits and packed into a uin64_t, see TargetLowering.h.

/// CondCodeActions - For each condition code (ISD::CondCode) keep a

/// LegalizeAction that indicates how instruction selection should

/// deal with the condition code.

uint64_t CondCodeActions[ISD::SETCC_INVALID];

What I suggest is the following:

Change the definition of CondCodeAction to:

uint64_t CondCodeActions[ISD::SETCC_INVALID][2];

setCondCodeAction then becomes:

void setCondCodeAction(ISD::CondCode CC, MVT VT,

LegalizeAction Action) {

assert(VT < MVT::LAST_VALUETYPE &&

(unsigned)CC < array_lengthof(CondCodeActions) &&

“Table isn’t big enough!”);

CondCodeActions[(unsigned)CC][VT.SimplyTy >> 5] &= ~(uint64_t(3UL) << (VT.SimpleTy - 32)*2);

CondCodeActions[(unsigned)CC][VT.SimpleTy >> 5] |= (uint64_t)Action << (VT.SimpleTy - 32)*2;

}

getCondCodeAction then becomes:

LegalizeAction

getCondCodeAction(ISD::CondCode CC, EVT VT) const {

assert((unsigned)CC < array_lengthof(CondCodeActions) &&

(unsigned)VT.getSimpleVT().SimpleTy < MVT::LAST_VECTOR_VALUETYPE &&

“Table isn’t big enough!”);

LegalizeAction Action = (LegalizeAction)

((CondCodeActions[CC][VT.getSimpleVT().SimpleTy >> 5] >> (2*(VT.getSimpleVT().SimpleTy - 32))) & 3);

assert(Action != Promote && “Can’t promote condition code!”);

return Action;

}

The other options are to use a BitVector, or to have a different array for each Legalized action. This approach however seems to use the minimum amount of memory/instructions.

Ideas?

Micah

Well, I found out the reason why this assert is here, and this is
problematic.

CondCodeActions only supports up to 32 different value types. Since
we are past 32, what LLVM has is broken.

Currently the 4 different Legalize states are stored in successive
bits and packed into a uin64_t, see TargetLowering.h. ///
CondCodeActions - For each condition code (ISD::CondCode) keep a ///
LegalizeAction that indicates how instruction selection should ///
deal with the condition code. uint64_t
CondCodeActions[ISD::SETCC_INVALID];

What I suggest is the following:
Change the definition of CondCodeAction to:
  uint64_t CondCodeActions[ISD::SETCC_INVALID][2];

setCondCodeAction then becomes:
void setCondCodeAction(ISD::CondCode CC, MVT VT,
                         LegalizeAction Action) {
    assert(VT < MVT::LAST_VALUETYPE &&
           (unsigned)CC < array_lengthof(CondCodeActions) &&
           "Table isn't big enough!");
    CondCodeActions[(unsigned)CC][VT.SimplyTy >> 5] &=
~(uint64_t(3UL) << (VT.SimpleTy - 32)*2);
CondCodeActions[(unsigned)CC][VT.SimpleTy >> 5] |= (uint64_t)Action
<< (VT.SimpleTy - 32)*2; }

getCondCodeAction then becomes:
LegalizeAction
  getCondCodeAction(ISD::CondCode CC, EVT VT) const {
    assert((unsigned)CC < array_lengthof(CondCodeActions) &&
           (unsigned)VT.getSimpleVT().SimpleTy <
MVT::LAST_VECTOR_VALUETYPE && "Table isn't big enough!");
    LegalizeAction Action = (LegalizeAction)
      ((CondCodeActions[CC][VT.getSimpleVT().SimpleTy >> 5] >>
(2*(VT.getSimpleVT().SimpleTy - 32))) & 3); assert(Action != Promote
&& "Can't promote condition code!"); return Action;
  }

The other options are to use a BitVector, or to have a different
array for each Legalized action. This approach however seems to use
the minimum amount of memory/instructions.

Ideas?

Your approach seems very similar to how I've fixed this problem
locally (I think that the only difference is the order of the arrays).
I've attached my version of the fix so that you can compare. I think
that, as a practical matter, this is the most economical approach.

-Hal

ccsize.patch (2.22 KB)

Yeah just the ordering are the real difference. Also, I use shifts and masks instead of conditionals and modules. My patch is attached. For me either patch is fine, but what LLVM has now is broken.

Either patch is fine, just need approval from someone to submit.

Micah

support_ccodes_over_32_types.patch (1.5 KB)

Yeah just the ordering are the real difference. Also, I use shifts
and masks instead of conditionals and modules. My patch is attached.

Sounds good. I'll send a review of your patch to the commits list.

-Hal