AcademySoftwareFoundation / AcademySoftwareFoundation/OpenShadingLanguage

missed optimization with return statement in function

Open
#1,146 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
2.3k
Forks
414
Avg merge
3d 1h
Merged PRs (30d)
10

Description

Consider this trivial shader:

```
vector foo(string a)
{
if( a == "" )
return 0;
return 1;
}

surface simple(string s = "")
{
P += foo(s);
}
```

running `testshade --debug simple.oso` yields, among a wall of text:
```
Optimized 8 ops to 6 (-25.0%)
Globals written: (1) P
```
A simple change to the function:
```
vector foo(string a)
{
if( a == "" )
return 0;
else
return 1;
}
```

and the output is now:
```
Optimized 9 ops to 1 (-88.9%)
Globals written: (0)
```
I would expect the original function to allow the same optimization.

### Versions

* OSL branch/version: 1.10.9 (testshade output above) / 3d0a284e997367b5b63bf8ea16911391fa1fea85 (where I originally found the issue in a larger network)
* OS: linux
* LLVM version: 9.0.1 / 6.0.0

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the two shader variants with testshade --debug simple.oso and compare their optimization output. Trace the optimization handling for return statements, then add a regression test showing that the original form receives the same optimization as the explicit else form.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.