From 0bc89bc13251fedc9ed90cf473d2e6eb7fda3abf Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Sun, 2 Jun 2024 10:27:13 +0800 Subject: Prevent hoisting of non-hoistable instructions in non-function values with code (#4250) --- source/slang/slang-ir-redundancy-removal.cpp | 4 ++-- source/slang/slang-ir.cpp | 12 ++++++++++ source/slang/slang-ir.h | 3 +++ tests/bugs/generic-initializer-inlining.slang | 33 +++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/bugs/generic-initializer-inlining.slang diff --git a/source/slang/slang-ir-redundancy-removal.cpp b/source/slang/slang-ir-redundancy-removal.cpp index c7986cfbc..b679d8438 100644 --- a/source/slang/slang-ir-redundancy-removal.cpp +++ b/source/slang/slang-ir-redundancy-removal.cpp @@ -25,9 +25,9 @@ struct RedundancyRemovalContext for (UInt i = 0; i < inst->getOperandCount(); i++) { auto operand = inst->getOperand(i); - if (getParentFunc(operand) != func) + if (!hasDescendent(func, operand)) { - // Global value won't prevent hoisting. + // Only prevent hoisting from operands local to this function continue; } auto operandParent = as(operand->getParent()); diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp index 3e96d61eb..b6d000d20 100644 --- a/source/slang/slang-ir.cpp +++ b/source/slang/slang-ir.cpp @@ -8530,6 +8530,18 @@ namespace Slang return nullptr; } + bool hasDescendent(IRInst* inst, IRInst* child) + { + auto parent = child->getParent(); + while (parent) + { + if (inst == parent) + return true; + parent = parent->getParent(); + } + return false; + } + IRInst* getGenericReturnVal(IRInst* inst) { if (auto gen = as(inst)) diff --git a/source/slang/slang-ir.h b/source/slang/slang-ir.h index 3a5384e8d..9e8be7fc5 100644 --- a/source/slang/slang-ir.h +++ b/source/slang/slang-ir.h @@ -2538,6 +2538,9 @@ bool isBuiltin(IRInst* inst); // Get the enclosuing function of an instruction. IRFunc* getParentFunc(IRInst* inst); + // Is child a descendent of inst +bool hasDescendent(IRInst* inst, IRInst* child); + // True if moving this inst will not change the semantics of the program bool isMovableInst(IRInst* inst); diff --git a/tests/bugs/generic-initializer-inlining.slang b/tests/bugs/generic-initializer-inlining.slang new file mode 100644 index 000000000..8f126e43a --- /dev/null +++ b/tests/bugs/generic-initializer-inlining.slang @@ -0,0 +1,33 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-cpu + +// CHECK: 1 +// CHECK-NEXT: 0 +// CHECK-NEXT: 0 +// CHECK-NEXT: 0 + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +struct C +{ + [ForceInline] + __init(U value) + { + for (int i = 0; i < 1; ++i) + {} + } +} + +static C c = C(1); + +int use(T x) +{ + return 1; +} + +[numthreads(1, 1, 1)] +void computeMain(uint i : SV_GroupIndex) +{ + outputBuffer[0] = use(c); +} + -- cgit v1.2.3