[llvm] [IR] Add new Range attribute using new ConstantRange Attribute type (PR #83171)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 8 13:31:23 PST 2024


andjo403 wrote:

hmm ok will try to look in to that my initial thought was something like 
```
class ConstantRangeAttributeImpl
    : public EnumAttributeImpl,
      private TrailingObjects<StringAttributeImpl, APInt::WordType> {
  friend TrailingObjects;

  unsigned NumBits;
  unsigned LowerNumWords;
  unsigned UpperNumWords;

public:
  ConstantRangeAttributeImpl(Attribute::AttrKind Kind, const ConstantRange &CR)
      : EnumAttributeImpl(ConstantRangeAttrEntry, Kind),
        NumBits(CR.getBitWidth()), LowerNumWords(CR.getLower().getNumWords()),
        UpperNumWords(CR.getUpper().getNumWords()) {
    APInt::WordType *TrailingAPInt = getTrailingObjects<APInt::WordType>();
    // Some users rely on zero-termination.
    llvm::copy(ArrayRef<uint64_t>(CR.getLower().getRawData(),
                                  CR.getLower().getNumWords()),
               TrailingAPInt);
    llvm::copy(ArrayRef<uint64_t>(CR.getLower().getRawData(),
                                  CR.getLower().getNumWords()),
               &TrailingAPInt[LowerNumWords]);
  }

  ConstantRange getConstantRangeValue() const {
    return {
        APInt(NumBits, ArrayRef<uint64_t>(getTrailingObjects<APInt::WordType>(),
                                          LowerNumWords)),
        APInt(NumBits,
              ArrayRef<uint64_t>(getTrailingObjects<APInt::WordType>() +
                                     LowerNumWords,
                                 UpperNumWords))};
  }
};
```

https://github.com/llvm/llvm-project/pull/83171


More information about the llvm-commits mailing list