-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[CodeExtractor] Refactor extractCodeRegion, fix alloca emission. #114419
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
[CodeExtractor] Refactor extractCodeRegion, fix alloca emission. #114419
Conversation
Thanks @Meinersbur for this refactoring. Would it be OK to upstream this incrementally in small patches for ease of review? I have taken one small change from this patch and created #115078. |
This is one of the patches split-off from llvm#114419 for ease of review. Co-authored-by: Michael Kruse <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for answering my questions. In the absence of anyone else to review this patch, it looks good to me.
Thanks for taking the time to clean up this code and fix the longstanding bugs.
/// When there are 3 or more exit blocks, leaving the extracted function via | ||
/// the first block it returns 0. When leaving via the second entry it | ||
/// returns 1, etc. | ||
SmallVector<BasicBlock *> SwitchCases; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think this should be "ExitBlocks"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intentionally named it SwitchCases
to indicate that it is not just an unordered list of exist blocks, but the order is used to determine the extracted function's return values and the replacement switch statement. I am not married to the name (could also be ExtractedFunctionReturnValues
), but I honestly think that ExitBlocks
would be a misleading name.
Despite this, do you still prefer ExitBlocks
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just any name that doesn't imply it's only used by a switch instruction.
In a similiar spirit as #115078 we could trivially refactor applyFirstDebugLoc in an NFC patch. Next we could just correct |
@kiranchandramohan I will do refactorings like In contrast I have no idea how Consider that one can review this code like new code by ignoring the previous state. If we are not able to do so we would never be able to add any new feature. Imagine I would add |
I was thinking of the smaller refactoring approach since I thought unfamiliar reviewers would struggle with making sense of the huge change. But since @tblah has gone ahead and made sense of it all, this does not hold. I agree that going ahead with this patch (as it is) is the best way forward and the pragmatic approach. |
…into users/meinersbur/irbuilder-extract-refactor
…einersbur/irbuilder-extract-refactor
…inersbur/irbuilder-extract-refactor
Reorganize the code into phases:
The motivation is #114669 to optionally clone the selected code region into the new function instead of moving it. The current structure made it difficult to add such functionality since there was no obvious place to do so, not made easier by some functions doing more than their name suggests. For instance, constructFunction modifies code outside the constructed function, but also function properties such as setPersonalityFn are derived somewhere else. Another example is emitCallAndSwitchStatement, which despite its name also inserts stores for output parameters.
Many operations also implicitly depend on the order they are applied which this patch tries to reduce. For instance, ExtractedFuncRetVals becomes the list exit blocks which also defines the return value when leaving via that block. It is computed early such that the new function's return instructions and the switch can be generated independently. Also, ExtractedFuncRetVals is combining the lists ExitBlocks and OldTargets which were not always kept consistent with each other or NumExitBlocks. The method recomputeExitBlocks() will update it when necessary.
The coding style partially contradict the current coding standard. For instance some local variable start with lower case letters. I updated some, but not all occurrences to make the diff match at least some lines as unchanged.
The patch D96854 introduced some confusion of function argument indexes this is fixed here as well, hence the patch is not NFC anymore. Tested in modified CodeExtractorTest.cpp. Patch D121061 introduced AllocationBlock, but not all allocas were inserted there.
Efectively includes the following fixes:
Originally submitted as https://reviews.llvm.org/D115218