Skip to content

Fix nondeterminism around patch constant functions - #2099

Merged
Tristan Labelle (tristanlabelle) merged 2 commits into
microsoft:masterfrom
tristanlabelle:fix-patch-const-func-nondeterminism
Apr 9, 2019
Merged

Fix nondeterminism around patch constant functions#2099
Tristan Labelle (tristanlabelle) merged 2 commits into
microsoft:masterfrom
tristanlabelle:fix-patch-const-func-nondeterminism

Conversation

@tristanlabelle

@tristanlabelle Tristan Labelle (tristanlabelle) commented Apr 2, 2019

Copy link
Copy Markdown
Contributor

We had disabled flaky tests, and they were due to a nondeterministic pointer recycling issue.

When multiple hull shaders point to a single patch constant buffer, we would iterate over the hull shaders, during which we would apply a transformation on the patch constant buffer and erase it. This means that subsequent hull shaders will be pointing to an already-deleted patch constant buffer. If the pointer got reused by a new function, we got into weird situations were we would remove a newer function while trying to clean an invalidated pointer.

The solution is to bin hull shaders by their common patch constant function. Transform the patch constant function without deleting the original, performing the replace on every using hull shader function, and only then deleting the original function.

There are probably more things wrong with this code, like using a non-counted set to keep track of which functions are patch constant functions, but this at least fixes the nondeterminism.

Fixes #1603

if (NewPatchConstantFunc) {
// Update all user hull shaders
for (Function *HullShaderFunc : PatchConstantFuncEntry.second)
DM.SetPatchConstantFunctionForHS(HullShaderFunc, NewPatchConstantFunc);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tex Riddell (@tex3d) If you can have a look at this SetPatchConstantFunctionForHS. I think it has more issues because it's using a set<Function*> to keep track of the patch constant functions, but that could break down if multiple hull shaders have the same patch constant function. This is all for supporting IsPatchConstantFunction(), and I'm wondering if it'd be so bad to implement that by iterating over all the entry points and seeing if any is a hull shader pointing to that function. It would certainly be less error-prone and less bookkeeping.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. This code doesn't fix that problem though, right? What about only removing from the set on RemoveFunction. Is there a scenario where a function goes from being a patch constant function to something exported but no longer a patch constant function? I don't think so...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, my code doesn't fix that, though it does fix the nondeterminism. I just spotted that additional issue while looking at that code,a nd I don't know if it causes actual bugs. To be correct, we would have to also iterate over all hull shader entry points and null out the patchConstFunction pointer in that RemoveFunction. And I can see more cases where we could get the set<> and the presence of non-null patchConstFunctions out of sync, which is why I'm thinking that maintaining that set may be more pain than it's worth.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filed issue #2115 on it. I'd like to get this in as-is so we at least have the nondeterminism fix because fixing the tracking of patch constant functions is a bit more involved.

@AppVeyorBot

Copy link
Copy Markdown

Build DirectXShaderCompiler 1.0.1658 completed (commit dc163e8639 by @MrTrillian)

@AppVeyorBot

Copy link
Copy Markdown

Build DirectXShaderCompiler 1.0.1692 completed (commit 0115889341 by @MrTrillian)

@tristanlabelle

Copy link
Copy Markdown
Contributor Author

Oops, I didn't mean to push that change... I started implementing using a counted set of patchConstantFunc usages but it's a mess. Too many parts of the code manipulate the DxilFunctionProps without caring about keeping the patchConstantFuncs in sync.

@tristanlabelle

Copy link
Copy Markdown
Contributor Author

Reverted the counted set change. I'd like to check this in to at least fix the nondeterminism issue as I'm not sure I'll have time to get to the patch constant function tracking issue now. Filed issue #2115 on it.

@AppVeyorBot

Copy link
Copy Markdown

Build DirectXShaderCompiler 1.0.1695 completed (commit 060aa12db0 by @MrTrillian)

@tristanlabelle
Tristan Labelle (tristanlabelle) merged commit 4167dd4 into microsoft:master Apr 9, 2019
@tristanlabelle
Tristan Labelle (tristanlabelle) deleted the fix-patch-const-func-nondeterminism branch April 9, 2019 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CompilerTest::QuickTest on lib_hs_shaders_only.hlsl is randomly failing in AppVeyor runs

3 participants