-
Notifications
You must be signed in to change notification settings - Fork 553
Iteratively run SimplificationPipeline until code optimization converges #1261
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,6 +197,40 @@ static bool satisfiesBackendContract(ModuleOp module, | |
return true; | ||
} | ||
|
||
// Forward declaration | ||
static llvm::hash_code hashOperation(Operation *); | ||
static llvm::hash_code hashBlock(Block &block) { | ||
llvm::hash_code hash(0); | ||
for (Operation &op : block.getOperations()) { | ||
llvm::hash_code opHash = hashOperation(&op); | ||
hash = llvm::hash_combine(hash, opHash); | ||
} | ||
return hash; | ||
} | ||
|
||
static llvm::hash_code hashRegion(Region ®ion) { | ||
llvm::hash_code hash(0); | ||
for (Block &block : region.getBlocks()) { | ||
llvm::hash_code blockHash = hashBlock(block); | ||
hash = llvm::hash_combine(hash, blockHash); | ||
} | ||
return hash; | ||
} | ||
|
||
static llvm::hash_code hashOperation(Operation *op) { | ||
llvm::hash_code hash(0); | ||
llvm::hash_code opHash = OperationEquivalence::computeHash( | ||
op, OperationEquivalence::ignoreHashValue, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add Also, I think we should hash the types of the results so that we keep iterating if the types get refined. |
||
OperationEquivalence::ignoreHashValue, | ||
OperationEquivalence::IgnoreLocations); | ||
hash = llvm::hash_combine(hash, opHash); | ||
for (auto ®ion : op->getRegions()) { | ||
llvm::hash_code regionHash = hashRegion(region); | ||
hash = llvm::hash_combine(hash, regionHash); | ||
} | ||
return hash; | ||
} | ||
|
||
namespace { | ||
class LowerToBackendContractPass | ||
: public LowerToBackendContractBase<LowerToBackendContractPass> { | ||
|
@@ -217,6 +251,9 @@ class LowerToBackendContractPass | |
options.backendLegalOps = backendLegalOps; | ||
createTorchSimplificationPipeline(pm, options); | ||
|
||
bool codeChanged = false; | ||
llvm::hash_code moduleHash = hashOperation(module); | ||
|
||
int i = 0; | ||
do { | ||
if (i++ == maxIterations) { | ||
|
@@ -234,7 +271,14 @@ class LowerToBackendContractPass | |
|
||
if (failed(runPipeline(pm, module))) | ||
return signalPassFailure(); | ||
} while (!satisfiesBackendContract(module)); | ||
|
||
llvm::hash_code newModuleHash = hashOperation(module); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a unit test of the hashing logic? |
||
codeChanged = (moduleHash != newModuleHash); | ||
moduleHash = newModuleHash; | ||
|
||
// Iterate until maxIterations is reached or | ||
// backend contract is satisified and code optimization converges. | ||
} while (!satisfiesBackendContract(module) || codeChanged); | ||
LLVM_DEBUG({ | ||
llvm::dbgs() << "LowerToBackendContractPass: " | ||
<< "succeeded after " << i | ||
|
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.
we should hash the block arguments too (at least their types)