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
124 lines
3.7 KiB
C++
124 lines
3.7 KiB
C++
//===-- NVPTXImageOptimizer.cpp - Image optimization pass -----------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This pass implements IR-level optimizations of image access code,
|
|
// including:
|
|
//
|
|
// 1. Eliminate istypep intrinsics when image access qualifier is known
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "NVPTX.h"
|
|
#include "NVVMProperties.h"
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
|
#include "llvm/IR/Constants.h"
|
|
#include "llvm/IR/Instructions.h"
|
|
#include "llvm/IR/Intrinsics.h"
|
|
#include "llvm/IR/IntrinsicsNVPTX.h"
|
|
#include "llvm/Pass.h"
|
|
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
class NVPTXImageOptimizer : public FunctionPass {
|
|
private:
|
|
static char ID;
|
|
SmallVector<Instruction*, 4> InstrToDelete;
|
|
|
|
public:
|
|
NVPTXImageOptimizer();
|
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
StringRef getPassName() const override { return "NVPTX Image Optimizer"; }
|
|
|
|
private:
|
|
bool replaceIsTypeP(Instruction &I, PTXOpaqueType Expected);
|
|
Value *cleanupValue(Value *V);
|
|
void replaceWith(Instruction *From, ConstantInt *To);
|
|
};
|
|
}
|
|
|
|
char NVPTXImageOptimizer::ID = 0;
|
|
|
|
NVPTXImageOptimizer::NVPTXImageOptimizer()
|
|
: FunctionPass(ID) {}
|
|
|
|
bool NVPTXImageOptimizer::runOnFunction(Function &F) {
|
|
if (skipFunction(F))
|
|
return false;
|
|
|
|
bool Changed = false;
|
|
InstrToDelete.clear();
|
|
|
|
// Look for call instructions in the function
|
|
for (BasicBlock &BB : F) {
|
|
for (Instruction &Instr : BB) {
|
|
if (CallInst *CI = dyn_cast<CallInst>(&Instr)) {
|
|
Function *CalledF = CI->getCalledFunction();
|
|
if (CalledF && CalledF->isIntrinsic()) {
|
|
// This is an intrinsic function call, check if its an istypep
|
|
switch (CalledF->getIntrinsicID()) {
|
|
default: break;
|
|
case Intrinsic::nvvm_istypep_sampler:
|
|
Changed |= replaceIsTypeP(Instr, PTXOpaqueType::Sampler);
|
|
break;
|
|
case Intrinsic::nvvm_istypep_surface:
|
|
Changed |= replaceIsTypeP(Instr, PTXOpaqueType::Surface);
|
|
break;
|
|
case Intrinsic::nvvm_istypep_texture:
|
|
Changed |= replaceIsTypeP(Instr, PTXOpaqueType::Texture);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Delete any istypep instances we replaced in the IR
|
|
for (Instruction *I : InstrToDelete)
|
|
I->eraseFromParent();
|
|
|
|
return Changed;
|
|
}
|
|
|
|
bool NVPTXImageOptimizer::replaceIsTypeP(Instruction &I,
|
|
PTXOpaqueType Expected) {
|
|
PTXOpaqueType OT = getPTXOpaqueType(*cleanupValue(I.getOperand(0)));
|
|
if (OT == PTXOpaqueType::None)
|
|
return false;
|
|
replaceWith(&I, ConstantInt::getBool(I.getContext(), OT == Expected));
|
|
return true;
|
|
}
|
|
|
|
void NVPTXImageOptimizer::replaceWith(Instruction *From, ConstantInt *To) {
|
|
// We implement "poor man's DCE" here to make sure any code that is no longer
|
|
// live is actually unreachable and can be trivially eliminated by the
|
|
// unreachable block elimination pass.
|
|
for (Use &U : From->uses()) {
|
|
if (CondBrInst *BI = dyn_cast<CondBrInst>(U)) {
|
|
BasicBlock *Dest = BI->getSuccessor(To->isZero() ? 1 : 0);
|
|
UncondBrInst::Create(Dest, BI->getIterator());
|
|
InstrToDelete.push_back(BI);
|
|
}
|
|
}
|
|
From->replaceAllUsesWith(To);
|
|
InstrToDelete.push_back(From);
|
|
}
|
|
|
|
Value *NVPTXImageOptimizer::cleanupValue(Value *V) {
|
|
if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
|
|
return cleanupValue(EVI->getAggregateOperand());
|
|
}
|
|
return V;
|
|
}
|
|
|
|
FunctionPass *llvm::createNVPTXImageOptimizerPass() {
|
|
return new NVPTXImageOptimizer();
|
|
}
|