From e5744434027cca2726869a080f5a763e550c9229 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 30 Sep 2021 12:08:16 +0200 Subject: [PATCH] Strip address information from typed function parameters attributes. (#42395) (cherry picked from commit dc627e7bec000fdae5c32d34340c167303b193c3) --- src/llvm-remove-addrspaces.cpp | 18 +++++++++++++++++- test/llvmpasses/remove-addrspaces.ll | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/llvm-remove-addrspaces.cpp b/src/llvm-remove-addrspaces.cpp index ada10c8d5f1f9..c3fbe84d0bf60 100644 --- a/src/llvm-remove-addrspaces.cpp +++ b/src/llvm-remove-addrspaces.cpp @@ -325,7 +325,7 @@ bool RemoveAddrspacesPass::runOnModule(Module &M) Function *NF = Function::Create( NFTy, F->getLinkage(), F->getAddressSpace(), Name, &M); - NF->copyAttributesFrom(F); + // no need to copy attributes here, that's done by CloneFunctionInto VMap[F] = NF; } @@ -379,6 +379,22 @@ bool RemoveAddrspacesPass::runOnModule(Module &M) &TypeRemapper, &Materializer); + // CloneFunctionInto unconditionally copies the attributes from F to NF, + // without considering e.g. the byval attribute type. + AttributeList Attrs = F->getAttributes(); + LLVMContext &C = F->getContext(); + for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) { + for (Attribute::AttrKind TypedAttr : + {Attribute::ByVal, Attribute::StructRet, Attribute::ByRef}) { + if (Type *Ty = Attrs.getAttribute(i, TypedAttr).getValueAsType()) { + Attrs = Attrs.replaceAttributeType(C, i, TypedAttr, + TypeRemapper.remapType(Ty)); + break; + } + } + } + NF->setAttributes(Attrs); + if (F->hasPersonalityFn()) NF->setPersonalityFn(MapValue(F->getPersonalityFn(), VMap)); diff --git a/test/llvmpasses/remove-addrspaces.ll b/test/llvmpasses/remove-addrspaces.ll index 2f34cf55ffc08..f8b6de024bfdd 100644 --- a/test/llvmpasses/remove-addrspaces.ll +++ b/test/llvmpasses/remove-addrspaces.ll @@ -101,3 +101,10 @@ loop: exit: ret i64 %sum } + + +; COM: check that address spaces in byval types are processed correctly +define void @byval_type([1 x {} addrspace(10)*] addrspace(11)* byval([1 x {} addrspace(10)*]) %0) { +; CHECK: define void @byval_type([1 x {}*]* byval([1 x {}*]) %0) + ret void +}