From 9b606f7a25f66ec4150fb499cf42f5240445ed85 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Fri, 9 Apr 2021 18:22:28 +0000 Subject: [PATCH] ShaderModule: Keep the tint::Source::File alive Tint diagnostics hold a raw pointer to the tint::Source::File, so that source snippets can be printed. The tint::Source::File pointer was taken from a stack allocation, and the diagnostics were left with a dead pointer. Have the ShaderModuleParseResult keep the tint::Source::File alive with a PIMPL unique_ptr. Change-Id: I80d77516afd4012e15992821e060ef3038eba1c1 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/47422 Commit-Queue: Ben Clayton Reviewed-by: Corentin Wallez Reviewed-by: Austin Eng --- src/dawn_native/ShaderModule.cpp | 16 ++++++++++++++-- src/dawn_native/ShaderModule.h | 4 ++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/dawn_native/ShaderModule.cpp b/src/dawn_native/ShaderModule.cpp index 9090119ef0..7774c61bf5 100644 --- a/src/dawn_native/ShaderModule.cpp +++ b/src/dawn_native/ShaderModule.cpp @@ -1047,6 +1047,17 @@ namespace dawn_native { return tintProgram != nullptr || spirv.size() > 0; } + // TintSource is a PIMPL container for a tint::Source::File, which needs to be kept alive for as + // long as tint diagnostics are inspected. + class TintSource { + public: + template + TintSource(ARGS&&... args) : file(std::forward(args)...) { + } + + tint::Source::File file; + }; + MaybeError ValidateShaderModuleDescriptor(DeviceBase* device, const ShaderModuleDescriptor* descriptor, ShaderModuleParseResult* parseResult) { @@ -1088,13 +1099,14 @@ namespace dawn_native { const auto* wgslDesc = static_cast(chainedDescriptor); - tint::Source::File file("", wgslDesc->source); + auto tintSource = std::make_unique("", wgslDesc->source); tint::Program program; - DAWN_TRY_ASSIGN(program, ParseWGSL(&file, outMessages)); + DAWN_TRY_ASSIGN(program, ParseWGSL(&tintSource->file, outMessages)); if (device->IsToggleEnabled(Toggle::UseTintGenerator)) { parseResult->tintProgram = std::make_unique(std::move(program)); + parseResult->tintSource = std::move(tintSource); } else { tint::transform::Manager transformManager; transformManager.append( diff --git a/src/dawn_native/ShaderModule.h b/src/dawn_native/ShaderModule.h index 2e8c3af949..ab3a271558 100644 --- a/src/dawn_native/ShaderModule.h +++ b/src/dawn_native/ShaderModule.h @@ -55,6 +55,9 @@ namespace dawn_native { using EntryPointMetadataTable = std::unordered_map>; + // Source for a tint program + class TintSource; + struct ShaderModuleParseResult { ShaderModuleParseResult(); ~ShaderModuleParseResult(); @@ -64,6 +67,7 @@ namespace dawn_native { bool HasParsedShader() const; std::unique_ptr tintProgram; + std::unique_ptr tintSource; std::vector spirv; std::unique_ptr compilationMessages; };