From 332f60c19336252d907b83882aa70665ca93a9d2 Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Fri, 12 May 2023 04:33:21 +0800 Subject: MVP for higher order functions (#2849) * MVP for higher order functions * Add shader subgroup partitioned glsl intrinsics * Implement parsing and checking for tuple types Currently there is no way to do anything useful with them from the source language however * neaten * Correct precedence of function type parsing * neaten * higher order function tests * function types of any arity * Inference for higher order functions * Add second test for unsynchronized params * regenerate vs projects * dx11 -> dx12 for saturated cooperations tests * Disable saturated cooperation tests on vulkan They fail on release builds in CI, not essential for the higher order function work however * remove saturated-cooperation tests * Remove unnecessary assert and clarify control flow in AddDeclRefOverloadCandidates * Add Tuple type name mangling * Use functype keyword to introduce function types * Add more inference tests for hof --------- Co-authored-by: Yong He --- .../higher-order-functions/fix.slang | 26 ++++++++++++++++++ .../higher-order-functions/generic.slang | 14 ++++++++++ .../higher-order-functions/inference-generic.slang | 18 +++++++++++++ .../higher-order-functions/inference.slang | 21 +++++++++++++++ .../higher-order-functions/overloaded.slang | 18 +++++++++++++ .../overloaded.slang.expected | 8 ++++++ .../higher-order-functions/simple.slang | 31 ++++++++++++++++++++++ .../simple.slang.expected.txt | 5 ++++ 8 files changed, 141 insertions(+) create mode 100644 tests/language-feature/higher-order-functions/fix.slang create mode 100644 tests/language-feature/higher-order-functions/generic.slang create mode 100644 tests/language-feature/higher-order-functions/inference-generic.slang create mode 100644 tests/language-feature/higher-order-functions/inference.slang create mode 100644 tests/language-feature/higher-order-functions/overloaded.slang create mode 100644 tests/language-feature/higher-order-functions/overloaded.slang.expected create mode 100644 tests/language-feature/higher-order-functions/simple.slang create mode 100644 tests/language-feature/higher-order-functions/simple.slang.expected.txt (limited to 'tests') diff --git a/tests/language-feature/higher-order-functions/fix.slang b/tests/language-feature/higher-order-functions/fix.slang new file mode 100644 index 000000000..812353477 --- /dev/null +++ b/tests/language-feature/higher-order-functions/fix.slang @@ -0,0 +1,26 @@ +//DISABLE_TEST(compute, vulkan):COMPARE_COMPUTE_EX():-vk -compute -shaderobj -output-using-type +//DISABLE_TEST(compute):COMPARE_COMPUTE_EX():-slang -compute -shaderobj -output-using-type +// Disabled due to https://github.com/shader-slang/slang/issues/2198 + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +func fix(float x) -> float +{ + let epsilon = 0.01; + let x_ = go(x); + if(abs(x_ - x) < epsilon) + return x_; + return fix(x_); +} + +func go(x : float) -> float +{ + return (x + 121 / x) / 2; +} + +[numthreads(4, 1, 1)] +void computeMain(uint tig : SV_GroupIndex) +{ + outputBuffer[tig] = uint(fix(11)); +} diff --git a/tests/language-feature/higher-order-functions/generic.slang b/tests/language-feature/higher-order-functions/generic.slang new file mode 100644 index 000000000..e7d63eb27 --- /dev/null +++ b/tests/language-feature/higher-order-functions/generic.slang @@ -0,0 +1,14 @@ +//TEST:SIMPLE: + +func foo(f : functype (float) -> int) -> int{ + return f(0); +} + +int bap(float) { + return 1; +} + +int zap() { + // We should be able to specify which foo we want + return foo(bap); +} diff --git a/tests/language-feature/higher-order-functions/inference-generic.slang b/tests/language-feature/higher-order-functions/inference-generic.slang new file mode 100644 index 000000000..52e65d0d9 --- /dev/null +++ b/tests/language-feature/higher-order-functions/inference-generic.slang @@ -0,0 +1,18 @@ +//DISABLE_TEST:SIMPLE: +// Disabled because we don't actually perform this inference yet + +func foo(f : functype (float) -> int) -> int +{ + return f(0); +} + +int boo(T) +{ + return 1; +} + +int zoo() +{ + // We should infer that we want boo + return foo(boo); +} diff --git a/tests/language-feature/higher-order-functions/inference.slang b/tests/language-feature/higher-order-functions/inference.slang new file mode 100644 index 000000000..c01f5b19e --- /dev/null +++ b/tests/language-feature/higher-order-functions/inference.slang @@ -0,0 +1,21 @@ +//TEST:SIMPLE: + +func x(f : functype (A) -> B) +{ } + +float f(int) +{ + return 0; +} + +func y(g : functype (A, B) -> C) +{ } + +void g(A, bool) +{ } + +void main() +{ + x(f); + y(g); +} diff --git a/tests/language-feature/higher-order-functions/overloaded.slang b/tests/language-feature/higher-order-functions/overloaded.slang new file mode 100644 index 000000000..ebbc6ed19 --- /dev/null +++ b/tests/language-feature/higher-order-functions/overloaded.slang @@ -0,0 +1,18 @@ +//TEST:SIMPLE: + +func foo(f : functype (float) -> int) -> int{ + return f(0); +} + +int bit(T) { + return 1; +} + +int bit(vector) { + return 1; +} + +int zit() { + // In an ideal world in this case we could infer that we want bit + return foo(bit); +} diff --git a/tests/language-feature/higher-order-functions/overloaded.slang.expected b/tests/language-feature/higher-order-functions/overloaded.slang.expected new file mode 100644 index 000000000..3d02bf06f --- /dev/null +++ b/tests/language-feature/higher-order-functions/overloaded.slang.expected @@ -0,0 +1,8 @@ +result code = -1 +standard error = { +tests/language-feature/higher-order-functions/overloaded.slang(17): error 39999: passing overloaded functions to higher order functions is not supported + return foo(bit); + ^ +} +standard output = { +} diff --git a/tests/language-feature/higher-order-functions/simple.slang b/tests/language-feature/higher-order-functions/simple.slang new file mode 100644 index 000000000..7c6bf5a26 --- /dev/null +++ b/tests/language-feature/higher-order-functions/simple.slang @@ -0,0 +1,31 @@ +//TEST(compute, vulkan):COMPARE_COMPUTE_EX():-vk -compute -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE_EX():-slang -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +func twice(f : functype (A) -> A, a : A) -> A +{ + return f(f(a)); +} + +func apply2(f : functype (A, B) -> C, a : A, b : B) -> C +{ + return f(a, b); +} + +func multiply(x : float, y : float) -> float +{ + return x * y; +} + +func square(x : float) -> float +{ + return apply2(multiply, x, x); +} + +[numthreads(4, 1, 1)] +void computeMain(uint tig : SV_GroupIndex) +{ + outputBuffer[tig] = uint(twice(square, -2.f)); +} diff --git a/tests/language-feature/higher-order-functions/simple.slang.expected.txt b/tests/language-feature/higher-order-functions/simple.slang.expected.txt new file mode 100644 index 000000000..d9d031441 --- /dev/null +++ b/tests/language-feature/higher-order-functions/simple.slang.expected.txt @@ -0,0 +1,5 @@ +type: uint32_t +16 +16 +16 +16 -- cgit v1.2.3