From afb8146e10626887e3eb9f479480d4f8a1ad6128 Mon Sep 17 00:00:00 2001 From: Gangzheng Tong Date: Tue, 16 Sep 2025 12:51:43 -0700 Subject: Diagnose error when the function args can't satisfy constexpr parameter requirements (#7269) ## Summary This PR enhances constexpr validation by adding proper error checking when function arguments cannot satisfy constexpr parameter requirements, addressing issue #6370. ## Problem Previously, when a function declared constexpr parameters, the compiler would attempt to propagate constexpr-ness to the call site arguments, but there was insufficient validation and error reporting when this propagation failed. This could lead silent failures where constexpr requirements weren't properly enforced ## Solution This PR adds checks that: 1. **Validates constexpr arguments**: When a function parameter is marked as `constexpr`, the compiler now explicitly checks that the corresponding argument can be marked as `constexpr` 2. **Issues clear compilation errors**: added `Diagnostics::argIsNotConstexpr`) 3. **Handles both call scenarios**: The validation works for both: - Direct function calls with IR-level function definitions - Calls to function from external modules Fixes #6370 --------- Co-authored-by: slangbot Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- tests/diagnostics/constexpr-param.slang | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/diagnostics/constexpr-param.slang (limited to 'tests/diagnostics/constexpr-param.slang') diff --git a/tests/diagnostics/constexpr-param.slang b/tests/diagnostics/constexpr-param.slang new file mode 100644 index 000000000..d41410b16 --- /dev/null +++ b/tests/diagnostics/constexpr-param.slang @@ -0,0 +1,56 @@ +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK1): +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK2): + +// Failure to pass compile-time-constant argument +// where it is expected. +// +// Test both callee and nested callee that require constexpr parameters. +// Test both function defined in the same module and external function. + +Texture2D input; +RWTexture2D output; + +int func_with_constexpr_params(constexpr int const_a) +{ + return const_a + const_a; +} + +int func_internal(int a) +{ + // Error: arg passed through isn't compile-time constant + // CHECK: ([[# @LINE+1]]): error 40013: + return func_with_constexpr_params(a); +} + +int func_external(int a) +{ + // CHECK1: ([[# @LINE+1]]): error 40012: + int2 offset = int2(a, a); + int3 location = int3(0, 0, 0); + // Load requires constexpr offset + return input.Load(location, offset); +} + +[shader("compute")] +void Test(uint3 thread_index : SV_DispatchThreadID) +{ + int result = 0; + + // Okay, immediate constant + result += func_with_constexpr_params(1); + + + int ii = 0; + if (thread_index.x > 0) + { + ii = 1; + } + // Pass non-compile-time-constant argument to function that requires constexpr parameter + // CHECK2: ([[# @LINE+1]]): error 40013: + result += func_with_constexpr_params(ii); + + // The nested function calls should issue compile errors + result += func_internal(ii); + result += func_external(ii); +} -- cgit v1.2.3