Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function calls like norecurse and hidden #6

Merged
merged 2 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,86 +27,102 @@ using namespace llvm;

namespace {
class PromotePointerKernArgsToGlobal : public FunctionPass {
// TODO: query the address space robustly.
static constexpr unsigned int GenericAddrSpace{0u};
static constexpr unsigned int GlobalAddrSpace{1u};
// TODO: query the address space robustly.
static constexpr unsigned int FlatAddrSpace = 0u;
static constexpr unsigned int GlobalAddrSpace = 1u;

void createPromotableCast(IRBuilder<>& Builder, Value *From, Value *To) {
From->replaceAllUsesWith(To);
void createPromotableCast(IRBuilder<>& Builder, Value *From, Value *To) {
From->replaceAllUsesWith(To);

Value *FToG = Builder.CreateAddrSpaceCast(
From,
cast<PointerType>(From->getType())
->getElementType()->getPointerTo(GlobalAddrSpace));
Value *GToF = Builder.CreateAddrSpaceCast(FToG, From->getType());
Value *FToG = Builder.CreateAddrSpaceCast(
From,
cast<PointerType>(
From->getType())->getElementType()->getPointerTo(GlobalAddrSpace));
Value *GToF = Builder.CreateAddrSpaceCast(FToG, From->getType());

To->replaceAllUsesWith(GToF);
}
To->replaceAllUsesWith(GToF);
}

void maybePromoteUse(IRBuilder<>& Builder, Instruction *UI) {
if (!UI)
return;
Value *createTemporary(IRBuilder<>& Builder, Type *Ty) {
// We cannot use IRBuilder since it might do the obvious folding, which
// would yield an undef value of a possibly primitive type, which cannot be
// disambiguated from other undefs of the same primitive type and would
// cause havoc when replaced with the promotable cast created later.
Value *UD = UndefValue::get(Builder.getInt8Ty());

Builder.SetInsertPoint(UI->getNextNonDebugInstruction());
return CastInst::CreateBitOrPointerCast(UD, Ty, "Tmp",
&*Builder.GetInsertPoint());
}

Value *Tmp = Builder.CreateBitCast(UndefValue::get(UI->getType()),
UI->getType());
createPromotableCast(Builder, UI, Tmp);
}
void maybePromoteUser(IRBuilder<>& Builder, Instruction *UI) {
if (!UI)
return;

void promoteArgument(IRBuilder<>& Builder, Argument *Arg) {
Value *Tmp = Builder.CreateBitCast(UndefValue::get(Arg->getType()),
Arg->getType());
createPromotableCast(Builder, Arg, Tmp);
}
Instruction *NI = UI->getNextNonDebugInstruction();
while (NI && isa<PHINode>(NI))
NI = NI->getNextNonDebugInstruction();

if (!NI)
return;

Builder.SetInsertPoint(NI);

createPromotableCast(Builder, UI, createTemporary(Builder, UI->getType()));
}

void promoteArgument(IRBuilder<>& Builder, Argument *Arg) {
createPromotableCast(Builder, Arg,
createTemporary(Builder, Arg->getType()));
}
public:
static char ID;
PromotePointerKernArgsToGlobal() : FunctionPass{ID} {}

bool runOnFunction(Function &F) override
{
if (F.getCallingConv() != CallingConv::AMDGPU_KERNEL)
return false;

SmallVector<Argument *, 8> PromotableArgs;
SmallVector<User *, 8> PromotableUses;
for (auto &&Arg : F.args()) {
for (auto &&U : Arg.users()) {
if (!U->getType()->isPointerTy())
continue;
if (U->getType()->getPointerAddressSpace() != GenericAddrSpace)
continue;

PromotableUses.push_back(U);
}

if (!Arg.getType()->isPointerTy())
continue;
if (Arg.getType()->getPointerAddressSpace() != GenericAddrSpace)
continue;

PromotableArgs.push_back(&Arg);
}

if (PromotableArgs.empty() && PromotableUses.empty())
return false;

static IRBuilder<> Builder{F.getContext()};
for (auto &&PU : PromotableUses)
maybePromoteUse(Builder, dyn_cast<Instruction>(PU));

Builder.SetInsertPoint(&F.getEntryBlock().front());
for (auto &&Arg : PromotableArgs)
promoteArgument(Builder, Arg);
return true;
static char ID;
PromotePointerKernArgsToGlobal() : FunctionPass(ID) {}

bool runOnFunction(Function &F) override
{
if (F.getCallingConv() != CallingConv::AMDGPU_KERNEL)
return false;

SmallVector<Argument *, 8> PromotableArgs;
SmallVector<User *, 8> PromotableUsers;
for (auto &&Arg : F.args()) {
for (auto &&U : Arg.users()) {
if (!U->getType()->isPointerTy())
continue;
if (U->getType()->getPointerAddressSpace() != FlatAddrSpace)
continue;

PromotableUsers.push_back(U);
}

if (!Arg.getType()->isPointerTy())
continue;
if (Arg.getType()->getPointerAddressSpace() != FlatAddrSpace)
continue;

PromotableArgs.push_back(&Arg);
}

if (PromotableArgs.empty() && PromotableUsers.empty())
return false;

IRBuilder<> Builder(F.getContext());
for (auto &&PU : PromotableUsers)
maybePromoteUser(Builder, dyn_cast<Instruction>(PU));

Builder.SetInsertPoint(&F.getEntryBlock().front());
for (auto &&Arg : PromotableArgs)
promoteArgument(Builder, Arg);

return true;
}
};
char PromotePointerKernArgsToGlobal::ID = 0;

static RegisterPass<PromotePointerKernArgsToGlobal> X{
"promote-pointer-kernargs-to-global",
"Promotes kernel formals of pointer type to point to the global address "
"space, since the actuals can only represent a global address.",
false,
false};
static RegisterPass<PromotePointerKernArgsToGlobal> X(
"promote-pointer-kernargs-to-global",
"Promotes kernel formals of pointer type to point to the global address "
"space, since the actuals can only represent a global address.",
false,
false);
}
Loading