[PATCH] D115216: [CodeExtractor] Optionally keep code in original function.

Michael Kruse via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 6 21:12:50 PST 2021


Meinersbur created this revision.
Meinersbur added reviewers: kiranchandramohan, ftynse, peixin, jdoerfert, clementval, Leporacanthicus, kiranktp, AMDChirag, fghanim, jdenny, MatsPetersson, AndrewLitteken, paquette, jroelofs, vzakhari.
Herald added subscribers: ormris, hiraditya.
Meinersbur requested review of this revision.
Herald added a subscriber: sstefan1.
Herald added a project: LLVM.

When extracting a region into a new function, optionally allow cloning basic blocks and instructions into the extracted function instead of moving them. The keeps the original code in the original function such that they can still be referenced -- and branched to -- in the original function.

The motivation is the use of CodeExtractor in the OpenMPIRBuilder. The implementation of `createParallel` first emits the parallel region into the lexical function, then uses CodeExtractor to outline that region into a new function. The problem here is that Clang's code generator will references some basic blocks for code inside as well as outside the region. This includes some special purpose block (`EHResumeBlock`, `TerminateLandingPad`, `TerminateHandler`, `UnreachableBlock`, ...) and cleanup/dtor code that is re-used from multiple scopes (see test case `extract-block-cleanup.ll`). Moving these blocks into a different function will result in malformed IR. The KeepOldBlocks option will instead clone the outlined code into a new function keeping the auxiliary code intact, relying on later DCE to remove code that indeed has become unreachable. Additionally, this code could also be uses as a fallback when threading/offloading is disabled via environment option.

Use of KeepOldBlocks by OpenMPIRBuilder is not part of this patch. For testing, we extend `llvm-extract` allowing the use of this option and thus making it more powerful.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115216

Files:
  llvm/include/llvm/Transforms/IPO.h
  llvm/include/llvm/Transforms/Utils/Cloning.h
  llvm/include/llvm/Transforms/Utils/CodeExtractor.h
  llvm/lib/Transforms/IPO/BlockExtractor.cpp
  llvm/lib/Transforms/Utils/CloneFunction.cpp
  llvm/lib/Transforms/Utils/CodeExtractor.cpp
  llvm/test/tools/llvm-extract/extract-block-cleanup.ll
  llvm/test/tools/llvm-extract/extract-block-multiple-exits.ll
  llvm/test/tools/llvm-extract/extract-block-sink.ll
  llvm/test/tools/llvm-extract/extract-block.ll
  llvm/test/tools/llvm-extract/extract-blocks-with-groups.ll
  llvm/tools/llvm-extract/llvm-extract.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115216.392274.patch
Type: text/x-patch
Size: 47990 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211207/f0695039/attachment.bin>


More information about the llvm-commits mailing list