Index: lib/IR/Core.cpp =================================================================== --- lib/IR/Core.cpp +++ lib/IR/Core.cpp @@ -1842,12 +1842,16 @@ unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) { auto *ASN = AttributeSetNode::get(unwrap(F)->getAttributes(), Idx); + if (!ASN) + return 0; return ASN->getNumAttributes(); } void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, LLVMAttributeRef *Attrs) { auto *ASN = AttributeSetNode::get(unwrap(F)->getAttributes(), Idx); + if (!ASN) + return; for (auto A: make_range(ASN->begin(), ASN->end())) *Attrs++ = wrap(A); } @@ -2173,6 +2177,8 @@ LLVMAttributeIndex Idx) { auto CS = CallSite(unwrap(C)); auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx); + if (!ASN) + return 0; return ASN->getNumAttributes(); } @@ -2180,6 +2186,8 @@ LLVMAttributeRef *Attrs) { auto CS = CallSite(unwrap(C)); auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx); + if (!ASN) + return; for (auto A: make_range(ASN->begin(), ASN->end())) *Attrs++ = wrap(A); } Index: tools/llvm-c-test/echo.cpp =================================================================== --- tools/llvm-c-test/echo.cpp +++ tools/llvm-c-test/echo.cpp @@ -379,11 +379,13 @@ auto Ctx = LLVMGetModuleContext(M); int ArgCount = LLVMGetNumArgOperands(Src); for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) { - for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { - if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) { - auto Val = LLVMGetEnumAttributeValue(SrcA); - auto A = LLVMCreateEnumAttribute(Ctx, k, Val); - LLVMAddCallSiteAttribute(Dst, i, A); + if (LLVMGetCallSiteAttributeCount(Src, i) > 0) { + for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { + if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) { + auto Val = LLVMGetEnumAttributeValue(SrcA); + auto A = LLVMCreateEnumAttribute(Ctx, k, Val); + LLVMAddCallSiteAttribute(Dst, i, A); + } } } } @@ -812,11 +814,13 @@ // Copy attributes for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F); i <= c; ++i) { - for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { - if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) { - auto Val = LLVMGetEnumAttributeValue(SrcA); - auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val); - LLVMAddAttributeAtIndex(F, i, DstA); + if (LLVMGetAttributeCountAtIndex(Cur, i) > 0) { + for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { + if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) { + auto Val = LLVMGetEnumAttributeValue(SrcA); + auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val); + LLVMAddAttributeAtIndex(F, i, DstA); + } } } }