[PATCH] D16602: StructurizeCFG: Add an option for skipping regions with only uniform branches

Christian K├Ânig via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 27 07:19:00 PST 2016


LGTM.

I was already wondering how we could integrate SGPR only branches into 
the Structurizer.

That approach seems to solve it rather nicely.

Regards,
Christian.

Am 27.01.2016 um 16:02 schrieb Tom Stellard:
> cc Christian.
>
> On Tue, Jan 26, 2016 at 09:57:45PM +0000, Tom Stellard wrote:
>> tstellarAMD created this revision.
>> tstellarAMD added a reviewer: arsenm.
>> tstellarAMD added a subscriber: llvm-commits.
>>
>> Tests for this will be added once the AMDGPU backend enables this
>> option.
>>
>> http://reviews.llvm.org/D16602
>>
>> Files:
>>    include/llvm/Transforms/Scalar.h
>>    lib/Transforms/Scalar/StructurizeCFG.cpp
>>
>> Index: lib/Transforms/Scalar/StructurizeCFG.cpp
>> ===================================================================
>> --- lib/Transforms/Scalar/StructurizeCFG.cpp
>> +++ lib/Transforms/Scalar/StructurizeCFG.cpp
>> @@ -11,6 +11,7 @@
>>   #include "llvm/ADT/MapVector.h"
>>   #include "llvm/ADT/PostOrderIterator.h"
>>   #include "llvm/ADT/SCCIterator.h"
>> +#include "llvm/Analysis/DivergenceAnalysis.h"
>>   #include "llvm/Analysis/LoopInfo.h"
>>   #include "llvm/Analysis/RegionInfo.h"
>>   #include "llvm/Analysis/RegionIterator.h"
>> @@ -161,6 +162,9 @@
>>   /// consist of a network of PHI nodes where the true incoming values expresses
>>   /// breaks and the false values expresses continue states.
>>   class StructurizeCFG : public RegionPass {
>> +  bool SkipUniformRegions;
>> +  DivergenceAnalysis *DA;
>> +
>>     Type *Boolean;
>>     ConstantInt *BoolTrue;
>>     ConstantInt *BoolFalse;
>> @@ -232,14 +236,21 @@
>>   
>>     void rebuildSSA();
>>   
>> +  bool hasOnlyUniformBranches(const Region *R);
>> +
>>   public:
>>     static char ID;
>>   
>>     StructurizeCFG() :
>>       RegionPass(ID) {
>>       initializeStructurizeCFGPass(*PassRegistry::getPassRegistry());
>>     }
>>   
>> +  StructurizeCFG(bool SkipUniformRegions) :
>> +    RegionPass(ID), SkipUniformRegions(SkipUniformRegions) {
>> +    initializeStructurizeCFGPass(*PassRegistry::getPassRegistry());
>> +  }
>> +
>>     using Pass::doInitialization;
>>     bool doInitialization(Region *R, RGPassManager &RGM) override;
>>   
>> @@ -250,6 +261,7 @@
>>     }
>>   
>>     void getAnalysisUsage(AnalysisUsage &AU) const override {
>> +    AU.addRequired<DivergenceAnalysis>();
>>       AU.addRequiredID(LowerSwitchID);
>>       AU.addRequired<DominatorTreeWrapperPass>();
>>       AU.addRequired<LoopInfoWrapperPass>();
>> @@ -264,6 +276,7 @@
>>   
>>   INITIALIZE_PASS_BEGIN(StructurizeCFG, "structurizecfg", "Structurize the CFG",
>>                         false, false)
>> +INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
>>   INITIALIZE_PASS_DEPENDENCY(LowerSwitch)
>>   INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
>>   INITIALIZE_PASS_DEPENDENCY(RegionInfoPass)
>> @@ -914,11 +927,33 @@
>>       }
>>   }
>>   
>> +bool StructurizeCFG::hasOnlyUniformBranches(const Region *R) {
>> +  for (const BasicBlock *BB : R->blocks()) {
>> +    const BranchInst *Br = dyn_cast<BranchInst>(BB->getTerminator());
>> +    if (!Br || !Br->isConditional())
>> +      continue;
>> +
>> +    if (!DA->isUniform(Br->getCondition()))
>> +      return false;
>> +    DEBUG(dbgs() << "BB: " << BB->getName() << " has uniform terminator\n");
>> +  }
>> +  return true;
>> +}
>> +
>>   /// \brief Run the transformation for each region found
>>   bool StructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
>>     if (R->isTopLevelRegion())
>>       return false;
>>   
>> +  if (SkipUniformRegions) {
>> +    DA = &getAnalysis<DivergenceAnalysis>();
>> +    // TODO: We could probably be smarter here with how we handle sub-regions.
>> +    if (hasOnlyUniformBranches(R)) {
>> +      DEBUG(dbgs() << "Skipping region with uniform control flow: " << *R << '\n');
>> +      return false;
>> +    }
>> +  }
>> +
>>     Func = R->getEntry()->getParent();
>>     ParentRegion = R;
>>   
>> @@ -947,7 +982,6 @@
>>     return true;
>>   }
>>   
>> -/// \brief Create the pass
>> -Pass *llvm::createStructurizeCFGPass() {
>> -  return new StructurizeCFG();
>> +Pass *llvm::createStructurizeCFGPass(bool SkipUniformRegions) {
>> +  return new StructurizeCFG(SkipUniformRegions);
>>   }
>> Index: include/llvm/Transforms/Scalar.h
>> ===================================================================
>> --- include/llvm/Transforms/Scalar.h
>> +++ include/llvm/Transforms/Scalar.h
>> @@ -262,7 +262,10 @@
>>   //
>>   // CFG Structurization - Remove irreducible control flow
>>   //
>> -Pass *createStructurizeCFGPass();
>> +///
>> +/// When \p SkipUniformRegions is true the structizer will not structurize
>> +/// regions that only contain uniform branches.
>> +Pass *createStructurizeCFGPass(bool SkipUniformRegions = false);
>>   
>>   //===----------------------------------------------------------------------===//
>>   //



More information about the llvm-commits mailing list