From 65ac9f3a9ddcb8bcfc099ffb29beaa9a92ba1f53 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 1 Apr 2024 14:32:30 -0700 Subject: Fix peephole optimization of `TypeEquals`. (#3865) Closes #3861. --- source/slang/slang-ir-peephole.cpp | 3 +- tests/language-feature/types/is-on-type.slang | 45 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/language-feature/types/is-on-type.slang diff --git a/source/slang/slang-ir-peephole.cpp b/source/slang/slang-ir-peephole.cpp index d4369da7a..88b26fbd3 100644 --- a/source/slang/slang-ir-peephole.cpp +++ b/source/slang/slang-ir-peephole.cpp @@ -960,7 +960,8 @@ struct PeepholeContext : InstPassBase { auto getTypeFromOperand = [](IRInst* operand) -> IRType* { - if (as(operand->getFullType()) || !operand->getFullType()) + if (as(operand->getFullType()) || !operand->getFullType() || + as(operand->getFullType())) return (IRType*)operand; return operand->getFullType(); }; diff --git a/tests/language-feature/types/is-on-type.slang b/tests/language-feature/types/is-on-type.slang new file mode 100644 index 000000000..e9dd48fcf --- /dev/null +++ b/tests/language-feature/types/is-on-type.slang @@ -0,0 +1,45 @@ + +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute + +// Test that `is` operator works on generic type param. + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +interface I +{ + +} + +struct A : I +{ + uint i; +}; + +struct B : I +{ + float2 f2; +}; + +func test(T t) -> int +{ + int rs = 0; + if (T is A) + { + rs++; + } + if (T is B) + { + rs+=2; + } + return rs; +} + +[shader("compute")] +[numthreads(1,1, 1)] +void computeMain() +{ + B b; + // CHECK: 2 + outputBuffer[0] = test(b); +} \ No newline at end of file -- cgit v1.2.3