Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spirv-val: Conditional Branch without an exit is invalid in loop header #5069

Merged

Conversation

cmarcelo
Copy link
Contributor

@cmarcelo cmarcelo commented Jan 18, 2023

From 2.16.2, for CFG:

Selections must be structured. That is, an OpSelectionMerge instruction is required to precede:

    an OpSwitch instruction

    an OpBranchConditional instruction that has different True Label and False Label operands where neither are declared merge blocks or Continue Targets.

@gfxstrand
Copy link
Contributor

I don't have approval permissions but this looks correct to me.

OpBranch %loop
%exit = OpLabel
OpReturn
OpFunctionEnd
Copy link
Collaborator

Choose a reason for hiding this comment

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

The Tint SPIRV-Reader currently handles this case.
When I modify this case to add an Fragment entry point, and remove Linkage, I get this:


$ tint --format wgsl b.spvasm
fn main_1() {
  loop {
    if (false) {
      continue;
    } else {
      break;
    }
  }
  return;
}

@fragment
fn main() {
  main_1();
}

Yeah, I went the extra mile to handle this extra case.
Not passing judgement on this PR. Just FYI.

@alan-baker
Copy link
Contributor

I agree with your interpretation of the spec, but I don't remember discussing this specific case in Khronos internal SPIR issue 115. I'm attempting to double check the Alloy model that was recently developed to see what was tested there.

@gfxstrand
Copy link
Contributor

I went back and did some digging and sadly neither the issue nor the meeting minutes were particularly informative. 😩 I do remember there being a discussion and that I pushed for the current rules. I don't remember what all else was said and I didn't bother to go dig up a recording.

@alan-baker
Copy link
Contributor

@cmarcelo have you tested this against CTS by chance? I assume it's ok, but I know LLVM would like to generate code like this sometimes.

@gfxstrand
Copy link
Contributor

Yes, we tested it against the CTS as well as a large pile of additional tests from the fuzzing folks.

As far as LLVM goes, you can get exactly the same thing by translating:

OpLoopMerge %merge, %continue
OpBranchConditional %a, %b

with

OpLoopMerge %merge, %continue
OpBranch %c
%c = OpLabel
OpSelectionMerge %continue
OpBranchConditional %a, %b

and let the early merge logic take care of the rest. It's exactly 3 more SPIR-V instructions. I don't think this will hurt LLVM's ability to generate SPIR-V. The difference is that we now explicitly have two different constructs: a selection and a loop, instead of a loop that's also kind-of a free selection.

@cmarcelo
Copy link
Contributor Author

CC: @Jack-Clark

alan-baker
alan-baker previously approved these changes Feb 1, 2023
Copy link
Contributor

@alan-baker alan-baker left a comment

Choose a reason for hiding this comment

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

The work on the alloy model hasn't revealed any surprising cases that are disallowed by this change (nor was it expected to). The change looks good and workarounds in generators are reasonable.

@alan-baker
Copy link
Contributor

I believe the VS2019 failures are due to this being an older branch. Could you please rebase this PR?

@cmarcelo
Copy link
Contributor Author

cmarcelo commented Feb 1, 2023

Rebased.

alan-baker
alan-baker previously approved these changes Feb 1, 2023
@alan-baker
Copy link
Contributor

It appears that several unit tests are broken by this change.

@cmarcelo
Copy link
Contributor Author

cmarcelo commented Feb 2, 2023

That's odd... I'll check what's up.

@cmarcelo
Copy link
Contributor Author

cmarcelo commented Feb 2, 2023

@alan-baker is there a way to see the details of the "CI-ubuntu-clang" runs?

@cmarcelo
Copy link
Contributor Author

cmarcelo commented Feb 2, 2023

Nevermind, managed to load other runs results here. The issue seems to be with fuzzer, I'll look into that.

@alan-baker
Copy link
Contributor

When the message is "Kokoro build finished" it generally indicates an internal error in the CI process. I try to retrigger those builds when I see them.

…chConditional

New IDs were selected to make clear the transformation being done here, instead
of reordering all IDs in between or refactoring the SPIR-V in the test.
From 2.16.2, for CFG:

    Selections must be structured. That is, an OpSelectionMerge
    instruction is required to precede:

    - an OpSwitch instruction

    - an OpBranchConditional instruction that has different True Label
      and False Label operands where neither are declared merge blocks
      or Continue Targets.
@cmarcelo
Copy link
Contributor Author

cmarcelo commented Feb 4, 2023

@alan-baker there were a few Fuzz internal tests that were assuming an initial state invalid. I've added a separate patch to this PR to fix them. Could you trigger the builds again?

@cmarcelo
Copy link
Contributor Author

cmarcelo commented Feb 4, 2023

For the record: I've tested by enabling -DSPIRV_BUILD_FUZZER=ON (that's not the default, which is why I've missed before).

@alan-baker alan-baker merged commit 0ce2bc4 into KhronosGroup:main Feb 6, 2023
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.

5 participants