From 3fe180ab7acd3c2b18e1ad4d7db825126bc32229 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Mon, 27 Sep 2021 10:24:33 +0200 Subject: [PATCH] Strip address information from byval type parameters. --- src/llvm-remove-addrspaces.cpp | 20 +++++++++++++++++++- test/llvmpasses/remove-addrspaces.ll | 7 +++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/llvm-remove-addrspaces.cpp b/src/llvm-remove-addrspaces.cpp index 7c6ef093fbcea..67e2f0a974520 100644 --- a/src/llvm-remove-addrspaces.cpp +++ b/src/llvm-remove-addrspaces.cpp @@ -325,7 +325,8 @@ 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 +388,23 @@ 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) { + if (Attrs.hasAttribute(i, Attribute::ByVal)) { + Type *Ty = Attrs.getAttribute(i, Attribute::ByVal).getValueAsType(); + if (!Ty) + continue; + + Attrs = Attrs.removeAttribute(C, i, Attribute::ByVal); + Attrs = Attrs.addAttribute( + C, i, Attribute::getWithByValType(C, TypeRemapper.remapType(Ty))); + } + } + 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 +}