Skip to content

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion lib/Dialect/Torch/Transforms/LowerToBackendContract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

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)

hash = llvm::hash_combine(hash, opHash);
}
return hash;
}

static llvm::hash_code hashRegion(Region &region) {
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,
Copy link
Contributor

@silvasean silvasean Aug 24, 2022

Choose a reason for hiding this comment

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

can you add /*arg=*/ comments here so we know the arg names?

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 &region : op->getRegions()) {
llvm::hash_code regionHash = hashRegion(region);
hash = llvm::hash_combine(hash, regionHash);
}
return hash;
}

namespace {
class LowerToBackendContractPass
: public LowerToBackendContractBase<LowerToBackendContractPass> {
Expand All @@ -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) {
Expand All @@ -234,7 +271,14 @@ class LowerToBackendContractPass

if (failed(runPipeline(pm, module)))
return signalPassFailure();
} while (!satisfiesBackendContract(module));

llvm::hash_code newModuleHash = hashOperation(module);
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down