Skip to content

Commit 79d754a

Browse files
committed
Merge branch 'natevm-bitfield-instructions' of github.com:natevm/slang into natevm-bitfield-instructions
2 parents fc0f6c8 + 9534072 commit 79d754a

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

source/slang-glslang/slang-glslang.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,10 @@
33

44

55
#include "glslang/Public/ResourceLimits.h"
6-
#include "StandAlone/Worklist.h"
7-
#include "glslang/Include/ShHandle.h"
86
#include "glslang/Public/ShaderLang.h"
97
#include "SPIRV/GlslangToSpv.h"
10-
#include "SPIRV/GLSL.std.450.h"
11-
#include "SPIRV/doc.h"
128
#include "SPIRV/disassemble.h"
139

14-
#include "glslang/MachineIndependent/localintermediate.h"
15-
1610
#include "slang.h"
1711

1812
#include "spirv-tools/optimizer.hpp"
@@ -23,6 +17,7 @@
2317
#endif
2418

2519
#include <memory>
20+
#include <mutex>
2621
#include <sstream>
2722

2823
// This is a wrapper to allow us to run the `glslang` compiler

source/slang/slang-check-overload.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// slang-check-overload.cpp
2+
#include "slang-ast-base.h"
23
#include "slang-check-impl.h"
34

45
#include "slang-lookup.h"
@@ -1199,6 +1200,28 @@ namespace Slang
11991200
return parent;
12001201
}
12011202

1203+
void countDistanceToGloablScope(DeclRef<Slang::Decl> const& leftDecl,
1204+
DeclRef<Slang::Decl> const& rightDecl,
1205+
int& leftDistance, int& rightDistance)
1206+
{
1207+
leftDistance = 0;
1208+
rightDistance = 0;
1209+
1210+
DeclRef<Decl> decl = leftDecl;
1211+
while(decl)
1212+
{
1213+
leftDistance++;
1214+
decl = decl.getParent();
1215+
}
1216+
1217+
decl = rightDecl;
1218+
while(decl)
1219+
{
1220+
rightDistance++;
1221+
decl = decl.getParent();
1222+
}
1223+
}
1224+
12021225
// Returns -1 if left is preferred, 1 if right is preferred, and 0 if they are equal.
12031226
//
12041227
int SemanticsVisitor::CompareLookupResultItems(
@@ -1324,6 +1347,24 @@ namespace Slang
13241347
}
13251348
}
13261349

1350+
// We need to consider the distance of the declarations to the global scope to resolve this case:
1351+
// float f(float x);
1352+
// struct S
1353+
// {
1354+
// float f(float x);
1355+
// float g(float y) { return f(y); } // will call S::f() instead of ::f()
1356+
// }
1357+
// 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,
1358+
// because this function will only choose the valid candidates. So if there is situation like this:
1359+
// void main() { S s; s.f(1.0);} or
1360+
// struct T { float g(y) { f(y); } }, there won't be ambiguity.
1361+
// So we just need to count which declaration is farther from the global scope and favor the farther one.
1362+
int leftDistance = 0;
1363+
int rightDistance = 0;
1364+
countDistanceToGloablScope(left.declRef, right.declRef, leftDistance, rightDistance);
1365+
if (leftDistance != rightDistance)
1366+
return leftDistance > rightDistance ? -1 : 1;
1367+
13271368
// TODO: We should generalize above rules such that in a tie a declaration
13281369
// A::m is better than B::m when all other factors are equal and
13291370
// A inherits from B.

tests/bugs/overload-ambiguous.slang

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// https://github.com/shader-slang/slang/issues/4476
2+
3+
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-cuda -compute -shaderobj
4+
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-cpu -compute -shaderobj
5+
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -compute -shaderobj
6+
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -compute -dx12 -shaderobj
7+
//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-vk -compute -shaderobj
8+
9+
//TEST_INPUT:ubuffer(data=[0 0], stride=4):out,name=outputBuffer
10+
RWStructuredBuffer<uint> outputBuffer;
11+
12+
13+
uint getData()
14+
{
15+
return 1u;
16+
}
17+
18+
struct DataObtainer
19+
{
20+
uint data;
21+
uint getData()
22+
{
23+
return data;
24+
}
25+
26+
uint getValue()
27+
{
28+
return getData(); // will call DataObtainer::getData()
29+
}
30+
31+
uint getValue2()
32+
{
33+
return ::getData(); // will call global getData()
34+
}
35+
}
36+
37+
RWStructuredBuffer<uint> output;
38+
39+
[numthreads(1, 1, 1)]
40+
[shader("compute")]
41+
void computeMain(uint3 threadID: SV_DispatchThreadID)
42+
{
43+
DataObtainer obtainer = {2u};
44+
outputBuffer[0] = obtainer.getValue();
45+
outputBuffer[1] = obtainer.getValue2();
46+
// BUF: 2
47+
// BUF-NEXT: 1
48+
}

0 commit comments

Comments
 (0)