2017-02-10 06:00:42 +08:00
|
|
|
//===-- AMDGPULowerIntrinsics.cpp -----------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-02-10 06:00:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AMDGPU.h"
|
2017-04-13 04:48:56 +08:00
|
|
|
#include "AMDGPUSubtarget.h"
|
2017-05-19 01:21:13 +08:00
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2017-07-07 10:00:06 +08:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2017-02-10 06:00:42 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Transforms/Utils/LowerMemIntrinsics.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "amdgpu-lower-intrinsics"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-02-01 08:39:02 +08:00
|
|
|
static int MaxStaticSize;
|
|
|
|
|
|
|
|
static cl::opt<int, true> MemIntrinsicExpandSizeThresholdOpt(
|
|
|
|
"amdgpu-mem-intrinsic-expand-size",
|
|
|
|
cl::desc("Set minimum mem intrinsic size to expand in IR"),
|
|
|
|
cl::location(MaxStaticSize),
|
|
|
|
cl::init(1024),
|
|
|
|
cl::Hidden);
|
|
|
|
|
2017-02-10 06:00:42 +08:00
|
|
|
|
|
|
|
class AMDGPULowerIntrinsics : public ModulePass {
|
2017-04-13 04:48:56 +08:00
|
|
|
private:
|
|
|
|
bool makeLIDRangeMetadata(Function &F) const;
|
|
|
|
|
2017-02-10 06:00:42 +08:00
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2017-05-19 01:21:13 +08:00
|
|
|
AMDGPULowerIntrinsics() : ModulePass(ID) {}
|
|
|
|
|
2017-02-10 06:00:42 +08:00
|
|
|
bool runOnModule(Module &M) override;
|
2017-07-07 10:00:06 +08:00
|
|
|
bool expandMemIntrinsicUses(Function &F);
|
2017-02-10 06:00:42 +08:00
|
|
|
StringRef getPassName() const override {
|
|
|
|
return "AMDGPU Lower Intrinsics";
|
|
|
|
}
|
2017-07-07 10:00:06 +08:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<TargetTransformInfoWrapperPass>();
|
|
|
|
}
|
2017-02-10 06:00:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
char AMDGPULowerIntrinsics::ID = 0;
|
|
|
|
|
|
|
|
char &llvm::AMDGPULowerIntrinsicsID = AMDGPULowerIntrinsics::ID;
|
|
|
|
|
2017-05-19 01:21:13 +08:00
|
|
|
INITIALIZE_PASS(AMDGPULowerIntrinsics, DEBUG_TYPE, "Lower intrinsics", false,
|
|
|
|
false)
|
2017-02-10 06:00:42 +08:00
|
|
|
|
|
|
|
// TODO: Should refine based on estimated number of accesses (e.g. does it
|
|
|
|
// require splitting based on alignment)
|
|
|
|
static bool shouldExpandOperationWithSize(Value *Size) {
|
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(Size);
|
2020-02-01 08:39:02 +08:00
|
|
|
return !CI || (CI->getSExtValue() > MaxStaticSize);
|
2017-02-10 06:00:42 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 10:00:06 +08:00
|
|
|
bool AMDGPULowerIntrinsics::expandMemIntrinsicUses(Function &F) {
|
2017-02-10 06:00:42 +08:00
|
|
|
Intrinsic::ID ID = F.getIntrinsicID();
|
2017-02-12 21:15:31 +08:00
|
|
|
bool Changed = false;
|
2017-02-10 06:00:42 +08:00
|
|
|
|
|
|
|
for (auto I = F.user_begin(), E = F.user_end(); I != E;) {
|
|
|
|
Instruction *Inst = cast<Instruction>(*I);
|
|
|
|
++I;
|
|
|
|
|
|
|
|
switch (ID) {
|
|
|
|
case Intrinsic::memcpy: {
|
|
|
|
auto *Memcpy = cast<MemCpyInst>(Inst);
|
|
|
|
if (shouldExpandOperationWithSize(Memcpy->getLength())) {
|
2017-07-07 10:00:06 +08:00
|
|
|
Function *ParentFunc = Memcpy->getParent()->getParent();
|
|
|
|
const TargetTransformInfo &TTI =
|
|
|
|
getAnalysis<TargetTransformInfoWrapperPass>().getTTI(*ParentFunc);
|
|
|
|
expandMemCpyAsLoop(Memcpy, TTI);
|
2017-02-10 06:00:42 +08:00
|
|
|
Changed = true;
|
|
|
|
Memcpy->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Intrinsic::memmove: {
|
|
|
|
auto *Memmove = cast<MemMoveInst>(Inst);
|
|
|
|
if (shouldExpandOperationWithSize(Memmove->getLength())) {
|
|
|
|
expandMemMoveAsLoop(Memmove);
|
|
|
|
Changed = true;
|
|
|
|
Memmove->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Intrinsic::memset: {
|
|
|
|
auto *Memset = cast<MemSetInst>(Inst);
|
|
|
|
if (shouldExpandOperationWithSize(Memset->getLength())) {
|
|
|
|
expandMemSetAsLoop(Memset);
|
|
|
|
Changed = true;
|
|
|
|
Memset->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-04-13 04:48:56 +08:00
|
|
|
bool AMDGPULowerIntrinsics::makeLIDRangeMetadata(Function &F) const {
|
2017-05-19 01:21:13 +08:00
|
|
|
auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
|
|
|
|
if (!TPC)
|
2017-04-13 04:48:56 +08:00
|
|
|
return false;
|
|
|
|
|
2017-05-19 01:21:13 +08:00
|
|
|
const TargetMachine &TM = TPC->getTM<TargetMachine>();
|
2017-04-13 04:48:56 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
for (auto *U : F.users()) {
|
|
|
|
auto *CI = dyn_cast<CallInst>(U);
|
|
|
|
if (!CI)
|
|
|
|
continue;
|
|
|
|
|
2018-07-12 04:59:01 +08:00
|
|
|
Changed |= AMDGPUSubtarget::get(TM, F).makeLIDRangeMetadata(CI);
|
2017-04-13 04:48:56 +08:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-02-10 06:00:42 +08:00
|
|
|
bool AMDGPULowerIntrinsics::runOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
for (Function &F : M) {
|
|
|
|
if (!F.isDeclaration())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (F.getIntrinsicID()) {
|
|
|
|
case Intrinsic::memcpy:
|
|
|
|
case Intrinsic::memmove:
|
|
|
|
case Intrinsic::memset:
|
|
|
|
if (expandMemIntrinsicUses(F))
|
|
|
|
Changed = true;
|
|
|
|
break;
|
2017-04-13 04:48:56 +08:00
|
|
|
|
|
|
|
case Intrinsic::amdgcn_workitem_id_x:
|
|
|
|
case Intrinsic::r600_read_tidig_x:
|
|
|
|
case Intrinsic::amdgcn_workitem_id_y:
|
|
|
|
case Intrinsic::r600_read_tidig_y:
|
|
|
|
case Intrinsic::amdgcn_workitem_id_z:
|
|
|
|
case Intrinsic::r600_read_tidig_z:
|
|
|
|
case Intrinsic::r600_read_local_size_x:
|
|
|
|
case Intrinsic::r600_read_local_size_y:
|
|
|
|
case Intrinsic::r600_read_local_size_z:
|
|
|
|
Changed |= makeLIDRangeMetadata(F);
|
|
|
|
break;
|
|
|
|
|
2017-02-10 06:00:42 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-05-19 01:21:13 +08:00
|
|
|
ModulePass *llvm::createAMDGPULowerIntrinsicsPass() {
|
|
|
|
return new AMDGPULowerIntrinsics();
|
2017-02-10 06:00:42 +08:00
|
|
|
}
|