Skip to content

Commit 6e1fb9b

Browse files
Merge pull request KhronosGroup#2203 from Try/msl-atomics-fix
MSL: fix extraction of global variables, in case of atomics
2 parents 1870ec7 + 43a59b7 commit 6e1fb9b

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#pragma clang diagnostic ignored "-Wmissing-prototypes"
2+
#pragma clang diagnostic ignored "-Wunused-variable"
3+
4+
#include <metal_stdlib>
5+
#include <simd/simd.h>
6+
#include <metal_atomic>
7+
8+
using namespace metal;
9+
10+
constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(64u, 1u, 1u);
11+
12+
static inline __attribute__((always_inline))
13+
void testAdd(threadgroup uint& var)
14+
{
15+
uint _29 = atomic_fetch_add_explicit((threadgroup atomic_uint*)&var, 1u, memory_order_relaxed);
16+
}
17+
18+
static inline __attribute__((always_inline))
19+
void testMin(threadgroup uint& var)
20+
{
21+
uint _31 = atomic_fetch_min_explicit((threadgroup atomic_uint*)&var, 2u, memory_order_relaxed);
22+
}
23+
24+
static inline __attribute__((always_inline))
25+
void testMax(threadgroup uint& var)
26+
{
27+
uint _33 = atomic_fetch_max_explicit((threadgroup atomic_uint*)&var, 3u, memory_order_relaxed);
28+
}
29+
30+
static inline __attribute__((always_inline))
31+
void testOr(threadgroup uint& var)
32+
{
33+
uint _35 = atomic_fetch_or_explicit((threadgroup atomic_uint*)&var, 5u, memory_order_relaxed);
34+
}
35+
36+
static inline __attribute__((always_inline))
37+
void testXor(threadgroup uint& var)
38+
{
39+
uint _37 = atomic_fetch_xor_explicit((threadgroup atomic_uint*)&var, 6u, memory_order_relaxed);
40+
}
41+
42+
static inline __attribute__((always_inline))
43+
void testExchange(threadgroup uint& var)
44+
{
45+
uint _39 = atomic_exchange_explicit((threadgroup atomic_uint*)&var, 7u, memory_order_relaxed);
46+
}
47+
48+
static inline __attribute__((always_inline))
49+
void testCompSwap(threadgroup uint& var)
50+
{
51+
uint _42;
52+
do
53+
{
54+
_42 = 8u;
55+
} while (!atomic_compare_exchange_weak_explicit((threadgroup atomic_uint*)&var, &_42, 9u, memory_order_relaxed, memory_order_relaxed) && _42 == 8u);
56+
}
57+
58+
static inline __attribute__((always_inline))
59+
void testStore(threadgroup uint& var)
60+
{
61+
atomic_store_explicit((threadgroup atomic_uint*)&var, 10u, memory_order_relaxed);
62+
}
63+
64+
static inline __attribute__((always_inline))
65+
void foo(threadgroup uint& var)
66+
{
67+
testAdd(var);
68+
testMin(var);
69+
testMax(var);
70+
testOr(var);
71+
testXor(var);
72+
testExchange(var);
73+
testCompSwap(var);
74+
testStore(var);
75+
}
76+
77+
kernel void main0()
78+
{
79+
threadgroup uint var;
80+
foo(var);
81+
}
82+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#version 460
2+
3+
#extension GL_KHR_memory_scope_semantics : enable
4+
5+
layout(local_size_x = 64) in;
6+
7+
shared uint var;
8+
9+
void testAdd()
10+
{
11+
atomicAdd(var, 1);
12+
}
13+
14+
void testMin()
15+
{
16+
atomicMin(var, 2);
17+
}
18+
19+
void testMax()
20+
{
21+
atomicMax(var, 3);
22+
}
23+
24+
void testAnd()
25+
{
26+
atomicAnd(var, 4);
27+
}
28+
29+
void testOr()
30+
{
31+
atomicOr(var, 5);
32+
}
33+
34+
void testXor()
35+
{
36+
atomicXor(var, 6);
37+
}
38+
39+
void testExchange()
40+
{
41+
atomicExchange(var, 7);
42+
}
43+
44+
void testCompSwap()
45+
{
46+
atomicCompSwap(var, 8, 9);
47+
}
48+
49+
void testStore()
50+
{
51+
atomicStore(var, 10u, gl_ScopeDevice, gl_StorageSemanticsShared, gl_SemanticsRelaxed);
52+
}
53+
54+
void foo()
55+
{
56+
testAdd();
57+
testMin();
58+
testMax();
59+
testOr();
60+
testXor();
61+
testExchange();
62+
testCompSwap();
63+
testStore();
64+
}
65+
66+
void main()
67+
{
68+
foo();
69+
}

spirv_msl.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -1898,9 +1898,18 @@ void CompilerMSL::extract_global_variables_from_function(uint32_t func_id, std::
18981898
case OpAtomicOr:
18991899
case OpAtomicXor:
19001900
case OpImageWrite:
1901+
{
19011902
if (needs_frag_discard_checks())
19021903
added_arg_ids.insert(builtin_helper_invocation_id);
1904+
uint32_t ptr = 0;
1905+
if (op == OpAtomicStore || op == OpImageWrite)
1906+
ptr = ops[0];
1907+
else
1908+
ptr = ops[2];
1909+
if (global_var_ids.find(ptr) != global_var_ids.end())
1910+
added_arg_ids.insert(ptr);
19031911
break;
1912+
}
19041913

19051914
// Emulate texture2D atomic operations
19061915
case OpImageTexelPointer:

0 commit comments

Comments
 (0)