Skip to content

Commit

Permalink
Remove AddrSpaceCast on function from llvm.global.annotations (#1337)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjodinchr authored Apr 19, 2024
1 parent 69e5c0b commit ff5d4a8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
10 changes: 10 additions & 0 deletions lib/AnnotationToMetadataPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "llvm/IR/Constants.h"
#include "llvm/IR/Operator.h"

#include "AnnotationToMetadataPass.h"
#include "Constants.h"
Expand All @@ -30,6 +31,7 @@ clspv::AnnotationToMetadataPass::run(Module &M, ModuleAnalysisManager &) {

// list of processed strings to delete at the end
SmallPtrSet<GlobalValue *, 4> to_erase;
SmallPtrSet<Constant *, 4> addrspacecast_to_erase;

ConstantArray *annotations_array =
dyn_cast<ConstantArray>(GV.getOperand(0));
Expand All @@ -38,6 +40,11 @@ clspv::AnnotationToMetadataPass::run(Module &M, ModuleAnalysisManager &) {
dyn_cast<ConstantStruct>(annotation_entry.get());

auto op0 = annotation_struct->getOperand(0);
if (isa<AddrSpaceCastOperator>(op0)) {
// We need to make sure to erase those to avoid keeping a reference on
// functions preventing them from being removed
addrspacecast_to_erase.insert(op0);
}
Function *entry_point = dyn_cast<Function>(op0->stripPointerCasts());

auto op1 = annotation_struct->getOperand(1);
Expand Down Expand Up @@ -66,6 +73,9 @@ clspv::AnnotationToMetadataPass::run(Module &M, ModuleAnalysisManager &) {
for (auto gv : to_erase) {
gv->eraseFromParent();
}
for (auto as : addrspacecast_to_erase) {
as->destroyConstant();
}
break;
}
}
Expand Down
7 changes: 2 additions & 5 deletions lib/LongVectorLoweringPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1961,11 +1961,8 @@ void clspv::LongVectorLoweringPass::cleanDeadFunctions() {
Progress = (DeadFunctions.size() < PreviousSize);
}

// TODO: uncomment assert.
// LLVM now creates some addrspacecast on function that are never used in any
// function. It prevents supposedly-dead functions from being removed
// assert(DeadFunctions.empty() &&
// "Not all supposedly-dead functions were removed!");
assert(DeadFunctions.empty() &&
"Not all supposedly-dead functions were removed!");
}

void clspv::LongVectorLoweringPass::cleanDeadGlobals() {
Expand Down
5 changes: 1 addition & 4 deletions lib/UndoByvalPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ PreservedAnalyses clspv::UndoByvalPass::run(Module &M,
// Update caller site.
for (auto User : Users) {
// Create new call instruction for new function without byval.
CallInst *Call = dyn_cast<CallInst>(User);
if (Call == nullptr) {
continue;
}
CallInst *Call = cast<CallInst>(User);
auto Callee = Call->getCalledFunction();

SmallVector<Value *, 8> Args;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
// CHECK-NOT: OpFunctionCall
// CHECK: OpFunctionEnd

__attribute__((noinline))
int func_3(local int *in, int n) { return in[n]; }
__attribute__((noinline))
int func_2(local int *in, int n) { return func_3(in, n); }
__attribute__((noinline))
int func_1(local int *in, int n) { return func_2(in, n); }
Expand Down

0 comments on commit ff5d4a8

Please sign in to comment.