Skip to content

Commit

Permalink
Merge branch 'master' into update-slang-rhi
Browse files Browse the repository at this point in the history
  • Loading branch information
csyonghe authored Jan 14, 2025
2 parents caf3a90 + 11575d2 commit 2786414
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
8 changes: 4 additions & 4 deletions docs/user-guide/03-convenience-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ int rs = foo.staticMethod(a,b);

### Mutability of member function

For GPU performance considerations, the `this` argument in a member function is immutable by default. If you modify the content in `this` argument, the modification will be discarded after the call and does not affect the input object. If you intend to define a member function that mutates the object, use `[mutating]` attribute on the member function as shown in the following example.
For GPU performance considerations, the `this` argument in a member function is immutable by default. Attempting to modify `this` will result in a compile error. If you intend to define a member function that mutates the object, use `[mutating]` attribute on the member function as shown in the following example.

```hlsl
struct Foo
Expand All @@ -159,14 +159,14 @@ struct Foo
[mutating]
void setCount(int x) { count = x; }
void setCount2(int x) { count = x; }
// This would fail to compile.
// void setCount2(int x) { count = x; }
}
void test()
{
Foo f;
f.setCount(1); // f.count is 1 after the call.
f.setCount2(2); // f.count is still 1 after the call.
f.setCount(1); // Compiles
}
```

Expand Down
12 changes: 10 additions & 2 deletions source/slang/slang-ir-simplify-cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,19 @@ static bool trySimplifyIfElse(IRBuilder& builder, IRIfElse* ifElseInst)
bool isFalseBranchTrivial = false;
if (isTrivialIfElse(ifElseInst, isTrueBranchTrivial, isFalseBranchTrivial))
{
// If both branches of `if-else` are trivial jumps into after block,
// If either branch of `if-else` is a trivial jump into after block,
// we can get rid of the entire conditional branch and replace it
// with a jump into the after block.
if (auto termInst = as<IRUnconditionalBranch>(ifElseInst->getTrueBlock()->getTerminator()))
IRUnconditionalBranch* termInst =
as<IRUnconditionalBranch>(ifElseInst->getTrueBlock()->getTerminator());
if (!termInst || (termInst->getTargetBlock() != ifElseInst->getAfterBlock()))
{
termInst = as<IRUnconditionalBranch>(ifElseInst->getFalseBlock()->getTerminator());
}

if (termInst)
{
SLANG_ASSERT(termInst->getTargetBlock() == ifElseInst->getAfterBlock());
List<IRInst*> args;
for (UInt i = 0; i < termInst->getArgCount(); i++)
args.add(termInst->getArg(i));
Expand Down
26 changes: 26 additions & 0 deletions tests/bugs/simplify-if-else.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//TEST:SIMPLE(filecheck=CHECK): -stage compute -entry computeMain -target hlsl
//CHECK: computeMain

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

[numthreads(4, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
vector<float32_t, 4> vvv = vector<float32_t, 4>(0);
float32_t ret = 0.0f;
if (vvv.y < 1.0f)
{
ret = 1.0f;
}
else
{
if (vvv.y > 1.0f && outputBuffer[3] == 3)
{
ret = 0.0f;
} else {
if (true) {}
}
}
outputBuffer[int(dispatchThreadID.x)] = int(ret);
}

0 comments on commit 2786414

Please sign in to comment.