diff --git a/lgc/include/lgc/patch/LowerGpuRt.h b/lgc/include/lgc/patch/LowerGpuRt.h index 65dcb6ccf7..3c07491cdf 100644 --- a/lgc/include/lgc/patch/LowerGpuRt.h +++ b/lgc/include/lgc/patch/LowerGpuRt.h @@ -34,7 +34,7 @@ #include "llvm/IR/PassManager.h" namespace lgc { -class BuilderImpl; +class Builder; class PipelineState; class GpurtGetStackSizeOp; @@ -76,6 +76,6 @@ class LowerGpuRt : public llvm::PassInfoMixin { PipelineState *m_pipelineState = nullptr; // Pipeline state llvm::SmallVector m_callsToLower; // Call instruction to lower llvm::SmallSet m_funcsToLower; // Functions to lower - BuilderImpl *m_builder = nullptr; + Builder *m_builder = nullptr; }; } // namespace lgc diff --git a/lgc/interface/lgc/Pipeline.h b/lgc/interface/lgc/Pipeline.h index 8248e584c9..6a346ac856 100644 --- a/lgc/interface/lgc/Pipeline.h +++ b/lgc/interface/lgc/Pipeline.h @@ -186,6 +186,7 @@ union Options { unsigned rtBoxSortHeuristicMode; // Ray tracing box sort heuristic mode unsigned rtStaticPipelineFlags; // Ray tracing static pipeline flags unsigned rtTriCompressMode; // Ray tracing triangle compression mode + bool useGpurt; // Whether GPURT is used }; }; static_assert(sizeof(Options) == sizeof(Options::u32All)); diff --git a/lgc/patch/LowerGpuRt.cpp b/lgc/patch/LowerGpuRt.cpp index afd9502e09..0cfe2c2b53 100644 --- a/lgc/patch/LowerGpuRt.cpp +++ b/lgc/patch/LowerGpuRt.cpp @@ -57,9 +57,8 @@ PreservedAnalyses LowerGpuRt::run(Module &module, ModuleAnalysisManager &analysi PipelineState *pipelineState = analysisManager.getResult(module).getPipelineState(); m_pipelineState = pipelineState; - BuilderImpl builderImpl(pipelineState); + Builder builderImpl(pipelineState->getContext()); m_builder = &builderImpl; - m_builder->setShaderStage(ShaderStageCompute); createGlobalStack(module); @@ -127,7 +126,6 @@ Value *LowerGpuRt::getThreadIdInGroup() const { // Create global variable for the stack // @param [in/out] module : LLVM module to be run on void LowerGpuRt::createGlobalStack(Module &module) { - struct Payload { bool needGlobalStack; bool needExtraStack; diff --git a/lgc/patch/Patch.cpp b/lgc/patch/Patch.cpp index 6d70d00780..38689b69dc 100644 --- a/lgc/patch/Patch.cpp +++ b/lgc/patch/Patch.cpp @@ -133,6 +133,14 @@ void Patch::addPasses(PipelineState *pipelineState, lgc::PassManager &passMgr, T if (patchTimer) LgcContext::createAndAddStartStopTimer(passMgr, patchTimer, true); + if (pipelineState->getOptions().useGpurt) { + // NOTE: Lower GPURT operations and run InstCombinePass before builder replayer, because some Op are going to be + // turned into constant value, so that we can eliminate unused `@lgc.create.load.buffer.desc` before getting into + // replayer. Otherwise, unnecessary `writes_uavs` and `uses_uav` may be set. + passMgr.addPass(LowerGpuRt()); + passMgr.addPass(createModuleToFunctionPassAdaptor(InstCombinePass())); + } + // We're using BuilderRecorder; replay the Builder calls now passMgr.addPass(BuilderReplayer()); @@ -142,8 +150,6 @@ void Patch::addPasses(PipelineState *pipelineState, lgc::PassManager &passMgr, T "// LLPC pipeline before-patching results\n")); } - passMgr.addPass(LowerGpuRt()); - const auto indirectMode = pipelineState->getOptions().rtIndirectMode; if (indirectMode == RayTracingIndirectMode::ContinuationsContinufy || indirectMode == RayTracingIndirectMode::Continuations) { diff --git a/llpc/context/llpcCompiler.cpp b/llpc/context/llpcCompiler.cpp index 5ee3fb977b..7586deebb0 100644 --- a/llpc/context/llpcCompiler.cpp +++ b/llpc/context/llpcCompiler.cpp @@ -1663,6 +1663,7 @@ Result Compiler::buildPipelineInternal(Context *context, ArrayRef gpurtShaderLibrary = createGpurtShaderLibrary(context); + setUseGpurt(&*pipeline); if (!gpurtShaderLibrary) return Result::ErrorInvalidShader; @@ -2677,6 +2678,16 @@ Result Compiler::generatePipeline(Context *context, unsigned moduleIndex, std::u return Result::Success; } +// ===================================================================================================================== +// Set this pipeline use GPURT library +// +// @param pipeline : The pipeline object +void Compiler::setUseGpurt(lgc::Pipeline *pipeline) { + auto options = pipeline->getOptions(); + options.useGpurt = true; + pipeline->setOptions(options); +} + // ===================================================================================================================== // Build single ray tracing pipeline ELF package. // @@ -2888,6 +2899,7 @@ Result Compiler::buildRayTracingPipelineInternal(RayTracingContext &rtContext, std::unique_ptr gpurtShaderLibrary; if (needGpurtShaderLibrary) { gpurtShaderLibrary = createGpurtShaderLibrary(mainContext); + setUseGpurt(&*pipeline); if (!gpurtShaderLibrary) return Result::ErrorInvalidShader; } diff --git a/llpc/context/llpcCompiler.h b/llpc/context/llpcCompiler.h index df71e5366e..c0d2dfdf61 100644 --- a/llpc/context/llpcCompiler.h +++ b/llpc/context/llpcCompiler.h @@ -199,6 +199,8 @@ class Compiler : public ICompiler { Result generatePipeline(Context *context, unsigned moduleIndex, std::unique_ptr module, ElfPackage &pipelineElf, lgc::Pipeline *pipeline, TimerProfiler &timerProfiler); + void setUseGpurt(lgc::Pipeline *pipeline); + std::vector m_options; // Compilation options MetroHash::Hash m_optionHash; // Hash code of compilation options GfxIpVersion m_gfxIp; // Graphics IP version info