aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
diff options
context:
space:
mode:
authorAndreas Jonson <andjo403@hotmail.com>2024-03-08 16:20:04 +0100
committerGitHub <noreply@github.com>2024-03-08 23:20:04 +0800
commite0d49066c1acfa4ae0f0e8ea49b0f0b6bb3f1a25 (patch)
tree0b5064aaae0b1548e5ffdf2d67d68a7aff3dd4f7 /llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
parentc4a89f1538bbcac19628570b6b489cd9ce4265e2 (diff)
downloadllvm-e0d49066c1acfa4ae0f0e8ea49b0f0b6bb3f1a25.zip
llvm-e0d49066c1acfa4ae0f0e8ea49b0f0b6bb3f1a25.tar.gz
llvm-e0d49066c1acfa4ae0f0e8ea49b0f0b6bb3f1a25.tar.bz2
[IR] Add new Range attribute using new ConstantRange Attribute type (#83171)
implementation as discussed in https://discourse.llvm.org/t/rfc-metadata-attachments-for-function-arguments/76420
Diffstat (limited to 'llvm/lib/Bitcode/Writer/BitcodeWriter.cpp')
-rw-r--r--llvm/lib/Bitcode/Writer/BitcodeWriter.cpp61
1 files changed, 41 insertions, 20 deletions
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 656f2a6..597f493 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -844,6 +844,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
return bitc::ATTR_KIND_CORO_ONLY_DESTROY_WHEN_COMPLETE;
case Attribute::DeadOnUnwind:
return bitc::ATTR_KIND_DEAD_ON_UNWIND;
+ case Attribute::Range:
+ return bitc::ATTR_KIND_RANGE;
case Attribute::EndAttrKinds:
llvm_unreachable("Can not encode end-attribute kinds marker.");
case Attribute::None:
@@ -856,6 +858,39 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
llvm_unreachable("Trying to encode unknown attribute");
}
+static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
+ if ((int64_t)V >= 0)
+ Vals.push_back(V << 1);
+ else
+ Vals.push_back((-V << 1) | 1);
+}
+
+static void emitWideAPInt(SmallVectorImpl<uint64_t> &Vals, const APInt &A) {
+ // We have an arbitrary precision integer value to write whose
+ // bit width is > 64. However, in canonical unsigned integer
+ // format it is likely that the high bits are going to be zero.
+ // So, we only write the number of active words.
+ unsigned NumWords = A.getActiveWords();
+ const uint64_t *RawData = A.getRawData();
+ for (unsigned i = 0; i < NumWords; i++)
+ emitSignedInt64(Vals, RawData[i]);
+}
+
+static void emitConstantRange(SmallVectorImpl<uint64_t> &Record,
+ const ConstantRange &CR) {
+ unsigned BitWidth = CR.getBitWidth();
+ Record.push_back(BitWidth);
+ if (BitWidth > 64) {
+ Record.push_back(CR.getLower().getActiveWords() |
+ (uint64_t(CR.getUpper().getActiveWords()) << 32));
+ emitWideAPInt(Record, CR.getLower());
+ emitWideAPInt(Record, CR.getUpper());
+ } else {
+ emitSignedInt64(Record, CR.getLower().getSExtValue());
+ emitSignedInt64(Record, CR.getUpper().getSExtValue());
+ }
+}
+
void ModuleBitcodeWriter::writeAttributeGroupTable() {
const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
VE.getAttributeGroups();
@@ -889,13 +924,17 @@ void ModuleBitcodeWriter::writeAttributeGroupTable() {
Record.append(Val.begin(), Val.end());
Record.push_back(0);
}
- } else {
- assert(Attr.isTypeAttribute());
+ } else if (Attr.isTypeAttribute()) {
Type *Ty = Attr.getValueAsType();
Record.push_back(Ty ? 6 : 5);
Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
if (Ty)
Record.push_back(VE.getTypeID(Attr.getValueAsType()));
+ } else {
+ assert(Attr.isConstantRangeAttribute());
+ Record.push_back(7);
+ Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
+ emitConstantRange(Record, Attr.getValueAsConstantRange());
}
}
@@ -1716,24 +1755,6 @@ void ModuleBitcodeWriter::writeDIGenericSubrange(
Record.clear();
}
-static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
- if ((int64_t)V >= 0)
- Vals.push_back(V << 1);
- else
- Vals.push_back((-V << 1) | 1);
-}
-
-static void emitWideAPInt(SmallVectorImpl<uint64_t> &Vals, const APInt &A) {
- // We have an arbitrary precision integer value to write whose
- // bit width is > 64. However, in canonical unsigned integer
- // format it is likely that the high bits are going to be zero.
- // So, we only write the number of active words.
- unsigned NumWords = A.getActiveWords();
- const uint64_t *RawData = A.getRawData();
- for (unsigned i = 0; i < NumWords; i++)
- emitSignedInt64(Vals, RawData[i]);
-}
-
void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) {