Skip to content

[SYCL][Fusion] Fix kernel fusion passes tests #7719

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

Merged
merged 1 commit into from
Dec 9, 2022
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
2 changes: 1 addition & 1 deletion sycl-fusion/passes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Module library for usage as library/pass-plugin with LLVM opt.
add_llvm_library(SYCLKernelFusion SHARED
add_llvm_library(SYCLKernelFusion MODULE
SYCLFusionPasses.cpp
kernel-fusion/SYCLKernelFusion.cpp
kernel-info/SYCLKernelInfo.cpp
Expand Down
14 changes: 6 additions & 8 deletions sycl-fusion/passes/cleanup/Cleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static void copyAttributesFrom(const BitVector &Mask, Function *NF,
NF->copyAttributesFrom(F);
// Drop masked-out attributes.
SmallVector<AttributeSet> Attributes;
const llvm::AttributeList PAL = NF->getAttributes();
const AttributeList PAL = NF->getAttributes();
std::transform(Mask.set_bits_begin(), Mask.set_bits_end(),
std::back_inserter(Attributes),
[&](unsigned I) { return PAL.getParamAttrs(I); });
Expand All @@ -47,11 +47,9 @@ static void copyAttributesFrom(const BitVector &Mask, Function *NF,

static Function *createMaskedFunction(const BitVector &Mask, Function *F) {
// Declare
llvm::FunctionType *NFTy =
createMaskedFunctionType(Mask, F->getFunctionType());
llvm::Function *NF =
Function::Create(NFTy, F->getLinkage(), F->getAddressSpace(),
F->getName(), F->getParent());
FunctionType *NFTy = createMaskedFunctionType(Mask, F->getFunctionType());
Function *NF = Function::Create(NFTy, F->getLinkage(), F->getAddressSpace(),
F->getName(), F->getParent());
copyAttributesFrom(Mask, NF, F);
NF->setComdat(F->getComdat());
NF->takeName(F);
Expand All @@ -74,7 +72,7 @@ static Function *createMaskedFunction(const BitVector &Mask, Function *F) {
// Copy metadata.
SmallVector<std::pair<unsigned, MDNode *>> MDs;
F->getAllMetadata(MDs);
for (auto MD : MDs) {
for (auto &MD : MDs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that I haven't thoroughly reviewed the rest of for (auto ...) loops (because the patch is quite huge), so there might be similar excessive copies somewhere else

NF->addMetadata(MD.first, *MD.second);
}
}
Expand Down Expand Up @@ -108,7 +106,7 @@ static void applyArgMask(const jit_compiler::ArgUsageMask &NewArgInfo,
const BitVector &Mask, Function *F,
ModuleAnalysisManager &AM) {
// Create the function without the masked-out args.
llvm::Function *NF = createMaskedFunction(Mask, F);
Function *NF = createMaskedFunction(Mask, F);
// Update the unused args mask.
jit_compiler::SYCLModuleInfo *ModuleInfo =
AM.getResult<SYCLModuleInfoAnalysis>(*NF->getParent()).ModuleInfo;
Expand Down
41 changes: 19 additions & 22 deletions sycl-fusion/passes/internalization/Internalization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

using namespace llvm;

// Corresponds to definition of spir_private and spir_local in
// "clang/lib/Basic/Target/SPIR.h", "SPIRDefIsGenMap".
constexpr static unsigned PrivateAS{0};
constexpr static unsigned LocalAS{3};

Expand Down Expand Up @@ -191,15 +193,12 @@ static void remapIndices(GetElementPtrInst *GEPI, std::size_t LocalSize) {
auto *NewIndex = [&]() -> Value * {
if (LocalSize == 1) {
return Builder.getInt64(0);
} else {
SmallVector<Value *> OldIndexValue = getIndices(Builder, GEPI);
auto *OldIndexSum =
std::accumulate(std::next(OldIndexValue.begin()), OldIndexValue.end(),
OldIndexValue[0], [&](Value *Lhs, Value *Rhs) {
return Builder.CreateAdd(Lhs, Rhs);
});
return Builder.CreateURem(OldIndexSum, Builder.getInt64(LocalSize));
}
SmallVector<Value *> OldIndexValue = getIndices(Builder, GEPI);
auto *OldIndexSum = std::accumulate(
std::next(OldIndexValue.begin()), OldIndexValue.end(), OldIndexValue[0],
[&](Value *Lhs, Value *Rhs) { return Builder.CreateAdd(Lhs, Rhs); });
return Builder.CreateURem(OldIndexSum, Builder.getInt64(LocalSize));
}();
GEPI->idx_begin()->set(NewIndex);
}
Expand Down Expand Up @@ -323,7 +322,7 @@ Error SYCLInternalizerImpl::checkArgsPromotable(
<< " of function " << F->getName().str() << ": "
<< SE.getMessage() << "\n";
});
llvm::Error NewErr =
Error NewErr =
createStringError(inconvertibleErrorCode(), ErrorMessage.str());
DeferredErrs = joinErrors(std::move(DeferredErrs), std::move(NewErr));
}
Expand All @@ -346,9 +345,9 @@ void SYCLInternalizerImpl::promoteCall(CallBase *C, const Value *Val,
const SmallVector<size_t> InternInfo =
getUsagesInternalization(C, Val, LocalSize);
assert(!InternInfo.empty() && "Value must be used at least once");
llvm::Function *NewF = promoteFunctionArgs(C->getCalledFunction(), InternInfo,
/* CreateAllocas */ false,
/*KeepOriginal*/ true);
Function *NewF = promoteFunctionArgs(C->getCalledFunction(), InternInfo,
/* CreateAllocas */ false,
/*KeepOriginal*/ true);

C->setCalledFunction(NewF);
}
Expand All @@ -369,9 +368,7 @@ void SYCLInternalizerImpl::promoteGEPI(GetElementPtrInst *GEPI,
void SYCLInternalizerImpl::promoteValue(Value *Val,
std::size_t LocalSize) const {
for (auto *U : Val->users()) {
auto *I = dyn_cast<Instruction>(U);
assert(I &&
"Cannot promote value used in a place other than an instruction");
auto *I = cast<Instruction>(U);
switch (I->getOpcode()) {
case Instruction::Call:
case Instruction::Invoke:
Expand Down Expand Up @@ -402,7 +399,7 @@ getPromotedFunctionType(FunctionType *OrigTypes,
if (Arg.value() == 0) {
continue;
}
llvm::Type *&Ty = Types[Arg.index()];
Type *&Ty = Types[Arg.index()];
// TODO: Catch this case earlier
if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
Ty = PointerType::getWithSamePointeeType(PtrTy, AS);
Expand All @@ -416,10 +413,10 @@ static Function *
getPromotedFunctionDeclaration(Function *F,
ArrayRef<std::size_t> PromoteToLocal,
unsigned AS, bool ChangeTypes) {
llvm::FunctionType *Ty = F->getFunctionType();
FunctionType *Ty = F->getFunctionType();
// If we do not need to change the types, we just copy the function
// declaration.
llvm::FunctionType *NewTy =
FunctionType *NewTy =
ChangeTypes ? getPromotedFunctionType(Ty, PromoteToLocal, AS) : Ty;
return Function::Create(NewTy, F->getLinkage(), F->getAddressSpace(),
F->getName(), F->getParent());
Expand Down Expand Up @@ -479,7 +476,7 @@ Value *replaceByNewAlloca(Argument *Arg, unsigned AS, std::size_t LocalSize) {
IRBuilder<> Builder{
&*Arg->getParent()->getEntryBlock().getFirstInsertionPt()};
auto *PtrTy = cast<PointerType>(Arg->getType());
llvm::Type *Ty = getElementTypeFromUses(Arg);
Type *Ty = getElementTypeFromUses(Arg);
assert(Ty && "Could not determine pointer element type");
auto *ArrTy = ArrayType::get(Ty, LocalSize);
auto *Alloca = Builder.CreateAlloca(ArrTy, PtrTy->getAddressSpace());
Expand All @@ -502,7 +499,7 @@ Function *SYCLInternalizerImpl::promoteFunctionArgs(
IntegerType::get(OldF->getContext(), AddressSpaceBitWidth), AS));

// We first declare the promoted function with the new signature.
llvm::Function *NewF =
Function *NewF =
getPromotedFunctionDeclaration(OldF, PromoteToLocal, AS,
/*ChangeTypes*/ !CreateAllocas);

Expand Down Expand Up @@ -554,7 +551,7 @@ Function *SYCLInternalizerImpl::promoteFunctionArgs(
const auto Index = I.index();
if (const auto *PtrTy =
dyn_cast<PointerType>(NewF->getArg(Index)->getType())) {
if (PtrTy->getAddressSpace() == 3) {
if (PtrTy->getAddressSpace() == LocalAS) {
NewInfo[Index] = NewAddrspace;
}
}
Expand All @@ -579,7 +576,7 @@ SYCLInternalizerImpl::operator()(Module &M, ModuleAnalysisManager &AM) const {
}
}
for (auto *F : ToUpdate) {
llvm::Expected<SmallVector<size_t>> IndicesOrErr =
Expected<SmallVector<size_t>> IndicesOrErr =
getInternalizationFromMD(F, Kind);
if (auto E = IndicesOrErr.takeError()) {
handleAllErrors(std::move(E), [](const StringError &SE) {
Expand Down
Loading