Files
TheRock/compiler/amd-llvm/llvm/lib/Target/BPF/BPFASpaceCastSimplifyPass.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

93 lines
3.1 KiB
C++

//===-- BPFASpaceCastSimplifyPass.cpp - BPF addrspacecast simplications --===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "BPF.h"
#include <optional>
#define DEBUG_TYPE "bpf-aspace-simplify"
using namespace llvm;
namespace {
struct CastGEPCast {
AddrSpaceCastInst *OuterCast;
// Match chain of instructions:
// %inner = addrspacecast N->M
// %gep = getelementptr %inner, ...
// %outer = addrspacecast M->N %gep
// Where I is %outer.
static std::optional<CastGEPCast> match(Value *I) {
auto *OuterCast = dyn_cast<AddrSpaceCastInst>(I);
if (!OuterCast)
return std::nullopt;
auto *GEP = dyn_cast<GetElementPtrInst>(OuterCast->getPointerOperand());
if (!GEP)
return std::nullopt;
auto *InnerCast = dyn_cast<AddrSpaceCastInst>(GEP->getPointerOperand());
if (!InnerCast)
return std::nullopt;
if (InnerCast->getSrcAddressSpace() != OuterCast->getDestAddressSpace())
return std::nullopt;
if (InnerCast->getDestAddressSpace() != OuterCast->getSrcAddressSpace())
return std::nullopt;
return CastGEPCast{OuterCast};
}
static PointerType *changeAddressSpace(PointerType *Ty, unsigned AS) {
return Ty->get(Ty->getContext(), AS);
}
// Assuming match(this->OuterCast) is true, convert:
// (addrspacecast M->N (getelementptr (addrspacecast N->M ptr) ...))
// To:
// (getelementptr ptr ...)
GetElementPtrInst *rewrite() {
auto *GEP = cast<GetElementPtrInst>(OuterCast->getPointerOperand());
auto *InnerCast = cast<AddrSpaceCastInst>(GEP->getPointerOperand());
unsigned AS = OuterCast->getDestAddressSpace();
auto *NewGEP = cast<GetElementPtrInst>(GEP->clone());
NewGEP->setName(GEP->getName());
NewGEP->insertAfter(OuterCast->getIterator());
NewGEP->setOperand(0, InnerCast->getPointerOperand());
auto *GEPTy = cast<PointerType>(GEP->getType());
NewGEP->mutateType(changeAddressSpace(GEPTy, AS));
OuterCast->replaceAllUsesWith(NewGEP);
OuterCast->eraseFromParent();
if (GEP->use_empty())
GEP->eraseFromParent();
if (InnerCast->use_empty())
InnerCast->eraseFromParent();
return NewGEP;
}
};
} // anonymous namespace
PreservedAnalyses BPFASpaceCastSimplifyPass::run(Function &F,
FunctionAnalysisManager &AM) {
SmallVector<CastGEPCast, 16> WorkList;
bool Changed = false;
for (BasicBlock &BB : F) {
for (Instruction &I : BB)
if (auto It = CastGEPCast::match(&I))
WorkList.push_back(It.value());
Changed |= !WorkList.empty();
while (!WorkList.empty()) {
CastGEPCast InsnChain = WorkList.pop_back_val();
GetElementPtrInst *NewGEP = InsnChain.rewrite();
for (User *U : NewGEP->users())
if (auto It = CastGEPCast::match(U))
WorkList.push_back(It.value());
}
}
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
}