From dc627e7bec000fdae5c32d34340c167303b193c3 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) --- 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 7c6ef093fbcea..841308d61fa02 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; } @@ -387,6 +387,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 +}