Index: llvm/include/llvm/Bitcode/BitcodeReader.h =================================================================== --- llvm/include/llvm/Bitcode/BitcodeReader.h +++ llvm/include/llvm/Bitcode/BitcodeReader.h @@ -255,6 +255,8 @@ return false; } + APInt readWideAPInt(ArrayRef Vals, unsigned TypeBits); + const std::error_category &BitcodeErrorCategory(); enum class BitcodeError { CorruptedBitcode = 1 }; inline std::error_code make_error_code(BitcodeError E) { Index: llvm/include/llvm/IR/DebugInfoMetadata.h =================================================================== --- llvm/include/llvm/IR/DebugInfoMetadata.h +++ llvm/include/llvm/IR/DebugInfoMetadata.h @@ -348,22 +348,26 @@ friend class LLVMContextImpl; friend class MDNode; - int64_t Value; - DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value, + APInt Value; + DIEnumerator(LLVMContext &C, StorageType Storage, APInt Value, bool IsUnsigned, ArrayRef Ops) : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops), Value(Value) { SubclassData32 = IsUnsigned; } + DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value, + bool IsUnsigned, ArrayRef Ops) + : DIEnumerator(C, Storage, APInt(64, Value, !IsUnsigned), IsUnsigned, + Ops) {} ~DIEnumerator() = default; - static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value, + static DIEnumerator *getImpl(LLVMContext &Context, APInt Value, bool IsUnsigned, StringRef Name, StorageType Storage, bool ShouldCreate = true) { return getImpl(Context, Value, IsUnsigned, getCanonicalMDString(Context, Name), Storage, ShouldCreate); } - static DIEnumerator *getImpl(LLVMContext &Context, int64_t Value, + static DIEnumerator *getImpl(LLVMContext &Context, APInt Value, bool IsUnsigned, MDString *Name, StorageType Storage, bool ShouldCreate = true); @@ -372,14 +376,22 @@ } public: - DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, bool IsUnsigned, StringRef Name), + DEFINE_MDNODE_GET(DIEnumerator, + (int64_t Value, bool IsUnsigned, StringRef Name), + (APInt(64, Value, !IsUnsigned), IsUnsigned, Name)) + DEFINE_MDNODE_GET(DIEnumerator, + (int64_t Value, bool IsUnsigned, MDString *Name), + (APInt(64, Value, !IsUnsigned), IsUnsigned, Name)) + DEFINE_MDNODE_GET(DIEnumerator, + (APInt Value, bool IsUnsigned, StringRef Name), (Value, IsUnsigned, Name)) - DEFINE_MDNODE_GET(DIEnumerator, (int64_t Value, bool IsUnsigned, MDString *Name), + DEFINE_MDNODE_GET(DIEnumerator, + (APInt Value, bool IsUnsigned, MDString *Name), (Value, IsUnsigned, Name)) TempDIEnumerator clone() const { return cloneImpl(); } - int64_t getValue() const { return Value; } + const APInt &getValue() const { return Value; } bool isUnsigned() const { return SubclassData32; } StringRef getName() const { return getStringOperand(0); } Index: llvm/lib/AsmParser/LLParser.cpp =================================================================== --- llvm/lib/AsmParser/LLParser.cpp +++ llvm/lib/AsmParser/LLParser.cpp @@ -3872,6 +3872,10 @@ DISPFlagField() : MDFieldImpl(DISubprogram::SPFlagZero) {} }; +struct MDAPSIntField : public MDFieldImpl { + MDAPSIntField() : ImplTy(APSInt()) {} +}; + struct MDSignedField : public MDFieldImpl { int64_t Min; int64_t Max; @@ -3950,6 +3954,16 @@ namespace llvm { +template <> +bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDAPSIntField &Result) { + if (Lex.getKind() != lltok::APSInt) + return TokError("expected integer"); + + Result.assign(Lex.getAPSIntVal()); + Lex.Lex(); + return false; +} + template <> bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDUnsignedField &Result) { @@ -4281,27 +4295,6 @@ return true; } -template <> -bool LLParser::ParseMDField(LocTy Loc, StringRef Name, - MDSignedOrUnsignedField &Result) { - if (Lex.getKind() != lltok::APSInt) - return false; - - if (Lex.getAPSIntVal().isSigned()) { - MDSignedField Res = Result.A; - if (ParseMDField(Loc, Name, Res)) - return true; - Result.assign(Res); - return false; - } - - MDUnsignedField Res = Result.B; - if (ParseMDField(Loc, Name, Res)) - return true; - Result.assign(Res); - return false; -} - template <> bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result) { LocTy ValueLoc = Lex.getLoc(); @@ -4475,17 +4468,20 @@ bool LLParser::ParseDIEnumerator(MDNode *&Result, bool IsDistinct) { #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ REQUIRED(name, MDStringField, ); \ - REQUIRED(value, MDSignedOrUnsignedField, ); \ + REQUIRED(value, MDAPSIntField, ); \ OPTIONAL(isUnsigned, MDBoolField, (false)); PARSE_MD_FIELDS(); #undef VISIT_MD_FIELDS - if (isUnsigned.Val && value.isMDSignedField()) + if (isUnsigned.Val && value.Val.isNegative()) return TokError("unsigned enumerator with negative value"); - int64_t Value = value.isMDSignedField() - ? value.getMDSignedValue() - : static_cast(value.getMDUnsignedValue()); + APSInt Value(value.Val); + // Add a leading zero so that unsigned values with the msb set are not + // mistaken for negative values when used for signed enumerators. + if (!isUnsigned.Val && value.Val.isUnsigned() && value.Val.isSignBitSet()) + Value = Value.zext(Value.getBitWidth() + 1); + Result = GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val)); Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -2318,7 +2318,7 @@ return Error::success(); } -static APInt readWideAPInt(ArrayRef Vals, unsigned TypeBits) { +APInt llvm::readWideAPInt(ArrayRef Vals, unsigned TypeBits) { SmallVector Words(Vals.size()); transform(Vals, Words.begin(), BitcodeReader::decodeSignRotatedValue); Index: llvm/lib/Bitcode/Reader/MetadataLoader.cpp =================================================================== --- llvm/lib/Bitcode/Reader/MetadataLoader.cpp +++ llvm/lib/Bitcode/Reader/MetadataLoader.cpp @@ -1277,14 +1277,24 @@ break; } case bitc::METADATA_ENUMERATOR: { - if (Record.size() != 3) + if (Record.size() < 3) return error("Invalid record"); IsDistinct = Record[0] & 1; bool IsUnsigned = Record[0] & 2; + bool IsBigInt = Record[0] & 4; + APInt Value; + + if (IsBigInt) { + const uint64_t BitWidth = Record[1]; + const unsigned NumWords = Record.size() - 3; + Value = readWideAPInt(makeArrayRef(&Record[3], NumWords), BitWidth); + } else + Value = APInt(64, unrotateSign(Record[1]), !IsUnsigned); + MetadataList.assignValue( - GET_OR_DISTINCT(DIEnumerator, (Context, unrotateSign(Record[1]), - IsUnsigned, getMDString(Record[2]))), + GET_OR_DISTINCT(DIEnumerator, + (Context, Value, IsUnsigned, getMDString(Record[2]))), NextMetadataNo); NextMetadataNo++; break; Index: llvm/lib/Bitcode/Writer/BitcodeWriter.cpp =================================================================== --- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -1529,12 +1529,32 @@ Record.clear(); } +static void emitSignedInt64(SmallVectorImpl &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 &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 &Record, unsigned Abbrev) { - Record.push_back((N->isUnsigned() << 1) | N->isDistinct()); - Record.push_back(rotateSign(N->getValue())); + const uint64_t IsBigInt = 1 << 2; + Record.push_back(IsBigInt | (N->isUnsigned() << 1) | N->isDistinct()); + Record.push_back(N->getValue().getBitWidth()); Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + emitWideAPInt(Record, N->getValue()); Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev); Record.clear(); @@ -2269,13 +2289,6 @@ Stream.ExitBlock(); } -static void emitSignedInt64(SmallVectorImpl &Vals, uint64_t V) { - if ((int64_t)V >= 0) - Vals.push_back(V << 1); - else - Vals.push_back((-V << 1) | 1); -} - void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal) { if (FirstVal == LastVal) return; @@ -2362,15 +2375,8 @@ Code = bitc::CST_CODE_INTEGER; AbbrevToUse = CONSTANTS_INTEGER_ABBREV; } else { // Wide integers, > 64 bits in size. - // 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 NWords = IV->getValue().getActiveWords(); - const uint64_t *RawWords = IV->getValue().getRawData(); - for (unsigned i = 0; i != NWords; ++i) { - emitSignedInt64(Record, RawWords[i]); - } + const APInt &V = IV->getValue(); + emitWideAPInt(Record, V); Code = bitc::CST_CODE_WIDE_INTEGER; } } else if (const ConstantFP *CFP = dyn_cast(C)) { Index: llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp +++ llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp @@ -2072,7 +2072,7 @@ // order, which is what MSVC does. if (auto *Enumerator = dyn_cast_or_null(Element)) { EnumeratorRecord ER(MemberAccess::Public, - APSInt::getUnsigned(Enumerator->getValue()), + APSInt(Enumerator->getValue(), true), Enumerator->getName()); ContinuationBuilder.writeMemberType(ER); EnumeratorCount++; Index: llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -1449,8 +1449,7 @@ DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer); StringRef Name = Enum->getName(); addString(Enumerator, dwarf::DW_AT_name, Name); - auto Value = static_cast(Enum->getValue()); - addConstantValue(Enumerator, IsUnsigned, Value); + addConstantValue(Enumerator, Enum->getValue(), IsUnsigned); if (IndexEnumerators) addGlobalName(Name, Enumerator, Context); } Index: llvm/lib/IR/AsmWriter.cpp =================================================================== --- llvm/lib/IR/AsmWriter.cpp +++ llvm/lib/IR/AsmWriter.cpp @@ -1647,6 +1647,8 @@ bool ShouldSkipNull = true); template void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true); + void printAPInt(StringRef Name, APInt Int, bool IsUnsigned, + bool ShouldSkipZero); void printBool(StringRef Name, bool Value, Optional Default = None); void printDIFlags(StringRef Name, DINode::DIFlags Flags); void printDISPFlags(StringRef Name, DISubprogram::DISPFlags Flags); @@ -1722,6 +1724,15 @@ Out << FS << Name << ": " << Int; } +void MDFieldPrinter::printAPInt(StringRef Name, APInt Int, bool IsUnsigned, + bool ShouldSkipZero) { + if (ShouldSkipZero && Int.isNullValue()) + return; + + Out << FS << Name << ": "; + Int.print(Out, !IsUnsigned); +} + void MDFieldPrinter::printBool(StringRef Name, bool Value, Optional Default) { if (Default && Value == *Default) @@ -1851,13 +1862,10 @@ Out << "!DIEnumerator("; MDFieldPrinter Printer(Out); Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false); - if (N->isUnsigned()) { - auto Value = static_cast(N->getValue()); - Printer.printInt("value", Value, /* ShouldSkipZero */ false); + Printer.printAPInt("value", N->getValue(), N->isUnsigned(), + /* ShouldSkipZero */ false); + if (N->isUnsigned()) Printer.printBool("isUnsigned", true); - } else { - Printer.printInt("value", N->getValue(), /* ShouldSkipZero */ false); - } Out << ")"; } Index: llvm/lib/IR/DIBuilder.cpp =================================================================== --- llvm/lib/IR/DIBuilder.cpp +++ llvm/lib/IR/DIBuilder.cpp @@ -246,7 +246,8 @@ DIEnumerator *DIBuilder::createEnumerator(StringRef Name, int64_t Val, bool IsUnsigned) { assert(!Name.empty() && "Unable to create enumerator without name"); - return DIEnumerator::get(VMContext, Val, IsUnsigned, Name); + return DIEnumerator::get(VMContext, APInt(64, Val, !IsUnsigned), IsUnsigned, + Name); } DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) { Index: llvm/lib/IR/DebugInfoMetadata.cpp =================================================================== --- llvm/lib/IR/DebugInfoMetadata.cpp +++ llvm/lib/IR/DebugInfoMetadata.cpp @@ -332,7 +332,7 @@ DEFINE_GETIMPL_STORE(DISubrange, (CountNode, Lo), Ops); } -DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value, +DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, APInt Value, bool IsUnsigned, MDString *Name, StorageType Storage, bool ShouldCreate) { assert(isCanonical(Name) && "Expected canonical MDString"); Index: llvm/lib/IR/LLVMContextImpl.h =================================================================== --- llvm/lib/IR/LLVMContextImpl.h +++ llvm/lib/IR/LLVMContextImpl.h @@ -355,19 +355,22 @@ }; template <> struct MDNodeKeyImpl { - int64_t Value; + APInt Value; MDString *Name; bool IsUnsigned; - MDNodeKeyImpl(int64_t Value, bool IsUnsigned, MDString *Name) + MDNodeKeyImpl(APInt Value, bool IsUnsigned, MDString *Name) : Value(Value), Name(Name), IsUnsigned(IsUnsigned) {} + MDNodeKeyImpl(int64_t Value, bool IsUnsigned, MDString *Name) + : Value(APInt(64, Value, !IsUnsigned)), Name(Name), + IsUnsigned(IsUnsigned) {} MDNodeKeyImpl(const DIEnumerator *N) : Value(N->getValue()), Name(N->getRawName()), IsUnsigned(N->isUnsigned()) {} bool isKeyOf(const DIEnumerator *RHS) const { - return Value == RHS->getValue() && IsUnsigned == RHS->isUnsigned() && - Name == RHS->getRawName(); + return APInt::isSameValue(Value, RHS->getValue()) && + IsUnsigned == RHS->isUnsigned() && Name == RHS->getRawName(); } unsigned getHashValue() const { return hash_combine(Value, Name); } Index: llvm/lib/Target/BPF/BTFDebug.cpp =================================================================== --- llvm/lib/Target/BPF/BTFDebug.cpp +++ llvm/lib/Target/BPF/BTFDebug.cpp @@ -171,7 +171,12 @@ struct BTF::BTFEnum BTFEnum; BTFEnum.NameOff = BDebug.addString(Enum->getName()); // BTF enum value is 32bit, enforce it. - BTFEnum.Val = static_cast(Enum->getValue()); + uint32_t Value; + if (Enum->isUnsigned()) + Value = static_cast(Enum->getValue().getZExtValue()); + else + Value = static_cast(Enum->getValue().getSExtValue()); + BTFEnum.Val = Value; EnumValues.push_back(BTFEnum); } } Index: llvm/test/Assembler/DIEnumeratorBig.ll =================================================================== --- /dev/null +++ llvm/test/Assembler/DIEnumeratorBig.ll @@ -0,0 +1,38 @@ +; Round-trip test for enumeration members using more than 64 bits + +; RUN: llvm-as %s -o - | llvm-dis | llvm-as | llvm-dis | FileCheck %s + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!14, !15, !16} +!llvm.ident = !{!17} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 7.0.1-8 (tags/RELEASE_701/final)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) +!1 = !DIFile(filename: "/tmp/foo.cpp", directory: "/") +!2 = !{!3, !9} + +!3 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "E0", file: !1, line: 2, baseType: !4, size: 128, flags: DIFlagEnumClass, elements: !6, identifier: "_ZTS2E0") +!4 = !DIDerivedType(tag: DW_TAG_typedef, name: "__int128_t", file: !1, baseType: !5) +!5 = !DIBasicType(name: "__int128", size: 128, encoding: DW_ATE_signed) +!6 = !{!7, !8} +!7 = !DIEnumerator(name: "D0", value: -170141183460469231731687303715884105728) +!8 = !DIEnumerator(name: "D1", value: 170141183460469231731687303715884105727) +; CHECK: !DICompositeType(tag: DW_TAG_enumeration_type, name: "E0"{{.*}} +; CHECK-NOT: FixedEnum +; CHECK: !DIBasicType(name: "__int128", size: 128, encoding: DW_ATE_signed) +; CHECK: !DIEnumerator(name: "D0", value: -170141183460469231731687303715884105728) +; CHECK: !DIEnumerator(name: "D1", value: 170141183460469231731687303715884105727) + +!9 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "E1", file: !1, line: 5, baseType: !10, size: 128, flags: DIFlagEnumClass, elements: !12, identifier: "_ZTS2E1") +!10 = !DIDerivedType(tag: DW_TAG_typedef, name: "__uint128_t", file: !1, line: 9, baseType: !11) +!11 = !DIBasicType(name: "unsigned __int128", size: 128, encoding: DW_ATE_unsigned) +!12 = !{!13} +!13 = !DIEnumerator(name: "D1", value: 2722258935367507707706996859454145691648, isUnsigned: true) +; CHECK: !DICompositeType(tag: DW_TAG_enumeration_type, name: "E1"{{.*}} +; CHECK-NOT: FixedEnum +; CHECK: !DIBasicType(name: "unsigned __int128", size: 128, encoding: DW_ATE_unsigned) +; CHECK: !DIEnumerator(name: "D1", value: 2722258935367507707706996859454145691648, isUnsigned: true) + +!14 = !{i32 2, !"Dwarf Version", i32 4} +!15 = !{i32 2, !"Debug Info Version", i32 3} +!16 = !{i32 1, !"wchar_size", i32 4} +!17 = !{!"clang version 7.0.1-8 (tags/RELEASE_701/final)"} Index: llvm/test/Bitcode/upgrade-enum-debug-info.ll =================================================================== --- /dev/null +++ llvm/test/Bitcode/upgrade-enum-debug-info.ll @@ -0,0 +1,21 @@ +; RUN: llvm-dis < %s.bc | FileCheck %s +; RUN: verify-uselistorder < %s.bc + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!10} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "x0", scope: !2, file: !3, line: 5, type: !5, isLocal: false, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "clang version 7.0.0 (/data/src/llvm/tools/clang 0c08d9830124a75675348b4eeb47256f3da6693d) (/data/src/llvm cf29510f52faa77b98510cd53276f564d1f4f41f)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4) +!3 = !DIFile(filename: "/upgrade-enum-di.ll", directory: "/") +!4 = !{!5} + +!5 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "E0", file: !3, line: 2, baseType: !6, size: 32, flags: DIFlagEnumClass, elements: !7, identifier: "_ZTS2E0") +!6 = !DIBasicType(name: "signed char", size: 32, encoding: DW_ATE_signed) +!7 = !{!8, !9} +!8 = !DIEnumerator(name: "A0", value: -2147483648) +; CHECK: !DIEnumerator(name: "A0", value: -2147483648) +!9 = !DIEnumerator(name: "B0", value: 2147483647) +; CHECK: !DIEnumerator(name: "B0", value: 2147483647) + +!10 = !{i32 2, !"Debug Info Version", i32 3} Index: llvm/unittests/IR/MetadataTest.cpp =================================================================== --- llvm/unittests/IR/MetadataTest.cpp +++ llvm/unittests/IR/MetadataTest.cpp @@ -1186,7 +1186,7 @@ TEST_F(DIEnumeratorTest, get) { auto *N = DIEnumerator::get(Context, 7, false, "name"); EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag()); - EXPECT_EQ(7, N->getValue()); + EXPECT_EQ(7, N->getValue().getSExtValue()); EXPECT_FALSE(N->isUnsigned()); EXPECT_EQ("name", N->getName()); EXPECT_EQ(N, DIEnumerator::get(Context, 7, false, "name")); @@ -1199,6 +1199,15 @@ EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); } +TEST_F(DIEnumeratorTest, getWithLargeValues) { + auto *N = DIEnumerator::get(Context, APInt::getMaxValue(128), false, "val"); + EXPECT_EQ(128U, N->getValue().countPopulation()); + EXPECT_EQ(N, + DIEnumerator::get(Context, APInt::getMaxValue(128), false, "val")); + EXPECT_NE(N, + DIEnumerator::get(Context, APInt::getMinValue(128), false, "val")); +} + typedef MetadataTest DIBasicTypeTest; TEST_F(DIBasicTypeTest, get) {