From 8a15efb37a33d3c2943be87a19cbf9b5e2e8432b Mon Sep 17 00:00:00 2001 From: ArielG-NV <159081215+ArielG-NV@users.noreply.github.com> Date: Fri, 1 Aug 2025 12:36:29 -0700 Subject: Drain sink when single-argument constructor call fail (#7883) * fix bug * fix test * push test changs for clarity * fix bug * fix test * push test changs for clarity * test what fails * remove redundant code --- source/slang/hlsl.meta.slang | 13 ++++++- source/slang/slang-check-conversion.cpp | 5 ++- source/slang/slang-check-overload.cpp | 16 ++++++-- tests/bugs/gh-7856.slang | 52 ++++++++++++++++++++++++++ tests/language-feature/pointer-cast-glsl.slang | 28 ++++++++++---- 5 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 tests/bugs/gh-7856.slang diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index 9fd5c8b6e..c3d2efaac 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -22665,7 +22665,7 @@ struct ConstBufferPointer [ForceInline] static ConstBufferPointer fromUInt(uint64_t val) { - return {(T*)val}; + return ConstBufferPointer(val); } [ForceInline] @@ -22679,8 +22679,17 @@ struct ConstBufferPointer { return _ptr != nullptr; } -} + __init(T* val) + { + _ptr = val; + } + + __init(uint64_t val) + { + _ptr = (T*)val; + } +} // // HLSL-like dynamic resources // https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_DynamicResources.html diff --git a/source/slang/slang-check-conversion.cpp b/source/slang/slang-check-conversion.cpp index 61d132627..a605928e3 100644 --- a/source/slang/slang-check-conversion.cpp +++ b/source/slang/slang-check-conversion.cpp @@ -1711,6 +1711,8 @@ bool SemanticsVisitor::_coerce( } } + bool result = true; + // Conceptually, we want to treat the conversion as // possible, but report it as ambiguous if we actually // need to reify the result as an expression. @@ -1723,6 +1725,7 @@ bool SemanticsVisitor::_coerce( } *outToExpr = CreateErrorExpr(fromExpr); + result = false; } if (!cachedMethod) @@ -1734,7 +1737,7 @@ bool SemanticsVisitor::_coerce( if (outCost) *outCost = bestCost; - return true; + return result; } else if (overloadContext.bestCandidate) { diff --git a/source/slang/slang-check-overload.cpp b/source/slang/slang-check-overload.cpp index 71e8488d8..9e5560b3a 100644 --- a/source/slang/slang-check-overload.cpp +++ b/source/slang/slang-check-overload.cpp @@ -2727,6 +2727,7 @@ Expr* SemanticsVisitor::ResolveInvoke(InvokeExpr* expr) // bool typeOverloadChecked = false; + DiagnosticSink collectedErrorsSink(getSourceManager(), nullptr); if (expr->arguments.getCount() == 1 && !as(expr) && !as(expr->arguments[0])) { @@ -2735,16 +2736,15 @@ Expr* SemanticsVisitor::ResolveInvoke(InvokeExpr* expr) if (isDeclRefTypeOf(typeType->getType())) { Expr* resultExpr = nullptr; - DiagnosticSink tempSink(getSourceManager(), nullptr); ConversionCost conversionCost = kConversionCost_None; - auto coerceResult = SemanticsVisitor(withSink(&tempSink)) + auto coerceResult = SemanticsVisitor(withSink(&collectedErrorsSink)) ._coerce( CoercionSite::ExplicitCoercion, typeType->getType(), &resultExpr, expr->arguments[0]->type, expr->arguments[0], - &tempSink, + &collectedErrorsSink, &conversionCost); if (auto resultInvokeExpr = as(resultExpr)) { @@ -2963,6 +2963,16 @@ Expr* SemanticsVisitor::ResolveInvoke(InvokeExpr* expr) // for language server to use. if (IsErrorExpr(outExpr)) { + // Drain our error sink of "saved errors" + if (collectedErrorsSink.getErrorCount()) + { + Slang::ComPtr blob; + collectedErrorsSink.getBlobIfNeeded(blob.writeRef()); + getSink()->diagnoseRaw( + Severity::Error, + static_cast(blob->getBufferPointer())); + } + if (auto invokeExpr = as(outExpr)) { invokeExpr->originalFunctionExpr = typeExpr; diff --git a/tests/bugs/gh-7856.slang b/tests/bugs/gh-7856.slang new file mode 100644 index 000000000..b1edec8b5 --- /dev/null +++ b/tests/bugs/gh-7856.slang @@ -0,0 +1,52 @@ +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -target spirv -entry computeMain -stage compute + +enum A +{ + V1, + V2, + V3 +} + +enum B +{ + V1, + V2, + V3 +} + +struct Foo +{ + int a; + __init(int b) { a = b; } + __init(Foo ptr) + { + } + + // internally Slang is throwing an error, we just don't drain the error + // from our sink + __init(uint64_t val) {} + __init(int64_t val) {} +} + +extension int64_t +{ + __init(Foo t) {} +} +extension uint64_t +{ + __init(Foo t) {} +} + +RWStructuredBuffer output; + +[shader("compute")] +[numthreads(1, 1, 1)] +void computeMain(uint3 threadId: SV_DispatchThreadID) +{ + Foo v1 = Foo(1); + + // CHECK: ([[# @LINE+1]]): error 30080 + Foo v2 = Foo(v1); + + output[0] = v1.a; +} diff --git a/tests/language-feature/pointer-cast-glsl.slang b/tests/language-feature/pointer-cast-glsl.slang index 9e51c4328..a9516dae2 100644 --- a/tests/language-feature/pointer-cast-glsl.slang +++ b/tests/language-feature/pointer-cast-glsl.slang @@ -1,14 +1,28 @@ -//TEST:SIMPLE(filecheck=GLSL):-target glsl -entry main +//TEST:SIMPLE(filecheck=GLSL):-target glsl -entry computeMain +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-vk -compute -emit-spirv-via-glsl // Test that pointer casts in GLSL generate constructor-style casts instead of C-style casts // This addresses issue https://github.com/shader-slang/slang/issues/7838 -//GLSL: BufferPointer__S1_2(address_0) +//TEST_INPUT: set address = ubuffer(data=[1 2 3 4 5 6 7 8], stride=4); +uniform uint64_t address; +//TEST_INPUT: set pointer = ubuffer(data=[1 2 3 4 5 6 7 8], stride=4); +uniform int* pointer; +//TEST_INPUT: set outputBuffer = out ubuffer(data=[0 0 0 0 0 0 0 0], stride=4); +RWStructuredBuffer outputBuffer; -[shader("vertex")] -float4 main(uint vertexID : SV_VertexID, uint64_t address) : SV_Position +// Ensure we have a proper GLSL supported ctor call for BDA +//GLSL: BufferPointer__{{[a-zA-Z0-9_]+}}({{[a-zA-Z0-9_]+}}_0) + +// Ensure our GLSL code compiles and runs +//BUF: 2 + +[shader("compute")] +[numthreads(1,1,1)] +void computeMain() : SV_Position { // This should generate BufferPointer(address) instead of (BufferPointer)address in GLSL - let buffer = ConstBufferPointer(address); - return buffer[vertexID]; -} \ No newline at end of file + let buffer1 = ConstBufferPointer(address); + let buffer2 = ConstBufferPointer(pointer); + outputBuffer[0] = buffer1[0] + buffer2[0]; +} -- cgit v1.2.3