<feed xmlns='http://www.w3.org/2005/Atom'>
<title>slang.git/source/slang/slang-ir-metal-legalize.cpp, branch master</title>
<subtitle>Making it easier to work with shaders</subtitle>
<id>https://git.yummers.dev/slang.git/atom?h=master</id>
<link rel='self' href='https://git.yummers.dev/slang.git/atom?h=master'/>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/'/>
<updated>2025-10-01T02:08:23+00:00</updated>
<entry>
<title>Enhance buffer load specialization pass to specialize past field extracts. (#8547)</title>
<updated>2025-10-01T02:08:23+00:00</updated>
<author>
<name>Yong He</name>
<email>yonghe@outlook.com</email>
</author>
<published>2025-10-01T02:08:23+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=e4611e2e30a3e5969d402f5ed7e72706a0e3b024'/>
<id>urn:sha1:e4611e2e30a3e5969d402f5ed7e72706a0e3b024</id>
<content type='text'>
This allows us to specialize functions whose argument is a sub element
of a constant buffer, instead of being only applicable to entire buffer
element. Closes #8421.

This change also implements a proper heuristic to determine when to
specialize the calls and defer the buffer loads.

This PR addresses a pathological case exposed in
`slangpy\slangpy\benchmarks\test_benchmark_tensor.py`, which used to
take 27ms to finish, and now takes 1.25ms.


For example, given:
```
struct Bottom
{
    float bigArray[1024];

    [mutating]
    void setVal(int index, float value) { bigArray[index] = value; }
}

struct Root
{
    Bottom top[2];
    [mutating]
    void setTopVal(int x, int y, float value)
    {
        top[x].setVal(y, value);
    }
}

RWStructuredBuffer&lt;Root&gt; sb;

[shader("compute")]
[numthreads(1, 1, 1)]
void compute_main(uint3 tid: SV_DispatchThreadID)
{
    sb[0].setTopVal(1, 2, 100.0f);
}
```

We are now able to specialize the call to `setTopVal` into:
```
void compute_main(uint3 tid: SV_DispatchThreadID)
{
    setTopVal_specialized(0, 1, 2, 100.0f);
}

void setTopVal_specialized(int sbIdx, int x, int y, float value)
{
      Bottom_setVal_specialized(sbIdx, x, y, value);
}

void Bottom_setVal_specialized(int sbIdx, int x, int y, float value)
{
     sb[sbIdx].top[x].bigArray[y] = value;
}
```

And get rid of all unnecessary loads. Achieving this requires a
combination of function call specialization and buffer-load-defer pass.
The buffer-load-defer pass has been completely rewritten to be more
correct and avoid introducing redundant loads.

This PR also adds tests to make sure pointers, bindless handles, and
loads from structured buffer or constant buffers works as expected.</content>
</entry>
<entry>
<title>Rewriting the lower-buffer-element-type pass to avoid unnecessary packing/unpacking. (#8526)</title>
<updated>2025-09-30T00:45:08+00:00</updated>
<author>
<name>Yong He</name>
<email>yonghe@outlook.com</email>
</author>
<published>2025-09-30T00:45:08+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=a6deb5ed82cb8fc6b4f4c5c5fee264e09f97ff89'/>
<id>urn:sha1:a6deb5ed82cb8fc6b4f4c5c5fee264e09f97ff89</id>
<content type='text'>
Part of the effort to improve the performance of generated SPIRV code.

The existing lower-buffer-element-type pass works by loading the entire
buffer element content from memory, and translate it to logical type
stored in a local variable at the earliest reference of a buffer handle.
This means that is can generate inefficient code that reads more than
necessary.

Consider this example:
```
struct BigStruct { bool values[1024]; }
ConstantBuffer&lt;BigStruct&gt; cb;

void test(BigStruct v)
{
      if (v.values[0]) { printf("ok"); }
}

[numthreads(1,1,1)]
void computeMain()
{
    test(cb);
}
```

In IR, the `computeMain` function before lower-buffer-element-type pass
is something like following:
```
func test:
   %v = param : BigStruct
   %barr = fieldExtract(%v, "values")
   %element = elementExtract(%barr, 0)
    ... // uses %element 

func computeMain:
  %v = load(cb)
  call %test %v
```

The existing lower-buffer-element-type pass will rewrite the bool array
in `BigStruct` into `int` array so it is legal in SPIRV. However, it
does so by inserting the translation on the first `load` of the constant
buffer:

```
struct BigStruct_std430 {
    int values[1024];
}
var cb : ConstantBuffer&lt;BigStruct_std430&gt;;
func computeMain:
   %tmpVar : var&lt;BigStruct&gt;
    call %unpackStorage(%tmpVar, cb)
   %v : BigStruct = load %tmpVar
   call %test %v
```

This means that the entire array will be loaded and translated to int,
before calling `test`, which only uses one element. It turns out that
the downstream compiler isn't always able to optimize out this
inefficient translation/copy.

This PR completely rewrites the way buffer-element-type lowering is
handled to avoid producing this inefficient code. It works in two parts:
first we turn on the `transformParamsToConstRef` pass for SPIRV target
as well, so we will translate the `test` function to take the `v`
parameter as `constref`. The second part is a redesigned
buffer-element-type pass that defers the storage-type to logical-type
translation until a value is actually used by a `load` instruction.

In this example, after `transformParamsToConstRef`, the IR is:

```
func test:
   %v = param : ConstRef&lt;BigStruct&gt;
   %barr = fieldAddr(%v, "values")
   %elementPtr = elementAddr(%barr, 0)
   %element = load(%elementPtr)
    ... // uses %element 

func computeMain:
  call %test %cb
```

The new `buffer-element-type-lowering` pass will take this IR, and
insert translation at latest possible time across the entire call graph,
and translate the IR into:

```
func test:
   %v = param : ConstRef&lt;BigStruct_std430&gt;
   %barr = fieldAddr(%v, "values")
   %elementPtr : ptr&lt;int&gt; = elementAddr(%barr, 0)
   %element_int = load(%elementPtr)
    %element = cast(%element_int) : %bool
    ... // uses %element 

func computeMain:
  call %test %cb
```

In this new IR, there is no longer a load and conversion of the entire
array.

See new comment in `slang-ir-lower-buffer-element-type.cpp` for more
details of how the pass works.

This PR also address many other issues surfaced by turning on
`transformParamsToConstRef` pass on SPIRV backend.

---------

Co-authored-by: slangbot &lt;186143334+slangbot@users.noreply.github.com&gt;</content>
</entry>
<entry>
<title>Diagnostic for metal ref mesh output assignment (#8365)</title>
<updated>2025-09-17T17:51:36+00:00</updated>
<author>
<name>James Helferty (NVIDIA)</name>
<email>jhelferty@nvidia.com</email>
</author>
<published>2025-09-17T17:51:36+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=8d67365b36ea43d339911eba5ee1693d75ae58e2'/>
<id>urn:sha1:8d67365b36ea43d339911eba5ee1693d75ae58e2</id>
<content type='text'>
When slang detects assignment to a mesh output reference on metal,
generate a diagnostic message. (Metal mesh shader outputs must be
assigned via 'set' instead of 'ref'.)

Fixes #7498</content>
</entry>
<entry>
<title>Fix metal segfault by check vectorValue before accessing (#7688)</title>
<updated>2025-07-11T20:16:10+00:00</updated>
<author>
<name>Gangzheng Tong</name>
<email>tonggangzheng@gmail.com</email>
</author>
<published>2025-07-11T20:16:10+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=e3afef145e67e74e054407606320be0e42a6aa63'/>
<id>urn:sha1:e3afef145e67e74e054407606320be0e42a6aa63</id>
<content type='text'>
* Check vectorValue before accessing

* Fix metal segfault by using IRElementExtract for general vector handling

Address review comments by replacing IRMakeVector-specific code with
IRElementExtract to handle any vector instruction type (IRIntCast, etc).
This makes the code more robust and fixes cases where float2(1,2)
creates IRIntCast instead of IRMakeVector.

Co-authored-by: Yong He &lt;csyonghe@users.noreply.github.com&gt;

* Fix sign comparison warning in metal legalize

Cast originalElementCount-&gt;getValue() to UInt to avoid comparison between signed and unsigned integers.

Co-authored-by: Yong He &lt;csyonghe@users.noreply.github.com&gt;

---------

Co-authored-by: Yong He &lt;yonghe@outlook.com&gt;
Co-authored-by: github-actions[bot] &lt;41898282+github-actions[bot]@users.noreply.github.com&gt;
Co-authored-by: Yong He &lt;csyonghe@users.noreply.github.com&gt;</content>
</entry>
<entry>
<title>Fix matrix division by scalar for Metal and WGSL targets (#6752)</title>
<updated>2025-04-14T20:48:17+00:00</updated>
<author>
<name>Darren Wihandi</name>
<email>65404740+fairywreath@users.noreply.github.com</email>
</author>
<published>2025-04-14T20:48:17+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=705d00ab8528e0d7c14f68b7d0e15fb57280c16e'/>
<id>urn:sha1:705d00ab8528e0d7c14f68b7d0e15fb57280c16e</id>
<content type='text'>
* Fix matrix division by scalar for Metal and WGSL targets

* Add tests

* Minor fix

* Fix compilation error

* Convert to multiplication for WGSL

* Minor cleanup

---------

Co-authored-by: Yong He &lt;yonghe@outlook.com&gt;</content>
</entry>
<entry>
<title>Fix depth texture sampling on Metal. (#6168)</title>
<updated>2025-01-25T05:39:13+00:00</updated>
<author>
<name>Yong He</name>
<email>yonghe@outlook.com</email>
</author>
<published>2025-01-25T05:39:13+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=3ecbeacd3b02e2a8c1b9796df1caced0aa34224f'/>
<id>urn:sha1:3ecbeacd3b02e2a8c1b9796df1caced0aa34224f</id>
<content type='text'>
</content>
</entry>
<entry>
<title>Reuse code for Metal and WGSL entry point legalization (#6063)</title>
<updated>2025-01-15T23:50:56+00:00</updated>
<author>
<name>Darren Wihandi</name>
<email>65404740+fairywreath@users.noreply.github.com</email>
</author>
<published>2025-01-15T23:50:56+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=9b977e59cf786bbb000b3b868b126c2b9a17d3f3'/>
<id>urn:sha1:9b977e59cf786bbb000b3b868b126c2b9a17d3f3</id>
<content type='text'>
* Refactor to reuse common for metal and wgsl entry point legalization

* refactor system val work item

* refactor simplify user names

* clean up fix semantic field of struct

* improve code layout

* split wgsl/metal to seperate classes and cleanup

* remove extra includes

* remove dead code comments

* minor cleanup

* squash merge from master and resolve conflict

* apply metal spec const thread count changes

* Revert "apply metal spec const thread count changes"

This reverts commit c42d707fd25ee0328598650d3235cd2322810ccc.

* Revert "squash merge from master and resolve conflict"

This reverts commit 06db88ef7001bdfe93fb23af35af0d026b255dee.

* Merge remote-tracking branch 'origin/master'

* apply metal spec const thread count changes

* Revert "apply metal spec const thread count changes"

This reverts commit 3b9e6f53cee2e6076ac2b7a0d015a1ed2cbbd627.

* Revert "Merge remote-tracking branch 'origin/master'"

This reverts commit 99869d573a46dadeb24445405f5a1e37a8e03d0d.

* apply metal spec const thread count changes

---------

Co-authored-by: Yong He &lt;yonghe@outlook.com&gt;</content>
</entry>
<entry>
<title>Implement specialization constant support in numthreads / local_size (#5963)</title>
<updated>2025-01-14T18:32:29+00:00</updated>
<author>
<name>Julius Ikkala</name>
<email>julius.ikkala@gmail.com</email>
</author>
<published>2025-01-14T18:32:29+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=cbdc7e1219e472fd74f7f559d7e417f233e7df39'/>
<id>urn:sha1:cbdc7e1219e472fd74f7f559d7e417f233e7df39</id>
<content type='text'>
* Allow using specialization constants in numthreads attribute

* Add support for GLSL local_size_x_id syntax

* Fix overeager specialization constant parsing

* Add diagnostics for specialization constant numthreads

* Remove unused variable

* Fix local_size_x_id not finding existing specialization constant

* Allow materializeGetWorkGroupSize to reference specialization constants

* Use SpvOpExecutionModeId for modes that require it

* Cleanup specialization constant numthreads code

* Add tests for specialization constant work group sizes

* Fix implicit Slang::Int -&gt; int32_t cast

* Fix querying thread group size in reflection API

---------

Co-authored-by: Yong He &lt;yonghe@outlook.com&gt;</content>
</entry>
<entry>
<title>WGSL: Convert signed vector shift amounts to unsigned (#6023)</title>
<updated>2025-01-10T19:05:05+00:00</updated>
<author>
<name>Anders Leino</name>
<email>aleino@nvidia.com</email>
</author>
<published>2025-01-10T19:05:05+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=803e0c9f9a9dc4b01e29ebbf3b37a5bba782ac83'/>
<id>urn:sha1:803e0c9f9a9dc4b01e29ebbf3b37a5bba782ac83</id>
<content type='text'>
* WGSL: Fixes for signed shift amounts

- Handle the case of vector shift amounts
 - Closes #5985
- Move handling of scalar case from emit to legalization
- Add tests for bitshifts.

* Move the binary operator legalization function to a common place

* Metal: Legalize binary operations

Closes #6029.

* Fix Metal filecheck test

The int shift amounts are now converted to unsigned.

* format code

---------

Co-authored-by: slangbot &lt;186143334+slangbot@users.noreply.github.com&gt;
Co-authored-by: Yong He &lt;yonghe@outlook.com&gt;</content>
</entry>
<entry>
<title>Lower varying parameters as pointers instead of SSA values. (#5919)</title>
<updated>2025-01-08T06:26:31+00:00</updated>
<author>
<name>Yong He</name>
<email>yonghe@outlook.com</email>
</author>
<published>2025-01-08T06:26:31+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=c43f6fa55aca23365c86c6ec1737d42be74d9d3e'/>
<id>urn:sha1:c43f6fa55aca23365c86c6ec1737d42be74d9d3e</id>
<content type='text'>
* Add executable test on matrix-typed vertex input.
* Fix emit logic of matrix layout qualifier.
* Pass fragment shader varying input by constref to allow EvaluateAttributeAtCentroid etc. to be implemented correctly.</content>
</entry>
</feed>
