Skip to content

spirv-fuzz: Add inline function transformation#3517

Merged
afd merged 18 commits intoKhronosGroup:masterfrom
andreperezmaselco:add-inline-function-transformation
Aug 25, 2020
Merged

spirv-fuzz: Add inline function transformation#3517
afd merged 18 commits intoKhronosGroup:masterfrom
andreperezmaselco:add-inline-function-transformation

Conversation

@andreperezmaselco
Copy link
Copy Markdown
Collaborator

Fixes #3505.

Copy link
Copy Markdown
Contributor

@afd afd left a comment

Choose a reason for hiding this comment

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

This is a preliminary review with some top level feedback from having studied the IsApplicable function - please see attached in one of the comments some proposed tests for the HasEarlyReturn method of Function, and a different way to implement it.

This is a complex transformation so I expect we'll need a bit of iteration in the review process, but in general this is looking really good!

@andreperezmaselco andreperezmaselco force-pushed the add-inline-function-transformation branch from 3f679e0 to 05add40 Compare July 17, 2020 16:42
Copy link
Copy Markdown
Contributor

@afd afd left a comment

Choose a reason for hiding this comment

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

The transformation is looking very promising! Some changes required.

@andreperezmaselco andreperezmaselco force-pushed the add-inline-function-transformation branch 2 times, most recently from 78cb58e to edfa85d Compare July 29, 2020 20:47
Copy link
Copy Markdown
Contributor

@afd afd left a comment

Choose a reason for hiding this comment

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

This is coming along really well!

Rather than inserting comments in the code, here is a patch with some changes to the way the transformation works that I suggest you study and then incorporate (editing it as you see fit, of course):

patch.txt

The main things I've done here are:

  • Do require that the returned id - if defined in the function - is mapped. Now that we're handling a return via a copy object this is necessary

  • Require that parameter ids are not mapped. I had a mistake in my test where I had mapped a parameter id and this led to an applicable transformation that then failed to apply properly (fixed in the patch)

  • Handle insertion of the OpCopyObject for OpReturnValue while the OpReturnValue is being processed, and not later. That's cleaner for various reasons.

Can you apply these changes, and for my next review can you include tests to check the following (your existing tests might have this; I didn't look yet):

  • A function that directly uses OpKill or OpUnreachable cannot be inlined - please open an issue for the fact that we can relax this in the future: we can inline functions that use OpKill/OpUnreachable if the call site is not in a continue construct, but let's not do that in this PR.

  • A function that has multiple returns cannot be inlined

In the next review round I will look at the fuzzer pass.

@andreperezmaselco andreperezmaselco force-pushed the add-inline-function-transformation branch from edfa85d to a0d8cff Compare August 4, 2020 18:27
@paulthomson
Copy link
Copy Markdown
Contributor

I have not reviewed this yet, but this is a reminder to ensure that inlined dead blocks are also marked as dead.

Copy link
Copy Markdown
Collaborator

@Vasniktel Vasniktel left a comment

Choose a reason for hiding this comment

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

This is a very nice transformation idea.

I've noticed some issues with the implementation and some tricky corner cases with the SPIR-V structured control-flow rules (which you are handling btw; I just left some comments regarding those).

And be sure to check Ally's previous review summary regarding OpKill and OpUnreachable.

Copy link
Copy Markdown
Contributor

@paulthomson paulthomson left a comment

Choose a reason for hiding this comment

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

This looks really good! And this is a hard transformation (as cloning multiple blocks is non-trivial). I think it is very close to being done, but I have left a few comments that need addressing. Although some are just minor comments about style.

@andreperezmaselco andreperezmaselco force-pushed the add-inline-function-transformation branch 2 times, most recently from 2ea7e51 to bfb5f38 Compare August 18, 2020 02:59
Copy link
Copy Markdown
Collaborator

@Vasniktel Vasniktel left a comment

Choose a reason for hiding this comment

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

This looks nice!

However, I'd suggest you refactor Apply method in the transformation since there is no telling what can break by storing blocks directly in the instruction list (I'd assume that dominator analysis won't work for inlined blocks).

I'd suggest you do the following:

  1. Split the caller block in two: the first part containing all the instructions before the call and the second part containing all the instructions starting with a call onward (see opt::BasicBlock::SplitBasicBlock).
  2. Start creating blocks to inline and insert them before the second block (opt::Function::InsertBasicBlockBefore).
  3. Remove the OpFunctionCall instruction from the second block.
  4. Add a terminator to the first block pointing to the entry block of the inlined function.

With this approach, I think you can remove the constraint that the call must be the last instruction before the OpBranch. Although, you would need to check that the caller block is not a loop header.

@andreperezmaselco andreperezmaselco force-pushed the add-inline-function-transformation branch 3 times, most recently from 15d2df7 to 0a5c041 Compare August 19, 2020 14:18
Copy link
Copy Markdown
Contributor

@afd afd left a comment

Choose a reason for hiding this comment

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

This is looking really good and is now very close!

Please see the comments that I just added requesting some (probably final) changes.

@andreperezmaselco andreperezmaselco force-pushed the add-inline-function-transformation branch from 0cebf4b to d0eea8b Compare August 21, 2020 15:45
Copy link
Copy Markdown
Contributor

@afd afd left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes.

This looks really good, and I'm happy to merge it!

@afd afd merged commit 5adc5ae into KhronosGroup:master Aug 25, 2020
dneto0 pushed a commit to dneto0/SPIRV-Tools that referenced this pull request Sep 14, 2024
Roll third_party/glslang/ 983698b..517f39e (1 commit)

KhronosGroup/glslang@983698b...517f39e

$ git log 983698b..517f39e --date=short --no-merges --format='%ad %ae %s'
2020-08-26 jmadill Suppress two override suggestion warnings.

Created with:
  roll-dep third_party/glslang

Roll third_party/googletest/ 1e315c5b1..df6b75949 (1 commit)

google/googletest@1e315c5...df6b759

$ git log 1e315c5b1..df6b75949 --date=short --no-merges --format='%ad %ae %s'
2020-08-26 absl-team Googletest export

Created with:
  roll-dep third_party/googletest

Roll third_party/spirv-tools/ 4dd1223..8a0ebd4 (14 commits)

KhronosGroup/SPIRV-Tools@4dd1223...8a0ebd4

$ git log 4dd1223..8a0ebd4 --date=short --no-merges --format='%ad %ae %s'
2020-08-31 jaebaek Correctly replace debug lexical scope of instruction (KhronosGroup#3718)
2020-08-28 afdx spirv-fuzz: Remove opaque pointer design pattern (KhronosGroup#3755)
2020-08-27 stefanomil spirv-fuzz: Create synonym via OpPhi and existing synonyms (KhronosGroup#3701)
2020-08-27 stefanomil Add LoopNestingDepth function to StructuredCFGAnalysis (KhronosGroup#3754)
2020-08-27 afdx spirv-fuzz: Do not make synonyms of void result ids (KhronosGroup#3747)
2020-08-26 greg Do not register DebugFunction for functions optimized away. (KhronosGroup#3749)
2020-08-26 jaebaek Handle DebugScope in compact-ids pass (KhronosGroup#3724)
2020-08-26 afdx spirv-fuzz: Overflow ids (KhronosGroup#3734)
2020-08-25 greg Fix DebugNoScope to not output InlinedAt operand. (KhronosGroup#3748)
2020-08-25 vasniktel spirv-fuzz: Split the fact manager into multiple files (KhronosGroup#3699)
2020-08-25 andreperezmaselco.developer spirv-fuzz: Add inline function transformation (KhronosGroup#3517)
2020-08-25 vasniktel spirv-fuzz: Fix MaybeGetZeroConstant (KhronosGroup#3740)
2020-08-24 greg Fix SSA-rewrite to remove DebugDeclare for variables without loads (KhronosGroup#3719)
2020-08-24 stevenperron Add undef for inlined void function (KhronosGroup#3720)

Created with:
  roll-dep third_party/spirv-tools
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.

Add inlining transformation to spirv-fuzz

4 participants