From 57c3f938221c427b78da7087f8a832ba4a271a7c Mon Sep 17 00:00:00 2001 From: Julius Ikkala Date: Fri, 23 May 2025 22:27:37 +0300 Subject: Implement throw & catch statements (#6916) * Implement throw statement It already existed in the IR, so only parsing, checking and lowering was missing. * Initial catch implementation Likely very broken. * Error out when catch() isn't last in scope * Prevent accessing variables from scope preceding catch As those may actually not be available at that point. * Add IError and use it in Result type lowering * Add diagnostic tests * Allow caught throws in non-throw functions * Fix catch propagating between functions & SPIR-V merge issue * Add test for non-trivial error types * Fix MSVC build * Fix invalid value type from Result lowering * Also lower error handling in templates * Lower result types only after specialization * Attempt to disambiguate error enums by witness table * Revert matching by witness, types should be distinct too * Don't assert valueField when getting Result's error value It may not exist if the function returns void, but getting the error value is still legitimate. * Update tests for new error numbers & get rid of expected.txt * Change catch lowering to resemble breaking a loop ... To make SPIR-V happy. * Fix dead catch blocks and invalid cached dominator tree * More SPIR-V adjustment * Lower catch as two nested loops * Add defer interaction test and revert broken defer changes * Fix enum type when throwing literals * Cleanup and bikeshedding * Document error handling mechanism * Fix table of contents * Use boolean tag in Result * Use anyValue storage for Result * Remove IError * Fix formatting * Eradicate success values from docs and tests * Use parseModernParamDecl for catch parameter * Implement do-catch syntax * Implement catch-all * Fix formatting * Fix marshalling native calls that throw --------- Co-authored-by: Yong He --- tests/language-feature/error-handling/basic.slang | 91 +++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tests/language-feature/error-handling/basic.slang (limited to 'tests/language-feature/error-handling/basic.slang') diff --git a/tests/language-feature/error-handling/basic.slang b/tests/language-feature/error-handling/basic.slang new file mode 100644 index 000000000..4cb270cb8 --- /dev/null +++ b/tests/language-feature/error-handling/basic.slang @@ -0,0 +1,91 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk -shaderobj +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -cpu -shaderobj + +// CHECK: 2 +// CHECK-NEXT: 0 +// CHECK-NEXT: 11 +// CHECK-NEXT: 12 +// CHECK-NEXT: 6 +// CHECK-NEXT: 1 + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +enum MyError1 +{ + Fail +}; + +enum MyError2 +{ + Fail1, + Fail2 = 0x12 +}; + +void throwingFunc() throws MyError1 +{ + throw MyError1.Fail; +} + +int maybeBadFunc1(int n) throws MyError1 +{ + if (n == 1) throw MyError1.Fail; + return n; +} + +int maybeBadFunc2(int n) throws MyError2 +{ + if (n == 2) throw MyError2.Fail2; + return n; +} + +int multiCatchFunc(int n) +{ + do + { + let a = try maybeBadFunc1(n); + let b = try maybeBadFunc2(n); + return a+b; + } + catch(err: MyError1) + { + return 0x11; + } + catch(err: MyError2) + { + return reinterpret(err); + } +} + +int containedThrow() +{ + do + { + throw MyError1.Fail; + } + catch(err: MyError1) + { + return 1; + } +} + +[numthreads(1, 1, 1)] +void computeMain(int3 dispatchThreadID: SV_DispatchThreadID) +{ + do + { + try throwingFunc(); + outputBuffer[0] = 1; + } + catch(err: MyError1) + { + outputBuffer[0] = 2; + } + + outputBuffer[1] = multiCatchFunc(0); + outputBuffer[2] = multiCatchFunc(1); + outputBuffer[3] = multiCatchFunc(2); + outputBuffer[4] = multiCatchFunc(3); + outputBuffer[5] = containedThrow(); +} -- cgit v1.2.3