From 63dcc7acf4784d95407866730a70f5d37d391b88 Mon Sep 17 00:00:00 2001 From: "T. Foley" Date: Thu, 27 May 2021 13:40:17 -0700 Subject: Fix a bug in struct inheritance (#1861) During lowering from AST to IR, the Slang compiler translates code that uses `struct` inheritance: ```hlsl struct Base { int a; } struct Derived : Base {} ``` into code where the inheritance relationship is "witnessed" by a simple field: ```hlsl struct Base { int a; } struct Derived { Base __anonymous_field__; } ``` The underlying bug here is that the `__anonymous_field__` that the compiler generated during IR lowering was not being given any linkage decorations (no mangled name). As a result, if multiple separately-compiled modules all access that field they could disagree on its identity as an IR instruction. This could lead to output code being generated where the declaration of `__anonymous_field__` uses one IR instruction, but accesses use another. This change includes a fix for the issue, and a test that serves as a reproducer for the original problem. --- source/slang/slang-emit.cpp | 9 +++++++++ source/slang/slang-lower-to-ir.cpp | 1 + 2 files changed, 10 insertions(+) (limited to 'source') diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index 74b11079d..16164b957 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -698,6 +698,15 @@ Result linkAndOptimizeIR( // bit_cast on basic types. lowerBitCast(targetRequest, irModule); eliminateDeadCode(irModule); + + + // We include one final step to (optionally) dump the IR and validate + // it after all of the optimization passes are complete. This should + // reflect the IR that code is generated from as closely as possible. + // +#if 0 + dumpIRIfEnabled(compileRequest, irModule, "OPTIMIZED"); +#endif validateIRModuleIfEnabled(compileRequest, irModule); return SLANG_OK; diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index de5a07c0d..ab488a41f 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -5475,6 +5475,7 @@ struct DeclLoweringVisitor : DeclVisitor // a field that holds the base type... // auto irKey = getBuilder()->createStructKey(); + addLinkageDecoration(context, irKey, inheritanceDecl); auto keyVal = LoweredValInfo::simple(irKey); setGlobalValue(context, inheritanceDecl, keyVal); return keyVal; -- cgit v1.2.3