Skip to content

Commit

Permalink
Merge branch 'master' into refl-api-spec-arg-types
Browse files Browse the repository at this point in the history
  • Loading branch information
saipraveenb25 authored Sep 16, 2024
2 parents 8422abf + c46ca4c commit e32ac67
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
7 changes: 1 addition & 6 deletions source/slang-glslang/slang-glslang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@


#include "glslang/Public/ResourceLimits.h"
#include "StandAlone/Worklist.h"
#include "glslang/Include/ShHandle.h"
#include "glslang/Public/ShaderLang.h"
#include "SPIRV/GlslangToSpv.h"
#include "SPIRV/GLSL.std.450.h"
#include "SPIRV/doc.h"
#include "SPIRV/disassemble.h"

#include "glslang/MachineIndependent/localintermediate.h"

#include "slang.h"

#include "spirv-tools/optimizer.hpp"
Expand All @@ -23,6 +17,7 @@
#endif

#include <memory>
#include <mutex>
#include <sstream>

// This is a wrapper to allow us to run the `glslang` compiler
Expand Down
41 changes: 41 additions & 0 deletions source/slang/slang-check-overload.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// slang-check-overload.cpp
#include "slang-ast-base.h"
#include "slang-check-impl.h"

#include "slang-lookup.h"
Expand Down Expand Up @@ -1199,6 +1200,28 @@ namespace Slang
return parent;
}

void countDistanceToGloablScope(DeclRef<Slang::Decl> const& leftDecl,
DeclRef<Slang::Decl> const& rightDecl,
int& leftDistance, int& rightDistance)
{
leftDistance = 0;
rightDistance = 0;

DeclRef<Decl> decl = leftDecl;
while(decl)
{
leftDistance++;
decl = decl.getParent();
}

decl = rightDecl;
while(decl)
{
rightDistance++;
decl = decl.getParent();
}
}

// Returns -1 if left is preferred, 1 if right is preferred, and 0 if they are equal.
//
int SemanticsVisitor::CompareLookupResultItems(
Expand Down Expand Up @@ -1324,6 +1347,24 @@ namespace Slang
}
}

// We need to consider the distance of the declarations to the global scope to resolve this case:
// float f(float x);
// struct S
// {
// float f(float x);
// float g(float y) { return f(y); } // will call S::f() instead of ::f()
// }
// We don't need to know the call site of 'f(y)', but only need to count the two candidates' distance to the global scope,
// because this function will only choose the valid candidates. So if there is situation like this:
// void main() { S s; s.f(1.0);} or
// struct T { float g(y) { f(y); } }, there won't be ambiguity.
// So we just need to count which declaration is farther from the global scope and favor the farther one.
int leftDistance = 0;
int rightDistance = 0;
countDistanceToGloablScope(left.declRef, right.declRef, leftDistance, rightDistance);
if (leftDistance != rightDistance)
return leftDistance > rightDistance ? -1 : 1;

// TODO: We should generalize above rules such that in a tie a declaration
// A::m is better than B::m when all other factors are equal and
// A inherits from B.
Expand Down
48 changes: 48 additions & 0 deletions tests/bugs/overload-ambiguous.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// https://github.com/shader-slang/slang/issues/4476

//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-cuda -compute -shaderobj
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-cpu -compute -shaderobj
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -compute -shaderobj
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -compute -dx12 -shaderobj
//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-vk -compute -shaderobj

//TEST_INPUT:ubuffer(data=[0 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<uint> outputBuffer;


uint getData()
{
return 1u;
}

struct DataObtainer
{
uint data;
uint getData()
{
return data;
}

uint getValue()
{
return getData(); // will call DataObtainer::getData()
}

uint getValue2()
{
return ::getData(); // will call global getData()
}
}

RWStructuredBuffer<uint> output;

[numthreads(1, 1, 1)]
[shader("compute")]
void computeMain(uint3 threadID: SV_DispatchThreadID)
{
DataObtainer obtainer = {2u};
outputBuffer[0] = obtainer.getValue();
outputBuffer[1] = obtainer.getValue2();
// BUF: 2
// BUF-NEXT: 1
}

0 comments on commit e32ac67

Please sign in to comment.