tint::transform::SingleEntryPoint: Preserve global 'const's

These can be used in all sorts of places which are not tracked by sem::Function::TransitivelyReferencedGlobals().
As they're not emitted as variables by any backend, just preserve them.

Fixed: tint:1598
Change-Id: I2696486cb2ffe8408bd5dd3090d7d600ca1d170f
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/101481
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Auto-Submit: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2022-09-07 13:05:24 +00:00
committed by Dawn LUCI CQ
parent 688c6dc808
commit eeda18d55e
9 changed files with 119 additions and 3 deletions

View File

@@ -86,11 +86,17 @@ void SingleEntryPoint::Run(CloneContext& ctx, const DataMap& inputs, DataMap&) c
ctx.dst->AST().AddGlobalVariable(ctx.Clone(override));
}
},
[&](const ast::Variable* v) { // var, let
if (referenced_vars.count(v)) {
ctx.dst->AST().AddGlobalVariable(ctx.Clone(v));
[&](const ast::Var* var) {
if (referenced_vars.count(var)) {
ctx.dst->AST().AddGlobalVariable(ctx.Clone(var));
}
},
[&](const ast::Const* c) {
// Always keep 'const' declarations, as these can be used by attributes and array
// sizes, which are not tracked as transitively used by functions. They also don't
// typically get emitted by the backend unless they're actually used.
ctx.dst->AST().AddGlobalVariable(ctx.Clone(c));
},
[&](const ast::Function* func) {
if (sem.Get(func)->HasAncestorEntryPoint(entry_point->symbol)) {
ctx.dst->AST().AddFunction(ctx.Clone(func));

View File

@@ -217,8 +217,14 @@ fn comp_main2() {
)";
auto* expect = R"(
const a : f32 = 1.0;
const b : f32 = 1.0;
const c : f32 = 1.0;
const d : f32 = 1.0;
@compute @workgroup_size(1)
fn comp_main1() {
let local_c : f32 = c;
@@ -536,5 +542,28 @@ fn comp_main1() {
EXPECT_EQ(expect, str(got));
}
TEST_F(SingleEntryPointTest, GlobalConstUsedAsArraySize) {
// See crbug.com/tint/1598
auto* src = R"(
const MY_SIZE = 5u;
type Arr = array<i32, MY_SIZE>;
@fragment
fn main() {
}
)";
auto* expect = src;
SingleEntryPoint::Config cfg("main");
DataMap data;
data.Add<SingleEntryPoint::Config>(cfg);
auto got = Run<SingleEntryPoint>(src, data);
EXPECT_EQ(expect, str(got));
}
} // namespace
} // namespace tint::transform