From cafd7e08fb8b56d51d2e5ea9b03755172cc648bc Mon Sep 17 00:00:00 2001 From: Haonan Yang Date: Thu, 5 Jan 2023 21:09:11 +0800 Subject: [PATCH] Do not use AttributeList::removeParamAttributes (#1795) AttributeList::removeParamAttributes function sets attribute at specified index empty so that return value of AttributeList::getNumAttrSet() keeps unchanged after that call. When call BuiltinCallMutator::removeArg function, there is assert failure on BuiltinCallMutator::doConversion() since new CallInst removed arg but still holds attribute of that removed arg. --- lib/SPIRV/SPIRVBuiltinHelper.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/SPIRV/SPIRVBuiltinHelper.cpp b/lib/SPIRV/SPIRVBuiltinHelper.cpp index a76fd7b944..6601ec8383 100644 --- a/lib/SPIRV/SPIRVBuiltinHelper.cpp +++ b/lib/SPIRV/SPIRVBuiltinHelper.cpp @@ -183,9 +183,22 @@ BuiltinCallMutator &BuiltinCallMutator::replaceArg(unsigned Index, BuiltinCallMutator &BuiltinCallMutator::removeArg(unsigned Index) { // If the argument being dropped is the last one, there is nothing to move, so // just remove the attributes. - if (Index == Args.size() - 1) - Attrs = Attrs.removeParamAttributes(CI->getContext(), Index); - else + if (Index == Args.size() - 1) { + // TODO: Remove this workaround when LLVM fixes + // https://github.com/llvm/llvm-project/issues/59746 on + // AttributeList::removeParamAttributes function. + // AttributeList::removeParamAttributes function sets attribute at + // specified index empty so that return value of + // AttributeList::getNumAttrSet() keeps unchanged after that call. When call + // BuiltinCallMutator::removeArg function, there is assert failure on + // BuiltinCallMutator::doConversion() since new CallInst removed arg but + // still holds attribute of that removed arg. + SmallVector ArgAttrs; + for (unsigned I = 0; I < Index; ++I) + ArgAttrs.push_back(Attrs.getParamAttrs(I)); + Attrs = AttributeList::get(CI->getContext(), Attrs.getFnAttrs(), + Attrs.getRetAttrs(), ArgAttrs); + } else moveAttributes(CI->getContext(), Attrs, Index + 1, Args.size() - Index - 1, Index); Args.erase(Args.begin() + Index);