-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement read of 3D images with unnormalised sampler (#1234)
We need to know where the sampler is coming from, so we need to inline every function containing a read image of a 3d image with non literal sampler. This PR depends on KhronosGroup/SPIRV-Headers#377 We use the information from the sampler mask to know if you need to use the original coordinates or the one we have normalised. Like for channel_image_order and channel_image_data_type we use metadata to pass information through the pipeline.
- Loading branch information
Showing
52 changed files
with
965 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright 2023 The Clspv Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "llvm/ADT/UniqueVector.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/Transforms/Utils/Cloning.h" | ||
|
||
#include "InlineFuncWithReadImage3DNonLiteralSampler.h" | ||
#include "SamplerUtils.h" | ||
|
||
#include <set> | ||
|
||
using namespace llvm; | ||
|
||
#define DEBUG_TYPE "inlinefuncwithreadimage3dnonliteralsamplerpass" | ||
|
||
PreservedAnalyses clspv::InlineFuncWithReadImage3DNonLiteralSamplerPass::run( | ||
Module &M, ModuleAnalysisManager &) { | ||
PreservedAnalyses PA; | ||
|
||
// Loop through our inline pass until they stop changing thing. | ||
bool changed = true; | ||
while (changed) { | ||
changed &= InlineFunctions(M); | ||
} | ||
|
||
return PA; | ||
} | ||
|
||
static bool FunctionShouldBeInlined(Function &F) { | ||
for (BasicBlock &BB : F) { | ||
for (Instruction &I : BB) { | ||
// If we have a call instruction... | ||
if (auto call = dyn_cast<CallInst>(&I)) { | ||
// ...which is calling read_image with a 3d image and a non literal | ||
// sampler | ||
if (clspv::isReadImage3DWithNonLiteralSampler(call)) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
static bool FunctionContainsReadImageWithSampler(Function &F) { | ||
for (BasicBlock &BB : F) { | ||
for (Instruction &I : BB) { | ||
// If we have a call instruction... | ||
if (auto call = dyn_cast<CallInst>(&I)) { | ||
auto Name = call->getCalledFunction()->getName(); | ||
if (Name.contains("read_image") && Name.contains("ocl_sampler")) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
bool clspv::InlineFuncWithReadImage3DNonLiteralSamplerPass::InlineFunctions( | ||
Module &M) { | ||
bool Changed = false; | ||
|
||
UniqueVector<CallInst *> WorkList; | ||
std::set<Function *> FunctionToInline; | ||
for (Function &F : M) { | ||
if (F.isDeclaration() || F.getCallingConv() == CallingConv::SPIR_KERNEL) { | ||
continue; | ||
} | ||
if (FunctionShouldBeInlined(F)) { | ||
FunctionToInline.insert(&F); | ||
} | ||
} | ||
|
||
if (FunctionToInline.empty()) { | ||
return false; | ||
} | ||
|
||
// If we detect a read image of a 3D image with a non literal sampler, we need | ||
// to inline every function with read_image because they might be using a non | ||
// literal sampler used to read a 3D image, thus also needing a rework. | ||
for (Function &F : M) { | ||
if (F.isDeclaration() || F.getCallingConv() == CallingConv::SPIR_KERNEL) { | ||
continue; | ||
} | ||
if (FunctionContainsReadImageWithSampler(F)) { | ||
FunctionToInline.insert(&F); | ||
} | ||
} | ||
|
||
for (Function &F : M) { | ||
for (BasicBlock &BB : F) { | ||
for (Instruction &I : BB) { | ||
// If we have a call instruction... | ||
if (auto call = dyn_cast<CallInst>(&I)) { | ||
// ...which is calling a function to inline | ||
if (FunctionToInline.count(call->getCalledFunction()) > 0) { | ||
WorkList.insert(call); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (CallInst *Call : WorkList) { | ||
InlineFunctionInfo IFI; | ||
Changed |= InlineFunction(*Call, IFI, false, nullptr, false).isSuccess(); | ||
} | ||
|
||
return Changed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2023 The Clspv Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/PassManager.h" | ||
|
||
#ifndef _CLSPV_LIB_INLINE_READ_IMAGE3D_NON_LITERAL_SAMPLER_H | ||
#define _CLSPV_LIB_INLINE_READ_IMAGE3D_NON_LITERAL_SAMPLER_H | ||
|
||
namespace clspv { | ||
|
||
struct InlineFuncWithReadImage3DNonLiteralSamplerPass | ||
: llvm::PassInfoMixin<InlineFuncWithReadImage3DNonLiteralSamplerPass> { | ||
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &); | ||
|
||
bool InlineFunctions(llvm::Module &M); | ||
}; | ||
} // namespace clspv | ||
|
||
#endif // _CLSPV_LIB_INLINE_READ_IMAGE3D_NON_LITERAL_SAMPLER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.